Average of price exceeding MAs

 

I have some code to work out the average amount of pips that price exceeds a 2 period LWMA on my chart.

Someone advised me it had to be worked out using the moving average but shouldn't it be using all the candles on the chart?

So, if price exceeds the LWMA, I increase the count to work out the average. If it doesn't exceed the LWMA, then I don't use it in the calculation.


//+------------------------------------------------------------------+
//|                                     Weekly 2LWMAs TP SL Diff.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

int scaleX=5,
    scaleY=5,
    offsetX=0,
    offsetY=0,
    fontSize=10,
    corner=2,
    symbolCodeBuy=233, // a symbol code for a buy signal
    symbolCodeSell=234, // sell signal
    symbolCodeNoSignal=183; // no signal
           
extern color signalBuyColor=Green, // color of the symbol of a buy signal
             signalSellColor=Red, // for a sell signal
             noSignalColor=Gray, // no signal
             textColor=Black; // color of all writings
             
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   double  varHigh = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, 0);
   double  varLow = iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, 0);
   double HAvalHigh=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,0,0); //high ExtMapBuffer1
   double HAvalLow=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,1,0); //low ExtMapBuffer2
   //SetIndexBuffer(0,ExtMapBuffer1);
   //SetIndexBuffer(1,ExtMapBuffer2);

   
   ObjectCreate("signal",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("signal",OBJPROP_CORNER,corner);
   ObjectSet("signal",OBJPROP_XDISTANCE,scaleX+offsetX);
   ObjectSet("signal",OBJPROP_YDISTANCE,scaleY+offsetY);
   ObjectSetText("signal",CharToStr(symbolCodeNoSignal),fontSize,"Wingdings",noSignalColor);
   
   //1 = up
   //0 = neutral
   //-1 = down
   
   if (GlobalVariableGet(Symbol()+"BiasDirection") == 1) {
      ObjectSetText("signal",CharToStr(symbolCodeBuy),fontSize,"Wingdings",signalBuyColor);
   }
   if (GlobalVariableGet(Symbol()+"BiasDirection") == 0) {
      ObjectSetText("signal",CharToStr(symbolCodeNoSignal),fontSize,"Wingdings",noSignalColor);
   }
   if (GlobalVariableGet(Symbol()+"BiasDirection") == -1) {
      ObjectSetText("signal",CharToStr(symbolCodeSell),fontSize,"Wingdings",signalSellColor);
   }
   
   //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;
   double bufferHigh[];
   double bufferLow[]; 
   double varavHigh = 9999; //prevent divide by zero
   double varavLow = 9999;

   while(i>=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= varPriceExceededHigh + (High[i+j] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j));
         }
         if (Low[i+j] <  iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j)) {
            varPriceExceededLowCount++;
            varPriceExceededLow= varPriceExceededLow + (iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j) - Low[i+j]);
         }
      } // for

      //calculate averages
      if (varPriceExceededHighCount!=0) varavHigh = varPriceExceededHigh/varPriceExceededHighCount;
      bufferHigh[i] = varavHigh;
      if (varPriceExceededLowCount!=0) varavLow = varPriceExceededLow/varPriceExceededLowCount;
      bufferLow[i] = varavLow;
      i--;                          
   } // while

   Comment (
               "Difference: " + DoubleToStr(varHigh - varLow, 5) +
               "\nHA Range: " + DoubleToStr(HAvalLow, 5) + "  -  " + DoubleToStr(HAvalHigh, 5) +
               "\nHigh: " + DoubleToStr(varHigh, 5) +
               "\nLow: " + DoubleToStr(varLow, 5) +
               "\nSL: " + DoubleToStr((varHigh - varLow)/2, 5) +
               "\nAv. above high: " + DoubleToStr(varavHigh, 5) +
               "\nAv. below low: " + DoubleToStr(varavLow, 5) +
               "\nLot size: " + DoubleToStr((AccountBalance()*0.00001)/(MarketInfo(Symbol(), MODE_TICKVALUE)),5)
   );

   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

any ideas if this is the correct averaging process?


I would also like to work out the average that price comes near the LWMA but doesn't touch it - perhaps I could just work out the overall average for this and say that

- the low comes to within an average of x of the lower LWMA

- the high comes to withing an average of x of the higher LWMA

?

 

hi i have to say that a read only fast over your code. what are you trying to investigate? the average pips (of the last 20 candels) which are above the upper lwma/under the lower lwma? if so you have to reset the variables in the beginning of each loop.

try this:

 while(i>=0)                      
   {
      //RESET THE TEMPORARY VARIABLES AFTER EACH LOOP
      varPriceExceededHighCount=0;
      varPriceExceededHigh=0;

      varPriceExceededLowCount=0;
      varPriceExceededLow=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= varPriceExceededHigh + (High[i+j] - iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_HIGH, i+j));
         }
         if (Low[i+j] <  iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j)) {
            varPriceExceededLowCount++;
            varPriceExceededLow= varPriceExceededLow + (iMA(NULL, 0, 2, 0, MODE_LWMA, PRICE_LOW, i+j) - Low[i+j]);
         }
      } // for

      //calculate averages
      if (varPriceExceededHighCount!=0) varavHigh = varPriceExceededHigh/varPriceExceededHighCount;
      bufferHigh[i] = varavHigh;
      if (varPriceExceededLowCount!=0) varavLow = varPriceExceededLow/varPriceExceededLowCount;
      bufferLow[i] = varavLow;
      i--;                          
   } // while

i hope i am getting right what you wanted to do.

//z

 

It's more that I want it over the whole range of candles on the chart. If the variables are reset then so is the average???

So, if price exceeds the LWMA, I count it toward the average for that. If it exceeds the lower then I count it towards the average of the low.

I should end up with out of say 50 candles something like

price exceeded the high 10 times by an average of 50pips each time &

price exceeded the low by 7 times by an average of 24 pips each time.