English

Start of the day in trading between two defined hours

 36 kot_filemon 2014.03.13 17:43   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.
 Moderator3523 GumRai 2014.03.14 01:21 #   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.
 3134 qjol 2014.03.14 02:03 #   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
 36 kot_filemon 2014.03.14 09:53 #   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 ?
 Moderator3523 GumRai 2014.03.14 10:47 #   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
 3134 qjol 2014.03.14 11:04 #   @ GumRai if i'm not mistaken `MidNight = iTime(Symbol(), PERIOD_D1, 0);`
 Moderator3523 GumRai 2014.03.14 12:44 #   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.
 36 kot_filemon 2014.03.14 14:33 #   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);                         }                             }         }      ```
 36 kot_filemon 2014.03.14 14:42 #   Or error is in calling function: bool TPT=TradePlacedToday();
 13128 WHRoeder 2014.03.14 15:01 #   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; ```
 36 kot_filemon 2014.03.14 16:21 #   Ok. I think I fixed it. I pasted body of function TradePLacedToday to function start(). Delete function TradePLacedToday, and it works.