(MQL5) How to limit trade function to open only 1 order only

 

Hello everyone, I'm new to coding, currently Im writing one that will open trade based on a specific time.

For example, if the current time is 21:00:00, and match the condition time, it will open a market order. However, the problem is the OnTick() function requests the order continously, it lead to 20-30 same positions in my trading tab.

Is there a way so I can request open a trade only (not by using OrderTotal, my further code cannot use that)

Below is my code (MQL5)

#include <Trade\Trade.mqh>
CTrade trade;
MqlDateTime GMT_Time;

void OnTick()
{
TimeToStruct(TimeGMT(),GMT_Time);

// OHLC INFO
   double open  = iOpen(Symbol(),PERIOD_CURRENT,0);
   double high  = iHigh(Symbol(),PERIOD_H1,1);
   double low   = iLow(Symbol(),PERIOD_H1,1);
   double close = iClose(Symbol(),PERIOD_CURRENT,0);

   double SLbuy = low;   // Stoploss buy order
   double SLsell = high; // Stoploss buy order
   double VOLsell = MathFloor((AccBalance * Risk) / (MathAbs(bid - SLsell) * pipValue)*100)/100;
   double VOLbuy = MathFloor((AccBalance * Risk) / (MathAbs(ask - SLbuy) * pipValue)*100)/100;

if (GMT_Time.hour==10) //timee.hour==8
            {
            if(GMT_Time.min==34) //timee.min==0
               {
                  
                  trade.Buy(vbuy1,NULL,ask,SLbuy,0,NULL);
                  trade.Sell(vsell1,NULL,bid,SLsell,0,NULL);

               }

            }
}
 
  1. if (GMT_Time.hour==10) //timee.hour==8
                {
                if(GMT_Time.min==34) //timee.min==0

    This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

  2. Wait for the proper time, disable until a new day.

    Not tested, not compiled, just typed.

    #define  MAX_DATETIME   D'3000.12.31 23:59:59'
    #define  HR1034 (10*3600+34*60)
    void OnTick()
    {
       datetime now = TimeGMT();
    
       static datetime todayDate=0;  datetime todayPrev=todayDate; 
       static datetime openTime;     todayDate=date(now); 
       if(todayDate != todayPrev)    openTime=HR1034; // New day, enable.
       if(time(now) >= openTime){    openTime=MAX_DATETIME; // Time to trade, disable additional.
    
          // OHLC INFO
          ⋮
    }   }
    Not tested, not compiled, just typed.
 
William Roeder #:
  1. This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

  2. Wait for the proper time, disable until a new day.

    Not tested, not compiled, just typed.

    Not tested, not compiled, just typed.
Thank you for your reply

1. Yes I know that sometimes there is no tick, but it is fine for me cause I trade during NY session. I assume most of the time during that, the market is vibrant enough for not appearing a tick. So it is not my problem (or not that serious)

2. Thank you, i will try including this in my code
Reason: