I try to create a simple trade bot in MetaTrader4 with MQL4 and with the help of AI I provide this code:
#property strict
// Input parameters
input double lotSize = 0.1;
input int maPeriod = 50;
input double riskPercentage = 2.0;
input int rsiPeriod = 14;
input int macdPeriod = 12;
input int macdSignalPeriod = 9;
input int bollingerPeriod = 20;
input int bollingerDevFactor = 2;
input double maxRiskPerTrade = 1.0;
input double maxDrawdown = 10.0;
input int maxConsecutiveLosses = 3;
input int maxTradesPerDay = 5;
input double atrMultiplier = 2.0;
input double trailingStopMultiplier = 1.0;
// Global variables
double maValue;
double currentPrice;
double rsiValue;
double macdValue;
double bollingerUpper;
double bollingerLower;
double stopLossPrice;
double takeProfitPrice;
double riskPerLot;
int maxLots;
double trailingStopPrice;
int maHandle;
int rsiHandle;
int macdHandle;
int bollingerHandle;
double accountBalance;
double riskAmount;
double atr;
double trailingStopDistance;
int consecutiveLosses;
int tradesToday;
int OnInit()
{
// Initialize moving average
maHandle = iMA(Symbol(), 0, maPeriod, 0, MODE_SMA, PRICE_CLOSE);
// Initialize RSI
rsiHandle = iRSI(Symbol(), 0, rsiPeriod, PRICE_CLOSE);
// Initialize MACD
macdHandle = iMACD(Symbol(), 0, macdPeriod, macdSignalPeriod, 0, PRICE_CLOSE);
// Initialize Bollinger Bands
bollingerHandle = iBands(Symbol(), 0, bollingerPeriod, bollingerDevFactor, 0, 0);
// Calculate risk amount
riskAmount = AccountBalance() * riskPercentage / 100.0;
// Initialize global variables
accountBalance = AccountBalance();
consecutiveLosses = 0;
tradesToday = 0;
return(INIT_SUCCEEDED);
}
void OnTick()
{
maValue = iMA(Symbol(), 0, maPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
currentPrice = MarketInfo(Symbol(), MODE_BID);
rsiValue = iRSI(Symbol(), 0, rsiPeriod, PRICE_CLOSE);
macdValue = iMACD(Symbol(), 0, macdPeriod, macdSignalPeriod, 0, 0);
bollingerUpper = iBands(Symbol(), 0, bollingerPeriod, bollingerDevFactor, 0, 1);
bollingerLower = iBands(Symbol(), 0, bollingerPeriod, bollingerDevFactor, 0, 2);
// Calculate ATR and trailing stop distance
atr = iATR(Symbol(), 0, 14);
trailingStopDistance = atr * trailingStopMultiplier;
// Check if maximum drawdown has been exceeded
if((AccountBalance() - accountBalance) / accountBalance * 100.0 < -maxDrawdown)
{
Print("Maximum drawdown exceeded. Trading halted.");
return;
}
// Check if maximum consecutive losses has been reached
if(consecutiveLosses >= maxConsecutiveLosses)
{
Print("Maximum consecutive losses reached. Trading halted.");
return;
}
// Check if maximum trades per day has been reached
if(tradesToday >= maxTradesPerDay)
{
Print("Maximum trades per day reached. Trading halted.");
return;
}
// Open buy trade if price crosses above MA and RSI is oversold
if(currentPrice > maValue && rsiValue < 30.0)
{
stopLossPrice = currentPrice - atr * atrMultiplier;
takeProfitPrice = currentPrice + atr * atrMultiplier;
riskPerLot = atr * atrMultiplier * lotSize;
maxLots = (int)(riskAmount / riskPerLot);
// Check if MACD is above the signal line and Bollinger bands are not too far apart
if(macdValue > iMACD(Symbol(), 0, macdPeriod, macdSignalPeriod, 0, 0, 1) && bollingerUpper - bollingerLower > 2.0 * Point)
{
// Check if maximum risk per trade has been exceeded
if(riskPerLot * lotSize / AccountBalance() * 100.0 > maxRiskPerTrade)
{
Print("Maximum risk per trade exceeded. Skipping trade.");
return;
}
if(maxLots > 0)
{
trailingStopPrice = currentPrice - trailingStopDistance;
int ticket = OrderSend(Symbol(), OP_BUY, lotSize, currentPrice, 3, stopLossPrice, takeProfitPrice, "Buy order", 0, 0, Green);
tradesToday++;
if(ticket > 0)
{
// Set trailing stop loss
double initialStopLoss = stopLossPrice;
while(OrderSelect(ticket, SELECT_BY_TICKET))
{
double currentProfit = OrderProfit();
double currentStopLoss = OrderStopLoss();
if(currentProfit > 0 && currentStopLoss < currentPrice - trailingStopDistance)
{
double newStopLoss = currentPrice - trailingStopDistance;
if(newStopLoss > initialStopLoss)
{
if(OrderModify(ticket, OrderOpenPrice(), newStopLoss, takeProfitPrice, 0, Green))
{
initialStopLoss = newStopLoss;
}
}
}
}
}
}
}
}
// Close buy trade if price falls below entry price plus a certain percentage
if(OrderType() == OP_BUY && currentPrice < (OrderOpenPrice() - (OrderOpenPrice() * 0.05)))
{
stopLossPrice = 0;
takeProfitPrice = 0;
riskPerLot = 0;
maxLots = 0;
trailingStopPrice = currentPrice + trailingStopDistance;
if(OrderClose(OrderTicket(), OrderLots(), stopLossPrice, takeProfitPrice, 0))
{
consecutiveLosses = OrderProfit() < 0 ? consecutiveLosses + 1 : 0;
}
}
}
void OnTrade()
{
// Check if trade was closed with a loss
if(OrderSelect(0, SELECT_BY_POS, MODE_HISTORY) && (OrderType() == OP_SELL || OrderType() == OP_BUY))
{
if(OrderProfit() < 0)
{
consecutiveLosses++;
}
else
{
consecutiveLosses = 0;
}
// Calculate dynamic take profit based on ATR and current price movement
double currentPrice = MarketInfo(Symbol(), MODE_BID);
double takeProfitPrice = currentPrice + (atr * atrMultiplier * 1.5);
// Check if the order is a buy order and take profit price is higher than the current take profit
if(OrderType() == OP_BUY && takeProfitPrice > OrderTakeProfit())
{
OrderModify(OrderTicket(), OrderOpenPrice(), takeProfitPrice, OrderStopLoss(), 0, Green);
}
}
}
void OnTimer()
{
// Reset trades today counter at midnight
if(TimeHour(TimeCurrent()) == 0 && TimeMinute(TimeCurrent()) == 0)
{
tradesToday = 0;
}
}
when I want to run it I got these warnings and errors:
'Moving Average.mq4' Moving Average.mq4 1 1
'iMA' - wrong parameters count Moving Average.mq4 48 15
possible loss of data due to type conversion Moving Average.mq4 48 13
'iRSI' - wrong parameters count Moving Average.mq4 51 16
possible loss of data due to type conversion Moving Average.mq4 51 14
'iMACD' - wrong parameters count Moving Average.mq4 54 17
possible loss of data due to type conversion Moving Average.mq4 54 15
'iBands' - wrong parameters count Moving Average.mq4 57 22
possible loss of data due to type conversion Moving Average.mq4 57 20
'iRSI' - wrong parameters count Moving Average.mq4 74 15
'iMACD' - wrong parameters count Moving Average.mq4 75 16
'iBands' - wrong parameters count Moving Average.mq4 76 21
'iBands' - wrong parameters count Moving Average.mq4 77 21
'iATR' - wrong parameters count Moving Average.mq4 80 10
'iMACD' - wrong parameters count Moving Average.mq4 113 22
possible loss of data due to type conversion Moving Average.mq4 162 64
declaration of 'currentPrice' hides global variable Moving Average.mq4 184 14
see previous declaration of 'currentPrice' Moving Average.mq4 21 8
declaration of 'takeProfitPrice' hides global variable Moving Average.mq4 185 14
see previous declaration of 'takeProfitPrice' Moving Average.mq4 27 8
return value of 'OrderModify' should be checked Moving Average.mq4 190 10
I'm unable to solve these issues because I'm amateur and I don't have sufficient knowledge to solve these, if anyone can help me, please help, I really need his/her help.