Download MetaTrader 5

set 12:00 am

To add comments, please log in or register
fly7680
636
fly7680  
Hello to all.

taking as an example the code iBarShift, how can I change the date in the hour 12:00 AM of the current day without having to continually change the day?

thank you
datetime some_time=D'2016.12.02 13:45';
      int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);
      Print("index of the bar for the time ",TimeToStr(some_time)," is ",shift);
iBarShift - Timeseries and Indicators Access - MQL4 Reference
iBarShift - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
iBarShift - Timeseries and Indicators Access - MQL4 Reference
honest_knave
Moderator
2326
honest_knave  

This will give you midnight: 

datetime when     = TimeCurrent();
datetime midnight = when - (when % 86400); // there are 86400 seconds in a day

HTH 

fly7680
636
fly7680  
Great work!

Thank you
fly7680
636
fly7680  

I added another code to find the highest value in X candles. but I can not make it work. The code does not have errors.

      datetime when     = TimeCurrent();
      datetime midnight = when - (when % 86400); // there are 86400 seconds in a day
      datetime some_time= midnight;
      int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);
      Print("index of the bar for the time ",TimeToStr(some_time)," is ",shift);
      
      double val;
      //--- calculating the highest value on the 20 consecutive bars in the range
      //--- from the 4th to the 23rd index inclusive on the current chart
      int val_index=iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,shift,1+i);
      if(val_index!=-1) val=High[val_index];
      else PrintFormat("Error in call iHighest. Error code=%d",GetLastError());
      
        
      //Indicator Buffer 1
      if( val )
        {
         Buffer1[i] = High[i];        
        }
fly7680
636
fly7680  
I solved thanks
fly7680
636
fly7680  

I'm trying to change this value to read 2 or more days, but if I enter a higher figure to 172,800 the result value does not change.

1 day = 86400

2 day = 172800

3 day = 345600

How could I solve?

      datetime when     = TimeCurrent();
      datetime midnight = when - (when % 86400); // there are 86400 seconds in a day
      datetime some_time= midnight;
      int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);
      Print("index of the bar for the time ",TimeToStr(some_time)," is ",shift);
honest_knave
Moderator
2326
honest_knave  
fly7680:

I'm trying to change this value to read 2 or more days, but if I enter a higher figure to 172,800 the result value does not change.

1 day = 86400

2 day = 172800

3 day = 345600

How could I solve?

      datetime when     = TimeCurrent();
      datetime midnight = when - (when % 86400); // there are 86400 seconds in a day
      datetime some_time= midnight;
      int      shift=iBarShift(Symbol(),PERIOD_CURRENT,some_time);
      Print("index of the bar for the time ",TimeToStr(some_time)," is ",shift);

% is giving you the remainder after division.

Just add 86400 to the midnight value for midnight tomorrow:

datetime when     = TimeCurrent();
datetime midnight = when - (when % 86400); // midnight today
midnight = midnight + 86400;               // midnight tomorrow
To add comments, please log in or register