Start of the day in trading between two defined hours

 

Hello,

I wonder what is mean "start of the day" in the trading between two defined hours and trade only once. Assumption:

1. GMT

2. start trading at 7 am and stop at 9 am.

Definition of "start trading day" should be:

now=TimeCurrent();

x=now-now%86400; or x=now-now%25200;

Thank you.

 

Simplest way is

    
 int h=TimeHour(TimeCurrent());
 if(h>=7 && h<9 )
 //Trade

of course that will be broker time, so you may have to adjust for that.

 
GumRai:

of course that will be broker time, so you may have to adjust for that.


or you can use TimeLocal or TimeGMT in case you don't want to be dependent on the brokers time

 
Thank you, but I think we did not understand in the my point of view. I mean "start of the day" in the one trade per day case. I know to trade one per day I need two loops (open trades and history trades), but in the history's loop I need use start of the day. "start of the day" is in this case (tradig between two defined hour per day) hour 7 a.m. or 00:00 ?
 
kot_filemon:
Thank you, but I think we did not understand in the my point of view. I mean "start of the day" in the one trade per day case. I know to trade one per day I need two loops (open trades and history trades), but in the history's loop I need use start of the day. "start of the day" is in this case (tradig between two defined hour per day) hour 7 a.m. or 00:00 ?
datetime Midnight = TimeCurrent() - (TimeCurrent()%(PERIOD_D1*60) ) ;
 //If startofDay is 7am
 datetime start_of_Day = Midnight + 7*PERIOD_H1*60 ; 

or it can be with an input

 input int start_hour = 7 ;  //This in inputs

 //This in the main program
 datetime Midnight = TimeCurrent() - (TimeCurrent()%(PERIOD_D1*60) ) ;
 datetime start_of_Day = Midnight + start_hour*PERIOD_H1*60 ; 

Again, this is based on broker time

 

@ GumRai if i'm not mistaken

MidNight = iTime(Symbol(), PERIOD_D1, 0);
 
qjol:

@ GumRai if i'm not mistaken

MidNight = iTime(Symbol(), PERIOD_D1, 0);
Absolutely, you are not mistaken and when getting values from a higher timeframe, you are less likely to get 4066 errors.
 
Thank you. I did according your suggestion and your suggestion here https://forum.mql4.com/61650 and other places but my function still work incorrect. Stll between these two hours, are too many trades (not only one). It looks like bool function doesn't transfer return(0) to stop trading.
 bool TradePlacedToday()
       {
         int MagicNumber=OrderMagicNumber();
         datetime Midnight=TimeCurrent()-(TimeCurrent()%(PERIOD_D1*60));
         datetime start_of_Day=Midnight+(7*PERIOD_H1*60);
                   
         for (int j=OrdersTotal()-1;j>=0;j--)
          {
            if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES))
             if (OrderSymbol()==Symbol())
              if (MagicNumber==OrderMagicNumber())
               {                   
                  datetime z=OrderOpenTime();
                  if (z>=start_of_Day)return(0);
               }
          }
         for (int k=OrdersHistoryTotal()-1;k>=0;k--)
            {
               if(OrderSelect(k,SELECT_BY_POS,MODE_HISTORY))
                 if (OrderMagicNumber()==MagicNumber)  
                   if (OrderSymbol()==Symbol())      
                { 
                  datetime opp=OrderOpenTime();          
                     if (opp>=start_of_Day)return(0);        
                }                
            }
        }      
 

Or error is in calling function:

bool TPT=TradePlacedToday();

 
GumRai: Absolutely, you are not mistaken and when getting values from a higher timeframe, you are less likely to get 4066 errors.
Less likely but not impossible. Impossible using chart time
#define HR2400 PERIOD_D1 * 60           // 86400 = 24 * 3600
int      TimeOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when % HR2400 );            }
datetime DateOfDay(datetime when=0){      if(when == 0)  when = TimeCurrent();
                                          return( when - TimeOfDay(when) );   }
//datetime Tomorrow( datetime when=0){      if(when == 0)  when = TimeCurrent();
//                                          return(DateOfDay(when) + HR2400);   }
//datetime Yesterday(datetime when=0){      if(when == 0)  when = TimeCurrent();
//   int iD1 = iBarShift(NULL, PERIOD_D1, DateOfDay(when) - 1);
//                                       return( iTime(NULL, PERIOD_D1, iD1) ); }

   datetime Midnight     = DateOfDay(),
            start_of_Day = Midnight + start_hour*3600; 
 
Ok. I think I fixed it. I pasted body of function TradePLacedToday to function start(). Delete function TradePLacedToday, and it works.
Reason: