-
Notifications
You must be signed in to change notification settings - Fork 64
/
StepNRTRsimple
316 lines (256 loc) · 6.58 KB
/
StepNRTRsimple
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
--logfile=io.open(getWorkingFolder().."\\LuaIndicators\\qlua_log.txt", "w")
min_price_step = 0
Settings=
{
Name = "*StepNRTR simple",
Length = 5,
value_type = "ATR",
Kv = 1.5,
StepSize = 0,
Percentage = 0,
Switch = 1, --1 - HighLow, 2 - CloseClose
ShowLine = 1,
ShowSignalLine = 0,
line =
{
{
Name = "NRTR",
Color = RGB(0, 64, 0),
Type = TYPE_LINE, --TYPE_DASHDOT,
Width = 2
},
{
Name = "NRTR_buy",
Color = RGB(89,217,107),
Type = TYPE_TRIANGLE_UP,
Width = 4
},
{
Name = "NRTR_sell",
Color = RGB(255,0,0),
Type = TYPE_TRIANGLE_DOWN,
Width = 4
},
{
Name = "signal_buy",
Color = RGB(40,240,250),
Type = TYPET_BAR,
Width = 4
},
{
Name = "signal_sell",
Color = RGB(255,0,255),
Type = TYPET_BAR,
Width = 4
}
}
}
function Init()
myNRTR = cached_NRTR()
return #Settings.line
end
function OnCalculate(index)
if index == 1 then
DSInfo = getDataSourceInfo()
min_price_step = getParamEx(DSInfo.class_code, DSInfo.sec_code, "SEC_PRICE_STEP").param_value
if tonumber(min_price_step) == 0 or min_price_step == nil then
min_price_step = 1
end
--WriteLog ("min_price_step "..tostring(min_price_step))
end
return myNRTR(index, Settings)
end
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
function FindExistCandle(I)
local out = I
while not CandleExist(out) and out > 0 do
out = out -1
end
return out
end
function dValue(i,param)
local v = param or "C"
if not CandleExist(i) then
return nil
end
if v == "O" then
return O(i)
elseif v == "H" then
return H(i)
elseif v == "L" then
return L(i)
elseif v == "C" then
return C(i)
elseif v == "V" then
return V(i)
elseif v == "M" then
return (H(i) + L(i))/2
elseif v == "T" then
return (H(i) + L(i)+C(i))/3
elseif v == "W" then
return (H(i) + L(i)+2*C(i))/4
elseif v == "ATR" then
local previous = i-1
if not CandleExist(previous) then
previous = FindExistCandle(previous)
end
return math.max(math.abs(H(i) - L(i)), math.abs(H(i) - C(previous)), math.abs(C(previous) - L(i)))
else
return C(i)
end
end
function cached_NRTR()
local cache_NRTR={}
local smax1={}
local smin1={}
local trend={}
local signal={}
return function(ind, Fsettings, ds)
local Fsettings=(Fsettings or {})
local p = 0
local ATR = 0
local index = ind
local Length = Fsettings.Length or 64
local Kv = Fsettings.Kv or 1
local v_type = Fsettings.value_type or "ATR"
local StepSize = Fsettings.StepSize or 0
local Percentage = Fsettings.Percentage or 0
local Switch = Fsettings.Switch or 1
local ShowLine = Fsettings.ShowLine or 0
local ShowSignalLine = Fsettings.ShowSignalLine or 1
local ratio=Percentage/100.0*min_price_step
local out1 = nil
local out2 = nil
local out3 = nil
local out4 = nil
local out5 = nil
if index == 1 then
cache_NRTR = {}
cache_NRTR[index] = 0
signal = {}
signal[index] = 0
smax1 = {}
smin1 = {}
trend = {}
smax1[index] = 0
smin1[index] = 0
trend[index] = 1
return nil
end
cache_NRTR[index] = cache_NRTR[index-1]
smax1[index] = smax1[index-1]
smin1[index] = smin1[index-1]
trend[index] = trend[index-1]
signal[index] = signal[index-1]
if not CandleExist(index) then
return nil
end
if index <= (Length + 3) then
return nil
end
--WriteLog ("---------------------------------")
--WriteLog ("index "..tostring(index))
--WriteLog ("C(index) "..tostring(C(index)))
--WriteLog ("H(index) "..tostring(H(index)))
--WriteLog ("L(index) "..tostring(L(index)))
local Step=StepSizeCalc(Length,Kv,StepSize,Switch,index)
if Step == 0 then Step = 1 end
local SizeP=Step*min_price_step
local Size2P=2*SizeP
--WriteLog ("Step "..tostring(Step))
local result
local previous = index-1
if not CandleExist(previous) then
previous = FindExistCandle(previous)
end
if Switch == 1 then
smax0=L(previous)+Size2P
smin0=H(previous)-Size2P
else
smax0=C(previous)+Size2P
smin0=C(previous)-Size2P
end
--WriteLog ("smax0 "..tostring(smax0))
--WriteLog ("smin0 "..tostring(smin0))
--WriteLog ("smax1[index] "..tostring(smax1[index]))
--WriteLog ("smin1[index] "..tostring(smin1[index]))
if C(index)>smax1[index] then trend[index] = 1 end
if C(index)<smin1[index] then trend[index]= -1 end
--WriteLog ("trend "..tostring(trend[index]))
if trend[index]>0 then
if smin0<smin1[index] then smin0=smin1[index] end
result=smin0+SizeP
else
if smax0>smax1[index] then smax0=smax1[index] end
result=smax0-SizeP
end
--WriteLog ("result "..tostring(result))
smax1[index] = smax0
smin1[index] = smin0
--WriteLog ("smax0 "..tostring(smax0))
--WriteLog ("smin0 "..tostring(smin0))
--WriteLog ("smax1[index] "..tostring(smax1[index]))
--WriteLog ("smin1[index] "..tostring(smin1[index]))
if trend[index]>0 then
cache_NRTR[index]=(result+ratio/Step)-Step*min_price_step
end
if trend[index]<0 then
cache_NRTR[index]=(result+ratio/Step)+Step*min_price_step
end
--WriteLog ("cache_NRTR "..tostring(cache_NRTR[index]))
if trend[index-1]>0 and trend[index-2]<0 then
out2 = O(index)
out3 = nil
end
if trend[index-1]<0 and trend[index-2]>0 then
out2 = nil
out3 = O(index)
end
if ShowLine == 1 and ShowSignalLine ~= 1 then
out1 = cache_NRTR[index]
end
if ShowSignalLine == 1 then
if trend[index]>0 then
out4 = cache_NRTR[index-1]
else
out5 = cache_NRTR[index-1]
end
end
return out1, out2, out3, out4, out5
end
end
function WriteLog(text)
logfile:write(tostring(os.date("%c",os.time())).." "..text.."\n");
logfile:flush();
LASTLOGSTRING = text;
end;
function StepSizeCalc(Len, Km, Size, Switch, index)
local result
if Size == 0 then
local Range=0.0
local ATRmax=-1000000
local ATRmin=1000000
for iii=1, Len do
if CandleExist(index-iii) then
if Switch == 1 then
Range=H(index-iii)-L(index-iii)
else
Range=math.abs(O(index-iii)-C(index-iii))
end
if Range>ATRmax then ATRmax=Range end
if Range<ATRmin then ATRmin=Range end
end
end
result = round(0.5*Km*(ATRmax+ATRmin)/min_price_step, nil)
else result=Km*Size
end
return result
end
function round(num, idp)
if idp and num then
local mult = 10^(idp or 0)
if num >= 0 then return math.floor(num * mult + 0.5) / mult
else return math.ceil(num * mult - 0.5) / mult end
else return num end
end