Hey everyone,
I’m having trouble backtesting my trading bot in MT4. The bot is supposed to execute trades based on RSI and ZigZag indicators, but when I try to backtest it using Strategy Tester, I get no trades and no results. I’ve added print statements to confirm buy and sell signals, but nothing is showing up in the logs either.
I suspect it could be an issue with trade execution or maybe the conditions for triggering trades aren’t being met in backtesting. I’ve also ensured that auto trading is enabled, and I’ve set up my historical data properly.
If anyone has experience with this, I’d really appreciate some help! Below is my most recent script:
//±-----------------------------------------------------------------+
//| RSI & ZigZag Trading Bot for MT4 |
//| Author: Your Name |
//±-----------------------------------------------------------------+
#property strict
// Input Parameters
input double LotSize = 0.1; // Trade lot size
input double StopLossPips = 30; // Stop Loss in pips
input double TakeProfitPips = 50; // Take Profit in pips
input int RSI_Period = 14; // RSI Calculation Period
input int Overbought = 70; // Overbought RSI Level
input int Oversold = 30; // Oversold RSI Level
input int ZigZagDepth = 12; // ZigZag Depth
// Indicator Handles
double rsiValue;
double zzValues[1000];
//±-----------------------------------------------------------------+
//| CopyBuffer function definition |
//±-----------------------------------------------------------------+
void CopyBuffer(int indicatorHandle, int bufferIndex, int startPos, int count, double &buffer)
{
for (int i = 0; i < count; i++)
{
buffer[i] = iCustom(NULL, 0, “ZigZag”, ZigZagDepth, 5, bufferIndex);
}
}
//±-----------------------------------------------------------------+
//| Check for Buy Signal |
//±-----------------------------------------------------------------+
bool CheckBuySignal()
{
rsiValue = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 0);
CopyBuffer(0, 0, 0, 3, zzValues);
if (rsiValue <= Oversold && zzValues[1] < zzValues[2]) {
Print("Buy signal detected");
return true;
}
return false;
}
//±-----------------------------------------------------------------+
//| Check for Sell Signal |
//±-----------------------------------------------------------------+
bool CheckSellSignal()
{
rsiValue = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 0);
CopyBuffer(0, 0, 0, 3, zzValues);
if (rsiValue >= Overbought && zzValues[1] > zzValues[2]) {
Print("Sell signal detected");
return true;
}
return false;
}
//±-----------------------------------------------------------------+
//| Execute Trade Function |
//±-----------------------------------------------------------------+
void ExecuteTrade(string tradeType)
{
if (!IsTradeAllowed())
{
Print(“Trade not allowed at this time”);
return;
}
double sl = StopLossPips * Point * 10;
double tp = TakeProfitPips * Point * 10;
double price = (tradeType == "BUY") ? Ask : Bid;
double slPrice = (tradeType == "BUY") ? (price - sl) : (price + sl);
double tpPrice = (tradeType == "BUY") ? (price + tp) : (price - tp);
RefreshRates();
int ticket = OrderSend(Symbol(), (tradeType == "BUY") ? OP_BUY : OP_SELL, LotSize, price, 10, slPrice, tpPrice, "RSI-ZigZag Bot", 0, 0, clrNONE);
if(ticket > 0)
{
Print("Trade executed: ", tradeType, " at ", price);
}
else
{
Print("Trade execution failed: ", tradeType, " Error: ", GetLastError());
}
}
//±-----------------------------------------------------------------+
//| Expert Advisor Start Function |
//±-----------------------------------------------------------------+
void OnTick()
{
if (CheckBuySignal())
{
Print(“Attempting to execute Buy trade”);
ExecuteTrade(“BUY”);
}
else if (CheckSellSignal())
{
Print(“Attempting to execute Sell trade”);
ExecuteTrade(“SELL”);
}
}