No errors in debug but no trades in back test

 

Hi all

Please may I have some help with this code. It is just a sample that i'm trying to get to work on the back tester. I copied it from Chat GPT. It gives no errors on the debug, but produces no trades on the back test. 



input int    MaxConsecutiveLosses = 5;              // Max consecutive losses before switching strategy

input double TrailingStopPercent = 0.1;            // Trailing Stop in percentage

input double TargetProfitPercent = 0.2;             // Take Profit in percentage

input int    TradingStartHour = 0;                  // No trading before this hour

input int    TradingEndHour = 23;                   // No trading after this hour

input string TradingSymbol = "SP500m";               // Trading symbol, e.g., "US500"

input ENUM_TIMEFRAMES TradingTimeframe = PERIOD_H1; // Timeframe, e.g., 1-hour



// Global Variables

bool TradeEnabled = true;                           // Flag to enable/disable trading

int ConsecutiveLossCount = 0;                       // Counter for consecutive losses

double PreviousStrategyProfit1 = 0;

double PreviousStrategyProfit2 = 0;



//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

{

    Print("Trading Expert Initialized.");

    return INIT_SUCCEEDED;

}



//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

    Print("Trading Expert Deinitialized.");

}



//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{

    // Get current time

    MqlDateTime currTime;

    TimeToStruct(TimeCurrent(), currTime);

    int currHour = currTime.hour;

    int currDayOfWeek = currTime.day_of_week;



    // Check if trading is allowed based on time and day

    if (IsTradingTime(currHour, currDayOfWeek))

    {

        // Determine trading conditions

        double currClose = iClose(TradingSymbol, TradingTimeframe, 0);

        double prevClose = iClose(TradingSymbol, TradingTimeframe, 1);

        bool isBuySignal = (currClose < prevClose);



        // Check for strategy performance and adjust trading state

        double currStrategyProfit = AccountInfoDouble(ACCOUNT_BALANCE);

        UpdateTradeState(currStrategyProfit);



        // Execute trading logic

        if (TradeEnabled)

        {

            if (isBuySignal)

            {

                OpenTrade(ORDER_TYPE_BUY);

            }

            else

            {

                OpenTrade(ORDER_TYPE_SELL);

            }

            }

         else 

          if (isBuySignal)

            {

                OpenTrade(ORDER_TYPE_SELL);

            }

            else

            {

                OpenTrade(ORDER_TYPE_BUY);

            }

        }

    }





//+------------------------------------------------------------------+

//| Function to check if trading is allowed based on time and day    |

//+------------------------------------------------------------------+

bool IsTradingTime(int hour, int dayOfWeek)

{

    if (hour < TradingStartHour || hour > TradingEndHour || dayOfWeek == 6 || dayOfWeek == 0)

    {

        return false; // Do not trade outside of allowed hours or on weekends

    }

    return true;

}



//+------------------------------------------------------------------+

//| Function to update trading state based on strategy performance   |

//+------------------------------------------------------------------+

void UpdateTradeState(double currStrategyProfit)

{

    if (currStrategyProfit < PreviousStrategyProfit1 && PreviousStrategyProfit1 < PreviousStrategyProfit2)

    {

        ConsecutiveLossCount++;

        if (ConsecutiveLossCount > MaxConsecutiveLosses)

        {

            TradeEnabled = false;

            Print("Trading disabled due to consecutive losses.");

        }

    }

    else if (currStrategyProfit < PreviousStrategyProfit1 && PreviousStrategyProfit1 > PreviousStrategyProfit2)

    {

        TradeEnabled = true;

        ConsecutiveLossCount = 0;

        Print("Trading enabled.");

    }



    PreviousStrategyProfit2 = PreviousStrategyProfit1;

    PreviousStrategyProfit1 = currStrategyProfit;

}



//+------------------------------------------------------------------+

//| Function to open a trade                                         |

//+------------------------------------------------------------------+

void OpenTrade(ENUM_ORDER_TYPE orderType)

{

    double price = (orderType == ORDER_TYPE_BUY) ? SymbolInfoDouble(TradingSymbol, SYMBOL_ASK) : SymbolInfoDouble(TradingSymbol, SYMBOL_BID);

    double lotSize = 1.0; // Modify as necessary

    double stopLossPrice, takeProfitPrice;



    if (orderType == ORDER_TYPE_BUY)

    {

        stopLossPrice = price - (TrailingStopPercent / 100) * price;

        takeProfitPrice = price + (TargetProfitPercent / 100) * price;

    }

    else

    {

        stopLossPrice = price + (TrailingStopPercent / 100) * price;

        takeProfitPrice = price - (TargetProfitPercent / 100) * price;

    }



    MqlTradeRequest request;

    MqlTradeResult result;

    ZeroMemory(request);

    request.action = TRADE_ACTION_DEAL;

    request.symbol = TradingSymbol;

    request.volume = lotSize;

    request.type = orderType;

    request.price = price;

    request.sl = stopLossPrice;

    request.tp = takeProfitPrice;

    request.deviation = 10;

    request.magic = 12345678;

    request.comment = (orderType == ORDER_TYPE_BUY) ? "Buy Trade" : "Sell Trade";



    if (OrderSend(request, result))

    {

        Print((orderType == ORDER_TYPE_BUY) ? "Buy order placed." : "Sell order placed.");

    }

    else

    {

        Print("Order failed: ", result.retcode);

    }


}
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
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each...