-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoch_calculator.cpp
More file actions
231 lines (90 loc) · 3.77 KB
/
stoch_calculator.cpp
File metadata and controls
231 lines (90 loc) · 3.77 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
#include "stoch_calculator.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QEventLoop>
#include <QObject>
#include <QString>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QVariant>
#include <iostream>
#include <vector>
#include <QString>
Stoch_calculator::Stoch_calculator()
{
}
void Stoch_calculator::set_k_period(int period){
Stoch_calculator::k_period=period;
}
void Stoch_calculator::set_smoothing(int smoothing){
Stoch_calculator::smooth=smoothing;
}
double Stoch_calculator::simple_stochastic_calculate(std::vector<double> high, std::vector<double> low, double close)
{
std::stable_sort(high.begin(),high.end());
std::stable_sort(low.begin(),low.end());
return (close-low[0])/(high[Stoch_calculator::k_period-1]-low[0]);
}
double Stoch_calculator::Get_K_stochastic(int timeframe_num, QString symbol){
QString suffix; //timeframe type
if(timeframe_num<60) suffix="m"; //minute
else if(timeframe_num>=60){
suffix="h"; //hour
timeframe_num=timeframe_num/60;
}else if(timeframe_num==1440){
suffix="d"; //day
timeframe_num=1;
}else if(timeframe_num==10080){
suffix="w"; //week
timeframe_num=1;
}
QNetworkAccessManager *acc=new QNetworkAccessManager;
const QString url="https://api.binance.com/api/v3/klines"; //get data from binance
QNetworkRequest req((QUrl(url+"?symbol="+symbol+"&interval="+QString::number(timeframe_num)+suffix+"&startTime="+QString::number(QDateTime::currentDateTime().toTime_t()-((Stoch_calculator::k_period+Stoch_calculator::smooth)*60*timeframe_num))+"000"+"&endTime="+QString::number(QDateTime::currentDateTime().toTime_t()-(timeframe_num*60))+"000")));
QNetworkReply *rep=acc->get(req);
QEventLoop loop;
QObject::connect(rep,&QNetworkReply::finished,&loop,&QEventLoop::quit);
loop.exec();
auto doc=QJsonDocument::fromJson(rep->readAll());
double data[Stoch_calculator::k_period+smooth-1][4]; //data[x][1]--> high
//data[x][2]--> low
//data[x][3]--> close
QJsonArray array=doc.array();
int i=0;
for(auto const json:array){
if(json.type()==QJsonValue::Array){
int n=0;
for(auto const element:json.toArray()){
if(element.type()==QJsonValue::String){
QVariant var=element.toVariant();
data[i][n]=var.toDouble();
n++;
if(n==4) break;
}
}
}
i++;
}
std::vector <double> high;
std::vector <double> low;
double sum_stochastic=0;
int x=0;
for(int i=1;i<=smooth;i++){
int index=0;
high.clear();
low.clear();
while(index<Stoch_calculator::k_period){
high.push_back(data[k_period+smooth-2-index-x][1]);
low.push_back(data[k_period+smooth-2-index-x][2]);
index++;
}
sum_stochastic+=Stoch_calculator::simple_stochastic_calculate(high,low,data[k_period+smooth-2-x][3]);
x++;
}
// double stochastic_level=(data[Stoch_calculator::k_period-1][3]-low[0])/(high[Stoch_calculator::k_period-1]-low[0]); //Stochastic_formula
return (sum_stochastic/smooth)*100;
}