-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathntradetg.h
487 lines (421 loc) · 14.9 KB
/
ntradetg.h
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#ifndef NTRADETG_H
#define NTRADETG_H
#include "nexamples.h"
#include "ntradetgtab.h"
//#include <math.h>
template <typename NType>
class NTradeTg: public NExamples<NType>
{
public:
NTradeTg();
NTradeTg(NTradeTg<NType>& obj);
virtual ~NTradeTg();
protected:
int period; //Период агрегации баров
NType koefTg; //Количество пунктов в цене
NType koefPrice; //Коэффициент преобразования цены
NType koefVolume; //Коэффициент преобразования объёма
int lenInBar; //Количество входных баров
NPriceDelta typeDelta; //Тип расчёта изменения цены
public:
NMatrix<NType> dataTg; //Преобразованная таблица данных
NTradeTgTab<NType> dataTab; //Таблица для обработки
NTradeTgTab<NType> dataZad; //Бары, преобразованные по периодам
public:
void setPeriod(int val);
int getPeriod();
void setKoefTg(NType val);
NType getKoefTg();
void setKoefPrice(NType val);
NType getKoefPrice();
void setKoefVolume(NType val);
NType getKoefVolume();
void setLenInBar(int val);
int getLenInBar();
NPriceDelta getTypeDelta();
public:
virtual void doShift(int shift, bool blShift); //Переместить на величину
virtual void deinit(); //Деинициализация
virtual void prerun(); //Предобработка данных
virtual void postrun(); //Постобработка данных
void calcPeriod(); //Бары, преобразованные по периодам
void calcTg(); //Преобразованная таблица данных
void calcExample(); //Формирование примеров
void funcInTgPrice(NArray<NType>& vec1, NArray<NType>& vec2, int ind, int jnd); //Преобразование
void funcInTgVolume(NArray<NType>& vec, int ind, int jnd);
NType funcOutTgPrice(NType& val);
public:
virtual void saveECSV(DataECSV& dt, string& parent);
virtual void loadECSV(DataECSV& dt, string& parent);
};
template <typename NType>
NTradeTg<NType>::NTradeTg():
NExamples<NType>()
{
this->koefTg = 10000;
this->koefPrice = 1;
this->koefVolume = 1;
this->period = 1;
this->lenInBar = 0;
this->typeDelta = NPriceDelta::PricePrev;
}
template <typename NType>
NTradeTg<NType>::NTradeTg(NTradeTg<NType>& obj):
NExamples<NType>(obj)
{
this->koefTg = obj.getKoefTg();
this->koefPrice = obj.getKoefPrice();
this->koefVolume = obj.getKoefVolume();
this->period = obj.getPeriod();
this->lenInBar = obj.getLenInBar();
this->typeDelta = obj.getTypeDelta();
}
template <typename NType>
NTradeTg<NType>::~NTradeTg()
{
this->deinit();
}
template <typename NType>
void NTradeTg<NType>::setPeriod(int val)
{
this->period = val;
}
template <typename NType>
int NTradeTg<NType>::getPeriod()
{
return this->period;
}
template <typename NType>
void NTradeTg<NType>::setKoefTg(NType val)
{
this->koefTg = val;
}
template <typename NType>
NType NTradeTg<NType>::getKoefTg()
{
return this->koefTg;
}
template <typename NType>
void NTradeTg<NType>::setKoefPrice(NType val)
{
this->koefPrice = val;
}
template <typename NType>
NType NTradeTg<NType>::getKoefPrice()
{
return this->koefPrice;
}
template <typename NType>
void NTradeTg<NType>::setKoefVolume(NType val)
{
this->koefVolume = val;
}
template <typename NType>
NType NTradeTg<NType>::getKoefVolume()
{
return this->koefVolume;
}
template <typename NType>
void NTradeTg<NType>::setLenInBar(int val)
{
this->lenInBar = val;
}
template <typename NType>
int NTradeTg<NType>::getLenInBar()
{
return this->lenInBar;
}
template <typename NType>
NPriceDelta NTradeTg<NType>::getTypeDelta()
{
return this->typeDelta;
}
template <typename NType>
void NTradeTg<NType>::doShift(int shift, bool blShift)
{
NExample<NType> *exm1, *exm2;
int ind;
//Задание типов примерам
for(ind = 0; ind < shift; ind++)
{
exm1 = this->get(this->beginset + ind);
if(blShift)
{
exm2 = this->get(this->endset + ind);
exm2->setTypeSet(exm1->getTypeSet());
}
if(this->blClearTypeSet)
{
//exm1->setEnergy(0);
exm1->setTypeSet(NSetType::NSetNone);
}
}
//Заполнение итоговой таблицы calc
if(this->blClearTypeSet)
{
for(ind = this->beginset; ind < this->beginset + shift; ind++)
{
this->dataZad.high_calc[ind+this->lenInBar+1] = 0;
this->dataZad.low_calc[ind+this->lenInBar+1] = 0;
this->dataZad.energy[ind+this->lenInBar+1] = 0;
this->dataZad.typeExample[ind+this->lenInBar+1] = NSetType::NSetNone;
}
}
this->shiftset(shift);
}
template <typename NType>
void NTradeTg<NType>::deinit()
{
this->dataTg.clear();
this->dataTab.deinit();
this->dataZad.deinit();
NExamples<NType>::deinit();
}
template <typename NType>
void NTradeTg<NType>::prerun()
{
if(this->limitset == 0 || this->limitset > this->dataTab.getLenRow()) {this->limitset = this->dataTab.getLenRow();}
this->calcPeriod();
this->calcTg();
this->calcExample();
this->filloutput();
this->filloutrun();
this->update();
//this->beginset = 0;
//this->testset = this->getLength();
//this->validset = this->getLength();
//this->endset = this->getLength();
}
template <typename NType>
void NTradeTg<NType>::postrun()
{
int pos;
NExample<NType>* exm;
if(!this->blOriginal) {this->recovery();}
//Заполнение вектора постобработки
for(pos = this->beginset; pos < this->endset; pos++)
{
exm = this->get(pos);
exm->outpostrun.clear();
exm->outpostrun.push(funcOutTgPrice(exm->outrun[0]));
exm->outpostrun.push(funcOutTgPrice(exm->outrun[1]));
}
//Заполнение итоговой таблицы calc
if(this->typeDelta == NPriceDelta::PricePrev)
{
for(pos = this->beginset; pos < this->endset; pos++)
{
exm = this->get(pos);
this->dataZad.high_calc[pos+this->lenInBar+1] = this->dataZad.high_real[pos+this->lenInBar] + exm->outpostrun[0];
this->dataZad.low_calc[pos+this->lenInBar+1] = this->dataZad.low_real[pos+this->lenInBar] + exm->outpostrun[1];
this->dataZad.energy[pos+this->lenInBar+1] = exm->getEnergy();
this->dataZad.typeExample[pos+this->lenInBar+1] = exm->getTypeSet();
}
}
else if(this->typeDelta == NPriceDelta::PriceClose)
{
for(pos = this->beginset; pos < this->endset; pos++)
{
exm = this->get(pos);
this->dataZad.high_calc[pos+this->lenInBar+1] = this->dataZad.close_real[pos+this->lenInBar] + exm->outpostrun[0];
this->dataZad.low_calc[pos+this->lenInBar+1] = this->dataZad.close_real[pos+this->lenInBar] + exm->outpostrun[1];
this->dataZad.energy[pos+this->lenInBar+1] = exm->getEnergy();
this->dataZad.typeExample[pos+this->lenInBar+1] = exm->getTypeSet();
}
}
}
template <typename NType>
void NTradeTg<NType>::calcPeriod()
{
int ind, knd = 0;
this->dataZad.init(this->limitset / this->period);
string vDate = this->dataTab.date[0];
string vTime = this->dataTab.time[0];
NType vOpen = this->dataTab.open_real[0];
NType vClose = this->dataTab.close_real[0];
NType curHigh, vHigh = this->dataTab.high_real[0];
NType curLow, vLow = this->dataTab.low_real[0];
NType vVolume = 0;
for(ind = 1; ind <= this->limitset; ind++)
{
vClose = this->dataTab.close_real[ind-1];
curHigh = this->dataTab.high_real[ind-1];
curLow = this->dataTab.low_real[ind-1];
vVolume += this->dataTab.volume_real[ind-1];
if(vHigh < curHigh) {vHigh = curHigh;}
if(vLow > curLow) {vLow = curLow;}
if(ind % this->period == 0)
{
this->dataZad.date[knd] = vDate;
this->dataZad.time[knd] = vTime;
this->dataZad.open_real[knd] = vOpen;
this->dataZad.high_real[knd] = vHigh;
this->dataZad.low_real[knd] = vLow;
this->dataZad.close_real[knd] = vClose;
this->dataZad.volume_real[knd] = vVolume;
if(ind < this->limitset)
{
vDate = this->dataTab.date[ind];
vTime = this->dataTab.time[ind];
vOpen = this->dataTab.open_real[ind];
vHigh = this->dataTab.high_real[ind];
vLow = this->dataTab.low_real[ind];
vVolume = 0;
knd++;
}
}
}
}
template <typename NType>
void NTradeTg<NType>::calcTg()
{
this->dataTg.init(this->dataZad.getLenRow()-1, 5, 0);
if(this->typeDelta == NPriceDelta::PricePrev)
{
for(int ind = 1; ind < this->dataTg.getLenRow(); ind++)
{
funcInTgPrice(this->dataZad.open_real, this->dataZad.open_real, ind, 0);
funcInTgPrice(this->dataZad.high_real, this->dataZad.high_real, ind, 1);
funcInTgPrice(this->dataZad.low_real, this->dataZad.low_real, ind, 2);
funcInTgPrice(this->dataZad.close_real, this->dataZad.close_real, ind, 3);
funcInTgVolume(this->dataZad.volume_real, ind, 4);
}
}
else if(this->typeDelta == NPriceDelta::PriceClose)
{
for(int ind = 1; ind < this->dataTg.getLenRow(); ind++)
{
funcInTgPrice(this->dataZad.close_real, this->dataZad.open_real, ind, 0);
funcInTgPrice(this->dataZad.close_real, this->dataZad.high_real, ind, 1);
funcInTgPrice(this->dataZad.close_real, this->dataZad.low_real, ind, 2);
funcInTgPrice(this->dataZad.close_real, this->dataZad.close_real, ind, 3);
funcInTgVolume(this->dataZad.volume_real, ind, 4);
}
}
}
template <typename NType>
void NTradeTg<NType>::calcExample()
{
this->emptyExamples();
int len = this->dataTg.getLenRow() - this->lenInBar;
NType value = 0;
for(int ind = 0; ind < len; ind++)
{
NExample<NType>* pExam = new NExample<NType>();
//Вход
for(int knd = 0; knd < this->lenInBar; knd++)
{
for(int jnd = 1; jnd < this->dataTg.getLenColumn(); jnd++)
{
value = this->dataTg[ind + knd][jnd];
pExam->input.push(value);
}
}
//Выход
value = this->dataTg[ind + this->lenInBar][1];
pExam->output.push(value);
value = this->dataTg[ind + this->lenInBar][2];
pExam->output.push(value);
this->push(pExam);
}
}
template <typename NType>
void NTradeTg<NType>::funcInTgPrice(NArray<NType>& vec1, NArray<NType>& vec2, int ind, int jnd)
{
NType dValue = vec2[ind] - vec1[ind-1];
dValue = NFUNC_ATAN(this->koefTg * this->koefPrice * dValue);
this->dataTg.set(dValue, ind-1, jnd);
}
template <typename NType>
void NTradeTg<NType>::funcInTgVolume(NArray<NType>& vec, int ind, int jnd)
{
NType dValue = vec[ind] - vec[ind-1];
dValue = NFUNC_ATAN(this->koefVolume * dValue);
this->dataTg.set(dValue, ind-1, jnd);
}
template <typename NType>
NType NTradeTg<NType>::funcOutTgPrice(NType& val)
{
return NFUNC_TAN(val) / (this->koefTg * this->koefPrice);
}
template <typename NType>
void NTradeTg<NType>::saveECSV(DataECSV& dt, string& parent)
{
NMatrix<string> str_mtrx;
vector<string> str_vec;
string str_val;
string fieldTab = parent + ".dataTab";
string fieldZad = parent + ".dataZad";
string fieldExamples = parent + ".examples";
dt.addGroup(parent, "");
str_val = to_string(this->period); dt.addElement(parent, "period", str_val, typeid(int).name());
str_val = to_string(this->koefTg); dt.addElement(parent, "koefTg", str_val, typeid(NType).name());
str_val = to_string(this->koefPrice); dt.addElement(parent, "koefPrice", str_val, typeid(NType).name());
str_val = to_string(this->koefVolume); dt.addElement(parent, "koefVolume", str_val, typeid(NType).name());
str_val = to_string(this->lenInBar); dt.addElement(parent, "lenInBar", str_val, typeid(int).name());
str_val = to_string(this->typeDelta); dt.addElement(parent, "typeDelta", str_val, typeid(int).name());
//to_matrix_string(str_mtrx, this->dataTg); dt.addElement(parent, "dataTg", str_mtrx, typeid(NType).name());
//this->dataTab.saveECSV(dt, fieldTab);
this->dataZad.saveECSV(dt, fieldZad);
NExamples<NType>::saveECSV(dt, fieldExamples);
}
template <typename NType>
void NTradeTg<NType>::loadECSV(DataECSV& dt, string& parent)
{
StructECSV* iter;
//NExample<NType>* exm;
NMatrix<string> str_mtrx;
//vector<string> str_vec;
string str_val;
vector<string> subpath;
string fieldTab = parent + ".dataTab";
string fieldZad = parent + ".dataZad";
string fieldExamples = parent + ".examples";
NTradeTg<NType>::deinit();
if(dt.isOneMatrix())
{
this->dataTab.loadECSV(dt, parent);
}
else
{
//Поля класса
size_t ind = dt.getShift();
iter = dt.modules[ind];
if(iter->cmpPath(parent, true)) {ind++;}
while(dt.isNextBlock(ind, parent))
{
iter = dt.modules[ind];
if(iter->getFieldValue(parent, "period", str_val)) {to_value(this->period, str_val); ind++;}
else if(iter->getFieldValue(parent, "koefTg", str_val)) {to_value(this->koefTg, str_val); ind++;}
else if(iter->getFieldValue(parent, "koefPrice", str_val)) {to_value(this->koefPrice, str_val); ind++;}
else if(iter->getFieldValue(parent, "koefVolume", str_val)) {to_value(this->koefVolume, str_val); ind++;}
else if(iter->getFieldValue(parent, "lenInBar", str_val)) {to_value(this->lenInBar, str_val); ind++;}
else if(iter->getFieldValue(parent, "typeDelta", str_val)) {to_value(this->typeDelta, str_val); ind++;}
else if(iter->getFieldValue(parent, "dataTg", str_mtrx)) {to_matrix_value(this->dataTg, str_mtrx); ind++;}
else if(iter->cmpPath(fieldTab, false)) //Субполя класса
{
dt.setShift(ind);
this->dataTab.loadECSV(dt, fieldTab);
ind = dt.getShift();
}
else if(iter->cmpPath(fieldZad, false)) //Субполя класса
{
dt.setShift(ind);
this->dataZad.loadECSV(dt, fieldZad);
ind = dt.getShift();
}
else if(iter->cmpPath(fieldExamples, false)) //Субполя класса
{
dt.setShift(ind);
NExamples<NType>::loadECSV(dt, fieldExamples);
ind = dt.getShift();
}
else
{
ind++;
}
}
dt.setShift(ind);
}
}
#endif // NTRADETG_H