-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtestReg.lua
More file actions
319 lines (265 loc) · 8.25 KB
/
testReg.lua
File metadata and controls
319 lines (265 loc) · 8.25 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
318
319
RegSettings = {
period = 182,
degree = 1, -- 1 -линейная, 2 - параболическая, - 3 степени
shift = 0.618,
kstd = 3, --отклонение сигма
periodATR = 0,
kATR = 0,
Size = 0
}
function initReg()
Reg = nil -- Линия регрессии
Trigger = nil
sx = nil
calcAlgoValue = nil -- Возвращаемая таблица
calcChartResults = nil -- Возвращаемая таблица
ATR=nil
calcATR = true
end
function iterateReg(iSec, cell)
local param1Min = 8
local param1Max = 38
local param1Step = 1
local param2Min = 1
local param2Max = 3
local param2Step = 1
local param3Min = 1
local param3Max = 10
local param3Step = 1
local param4Min = 10
local param4Max = 10
local param4Step = 1
local param5Min = 0.6
local param5Max = 0.6
local param5Step = 0.05
if fixedstop then
param4Min = 10
param4Max = 10
param4Step = 1
param5Min = 0.6
param5Max = 0.6
param5Step = 0.05
end
local settingsTable = {}
local allCount = 0
for param1 = param1Min, param1Max, param1Step do
--_param2Min = math.max(math.ceil(param1+1), param2Min)
--for param2 = _param2Min, param2Max, param2Step do
for param2 = param2Min, param2Max, param2Step do
--for param3 = param3Min, math.ceil(0.8*param1), param3Step do
for param3 = param3Min, param3Max, param3Step do
for param4 = param4Min, param4Max, param4Step do
for param5 = param5Min, param5Max, param5Step do
allCount = allCount + 1
settingsTable[allCount] = {
period = param1,
degree = param2, -- 1 -линейная, 2 - параболическая, - 3 степени
shift = param3,
kstd = 3, --отклонение сигма
periodATR = param4,
kATR = param5,
Size = Size
}
end
end
end
end
end
iterateAlgorithm(iSec, cell, settingsTable)
end
function Reg(index, settings, DS)
local period = settings.period or 182
local degree = settings.degree or 1
local kstd = settings.kstd or 3
local shift = settings.shift or 1
local periodATR = settings.periodATR or 10
kATR = settings.kATR or 0.65
local indexToCalc = 1000
indexToCalc = settings.Size or indexToCalc
local beginIndexToCalc = settings.beginIndexToCalc or math.max(1, settings.beginIndex - indexToCalc)
local endIndexToCalc = settings.endIndex or DS:Size()
if index == nil then index = 1 end
period = math.min(period, DS:Size())
local p = 0
local n = 0
local f = 0
local qq = 0
local mm = 0
local tt = 0
local ii = 0
local jj = 0
local kk = 0
local ll = 0
local nn = 0
local sq = 0
local i0 = 0
local mi = 0
local ai={{1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}}
local b={}
local x={}
p = period
nn = degree+1
if index == beginIndexToCalc or index == 1 then
--myLog("Показатель Period "..tostring(period))
--myLog("Показатель degree "..tostring(degree))
--myLog("Показатель shift "..tostring(shift))
--myLog("--------------------------------------------------")
--- sx
sx={}
sx[1] = p+1
for mi=1, nn*2-2 do
local sum=0
for n=i0, i0+p do
sum = sum + math.pow(n,mi)
end
sx[mi+1] = sum
end
Reg = {}
Reg[index]= 0
Trigger = {}
Trigger[index]= 0
ATR = {}
ATR[index] = 0
trend = {}
trend[index] = 1
calcAlgoValue = {}
calcAlgoValue[index] = 0
calcChartResults = {}
calcChartResults[index] = {}
return calcAlgoValue, trend, calcChartResults
end
Reg[index] = Reg[index-1]
Trigger[index] = Trigger[index-1]
calcAlgoValue[index] = calcAlgoValue[index-1]
calcChartResults[index] = calcChartResults[index-1]
trend[index] = trend[index-1]
ATR[index] = ATR[index-1]
if index<(periodATR+beginIndexToCalc) then
ATR[index] = 0
elseif index==(periodATR+beginIndexToCalc) then
local sum=0
for i = 1, periodATR do
sum = sum + dValue(i)
end
ATR[index]=sum / periodATR
elseif index>(periodATR+beginIndexToCalc) then
ATR[index]=(ATR[index-1] * (periodATR-1) + dValue(index)) / periodATR
--ATR[index] = kawg*dValue(index)+(1-kawg)*ATR[index-1]
end
if index <= beginIndexToCalc + (math.max(period, periodATR) + shift + 1) or index > endIndexToCalc then
return calcAlgoValue, trend, calcChartResults
end
local typeVal = 'C'
--- syx
for mi=1, nn do
sum = 0
for n=i0, i0+p do
if DS:C(index+n-period) ~= nil then
if mi==1 then
sum = sum + dValue(index+n-period,typeVal)
else
sum = sum + dValue(index+n-period,typeVal)*math.pow(n,mi-1)
end
end
end
b[mi]=sum
end
--- Matrix
for jj=1, nn do
for ii=1, nn do
kk=ii+jj-1
ai[ii][jj]=sx[kk]
end
end
--- Gauss
for kk=1, nn-1 do
ll=0
mm=0
for ii=kk, nn do
if math.abs(ai[ii][kk])>mm then
mm=math.abs(ai[ii][kk])
ll=ii
end
end
if ll==0 then
return calcAlgoValue
end
if ll~=kk then
for jj=1, nn do
tt=ai[kk][jj]
ai[kk][jj]=ai[ll][jj]
ai[ll][jj]=tt
end
tt=b[kk]
b[kk]=b[ll]
b[ll]=tt
end
for ii=kk+1, nn do
qq=ai[ii][kk]/ai[kk][kk]
for jj=1, nn do
if jj==kk then
ai[ii][jj]=0
else
ai[ii][jj]=ai[ii][jj]-qq*ai[kk][jj]
end
end
b[ii]=b[ii]-qq*b[kk]
end
end
x[nn]=b[nn]/ai[nn][nn]
for ii=nn-1, 1, -1 do
tt=0
for jj=1, nn-ii do
tt=tt+ai[ii][ii+jj]*x[ii+jj]
x[ii]=(1/ai[ii][ii])*(b[ii]-tt)
end
end
---
--for n=i0, i0+p do
local n = p
sum=0
for kk=1, degree do
sum = sum + x[kk+1]*math.pow(n,kk)
end
--fx_buffer[n]=x[1]+sum
Reg[index]=round(x[1]+sum, 5)
--end
--- Std
--sq=0.0
--for n=i0, i0+p do
-- if dValue(index+n-period,typeVal) ~= nil then
-- sq = sq + math.pow(dValue(index+n-period,typeVal)-fx_buffer[n],2)
-- end
--end
--
--sq = math.sqrt(sq/(p-1))*kstd
--
--for n=i0, i0+p do
-- sqh_buffer[index+n-period]=round(fx_buffer[n]+sq, 5)
-- sql_buffer[index+n-period]=round(fx_buffer[n]-sq, 5)
--end
Trigger[index] = 2.0*Reg[index]-(Reg[index-shift] or 0)
local isUpPinBar = DS:C(index)>DS:O(index) and (DS:H(index)-DS:C(index))/(DS:H(index) - DS:L(index))>=0.5
local isLowPinBar = DS:C(index)<DS:O(index) and (DS:C(index)-DS:L(index))/(DS:H(index) - DS:L(index))>=0.5
local isBuy = trend[index] <= 0 and Reg[index] > Reg[index-1] and Trigger[index] > Reg[index]
local isSell = trend[index] >= 0 and Reg[index] < Reg[index-1] and Trigger[index] < Reg[index]
if isBuy then
trend[index] = 1
end
if isSell then
trend[index] = -1
end
calcAlgoValue[index] = DS:C(index)
calcChartResults[index] = {Reg[index], Trigger[index]}
--calcChartResults[index] = {calcAlgoValue[index], TEMA[index-shift]}
--myLog("index "..tostring(index)..", calcChartResults1 "..tostring(calcChartResults[index][1])..", calcChartResults2 "..tostring(calcChartResults[index][2]))
return calcAlgoValue, trend, calcChartResults
end
local newIndex = #ALGORITHMS['names']+1
ALGORITHMS['names'][newIndex] = "Reg"
ALGORITHMS['initParams'][newIndex] = initReg
ALGORITHMS['initAlgorithms'][newIndex] = initReg
ALGORITHMS['itetareAlgorithms'][newIndex] = iterateReg
ALGORITHMS['calcAlgorithms'][newIndex] = Reg
ALGORITHMS['tradeAlgorithms'][newIndex] = simpleTrade
ALGORITHMS['settings'][newIndex] = RegSettings