I need to compute pivot lines every day. Any idea why my code doesn't work ?

 
CTrade trade;
datetime M5, D1;
int yesterday;
double pivotVal, res1, supp1;

int OnInit()
{
   M5=iTime(Symbol(),PERIOD_M5,0);
   D1=iTime(Symbol(),PERIOD_D1, 0);
   yesterday = 0;
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   if(D1!=iTime(Symbol(), PERIOD_D1,0))
   {
      D1=iTime(Symbol(),PERIOD_D1, 0);
      computeAndDrawCurrentPivot();
   }
 }
 
 void computeAndDrawCurrentPivot(){
   double high[], low[], close[];
   CopyClose(_Symbol,PERIOD_D1,yesterday,1,close);
   CopyHigh(_Symbol,PERIOD_D1,yesterday,1,high);
   CopyLow(_Symbol,PERIOD_D1,yesterday,1,low);
   double yesterdayHigh=high[0];
   double yesterdayLow=low[0];
   double yesterdayClose=close[0];
   Print("YesterdayLow: ",yesterdayLow);
   Print("YesterdayHigh: ", yesterdayHigh);
   pivotVal=NormalizeDouble((yesterdayHigh+yesterdayLow+yesterdayClose)/3.,_Digits);
   yesterday = yesterday++;
 }

Low and High price display the same value each day... Why ?

 
Baptiste Arnaud:

Low and High price display the same value each day... Why ?

Candle 0 is "current forming candle or bar"
depending on the time you request the candle, the low, high and close values might still forming.

 You request them at "candle start" where OHLC values have (mostly) same values.


 
Soewono Effendi:
Candle 0 is "current forming candle or bar"
depending on the time you request the candle, the low, high and close values might still forming.

 You request them at "candle start" where OHLC values have (mostly) same values.


Thank you my friend


CopyClose(_Symbol,PERIOD_D1,yesterday+1,1,close);
CopyHigh(_Symbol,PERIOD_D1,yesterday+1,1,high);
CopyLow(_Symbol,PERIOD_D1,yesterday+1,1,low);


It does the trick

 
Baptiste Arnaud:

Thank you my friend



It does the trick

nice.
 
  1. int OnInit(){
       M5=iTime(Symbol(),PERIOD_M5,0);
       D1=iTime(Symbol(),PERIOD_D1, 0);
    Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  2. Also unnecessary:
    datetime now = TimeCurrent();
       M5=now - now % PeriodSecions(PERIOD_M5);
       D1=now - now % PeriodSecions(PERIOD_D1);
              Find bar of the same time one day ago - Simple Trading Strategies - MQL4 programming forum

  3. void OnTick(){
       if(D1!=iTime(Symbol(), PERIOD_D1,0))
    On MT4: Unless the current chart is that specific symbols/TFs 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

    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

Reason: