I need help , find any mistakes in my coding
import MetaTrader5 as mt5
import pandas as pd
import numpy as np
import time
# Define moving average periods and styles
ma_period_red = 450
ma_shift_red = 0
ma_method_red = mt5.MA_METHOD_LINEARWEIGHTED
ma_apply_red = mt5.MA_PRICE_LOW
ma_style_red = mt5.LINE_STYLE_SOLID
ma_width_red = 3
ma_period_blue = 75
ma_shift_blue = 0
ma_method_blue = mt5.MA_METHOD_LINEARWEIGHTED
ma_apply_blue = mt5.MA_PRICE_LOW
ma_style_blue = mt5.LINE_STYLE_SOLID
ma_width_blue = 3
# Define trade parameters
account_balance = mt5.account_info().balance
if account_balance < 50:
lot_size = 0.01
max_trades = np.random.randint(1, 6)
elif account_balance >= 50 and account_balance < 100:
lot_size = 1.0
max_trades = np.random.randint(3, 6)
else:
lot_size = 2.0
max_trades = np.random.randint(5, 11)
stop_loss = 30
trailing_stop = 3
# Set up the MetaTrader5 connection
if not mt5.initialize():
print("initialize() failed, error code =", mt5.last_error())
quit()
# Get all available symbols from the Market Watch
symbols = mt5.symbols_get()
# Filter out symbols with unsupported types, such as futures and options
supported_types = [mt5.SYMBOL_TYPE_FOREX, mt5.SYMBOL_TYPE_CFD]
symbols = [s for s in symbols if s.path[1] in supported_types]
# Set the timeframe for the chart
timeframe = mt5.TIMEFRAME_H1
# Loop over each symbol and trade
for s in symbols:
# Select the symbol for the chart
symbol = s.name
# Request the historical price data for the symbol
rates = mt5.copy_rates_from(symbol, timeframe, mt5.TIME_CURRENT)
# Convert the price data to a Pandas DataFrame
df = pd.DataFrame(rates)
df['time'] = pd.to_datetime(df['time'], unit='s')
df.set_index('time', inplace=True)
# Calculate the moving averages
ma_red = df['low'].rolling(ma_period_red).apply(lambda x: np.dot(x, np.linspace(1, 0, ma_period_red)), raw=True)
ma_red.index = df.index
ma_red.name = 'ma_red'
ma_blue = df['low'].rolling(ma_period_blue).apply(lambda x: np.dot(x, np.linspace(1, 0, ma_period_blue)), raw=True)
ma_blue.index = df.index
ma_blue.name = 'ma_blue'
# Check for moving average crossovers and execute trades
ma_crossover = np.where(ma_blue.shift(1) < ma_red.shift(1), 1, -1)
ma_crossover[0:ma_period_red] = 0
ma_crossover_df = pd.DataFrame(ma_crossover, index=df.index, columns=['ma_crossover'])
for i in range(1, len(ma_crossover)):
if ma_crossover[i] == 1 and ma_crossover[i-1] == -1:
# execute buy trade
print('Buying', symbol)
order = mt5.order_send(symbol=symbol, action=mt5.ORDER_TYPE_BUY, volume=lot_size, slippage=3, deviation=20, type_filling=mt5.ORDER_FILLING_FOK)
if order.retcode != mt5.TRADE_RETCODE_DONE:
print("Error executing buy trade:", order.comment)
else:
print("Buy trade executed successfully:", order)
elif ma_crossover[i] == -1 and ma_crossover[i-1] == 1:
# execute sell trade
print('Selling', symbol)
order = mt5.order_send(symbol=symbol, action=mt5.ORDER_TYPE_SELL, volume=lot_size, slippage=3, deviation=20, type_filling=mt5.ORDER_FILLING_FOK)
if order.retcode != mt5.TRADE_RETCODE_DONE:
print("Error executing sell trade:", order.comment)
else:
print("Sell trade executed successfully:", order)
# Plot the price data and moving averages on the chart
mt5.chart_create(symbol=symbol, timeframe=timeframe)
mt5.chart_set_integer(mt5.CHART_SHOW_OHLC, False)
mt5.chart_set_integer(mt5.CHART_SHOW_BID_LINE, False)
mt5.chart_set_integer(mt5.CHART_SHOW_PERIOD_SEP, True)
mt5.chart_set_integer(mt5.CHART_SHOW_GRID, True)
mt5.chart_set_integer(mt5.CHART_SHOW_VOLUME, False)
mt5.chart_set_integer(mt5.CHART_SHOW_OBJECT_DESCR, False)