Problem with ObjectGetValueByTime asap help needed

 

So I use this function to get the value of a trendline for a specific bar:

double trendline(int i)
{
   double trendline=ObjectGetValueByTime(0,"Trendline",iTime(NULL, PERIOD_D1, i),0);
   
   return trendline;
}

then I compare it with bar close using:

if(barclose(1)>trendline(1) && barclose(2)<trendline(2))
{
  entry="buy";
}

with

double barclose(int i)
{   
   MqlRates PriceInformation[];
   ArraySetAsSeries(PriceInformation,true);
   int Data=CopyRates(Symbol(),PERIOD_D1,0,5,PriceInformation);
   double barclose=PriceInformation[i].close;
   
   return barclose;
}

however when I tested this in real life today it produced entry= "buy"  when it shouldn't.

Does anyone have an idea why it could happen?

Should I use ChartRedraw(0);  ?

 
iTime(NULL, PERIOD_D1, i)
int Data=CopyRates(Symbol(),PERIOD_D1,0,5,PriceInformation);
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

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

 
William Roeder:
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

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

Hi William,

thanks for you comment. However the chart on which the expert is placed is the specific symbol / tf. And I use ontick

 
Demosfen: xpert is placed is the specific symbol / tf.
Your code breaks if you change to any timeframe not the D1. It's your money.
Reason: