How do I download history from other symbols?

 

How do I download data from an indicator without an error?

i is the loop from Bars-1 to 0
tf = PERIOD_CURRENT;
datetime dt = iTime(NULL,tf,i);
int shift = iBarShift("EURUSD",tf,dt,true);

Errors on M1 and H1:

EURUSD,M1: Time 2020.04.10 23:58 iBarShift Error: 4051, i = 0, shift = 0 (the bar at 2020.04.10 23:58 is present)
EURUSD,H1: Time 2019.12.04 02:00 iBarShift Error: 4051, i = 2151, shift = 2151 (depends on the first visible bar, or the first downloaded from broker, when I press "PgUp", but I do not want to press "PgUp", especially if I get data from another chart)

On current symbol, it returns correct bar (but with error), on another symbol, it returns -1.
 
i is the loop from Bars to 0

There is no bar index equal to Bars, the highest is Bars-1.

 
Of course I have no more than Bars-1. This is an indicator, and this is a loop from limit to 0, which is rates_total-1 when prev_calculated=0 or rates_total - prev_calculated in another case. I don't know, what is it really equal, but at least I have a string if (i>=Bars-1) continue;.
 
aura:
Of course I have no more than Bars-1. 

There is no "Of course".

You stated

i is the loop from Bars to 0
tf = PERIOD_CURRENT;
datetime dt = iTime(NULL,tf,i);
int shift = iBarShift("EURUSD",tf,dt,true);

If you are getting values for a different symbol, you should check that there is an equivalent bar index for that symbol.

If the chart has 2000 bars and the symbol that you are trying to get data from only has 1000, you will get the error

 
  1. Post real code that compiles. There are no mind readers here and our crystal balls are cracked.
  2. Bars is the current chart only.
  3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) 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
 

Use iBars() to see the available number of bars for other instruments.

iBars - Timeseries and Indicators Access - MQL4 Reference
iBars - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
iBars - Timeseries and Indicators Access - MQL4 Reference
 

Too bad. The error was in completely different function. After the iBarShift it was the first time when it was checked.

But the question remains: How to download history on other symbols so that it match history on current symbol?

 
aura: But the question remains: How to download history on other symbols so that it match history on current symbol?

Perhaps you should read the manual.
          Timeseries and Indicators Access - MQL4 Reference

and #4 № 3
 

Maybe it's not me who need to read the manual.


//+------------------------------------------------------------------+
//|                                                          asd.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int tf = PERIOD_CURRENT;
   int bars_current = iBars(NULL,tf);
   int bars_GBPUSD = iBars("GBPUSD",tf);
   int bars_USDCAD = iBars("USDCAD",tf);
   Print("Current ",bars_current);
   Print("GBPUSD ",bars_GBPUSD);
   Print("USDCAD ",bars_USDCAD);
   Print(iTime("USDCAD",tf,bars_USDCAD-1));
   Print(iTime("USDCAD",tf,bars_USDCAD));
   Print(iTime("USDCAD",tf,bars_USDCAD+1));
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


2020.04.12 21:01:14.047    asd EURUSD,M15: 1970.01.01 00:00:00
2020.04.12 21:01:14.047    asd EURUSD,M15: 1970.01.01 00:00:00
2020.04.12 21:01:14.047    asd EURUSD,M15: 2020.03.06 03:30:00
2020.04.12 21:01:14.047    asd EURUSD,M15: USDCAD 2470
2020.04.12 21:01:14.047    asd EURUSD,M15: GBPUSD 2470
2020.04.12 21:01:14.047    asd EURUSD,M15: Current 9537


Download ME the bars above the "iBars".

 
   Print(iTime("USDCAD",tf,bars_USDCAD));
   Print(iTime("USDCAD",tf,bars_USDCAD+1));

Why are you trying to get the time for bars that don't exist?

 

If history can be downloaded using "PgUp" button on current chart, then it's present.

On the other charts it's not downloaded, and indicator not getting correct values.


2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: src bar 48 EURJPY.rann 2020.04.13 00:45:00 dst bar 48
2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: src bar 48 EURGBP.rann 2020.04.13 00:45:00 dst bar 48
2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: src bar 48 EURCHF.rann 1970.01.01 00:00:00 dst bar -1
2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: OnCalculate: iTime 3 Error 4051
2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: src bar 48 EURCAD.rann 2020.04.13 00:45:00 dst bar 48
2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: src bar 48 EURAUD.rann 2020.04.13 00:45:00 dst bar 48
2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: src bar 48 EURUSD.rann 2020.04.13 00:45:00 dst bar 48
2020.04.13 13:49:28.814    asdasd EURUSD.rann,M15: OnCalculate: EURCHF.rann iTime 2 Error 4051

How to fix this?

Files:
asdasd.mq4  4 kb
Reason: