I need help with an ea i have developed can you help me to run on mt5
####
####
Code:
import MetaTrader5 as mt5
from datetime import datetime, time, timedelta
# connect to MetaTrader 5
if not mt5.initialize():
print("initialize() failed")
mt5.shutdown()
exit()
# set up the trade parameters
symbol = "GBPJPY"
lot_size = 1
stop_loss_pips = 20
# switch to the daily timeframe
if not mt5.symbol_select(symbol, True):
print("symbol_select({}) failed, error code={}".format(symbol, mt5.last_error()))
mt5.shutdown()
quit()
# get the previous daily candle and its high and low levels
prev_daily_candle = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_D1, 1, 2)[1]
daily_high = prev_daily_candle['high']
daily_low = prev_daily_candle['low']
# switch back to the 15-minute timeframe
if not mt5.symbol_select(symbol, False):
print("symbol_select({}) failed, error code={}".format(symbol, mt5.last_error()))
mt5.shutdown()
quit()
# set up the time range for the program to run (between 23pm and 9pm GMT)
start_time = time(hour=23)
end_time = time(hour=21)
current_time = datetime.utcnow().time()
if current_time < start_time or current_time >= end_time:
print("The program is only allowed to run between 11pm and 9pm GMT")
mt5.shutdown()
quit()
#wait for the engulfing candle and pin bar retest
while True:
# check if the daily low has been engulfed
last_15min_candle = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_M15, 1, 1)[0]
if last_15min_candle['low'] > daily_low and last_15min_candle['close'] > daily_low:
# check if the engulfing candle body is above the daily high
if last_15min_candle['close'] > last_15min_candle['open']:
# wait for the pin bar retest
while True:
# check if it is past 9pm GMT
current_time = datetime.utcnow().time()
if current_time >= end_time:
print("It's past 9pm GMT. Stopping the program...")
mt5.shutdown()
quit()
# wait for the engulfing candle and pin bar retest
while True:
# check if the previous daily low has been engulfed
last_15min_candle = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_M15, 1, 1)[0]
if last_15min_candle['low'] < daily_low and last_15min_candle['close'] < daily_low:
# check if the engulfing candle body is below the daily low
if last_15min_candle['close'] < last_15min_candle['open']:
# wait for the pin bar retest
while True:
# check if it is past 9pm GMT
current_time = datetime.utcnow().time()
if current_time >= end_time:
print("It's past 9pm GMT. Stopping the program...")
mt5.shutdown()
quit()
# check if the price has retraced to the daily low level
current_price = mt5.symbol_info_tick(symbol).bid
if current_price >= daily_low:
# check if the current candle is a pin bar
last_15min_candles = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_M15, 1, 2)
pin_bar = False
if last_15min_candles[0]['open'] > last_15min_candles[0]['close'] and \
last_15min_candles[0]['close'] >= last_15min_candles[0]['high'] - \
(last_15min_candles[0]['high'] - last_15min_candles[0]['low']) * 0.3:
pin_bar = True
if pin_bar and last_15min_candles[1]['low'] > daily_low and \
last_15min_candles[0]['low'] <= daily_low - mt5.symbol_info(symbol).point * stop_loss_pips:
# place the sell order with stop loss
sell_request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot_size,
"type": mt5.ORDER_TYPE_SELL,
"price": current_price,
"sl": last_15min_candles[0]['high'] + mt5.symbol_info(symbol).point * stop_loss_pips,
"magic": 123456,
"comment": "Python trade",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_IOC
}
result = mt5.order_send(sell_request)
if result.retcode != mt5.TRADE_RETCODE_DONE:
print("Order failed with retcode={}".format(result.retcode))
else:
print("Order placed with position #{}".format(result.order))
break
mt5.sleep(5000) # wait 5 seconds before checking again