-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (140 loc) · 6.84 KB
/
main.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
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
import numpy as np
import pandas as pd
import logging
import time
import datetime
import MetaTrader5 as mt5
from models.model import model
from mt5_actions.authorize import login
from mt5_actions.rates import get_curr_rates
from mt5_actions.order import buy_order, sell_order, check_order,change_tp_sl
from mt5_global.settings import symbol, timeframe,time_series,Debug,timezone,Trade_with_signals
from models.model import scaler
import talib as ta
from technical_analysis.signal import Signal
saved_model = None
def trade_with_model():
if not login():
print('login failed')
return
rates = get_curr_rates(symbol,timeframe, time_series)
while True:
try:
signal = Signal()
signal.get_signal()
curr_rate =get_curr_rates(symbol,timeframe, time_series)
curr_rate_frame = pd.DataFrame(curr_rate)
curr_rate_frame_last = curr_rate_frame.tail(1)
previous_rates_frame = pd.DataFrame(rates)
previous_rates_frame_last = previous_rates_frame.tail(1)
#check for a new candle to be formed
if int(curr_rate_frame_last['time'])== int(previous_rates_frame_last['time']):
time.sleep(2)
continue
with open('C:\mt5_bots\mt5_EA_v3\logs_dir\signal.log','a') as f:
f.write(str(datetime.datetime.now(timezone)))
f.write(str(Signal().get_signal()))
#add new line
f.write('\n')
previous_rates_frame=previous_rates_frame.drop(['real_volume'], axis=1)
previous_rates_frame['momentum'] = previous_rates_frame['open'] - previous_rates_frame['close']
previous_rates_frame['average'] = (previous_rates_frame['high'] + previous_rates_frame['low'])/2
previous_rates_frame['range'] = previous_rates_frame['high'] - previous_rates_frame['low']
previous_rates_frame['ohlc'] = (previous_rates_frame['open'] + previous_rates_frame['high'] + previous_rates_frame['low'] + previous_rates_frame['close'])/4
previous_rates_frame['time'] = pd.to_datetime(previous_rates_frame['time'], unit='s')
previous_rates_frame = previous_rates_frame.set_index('time')
x = scaler.transform(previous_rates_frame)
x = x.reshape(x.shape[0],x.shape[1],time_series)
#predict
prediction = model.predict(x) #scale back
print("prediction: ",prediction)
prediction_copies = np.repeat(prediction,10, axis=-1)
prediction = scaler.inverse_transform(prediction_copies)
print("prediction: ",prediction)
prediction = np.round(prediction[-1][3],5)
#get current price
curr_price = mt5.symbol_info_tick(symbol).ask
curr_price_bid = mt5.symbol_info_tick(symbol).bid
print("#"*50)
print('prediction: ',prediction)
print('current ask price: ',curr_price)
print('current bid price: ',curr_price_bid)
#log prediction,current price and signal type to signal_prediction.log
with open('C:\mt5_bots\mt5_EA_v3\logs_dir\signal_prediction.log','a') as f:
f.write(str(datetime.datetime.now(timezone)))
if prediction > curr_price:
f.write('buy')
elif prediction < curr_price:
f.write('sell')
f.write(str(signal.check_signal_type()))
#add new line
f.write('\n')
order_dic = check_order()
if prediction > mt5.symbol_info_tick(symbol).ask:
if order_dic['buy']:
print('buy order already exists')
continue
buy_order(prediction,symbol)
elif prediction < mt5.symbol_info_tick(symbol).bid:
if order_dic['sell']:
print('sell order already exists')
continue
sell_order(prediction,symbol)
else:
print('no action')
rates = get_curr_rates(symbol,timeframe, time_series)
except Exception as e:
#log the error to error.log file using python logging module
logging.basicConfig(filename='C:\mt5_bots\mt5_EA_v3\logs_dir\error.log',level=logging.ERROR,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',)
logging.exception(e)
print("order failed")
time.sleep(2)
pass
def trade_with_signal():
rates = get_curr_rates(symbol,timeframe, time_series)
print('.... waiting for signal ....')
while True:
try:
#check for spread if spread is greater than 5 pips do not trade
change_tp_sl()
curr_rate =get_curr_rates(symbol,timeframe, time_series)
curr_rate_frame = pd.DataFrame(curr_rate)
curr_rate_frame_last = curr_rate_frame.tail(1)
previous_rates_frame = pd.DataFrame(rates)
previous_rates_frame_last = previous_rates_frame.tail(1)
if int(curr_rate_frame_last['time'])== int(previous_rates_frame_last['time']):
continue
if mt5.symbol_info_tick(symbol).ask - mt5.symbol_info_tick(symbol).bid < 20:
signal = Signal()
signal.get_signal()
#log signal to signal.log
with open('C:\mt5_bots\mt5_EA_v3\logs_dir\signal.log','a') as f:
f.write(str(datetime.datetime.now(timezone)))
f.write(str(Signal().get_signal()))
#add new line
f.write('\n')
if signal.check_signal_type() == 'buy':
price = mt5.symbol_info_tick(symbol).ask+0.00100
buy_order(price,symbol)
elif signal.check_signal_type() == 'sell':
price = mt5.symbol_info_tick(symbol).bid-0.00100
sell_order(price,symbol)
else:
print('no signal')
rates = get_curr_rates(symbol,timeframe, time_series)
print('.... waiting for another signal ....')
else:
print('spread is greater than 5 pips')
continue
except Exception as e:
#log the error to error.log file using python logging module
logging.basicConfig(filename='C:\mt5_bots\mt5_EA_v3\logs_dir\error.log',level=logging.ERROR,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',)
logging.exception(e)
print("order failed")
rates = get_curr_rates(symbol,timeframe, time_series)
continue
if __name__ == "__main__":
if Trade_with_signals:
trade_with_signal()
else:
trade_with_model()