Daily Average movement over a certain period of All Currency Pairs

 

Hello,

I'm having some difficulty writing a script that will calculate the average pip movement over a period of 20 days for every currency pair, and then exporting those values to a .csv file.

Here is the code:

int R20=0;
   int i=0;
   int n = 0;
   int handle;
   string currencies[100];
   string curavg[100];
   
   
   //Define currencies in array
   
   currencies[0] = "EURUSD";
   currencies[1] = "USDJPY";
   currencies[2] = "GBPUSD";
   currencies[3] = "USDCHF";
   currencies[4] = "USDCAD";
   currencies[5] = "AUDUSD";
   currencies[6] = "NZDUSD";
   currencies[7] = "EURGBP";
   currencies[8] = "EURJPY";
   currencies[9] = "EURCHF";  
   currencies[10] = "GBPCHF";
   currencies[11] = "GBPJPY";
   currencies[12] = "AUDJPY";
   currencies[13] = "CHFJPY";
   currencies[14] = "EURCAD";
   currencies[15] = "EURAUD";
   currencies[16] = "AUDCAD";
   currencies[17] = "AUDNZD";
   currencies[18] = "NZDJPY";
   currencies[19] = "AUDCHF"; 
   currencies[20] = "EURDKK";
   currencies[21] = "EURNZD";
   currencies[22] = "GBPNZD";
   currencies[23] = "NZDCHF";
   currencies[24] = "USDCZK";
   currencies[25] = "USDDKK";
   currencies[26] = "USDMXN";
   currencies[27] = "USDPLN";
   currencies[28] = "USDSGD";
   currencies[29] = "USDZAR";
   currencies[30] = "GBPCAD";
   currencies[31] = "CADJPY";
   
    

 //calculate average over period of 20, and put values into array  
while (n <= 31)

{
   for(i=1;i<=20;i++)
   R20   =    R20 +  (iHigh(currencies[n],PERIOD_D1,i)-iLow(currencies[n],PERIOD_D1,i))/Point;

   R20 = R20/20;
    
   curavg[n] = R20;
 
n = n + 1;

}

i = 0;

  
 //Write Data To .csv File 
  
   handle=FileOpen("D1Avg.csv", FILE_CSV|FILE_WRITE, ',');

   while (i <= 31)
   
   {
   
   
     if(handle>0)
    {
     FileWrite(handle,currencies[i],curavg[i]  );
    
     
    }
        i = i + 1;
        
        }
        
        
   FileClose(handle);   

Now, my problem is the following:

The code seems to work fine, but if I run the script, all values except the timeframe that I am on is rubbish. If I go the another currency window, then that currency value is correct except all of the other currency pairs.

What can be the problem? Is there some way to load the data from all the currency pairs before doing the calculations? I think the correct values are not load correctly before the calculations are done.

Thanks!

 

Saidar wrote >>

Is there some way to load the data from all the currency pairs before doing the calculations? I think the correct values are not load correctly before the calculations are done.

That's exactly your problem - calls to iHigh() or iLow() for none-existing data will return zero. There is no automated way of downloading data via code (in MQL4... MQL5 has functions for that). You can open the history center, select a symbol and double-click 'Daily'; repeat for all symbols. Note that this downloads data from your broker (as opposed to pressing the 'Download' button which causes data to download from MQ).
 

Thanks Gordon,

I checked my history center, all currencies have data loaded for the day1 timeframe.

The problem still persists.

The only currency that is calculated correctly is the currency that I am on at the moment (window opened).

 

S

> average pip movement over a period of 20 days

Much simpler to use the Daily ATR of that period

OTTOMH, you multiply non-yen pairs by 10000 to get the full pips, yen pairs multiply by 100

FWIW
-BB-

 

You forgot to reset R20 at the end of each iteration. Also should divide by a different Point for each pair - via MarkInfo(symbol, MODE_POINT).

 

Wait, I see a patter emerging here.

When I'm on a a JPY pair the USD and EUR and other similar pairs are divided by a factor of 100, when I'm on a USD and EUR pair the JPY pairs are increased with a factor of 100.

What?????

 
Oh yea, that was a problem indeed thanks! But it did not solve the problem that I described above
 
I think I was editing my previous post while u were posting... Again - "should divide by a different Point for each pair - via MarkInfo(symbol, MODE_POINT)".
 
gordon:
I think I was editing my previous post while u were posting... Again - "should divide by a different Point for each pair - via MarkInfo(symbol, MODE_POINT)".


Oh ok,

Thanks Gordon, you are my hero

 

Ok, all seems fine now. Look at the results:

EURUSD,1646

USDJPY,1212

GBPUSD,1744

USDCHF,1172

USDCAD,1303

AUDUSD,1070

NZDUSD,940

EURGBP,1030

EURJPY,2485

EURCHF,626

GBPCHF,309

GBPJPY,2796

AUDJPY,208

CHFJPY,174

EURCAD,1701

EURAUD,1509

AUDCAD,826

AUDNZD,1021

NZDJPY,1503

AUDCHF,1146

EURDKK,129

EURNZD,2086

GBPNZD,2597

NZDCHF,975

USDCZK,70835

USDDKK,7141

USDMXN,17206

USDPLN,6931

USDSGD,830

USDZAR,13559

GBPCAD,1842

CADJPY,2002

Do they look fine to you? The MXN and CZK pairs seems a little high to me?

UPDATE: If you look on the chart they seem to be accurate. This is quite insane hey exactly why I wanted this tool.

Thanks again for your help

 
No idea... My partners look at numbers, not me.