-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBaseAlgorithmEasy.hpp
More file actions
212 lines (197 loc) · 11.3 KB
/
Copy pathBaseAlgorithmEasy.hpp
File metadata and controls
212 lines (197 loc) · 11.3 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
/*
* binary-cpp-api - Binary C++ API client
*
* Copyright (c) 2018 Elektro Yar. Email: git.electroyar@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef BASEALGORITHMEASY_HPP_INCLUDED
#define BASEALGORITHMEASY_HPP_INCLUDED
//------------------------------------------------------------------------------
#include "HistoricalDataEasy.hpp"
#include "BasePayoutModelEasy.hpp"
#include "BinaryApiCommon.hpp"
//------------------------------------------------------------------------------
namespace BaseAlgorithmEasy
{
using namespace BinaryApiCommon;
//------------------------------------------------------------------------------
/** \brief Класс для построения алгоритмов торговли
*/
class BigFatherRobots {
public:
/** \brief Сбросить состояния индикаторов
* Данный метод очищает внутренние состояния индикаторов
*/
virtual void reset_indicators() {};
/** \brief Протестировать индикаторы робота на цене
* \param price цена
* \param timestamp временная метка
* \param indx индекс котировок
* \param forecast состояние прогнозирования робота
* \return вернет 0 в случае успеха
*/
virtual int test_indicators(double price, unsigned long long timestamp, int indx, int &forecast) {return DATA_NOT_AVAILABLE;};
/** \brief Обновить внутренние состояния индикаторов робота
* \param price цена
* \param timestamp временная метка
* \param indx индекс котировок
* \param forecast состояние прогнозирования робота
* \return вернет 0 в случае успеха
*/
virtual int update_indicators(double price, unsigned long long timestamp, int indx, int &forecast) {return DATA_NOT_AVAILABLE;};
/** \brief Обновить внутренние состояния индикаторов робота
* \param price цены
* \param timestamp временная метка
* \param forecast состояние прогнозов робота
* \return вернет 0 в случае успеха
*/
virtual int update_indicators(std::vector<double> &price, unsigned long long timestamp, std::vector<int> &forecast, std::vector<int> &bots) {return DATA_NOT_AVAILABLE;};
/** \brief Получить время экспирации опциона (в секундах)
* \return время экспирации опциона (значение меньше 0 означает ошибку)
*/
virtual int get_duration() {return DATA_NOT_AVAILABLE;};
/** \brief Установить длительность бинарного опциона
* \param duration длительность бинарного опциона в секундах
*/
virtual void set_duration(int duration) {};
/** \brief Получить задержку перед началом работы робота
* \return задержка в секундах
*/
virtual int get_delay_before_work() {
return DATA_NOT_AVAILABLE;
}
};
//------------------------------------------------------------------------------
/** \brief Класс робота для торговли по индикатору Боллинджер
*/
class SimpleBbRobot : public BigFatherRobots {
private:
std::vector<IndicatorsEasy::BollingerBands<double>> iBB;
int duration_ = xtime::SECONDS_IN_MINUTE * 3;
int delay_before_work_ = 0;
const int bot_id_ = 0;
public:
SimpleBbRobot() {};
/** \brief Инициализировать робота
* \param period периоды Боллинджера
* \param std_factor множители стандартного отклонения Боллинджера
*/
SimpleBbRobot(std::vector<int> periods, std::vector<double> std_factors) {
if(periods.size() != std_factors.size() || periods.size() == 0)
return;
iBB.resize(periods.size());
for(size_t i = 0; i < periods.size(); ++i) {
iBB[i] = IndicatorsEasy::BollingerBands<double>(periods[i], std_factors[i]);
delay_before_work_ = std::max(delay_before_work_, periods[i]);
}
delay_before_work_ *= xtime::SECONDS_IN_MINUTE;
};
/** \brief Сбросить состояния индикаторов
* Данный метод очищает внутренние состояния индикаторов
*/
virtual void reset_indicators()
{
for(size_t i = 0; i < iBB.size(); ++i) {
iBB[i].clear();
}
}
/** \brief Протестировать робота на цене
* \param price цена
* \param timestamp временная метка
* \param indx индекс котировок
* \param forecast состояние прогнозирования робота
* \return вернет 0 в случае успеха
*/
virtual int test_indicators(double price, unsigned long long timestamp, int indx, int &forecast)
{
forecast = NO_FORECAST;
if(indx >= iBB.size())
return INVALID_PARAMETER;
double ml, tl, bl;
int err = iBB[indx].test(price, tl, ml, bl);
if(err != OK) return err;
if(price > tl) {
forecast = DN;
} else
if(price < bl) {
forecast = UP;
}
return OK;
}
/** \brief Обновить внутренние состояния робота
* \param price цена
* \param timestamp временная метка
* \param indx индекс котировок
* \param forecast состояние прогнозирования робота
* \return вернет 0 в случае успеха
*/
virtual int update_indicators(double price, unsigned long long timestamp, int indx, int &forecast)
{
forecast = NO_FORECAST;
if(indx >= iBB.size())
return INVALID_PARAMETER;
double ml, tl, bl;
int err = iBB[indx].update(price, tl, ml, bl);
if(err != OK) return err;
if(price > tl) {
forecast = DN;
} else
if(price < bl) {
forecast = UP;
}
return OK;
}
/** \brief Обновить внутренние состояния робота
* \param price цены всех валютных пар
* \param timestamp временная метка
* \param forecast состояния прогнозирования робота
* \return вернет 0 в случае успеха
*/
virtual int update_indicators(std::vector<double> &price, unsigned long long timestamp, std::vector<int> &forecast, std::vector<int> &bots)
{
if(price.size() != iBB.size())
return INVALID_PARAMETER;
forecast.resize(iBB.size());
bots.resize(iBB.size());
for(size_t n = 0; n < iBB.size(); ++n) {
update_indicators(price[n], timestamp, n, forecast[n]);
bots[n] = bot_id_;
}
return OK;
}
/** \brief Установить длительность бинарного опциона
* \param duration длительность бинарного опциона в секундах
*/
virtual void set_duration(int duration) {
duration_ = duration;
};
/** \brief Получить время экспирации опциона (в секундах)
* \return время экспирации опциона (значение меньше 0 означает ошибку)
*/
virtual int get_duration() {return duration_;};
/** \brief Получить задержку перед началом работы робота
* \return задержка в секундах
*/
virtual int get_delay_before_work() {return delay_before_work_;}
};
//------------------------------------------------------------------------------
}
//------------------------------------------------------------------------------
#endif // BASEALGORITHMEASY_HPP_INCLUDED