-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathnrtr_my.lua
More file actions
317 lines (247 loc) · 5.79 KB
/
nrtr_my.lua
File metadata and controls
317 lines (247 loc) · 5.79 KB
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
317
SMA="SMA"
WMA="WMA"
EMA="EMA"
SMMA="SMMA"
WILLMA="WILLMA"
Settings=
{
Name = "**NRTR_LUA",
period = 21,
multiple = 0.7,
value_type = "ATR",
showNRTR = 0,
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(128,0,255),
Type = TYPE_POINT,
Width = 2
}
}
}
function Init()
myNRTR = cached_NRTR()
NRTR_buy = nil
NRTR_sell = nil
return 3
end
function OnCalculate(index)
if index < Settings.period then
return nil, nil
end
NRTR_buy = nil
NRTR_sell = nil
NRTR_1 = nil
if not CandleExist(index) then
return nil
end
NRTR, NRTR_1 = myNRTR(index, Settings.period, Settings.value_type, Settings.multiple)
NRTR_1 = NRTR_1 or C(index)
NRTR = NRTR or C(index)
local previous = index-1
if not CandleExist(previous) then
previous = FindExistCandle(previous)
end
if C(index) > NRTR and C(previous) < NRTR_1 then
NRTR_buy = NRTR
NRTR_sell = nil
end
if C(index) < NRTR and C(previous) > NRTR_1 then
NRTR_buy = nil
NRTR_sell = NRTR
end
if Settings.showNRTR == 1 then
return NRTR_buy, NRTR_sell, NRTR
else
return NRTR_buy, NRTR_sell
end
end
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
function FindExistCandle(I)
local out = I
while not CandleExist(out) do
out = out -1
end
return out
end
function dValue(i,param)
local v = param or "C"
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 cache_EMA={}
local cache_HPrice={}
local cache_LPrice={}
local ATR_MA=fWMA()
return function(ind, _p, v_t, _m, kk)
local p = 0
local ATR = 0
local p_ema = 0
local v_type = v_t
local period = _p
local multiple = _m
local index = ind
local k = kk or 2/(period+1)
local trend = 0
local reverse = 0
local HiPrice = 0
local LowPrice = 0
if index == 1 then
cache_NRTR = {}
cache_EMA = {}
cache_HPrice = {}
cache_LPrice = {}
if CandleExist(index) then
cache_EMA[index] = math.abs(H(i) - L(i))
cache_NRTR[index] = C(index)
cache_LPrice[index] = C(index)
cache_HPrice[index] = C(index)
else
cache_EMA[index] = 0
cache_NRTR[index] = 0
cache_LPrice[index] = 0
cache_HPrice[index] = 0
end
return nil
end
if index <= period or not CandleExist(index) then
cache_NRTR[index] = cache_NRTR[index-1]
cache_EMA[index] = cache_EMA[index-1]
cache_HPrice[index] = cache_HPrice[index-1]
cache_LPrice[index] = cache_LPrice[index-1]
return nil
end
p = cache_NRTR[index-1] or C(index)
--p_ema = cache_EMA[index-1] or dValue(index, v_type)
--ATR = k*dValue(index, v_type)+(1-k)*p_ema
--ATR = (cache_EMA[index-1]*(period-1)+ dValue(index, v_type)) / period
--Average True Range
cache_EMA[index] = dValue(index, v_type)
ATR = ATR_MA(index, period, cache_EMA, nil)
if C(index) <= p then
trend = 1
end
if C(index) >= p then
trend = -1
end
cache_HPrice[index] = highestHigh(index, period)
cache_LPrice[index] = lowestLow(index, period)
if trend >= 0 then
cache_HPrice[index] = math.max( C(index), cache_HPrice[index])
reverse = cache_HPrice[index] - ATR*multiple
if C(index) <= reverse then
trend = -1;
cache_LPrice[index] = C(index)
reverse = C(index) + ATR*multiple
end
end
if trend <= 0 then
cache_LPrice[index] = math.min( C(index), cache_LPrice[index] )
reverse = cache_LPrice[index] + ATR*multiple
if C(index) >= reverse then
trend = 1
cache_HPrice[index] = C(index)
reverse = C(index) - ATR*multiple
end
end
cache_NRTR[index] = reverse
return reverse, cache_NRTR[index-1]
end
end
function highestHigh(index, period)
if index == 1 then
return nil
else
local highestHigh = H(index)
for i = math.max(index - period, 2), index, 1 do
if CandleExist(i) then
if H(i) > highestHigh then
highestHigh = H(i)
end
end
end
return highestHigh
end
end
function lowestLow(index, period)
if index == 1 then
return nil
else
local lowestLow = L(index)
for i = math.max(index - period, 2), index, 1 do
if CandleExist(i) then
if L(i) < lowestLow then
lowestLow = L(i)
end
end
end
return lowestLow
end
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
function fWMA()
return function (Index, Period, ds, idp)
local Out = nil
if Index >= Period then
local sum = 0
local step = Period
for i = Index-Period+1, Index do
if ds[i] == nil then
ds[i] = 0
end
sum = (step*ds[i]) + sum
step = step - 1
end
Out = (2*sum)/(Period*(Period-1))
end
return round(Out,idp)
end
end