-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathta_test.py
More file actions
380 lines (329 loc) · 16.2 KB
/
ta_test.py
File metadata and controls
380 lines (329 loc) · 16.2 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
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
import pandas as pd
from pandas import Timedelta
from signals import *
from datetime import timedelta, datetime
from interval_convertor import interval_to_int, str_to_interval, interval_to_string
from signals import get_signal_by_type
def income_percent_diff(df1, df2):
if len(df1) > 0 and len(df2) > 0:
print("Кількість угод в новій стратегії:", len(df1.index))
print("Кількість угод в старій стратегії:", len(df2.index))
for i in range(1, 11):
print()
profit_data_1 = df1["is_profit_after_bars_" + str(i)].value_counts()
profit_data_2 = df2["is_profit_after_bars_" + str(i)].value_counts()
try:
los1 = profit_data_1[False]
except Exception:
los1 = 1
try:
los2 = profit_data_2[False]
except Exception:
los2 = 1
try:
prof1 = profit_data_1[True]
except Exception:
prof1 = 0
try:
prof2 = profit_data_2[True]
except Exception:
prof2 = 0
profit_percent_1 = prof1 / los1
profit_percent_2 = prof2 / los2
print(f" Час угоди {i} свічок \n\tРізниця профіту:", profit_percent_1 - profit_percent_2)
print(f" Профіт новий:", profit_percent_1)
print(f" Профіт старий:", profit_percent_2, "\n")
else:
print("no data")
def income_percent(df, column):
if len(df) > 0:
print("Кількість угод в новій стратегії:", len(df.index), 5000/len(df.index))
profit_data = df[column].value_counts()
try:
los1 = profit_data[False]
except Exception:
los1 = 1
try:
prof1 = profit_data[True]
except Exception:
prof1 = 0
profit_percent = prof1 / los1
print(f"Профіт: \n", profit_percent)
else:
print("no data")
def reverse_results(df):
if len(df) > 0:
for i in range(1, 11):
new_profit = df.loc[df.index, "is_profit_after_bars_" + str(i)].apply(lambda el: not (bool(el)))
df.loc[df.index, "is_profit_after_bars_" + str(i)] = new_profit
return df
def import_dfs(paths, elements_count=-1):
if elements_count > 0:
df = pd.read_csv(paths[0]).loc[:elements_count]
else:
df = pd.read_csv(paths[0])
df = df.drop(columns=["Unnamed: 0"])
for path in paths[1:]:
if elements_count > 0:
df = pd.concat([df, pd.read_csv(path).loc[:elements_count]]).reset_index(drop=True)
else:
df = pd.concat([df, pd.read_csv(path)]).reset_index(drop=True)
return df
def get_analzied_df(df):
def multicolumn_analize(df: pd.DataFrame):
return_df = pd.DataFrame()
main_column = "SuperOrderBlock"
one_of_columns = ["Volume", "UMA", "ScalpPro"] # "Volume", "UMA", "ScalpPro" "ScalpPro8108", "Volume2", "UMA20", "NW"
for ind in df.index:
columns_long_count = 0
columns_short_count = 0
for col in one_of_columns:
df_el = df.loc[ind, col]
if df_el == LongSignal().type:
columns_long_count += 1
elif df_el == ShortSignal().type:
columns_short_count += 1
counts = [columns_long_count, columns_short_count]
cols_count = max(counts)
if cols_count >= 2:
index = counts.index(cols_count)
df_el = df.loc[ind, main_column]
if df_el == ShortSignal().type and index == 1 or df_el == LongSignal().type and index == 0:
return_df = pd.concat([return_df, df.loc[ind:ind]])
# print("проводимо аналіз для індикаторів:", one_of_columns, main_column)
return return_df
col = "SuperOrderBlock"
df_long_old = df[df[col] == LongSignal().type]
df_short_old = df[df[col] == ShortSignal().type]
# reverse_results(df_short_old)
return_df = pd.concat([df_long_old, df_short_old]).reset_index(drop=True)
return_df = multicolumn_analize(return_df).reset_index(drop=True)
return return_df
def get_analzied_df_multitimeframe(df, sob_dfs, successful_sob_counts):
indexes_to_delete = []
for row_index in df.index:
sob_count = 0
_date = datetime.strptime(df.loc[row_index, "datetime"], '%Y-%m-%d %H:%M:%S')
_date = timedelta(days=_date.day, hours=_date.hour, minutes=_date.minute)
_date = _date / Timedelta(minutes=1)
for sob_df_index in range(len(sob_dfs)):
sob_indexes_to_delete = []
prev_date_date = None
prev_date = None
prev_index = None
next_date = False
for sob_df_row_index in sob_dfs[sob_df_index].index:
if next_date:
if sob_dfs[sob_df_index].loc[sob_df_row_index, "SOB_No_Delta"] == df.loc[row_index, "SOB_No_Delta"]:
sob_count += 1
break
sob_date = datetime.strptime(sob_dfs[sob_df_index].loc[sob_df_row_index, "datetime"],
'%Y-%m-%d %H:%M:%S')
sob_date = timedelta(days=sob_date.day, hours=sob_date.hour, minutes=sob_date.minute)
sob_date = sob_date / Timedelta(minutes=1)
if prev_date is None:
prev_date = sob_date
prev_date_date = sob_dfs[sob_df_index].loc[sob_df_row_index, "datetime"]
prev_index = sob_df_row_index
continue
if _date >= prev_date:
break
is_needed_date = sob_date <= _date < prev_date
if is_needed_date:
next_date = True
else:
sob_indexes_to_delete.append(prev_index)
prev_index = sob_df_row_index
prev_date_date = sob_dfs[sob_df_index].loc[sob_df_row_index, "datetime"]
prev_date = sob_date
if len(sob_indexes_to_delete) > 0:
sob_dfs[sob_df_index] = sob_dfs[sob_df_index].drop(index=sob_indexes_to_delete)
if sob_count < successful_sob_counts:
indexes_to_delete.append(row_index)
print("old ind to del", indexes_to_delete)
if len(indexes_to_delete) > 0:
df = df.drop(index=indexes_to_delete)
return df
def get_analzied_df_multitimeframe_with_dealtime(df, sob_dfs, successful_sob_counts):
df["dealtime"] = None
indexes_to_delete = []
# print(df.to_string())
for row_index in df.index:
sob_count = 0
intervals = []
_date = datetime.strptime(df.loc[row_index, "datetime"], '%Y-%m-%d %H:%M:%S')
_date = timedelta(days=_date.day, hours=_date.hour, minutes=_date.minute)
_date = _date / Timedelta(minutes=1)
for sob_df_index in range(len(sob_dfs)):
sob_indexes_to_delete = []
prev_date_date = None
prev_date = None
prev_index = None
next_date = False
for sob_df_row_index in sob_dfs[sob_df_index].index:
if next_date:
if sob_dfs[sob_df_index].loc[sob_df_row_index, "SOB_No_Delta"] == df.loc[row_index, "SOB_No_Delta"]:
intervals.append(sob_dfs[sob_df_index].loc[sob_df_row_index, "interval"])
sob_count += 1
break
sob_date = datetime.strptime(sob_dfs[sob_df_index].loc[sob_df_row_index, "datetime"],
'%Y-%m-%d %H:%M:%S')
sob_date = timedelta(days=sob_date.day, hours=sob_date.hour, minutes=sob_date.minute)
sob_date = sob_date / Timedelta(minutes=1)
if prev_date is None:
prev_date = sob_date
prev_date_date = sob_dfs[sob_df_index].loc[sob_df_row_index, "datetime"]
prev_index = sob_df_row_index
continue
if _date >= prev_date:
break
is_needed_date = sob_date <= _date < prev_date
if is_needed_date:
next_date = True
else:
sob_indexes_to_delete.append(prev_index)
prev_index = sob_df_row_index
prev_date_date = sob_dfs[sob_df_index].loc[sob_df_row_index, "datetime"]
prev_date = sob_date
if len(sob_indexes_to_delete) > 0:
sob_dfs[sob_df_index] = sob_dfs[sob_df_index].drop(index=sob_indexes_to_delete)
if sob_count < successful_sob_counts:
indexes_to_delete.append(row_index)
continue
else:
# print()
# print(df.loc[row_index, "symbol"], df.loc[row_index, "interval"], df.loc[row_index, "datetime"])
# print(intervals)
interval_minutes = interval_to_int(str_to_interval(df.loc[row_index, "interval"]))
sum = interval_minutes
for interval in intervals:
sum += interval_to_int(str_to_interval(interval))
sum /= len(intervals)+1
sum = round(sum/interval_minutes, 0)
df.loc[row_index, "dealtime"] = int(sum)
if len(indexes_to_delete) > 0:
df = df.drop(index=indexes_to_delete)
return df
paths = [
["debug/updated_EURUSD_Intervalin_1_minute.csv",
"debug/updated_EURUSD_Intervalin_3_minute.csv",
"debug/updated_EURUSD_Intervalin_5_minute.csv",
"debug/updated_EURUSD_Intervalin_15_minute.csv",
"debug/updated_EURUSD_Intervalin_30_minute.csv",
"debug/updated_EURUSD_Intervalin_45_minute.csv",
"debug/updated_EURUSD_Intervalin_1_hour.csv",
"debug/updated_EURUSD_Intervalin_2_hour.csv"],
["debug/updated_AUDCAD_Intervalin_1_minute.csv",
"debug/updated_AUDCAD_Intervalin_3_minute.csv",
"debug/updated_AUDCAD_Intervalin_5_minute.csv",
"debug/updated_AUDCAD_Intervalin_15_minute.csv",
"debug/updated_AUDCAD_Intervalin_30_minute.csv",
"debug/updated_AUDCAD_Intervalin_45_minute.csv",
"debug/updated_AUDCAD_Intervalin_1_hour.csv",
"debug/updated_AUDCAD_Intervalin_2_hour.csv"],
["debug/updated_AUDUSD_Intervalin_1_minute.csv",
"debug/updated_AUDUSD_Intervalin_3_minute.csv",
"debug/updated_AUDUSD_Intervalin_5_minute.csv",
"debug/updated_AUDUSD_Intervalin_15_minute.csv",
"debug/updated_AUDUSD_Intervalin_30_minute.csv",
"debug/updated_AUDUSD_Intervalin_45_minute.csv",
"debug/updated_AUDUSD_Intervalin_1_hour.csv",
"debug/updated_AUDUSD_Intervalin_2_hour.csv"],
["debug/updated_EURJPY_Intervalin_1_minute.csv",
"debug/updated_EURJPY_Intervalin_3_minute.csv",
"debug/updated_EURJPY_Intervalin_5_minute.csv",
"debug/updated_EURJPY_Intervalin_15_minute.csv",
"debug/updated_EURJPY_Intervalin_30_minute.csv",
"debug/updated_EURJPY_Intervalin_45_minute.csv",
"debug/updated_EURJPY_Intervalin_1_hour.csv",
"debug/updated_EURJPY_Intervalin_2_hour.csv"],
["debug/updated_EURCAD_Intervalin_1_minute.csv",
"debug/updated_EURCAD_Intervalin_3_minute.csv",
"debug/updated_EURCAD_Intervalin_5_minute.csv",
"debug/updated_EURCAD_Intervalin_15_minute.csv",
"debug/updated_EURCAD_Intervalin_30_minute.csv",
"debug/updated_EURCAD_Intervalin_45_minute.csv",
"debug/updated_EURCAD_Intervalin_1_hour.csv",
"debug/updated_EURCAD_Intervalin_2_hour.csv"],
["debug/updated_AUDCHF_Intervalin_1_minute.csv",
"debug/updated_AUDCHF_Intervalin_3_minute.csv",
"debug/updated_AUDCHF_Intervalin_5_minute.csv",
"debug/updated_AUDCHF_Intervalin_15_minute.csv",
"debug/updated_AUDCHF_Intervalin_30_minute.csv",
"debug/updated_AUDCHF_Intervalin_45_minute.csv",
"debug/updated_AUDCHF_Intervalin_1_hour.csv",
"debug/updated_AUDCHF_Intervalin_2_hour.csv"],
["debug/updated_GBPUSD_Intervalin_1_minute.csv",
"debug/updated_GBPUSD_Intervalin_3_minute.csv",
"debug/updated_GBPUSD_Intervalin_5_minute.csv",
"debug/updated_GBPUSD_Intervalin_15_minute.csv",
"debug/updated_GBPUSD_Intervalin_30_minute.csv",
"debug/updated_GBPUSD_Intervalin_45_minute.csv",
"debug/updated_GBPUSD_Intervalin_1_hour.csv",
"debug/updated_GBPUSD_Intervalin_2_hour.csv"],
["debug/updated_AUDJPY_Intervalin_1_minute.csv",
"debug/updated_AUDJPY_Intervalin_3_minute.csv",
"debug/updated_AUDJPY_Intervalin_5_minute.csv",
"debug/updated_AUDJPY_Intervalin_15_minute.csv",
"debug/updated_AUDJPY_Intervalin_30_minute.csv",
"debug/updated_AUDJPY_Intervalin_45_minute.csv",
"debug/updated_AUDJPY_Intervalin_1_hour.csv",
"debug/updated_AUDJPY_Intervalin_2_hour.csv"],
["debug/updated_GBPAUD_Intervalin_1_minute.csv",
"debug/updated_GBPAUD_Intervalin_3_minute.csv",
"debug/updated_GBPAUD_Intervalin_5_minute.csv",
"debug/updated_GBPAUD_Intervalin_15_minute.csv",
"debug/updated_GBPAUD_Intervalin_30_minute.csv",
"debug/updated_GBPAUD_Intervalin_45_minute.csv",
"debug/updated_GBPAUD_Intervalin_1_hour.csv",
"debug/updated_GBPAUD_Intervalin_2_hour.csv"]
]
# currencies = price_parser.get_currencies()
# intervals = [Interval.in_1_minute, Interval.in_3_minute, Interval.in_5_minute, Interval.in_15_minute,
# Interval.in_30_minute, Interval.in_45_minute, Interval.in_1_hour, Interval.in_2_hour]
#
# for path_arr_ind in range(len(paths)):
# curr = currencies[path_arr_ind]
# for ind in range(len(paths[path_arr_ind])):
# interval = intervals[ind]
# path_save = paths[path_arr_ind][ind]
# df_save = import_dfs([path_save])
#
# df_save["interval"] = interval
# df_save["symbol"] = curr[0]
# df_save.to_csv(path_save)
result_dfs_old = pd.DataFrame()
result_dfs_new = []
for path_arr in paths:
for ind in range(len(path_arr)-5):
df = import_dfs([path_arr[ind]])
sob_dfs = []
df_analized = get_analzied_df(df)
df_analized = df_analized.sort_values("datetime", ascending=False).reset_index(drop=True)
for sob_count in range(5):
result_dfs_new.append(pd.DataFrame())
for k in range(ind+1, len(path_arr)-3):
sob_dfs.append(import_dfs(path_arr[k:k+1]))
df_analized_mtf = get_analzied_df_multitimeframe_with_dealtime(df_analized, sob_dfs, sob_count)
result_dfs_new[sob_count] = pd.concat([result_dfs_new[sob_count], df_analized_mtf]).reset_index(drop=True)
for sob_count in range(5):
result_dfs_new[sob_count] = result_dfs_new[sob_count].sort_values("datetime").reset_index(drop=True)
result_dfs_new[sob_count]["profit_dealtime"] = None
result_dfs_new[sob_count]["profit_dealtime_6"] = None
for el_index in result_dfs_new[sob_count].index:
path = f"debug/updated_{result_dfs_new[sob_count].loc[el_index, 'symbol']}_{interval_to_string(str_to_interval(result_dfs_new[sob_count].loc[el_index, 'interval']))}.csv"
df_new_analize_half = import_dfs([path])
deal_time = int(result_dfs_new[sob_count].loc[el_index, "dealtime"])
index = df_new_analize_half.index[result_dfs_new[sob_count].loc[el_index, "datetime"] == df_new_analize_half["datetime"]].values[0] - deal_time
index_6 = df_new_analize_half.index[result_dfs_new[sob_count].loc[el_index, "datetime"] == df_new_analize_half["datetime"]].values[0] - 6
signal = get_signal_by_type(result_dfs_new[sob_count].loc[el_index, "SuperOrderBlock"])
open_price = result_dfs_new[sob_count].loc[el_index, "close_price"]
if index >= 0:
close_price = df_new_analize_half.loc[index, "close_price"]
result_dfs_new[sob_count].loc[el_index, "profit_dealtime"] = signal.is_profit(open_price, close_price)
if index_6 >= 0:
close_price_6 = df_new_analize_half.loc[index_6, "close_price"]
result_dfs_new[sob_count].loc[el_index, "profit_dealtime_6"] = signal.is_profit(open_price, close_price_6)
print("\nSOB count", sob_count)
income_percent(result_dfs_new[sob_count], "profit_dealtime")
income_percent(result_dfs_new[sob_count], "profit_dealtime_6")