How to get the previous traded day from datetime?

 
Hi,

I'm working on an indicator which pics the current datetime time[] and, from that, tries to get the data from the previous trading date relative to the current one. 

At first to get this, I tried simply picking the datetime variable and subtracting the amount of seconds of 1 day. Failure: if the current day was a monday, then that would get me a sunday which had no data.

Then I tried a system of if-elfse where the amount of seconds subtracted was relative to the current day_of_week. Failure: hollydays happen during the week.

Now my solution is using a do-while system which is totally ugly. To avoid that, I'ld like to know if there is any better way of getting this. Any help appreciated!
 

I think it's something along iTime(NULL,PERIOD_D1,1) and then adding the time of day, or the distance between TimeCurrent() and iTime(NULL,PERIOD_D1,0).

datetime timeofday = TimeCurrent() - iTime(NULL,PERIOD_D1,0);
datetime lasttradingdaysametime = iTime(NULL,PERIOD_D1,1) + timeofday;
 
Martin Bittencourt:
Hi,

   MqlRates rates[1]; 
   CopyRates(_Symbol,PERIOD_D1,1,1,rates); 
   Print("Last Trading Date: ",TimeToString(rates[0].time,TIME_DATE));
 
  1. datetime timeofday = TimeCurrent() - iTime(NULL,PERIOD_D1,0);
    datetime lasttradingdaysametime = iTime(NULL,PERIOD_D1,1) + timeofday;
    On MT4: Unless the current chart is that specific pair/ TF referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

    On MT5: Unless the chart is that specific pair/ TF, you must Synchronize the terminal Data from the Server.
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03

  2. Find bar of the same time one day ago - Simple Trading Strategies - MQL4 programming forum
 

Hi folks,


first of all, thanks for the reply. Unfortunately, if I understood them right, the don't suit my situation. For I'm doing an indicator and so the "get the last trading date" is relative to the evaluation of the current bar inside the OnCalcualte of the indicator. So solutions such as the ones above, while good inside the EA running at each day, doesn't work for me.

Note: I've tried tickfenix's answers and confirmed my negative suspicions; lippmaje's answer I didn't tried because of the many problems pointed out by William.

 
Martin Bittencourt:

Hi folks,


first of all, thanks for the reply. Unfortunately, if I understood them right, the don't suit my situation. For I'm doing an indicator and so the "get the last trading date" is relative to the evaluation of the current bar inside the OnCalcualte of the indicator. So solutions such as the ones above, while good inside the EA running at each day, doesn't work for me.

Note: I've tried tickfenix's answers and confirmed my negative suspicions; lippmaje's answer I didn't tried because of the many problems pointed out by William.

Unless you didn't explain clearly what you want, the approach using index 1 of PERIOD_D1, for previous trading day data is the right one. Indicator/EA distinction is irrelevant, running each day is irrelevant.
Reason: