input double Risk = 10.0; // Percentage for stop loss (10%)
input double Reward = 30.0; // Percentage for take profit (30% - 1:3 risk-reward ratio)
double contractSize = 1.0; // Starting contract size
void OnStart()
{
if (Period() != PERIOD_M5) // Check if the chart is not M5
{
Print("This EA is designed for the 5-minute (M5) chart only. Please apply it on an M5 chart.");
return;
}
if (!PositionSelect("XAUUSD")) // Check if there's an open position
{
double stopLoss = NormalizeDouble(contractSize * Risk / 100.0, 2); // Calculate stop loss
double takeProfit = NormalizeDouble(contractSize * Reward / 100.0, 2); // Calculate take profit
// Place a market buy order with calculated stop loss and take profit
OrderSend("XAUUSD", ORDER_TYPE_BUY, contractSize, SymbolInfoDouble("XAUUSD", SYMBOL_ASK), 10, 0, 0, "Buy Order", 0, clrBlue);
OrderSend("XAUUSD", ORDER_TYPE_SELL, contractSize, SymbolInfoDouble("XAUUSD", SYMBOL_BID), 10, 0, stopLoss, takeProfit, 0, clrRed);
}
}
void OnTick()
{
// Add tick event logic here if needed
}
void OnTimer()
{
// Add timer event logic here if needed
}
void OnTrade()
{
if (PositionsTotal() == 0) // Check if all positions are closed
{
OnStart(); // Open a new trade after the previous one has been closed
}
}
Every time I try to compile I get 'OrderSend' - wrong parameters count error. Can someone help me solve this issue? I am using MT5. I have tried changing the name but I would still get the error.
It looks like you are trying to use the MQL4 (MetaTrader 4)
OrderSend
function in MQL5 (MetaTrader 5), this is why you are getting errors.The way orders are placed differs greatly between MQL4 and MQL5.
I would suggest reading the following page which has worked examples.
OrderSend