forked from Amar0628/MQL5-Python-Backtesting
-
Notifications
You must be signed in to change notification settings - Fork 27
/
actionWriter.py
100 lines (77 loc) · 4.15 KB
/
actionWriter.py
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
from datetime import datetime
import time
import os
import copy, sys
import pandas as pd
import numpy as np
import talib
import json
from output import output
class actionWriter():
def __init__(self, trading_algrithm):
self.trading_algrithm = trading_algrithm
def write_strategies(self, data):
with open('action_test.txt', 'w') as outfile:
json.dump(data, outfile)
def save2csv(self,output_save, predict_result, contents, signal, prev_signal, df):
output_save.save_csv(contents, df, signal, prev_signal, predict_result)
def cleanFile(self, filename):
del_f = open(filename, "w")
del_f.close()
def run(self):
filename = "time_close_csv_test.csv"
pre_Timebar = 0
output_save = output()
check_point = 0
if os.path.isfile(filename) and os.stat(filename).st_size != 0:
print("File exist and not empty")
while True:
if os.stat(filename).st_size != 0:
try:
with open(filename, encoding='utf-16') as f:
contents = f.read()
# you may also want to remove whitespace characters like `\n` at the end of each line
contents = contents.splitlines()
contents = [x.split('\t') for x in contents]
for i in range(len(contents)):
contents[i][0] = datetime.strptime(contents[i][0], '%Y.%m.%d %H:%M:%S')
contents[i][1] = float(contents[i][1]) #open
contents[i][2] = float(contents[i][2]) #high
contents[i][3] = float(contents[i][3]) #low
contents[i][4] = float(contents[i][4]) #close
contents[i][5] = int(contents[i][5]) #tick value
newTimebar = contents[-1][0]
curr_position = contents[-1][-1]
curr_close_price = contents[-1][4]
if curr_position == "Ending":
print(">>>------------------------<<<")
output_save.output_csv()
print(">>> Server Stop <<<")
break
else:
if pre_Timebar != newTimebar:
pre_Timebar = copy.deepcopy(newTimebar)
print("Timebar: ",pre_Timebar)
print("curr_close_price: ",curr_close_price)
print("curr_position", curr_position)
# code from example2.py, send the data to the main_DecisionMaker.py
predict_result, signal, prev_signal, df = self.trading_algrithm.predict(contents)
if type(predict_result) is not dict:
raise ValueError("Value must return a dictionary type")
print("predict_result","\t",predict_result)
# write the result to txt or csv
self.write_strategies(predict_result)
# self.cleanFile(filename)
self.save2csv(output_save, predict_result, contents, signal, prev_signal, df)
check_point += 1
if check_point % 50 == 0:
output_save.output_csv()
else:
time.sleep(0.003)
except :
continue
else:
# print("File is empty")
time.sleep(0.001)
else:
print("File not exist")