av. movement of pips beyond a band?

 

I have a band on my chart, which is the 2 LWMA high and 2 LWMA low.

I can work out the distance between the 2 easily enough but what I want to work out is the average amount of pips that price exceeds the bands over say the last 20 periods.

Any ideas on how to do that?

 
loop over N bars, compute and sum the difference, divide by the count.
 
WHRoeder:
loop over N bars, compute and sum the difference, divide by the count.

How about this?

Doesn't seem to calculate correctly...


   //Average move beyond the LWMAs
   int i,                           
       Counted_bars;                

   Counted_bars=IndicatorCounted(); 
   i=Bars-Counted_bars-1;           
   double varPriceExceededHigh=0;
   double varPriceExceededLow=0;
   double varPriceExceededHighCount=0; 
   double varPriceExceededLowCount=0; 

while(i>=0)                      
     {
      if (High[i] > iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i)) {
       //count this bar towards the total and add it to the var
       varPriceExceededHighCount++;
       varPriceExceededHigh= varPriceExceededHigh + (High[i] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0));
     }
      if (Low[i] <  iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i)) {
      varPriceExceededLowCount++;
      varPriceExceededLow= varPriceExceededLow + (iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, 0) - Low[i]);
     }
     //when the bar doesn't exceede the high or the low don't do any calculating
      i--;                          
     }

//calculate averages
double varavHigh = varPriceExceededHigh/varPriceExceededHighCount;
double varavLow = varPriceExceededLow/varPriceExceededLowCount;
 

Do you want the number of pips? Remember that doing High[i] - iMA() will give you the difference according to your broker's digits, i.e. 0.0002.

 
ubuntuboy:

Do you want the number of pips? Remember that doing High[i] - iMA() will give you the difference according to your broker's digits, i.e. 0.0002.


I think it was this line, I needed to change the end to i:

varPriceExceededHigh= varPriceExceededHigh + (High[i] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0));

Pips I can get from ther Point variable I think.
 
 i=Bars-Counted_bars-1;           
   double varPriceExceededHigh=0;
   double varPriceExceededLow=0;
   double varPriceExceededHighCount=0; 
   double varPriceExceededLowCount=0; 

while(i>=0)                      
     {
      if (High[i] > iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i)) {
       //count this bar towards the total and add it to the var
       varPriceExceededHighCount++;
       varPriceExceededHigh= varPriceExceededHigh + 
              (High[i] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0));
     }
 ...
      i--;                          
     }

//calculate averages
double varavHigh = varPriceExceededHigh/varPriceExceededHighCount;

This is computing the average over all bars vs the CURRENT moving average. You want the average over 20 vs the MA at that time

 i=Bars-Counted_bars-1;           
while(i>=0)                      
     {
   double varPriceExceededHigh=0;
   double varPriceExceededLow=0;
   double varPriceExceededHighCount=0; 
   double varPriceExceededLowCount=0; 
   for (int j=19; j>=0; j--) {

      if (High[i+j] > iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j)) {
       //count this bar towards the total and add it to the var
       varPriceExceededHighCount++;
       varPriceExceededHigh += (High[i+j] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j));
     }
...
   } // for
   //calculate averages
   double varavHigh = varPriceExceededHigh/varPriceExceededHighCount;
   bufferHigh[i] = varavHigh;
      i--;                          
} // while

 

Why 20 out of interest?

On a weekly chart, I'd like to see the average distance that price exceeds the 2 period LWMA high and get the average.

Same for the low.

Doesn;t 20 restrict it to only the recent 20 bars?

Reason: