forked from nick-nh/qlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepNRTR.lua
316 lines (253 loc) · 6.83 KB
/
StepNRTR.lua
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("C:\\SBERBANK\\QUIK_SMS\\LuaIndicators\\qlua_log.txt", "w")
min_price_step = 0
sec_code = ""
timescale = ""
local w32 = require("w32")
Settings=
{
Name = "*StepNRTR",
Length = 5,
value_type = "ATR",
Kv = 1.5,
StepSize = 0,
Percentage = 0,
Switch = 1, --1 - HighLow, 2 - CloseClose
ShowLine = 1,
PlaySound = 1,
line =
{
{
Name = "NRTR_buy",
Color = RGB(40,240,250),
Type = TYPE_POINT,
Width = 4
},
{
Name = "NRTR_sell",
Color = RGB(255,0,255),
Type = TYPE_POINT,
Width = 4
},
{
Name = "NRTR",
Color = RGB(0, 64, 0),
Type = TYPE_LINE, --TYPE_DASHDOT,
Width = 2
}
}
}
function Init()
myNRTR = cached_NRTR()
return #Settings.line
end
function OnCalculate(index)
if index == 1 then
DSInfo = getDataSourceInfo()
sec_code = DSInfo.sec_code
timescale = DSInfo.interval
min_price_step = getParamEx(DSInfo.class_code, DSInfo.sec_code, "SEC_PRICE_STEP").param_value
--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 isMessage={}
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 PlaySound = Fsettings.PlaySound or 0
local ratio=Percentage/100.0*min_price_step
local out1 = nil
local out2 = nil
local out3 = nil
SetValue(index-11, 3, nil)
SetValue(index-1, 3, nil)
if index == 1 then
cache_NRTR = {}
cache_NRTR[index] = 0
smax1 = {}
smin1 = {}
trend = {}
smax1[index] = L(index)
smin1[index] = H(index)
trend[index] = 1
isMessage = {}
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]
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,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
if trend[index]>0 and trend[index-1]<0 then
out1 = O(index)
out2 = nil
end
if trend[index]<0 and trend[index-1]>0 then
out1 = nil
out2 = O(index)
end
--сообщения
if index == Size() and trend[index-1]>0 and trend[index-2]<0 and isMessage[index] == nil then
message("Buy "..tostring(sec_code).." timescale "..tostring(timescale))
if PlaySound == 1 then
PaySoundFile("c:\\windows\\media\\Alarm03.wav")
end
isMessage[index] = 1
end
if index == Size() and trend[index-1]<0 and trend[index-2]>0 and isMessage[index] == nil then
message("Sell "..tostring(sec_code).." timescale "..tostring(timescale))
if PlaySound == 1 then
PaySoundFile("c:\\windows\\media\\Alarm03.wav")
end
isMessage[index] = 1
end
if ShowLine == 1 then
SetValue(index-10, 3, cache_NRTR[index])
out3 = cache_NRTR[index]
end
return out1, out2, out3
end
end
function WriteLog(text)
logfile:write(tostring(os.date("%c",os.time())).." "..text.."\n");
logfile:flush();
LASTLOGSTRING = text;
end;
function PaySoundFile(file_name)
w32.mciSendString("CLOSE QUIK_MP3")
w32.mciSendString("OPEN \"" .. file_name .. "\" TYPE MpegVideo ALIAS QUIK_MP3")
w32.mciSendString("PLAY QUIK_MP3")
end
function StepSizeCalc(Len, Km, Size, 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
Range=H(index-iii)-L(index-iii)
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