Problem with Finding Average Pips per bar for London and NY sessions

 

Hi all! As explained in the subject, I am having difficulties calculating the average pip per bar for only the London and NY trading sessions

Here's a summary of what I did, lest my code doesn't make sense to some of you:

  • I first created an array, inputting every bar that is within the London and NY session as a new element, each time resizing my array (since I don't know ultimately how many elements will be in the array)
  • After filling the array, I summed up all the elements in the array to give me my total pips. (I'm pretty sure there's no problem with this as I have checked this separately)
  • Finally I divided the total pips with the number of elements in the array to give me my average pips per bar. (Similarly, I've checked this, so I'm pretty sure there is no error with this section)

Could any kind soul help me out on this, what's wrong with the inputting of elements in the array such that my code wouldn't run?

Many thanks in advance!

//+------------------------------------------------------------------+
//| Declaration of variables
//+------------------------------------------------------------------+
   input int NumberofBarsforAverage = 260000;
   datetime LondonStart = 10.00;                                  // client server time (currently 2 hours diff)
   datetime NYClose = 23.59;                                      // client server time 
      

//+------------------------------------------------------------------+
//| Average pips per candle
//+------------------------------------------------------------------+
double AveragePipsPerCandle()
   {
   RefreshRates();
   double RawAveragePips[];                                       // create array of Pips from last n candles
   ArrayResize(RawAveragePips, 1);                                // resizing array basesd on number of candles                             
   
   for (int i=0; i<=NumberofBarsforAverage-1; i++)
      {
      int Time_Hour = TimeHour(iTime(Symbol(),0,i+1));
      double Time_Min = TimeMinute(iTime(Symbol(),0,i+1));
      double Time_Min_inHour = Time_Min/100;
      double TimeofBar = Time_Hour + Time_Min_inHour;
      
      if((TimeofBar >= LondonStart) && (TimeofBar <= NYClose)) 
         {
         ArrayResize(RawAveragePips,ArraySize(RawAveragePips)+1);
         RawAveragePips[ArraySize(RawAveragePips)] = NormalizeDouble((High[i+1] - Low[i+1])  // High-Low
         *MathPow(10,Digits-1),1);
         }
      else continue;   
      }
   Alert("Array Size is " + ArraySize(RawAveragePips));   
                  
   double TotalPips = 0;
   
   for (int iArray = ArraySize(RawAveragePips)-1; 
      iArray >= 0; iArray--)
         TotalPips += RawAveragePips[iArray];                     // summation of all elements in the array
         
   Alert("Total Pips = " + TotalPips);  
    
   int AveragePips = 
      NormalizeDouble(TotalPips/NumberofBarsforAverage,1);        // calculate average Pips from last n candles
   return AveragePips;   
   }    
 

Hi,

one problem of your code is the line:

int AveragePips = NormalizeDouble(TotalPips/NumberofBarsforAverage,1);        // calculate average Pips from last n candles


here you divide TotalPips througt NumberofBarsforAverage which is a const of 260000 even if
you have much less elements in your array. So you could use a separate counter or you take
the size of the array for the division.


Another problem is that AvaragePips is of type int - you should use a double instead of an int
(even your function is declared as a double function and you return an int)

And of course it is very inefficient to increase the array by one element on each pass.

I hope, this helps you.

Best regards,

 
Dion Tan: Hi all! As explained in the subject, I am having difficulties calculating the average pip per bar for only the London and NY trading sessions

May I suggest a simplification of your issue (not an exact correction of your code, but something so simple that it saves you a lot of work)?

You are trying to calculate the equivalent of Average Range of a Bar for a time period between London Open and New York Close ( 14 Hours).

Now the value of a bar's range is close in value to its True Range value, and given that the standard ATR(Average true Range) Indicator uses a Simple Moving Average, then a ATR of a period equivalent to the 14 hours will give you similar results as what you are trying to achieve, if you obtain the value at the last bar of that period (closing bar of New York close).

For example, on a H1 chart, the ATR(14) at closing bar of New York close, will give similar results as the code you are trying to calculate, just as for a M15 chart, an ATR(56) will be similar.

This will reduce your project to 1 or 2 lines of code using iATR() function. It is not exactly the same, but it is very close and will save you plenty of coding  and be more efficient.

 

Hi Werner, 

Thanks so much for spotting my mistakes, I managed to solve it some how!



Werner Klehr:

Hi,

one problem of your code is the line:


here you divide TotalPips througt NumberofBarsforAverage which is a const of 260000 even if
you have much less elements in your array. So you could use a separate counter or you take
the size of the array for the division.


Another problem is that AvaragePips is of type int - you should use a double instead of an int
(even your function is declared as a double function and you return an int)

And of course it is very inefficient to increase the array by one element on each pass.

I hope, this helps you.

Best regards,

 

Hi Fernando, 

That's a great idea, I never thought of it myself before. Thanks for the recommendation!


Fernando Carreiro:

May I suggest a simplification of your issue (not an exact correction of your code, but something so simple that it saves you a lot of work)?

You are trying to calculate the equivalent of Average Range of a Bar for a time period between London Open and New York Close ( 14 Hours).

Now the value of a bar's range is close in value to its True Range value, and given that the standard ATR(Average true Range) Indicator uses a Simple Moving Average, then a ATR of a period equivalent to the 14 hours will give you similar results as what you are trying to achieve, if you obtain the value at the last bar of that period (closing bar of New York close).

For example, on a H1 chart, the ATR(14) at closing bar of New York close, will give similar results as the code you are trying to calculate, just as for a M15 chart, an ATR(56) will be similar.

This will reduce your project to 1 or 2 lines of code using iATR() function. It is not exactly the same, but it is very close and will save you plenty of coding  and be more efficient.

 
Dion Tan: Hi Fernando, That's a great idea, I never thought of it myself before. Thanks for the recommendation!
You are welcome!