Crossover and RSI trading bot not entering trade in strategy tester

 

I am not a coding expert, but I made a trading bot to enter and close trades based om specific rules, I tried test it in the strategy tester but it is not entering any trades. Can someone please help me to solve this issue ?


Below is the code:


// Input parameters

input int fastMAPeriod = 7;
input int slowMAPeriod = 15;
input int rsiPeriod = 14;
input double rsiBuyLevelMin = 55;
input double rsiBuyLevelMax = 75;
input double rsiSellLevelMin = 25;
input double rsiSellLevelMax = 45;
input string StartTime = "08:00";
input string EndTime = "22:00";
input double LotSize = 0.1;
input double StopLossPercentage = 1; // Stop loss percentage of the account balance
input double TakeProfitPercentage = 3; // Stop loss percentage of the account balance

//+------------------------------------------------------------------+
//| Custom tick event function |
//+------------------------------------------------------------------+
void OnTick()
{
if (!IsWithinTradingTime())
return;
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double stopLossAmount = accountBalance * StopLossPercentage / 100.0;
double takeProfitAmount = accountBalance * TakeProfitPercentage / 100.0;
double fastMAVal = iMA(NULL, 0, fastMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
double slowMAVal = iMA(NULL, 0, slowMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
double rsi = iRSI(NULL, 0, rsiPeriod, PRICE_CLOSE);

// Check for buy trade conditions
if (fastMAVal > slowMAVal && rsi > rsiBuyLevelMin && rsi < rsiBuyLevelMax)
{
double entryPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double stopLossPrice = entryPrice - StopLossPercentage;
double takeProfitPrice = entryPrice + TakeProfitPercentage;


    // Place the buy trade
    MqlTradeRequest buyRequest;
    MqlTradeResult buyResult;
    ZeroMemory(buyRequest);
    buyRequest.action = TRADE_ACTION_DEAL;
    buyRequest.symbol = Symbol();
    buyRequest.volume = LotSize;
    buyRequest.price = entryPrice;
    buyRequest.sl = stopLossPrice;
    buyRequest.tp = takeProfitPrice;
    buyRequest.type = ORDER_TYPE_BUY;

    if (OrderSend(buyRequest, buyResult))
    {
        Print("Buy trade placed successfully");
    }
    else
    {
        Print("Failed to place buy trade. Error code:", GetLastError());
    }
}

// Check for sell trade conditions
if (fastMAVal < slowMAVal && rsi >= rsiSellLevelMin && rsi <= rsiSellLevelMax)
{
double entryPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double stopLossPrice = entryPrice + StopLossPercentage;
double takeProfitPrice = entryPrice - TakeProfitPercentage;

    // Place the sell trade
    MqlTradeRequest sellRequest;
    MqlTradeResult sellResult;
    ZeroMemory(sellRequest);
    sellRequest.action = TRADE_ACTION_DEAL;
    sellRequest.symbol = Symbol();
    sellRequest.volume = LotSize;
    sellRequest.price = entryPrice;
    sellRequest.sl = stopLossPrice;
    sellRequest.tp = takeProfitPrice;
    sellRequest.type = ORDER_TYPE_SELL;

    if (OrderSend(sellRequest, sellResult))
    {
        Print("Sell trade placed successfully");
    }
    else
    {
        Print("Failed to place sell trade. Error code:", GetLastError());
    }
}
}

// Function to check if current time is within trading hours
bool IsWithinTradingTime()
{
datetime currentTime = TimeLocal();
datetime startTime = StringToTime(StartTime + ":00");
datetime endTime = StringToTime(EndTime + ":00");
return (currentTime >= startTime && currentTime <= endTime);
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trade Operation Types - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Just for every one information this is a mql5 code, can anyone please help me to fix the issue ?
 

Your code is accessing indicator value in the wrong way.

You have to use CopyBuffer to access indicators values.

 

The code you've provided looks mostly fine, but I've noticed a couple of issues that could be preventing your EA from placing trades in the strategy tester. Here's a revised version of your code with the necessary fixes:

// ... (Input parameters and OnTick declaration)

//+------------------------------------------------------------------+
//| Custom tick event function                                       |
//+------------------------------------------------------------------+
void OnTick()
{
    if (!IsWithinTradingTime())
        return;

    double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
    double stopLossAmount = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point; // Use stop level instead of percentage
    double takeProfitAmount = MarketInfo(Symbol(), MODE_MINLOT) * LotSize * Point * TakeProfitPercentage; // Use min lot size

    double fastMAVal = iMA(NULL, 0, fastMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
    double slowMAVal = iMA(NULL, 0, slowMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
    double rsi = iRSI(NULL, 0, rsiPeriod, PRICE_CLOSE);

    // Check for buy trade conditions
    if (fastMAVal > slowMAVal && rsi > rsiBuyLevelMin && rsi < rsiBuyLevelMax)
    {
        double entryPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
        double stopLossPrice = entryPrice - stopLossAmount;
        double takeProfitPrice = entryPrice + takeProfitAmount;

        // Place the buy trade
        MqlTradeRequest buyRequest;
        MqlTradeResult buyResult;
        ZeroMemory(buyRequest);
        buyRequest.action = TRADE_ACTION_DEAL;
        buyRequest.symbol = Symbol();
        buyRequest.volume = LotSize;
        buyRequest.price = entryPrice;
        buyRequest.sl = stopLossPrice;
        buyRequest.tp = takeProfitPrice;
        buyRequest.type = ORDER_TYPE_BUY;

        if (OrderSend(buyRequest, buyResult))
        {
            Print("Buy trade placed successfully");
        }
        else
        {
            Print("Failed to place buy trade. Error code:", GetLastError());
        }
    }

    // Check for sell trade conditions
    if (fastMAVal < slowMAVal && rsi >= rsiSellLevelMin && rsi <= rsiSellLevelMax)
    {
        double entryPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID);
        double stopLossPrice = entryPrice + stopLossAmount;
        double takeProfitPrice = entryPrice - takeProfitAmount;

        // Place the sell trade
        MqlTradeRequest sellRequest;
        MqlTradeResult sellResult;
        ZeroMemory(sellRequest);
        sellRequest.action = TRADE_ACTION_DEAL;
        sellRequest.symbol = Symbol();
        sellRequest.volume = LotSize;
        sellRequest.price = entryPrice;
        sellRequest.sl = stopLossPrice;
        sellRequest.tp = takeProfitPrice;
        sellRequest.type = ORDER_TYPE_SELL;

        if (OrderSend(sellRequest, sellResult))
        {
            Print("Sell trade placed successfully");
        }
        else
        {
            Print("Failed to place sell trade. Error code:", GetLastError());
        }
    }
}

// ... (IsWithinTradingTime function)
I didn't incorporate CopyBuffer because it can make the code more complex, especially for someone who might not be very experienced in coding. However, if you're looking to further enhance the efficiency and accuracy of your EA, using CopyBuffer for accessing indicator values is a good idea.

Hope this helps!