How to update all currency pair data

 

I am writing a script which analyses all 28 major pairs by looking at the differences between the last bar close and the close numerous bars ago.

Often when I run it, the data is not current for most of the pairs. If I run it a second time straight away the data is current and it works. If I leave it for a while and try running it, the data won't have updated from the previous run for most of the pairs. 

How can I get it to run the first time everytime with up to date data?

The basic code is:

input ENUM_TIMEFRAMES  TimeFrame = PERIOD_H1;
input int BarShift = 10;

void OnStart()

double Difference[8,7];

string CurrencyPairs[8,7] = {"EURUSD","GBPUSD","AUDUSD","NZDUSD","USDCHF","USDJPY","USDCAD",
                          "EURGBP","EURCAD","EURAUD","EURNZD","EURJPY","EURUSD","EURCHF",
                          "AUDUSD","AUDCHF","EURAUD","AUDJPY","GBPAUD","AUDCAD","AUDNZD",
                          "USDJPY","EURJPY","GBPJPY","AUDJPY","CHFJPY","NZDJPY","CADJPY",
                          "USDCAD","CADCHF","EURCAD","CADJPY","GBPCAD","AUDCAD","NZDCAD",
                          "GBPUSD","GBPCHF","EURGBP","GBPJPY","GBPAUD","GBPCAD","GBPNZD",
                          "NZDUSD","NZDCHF","EURNZD","NZDJPY","GBPNZD","AUDNZD","NZDCAD",
                          "EURCHF","GBPCHF","AUDCHF","NZDCHF","CADCHF","USDCHF","CHFJPY",
                          };

 for(i=0; i<=7 ; i++)
    {
       for(j=0; j<=6; j++)
         {

            Difference[i,j] = iClose(CurrencyPairs[i,j],TimeFrame,1)  -  iClose(CurrencyPairs[i,j],TimeFrame,BarShift+1);
         }

   }

Thanks

 
On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
          Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
          Corrected

On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
          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

 
Spotthechook:

I am writing a script which analyses all 28 major pairs by looking at the differences between the last bar close and the close numerous bars ago.

Often when I run it, the data is not current for most of the pairs. If I run it a second time straight away the data is current and it works. If I leave it for a while and try running it, the data won't have updated from the previous run for most of the pairs. 

How can I get it to run the first time everytime with up to date data?

One simple way, which I always do, is to keep all the charts opened.

And for added insurance (as charts only get updated as and when new ticks arrive), I make sure the time of all their latest bars are equal before doing any computation.