I have a problem that I need your help to suggest.

 


//I have a problem that I need your help to suggest. In the backtest process, there is only one open order. after it closed There are no more open orders. How do I fix it?



#include <Trade/Trade.mqh> CTrade trade;
//+------------------------------------------------------------------+
int handleRsi;
ulong posTicket;
double ask, bid;

int OnInit()
{
   //Set Indicator RSI ----------------------------------------------
   handleRsi = iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE);
   ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
  
}
//+------------------------------------------------------------------+
void OnTick()
{
   //Set open trading----------------------------------------
      double rsi[];
      CopyBuffer(handleRsi, 0, 0, 1, rsi);
      Comment("\nRSI: ", rsi[0]);
         
      if (rsi[0] < 40){
         Comment("\n,\nRSI <30 ");
         if (posTicket > 0 && PositionSelectByTicket(posTicket)){
            int posType = (int)PositionGetInteger(POSITION_TYPE);
            if (posType == POSITION_TYPE_SELL){
               if (trade.PositionClose(posTicket)){
                  Print("Position closed successfully");
                  posTicket = 0; // Reset posTicket after closing the position
               }
               else{
                  Print("Failed to close position");
               }
            }
         }
         if (posTicket <= 0){
            posTicket = trade.Buy(0.01, _Symbol);
            posTicket = trade.ResultOrder();
         }
      }
      else if (rsi[0] > 60){
         Comment("\n,\nRSI >70 ");
         if (posTicket > 0 && PositionSelectByTicket(posTicket)){
            int posType = (int)PositionGetInteger(POSITION_TYPE);
            if (posType == POSITION_TYPE_BUY){
               if (trade.PositionClose(posTicket)){
                  Print("Position closed successfully");
                  posTicket = 0; // Reset posTicket after closing the position
               }
               else {
                  Print("Failed to close position");
               }
            }
         }

         if (posTicket <= 0){
            posTicket = trade.Sell(0.01, _Symbol);
            posTicket = trade.ResultOrder();
         }
      }

   //Set Modify Trading------------------------------------
      if (PositionSelectByTicket(posTicket)){
         double posPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         double posSl = PositionGetDouble(POSITION_SL);
         double posTp = PositionGetDouble(POSITION_TP);
         int posType = (int)PositionGetInteger(POSITION_TYPE);

         if (posType == POSITION_TYPE_BUY){
            if (posSl == 0){
               double sl = posPrice - 0.00300; // Input SL 300
               double tp = posPrice + 0.00500; // Input TP 500
               trade.PositionModify(posTicket, sl, tp);
            }
         }
         else if (posType == POSITION_TYPE_SELL){
            if (posSl == 0){
               double sl = posPrice + 0.00300; // Input SL 300
               double tp = posPrice - 0.00500; // Input TP 500
               trade.PositionModify(posTicket, sl, tp);
            }
         }
         else{
            posTicket = 0;
         }
      }
}

I have a problem that I need your help to suggest. In the backtest process, there is only one open order. after it closed There are no more open orders. How do I fix it?


         

Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC", "Every tick" and "Every tick based on real ticks" using actual historical data.
 
  1. Thepborworn Makool:. How do I fix it?        

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  2.                double sl = posPrice - 0.00300; // Input SL 300
                   double tp = posPrice + 0.00500; // Input TP 500

    Don't hard code numbers. If you mean 300 points write that. Code fails on JPY pairs.

  3. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  4. int OnInit(){
       ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
       bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Reason: