Help please: TMA indicator

 

Hi, I am trying to create a triple MA indicator. It is supposed to plot the average High prices, average Low prices, and average Closing prices on the chart. The average High prices is plotted correctly, but the other two are plotted way below the actual price movement. What is wrong with my code ?

//+------------------------------------------------------------------+

//| Triple Moving Average                                                      |

//+------------------------------------------------------------------+

void tma()

  {

   double sumH=0;

   double sumL=0;

   double sumC=0;

   int    i,pos=Bars-ExtCountedBars-1;

//---- initial accumulation

   if(pos<MA_Period) pos=MA_Period;

   for(i=1;i<MA_Period;i++,pos--)

      sumH+=High[pos];

      sumL+=Low[pos];

      sumC+=Close[pos];

//---- main calculation loop

   while(pos>=0)

     {

      sumH+=High[pos];

      sumL+=Low[pos];

      sumC+=Close[pos];

      ExtMapBufferH[pos]=sumH/MA_Period;

      ExtMapBufferL[pos]=sumL/MA_Period;

      ExtMapBufferC[pos]=sumC/MA_Period;

       sumC-=Close[pos+MA_Period-1];

       sumH-=High[pos+MA_Period-1];

       sumL-=Low[pos+MA_Period-1];

        pos--;

     }

 

Thanks for your help. 

 
ExtMapBufferH[i]=iMA(Symbol(),0,MA_Period,0,MODE_SMA,PRICE_HIGH,i);
ExtMapBufferL[i]=iMA(Symbol(),0,MA_Period,0,MODE_SMA,PRICE_LOW,i);
ExtMapBufferC[i]=iMA(Symbol(),0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i);
 
Sjarko: What is wrong with my code ?

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2.    int    i,pos=Bars-ExtCountedBars-1;
       for(i=1;i<MA_Period;i++,pos--)
          sumH+=High[pos];
          sumL+=Low[pos];
          sumC+=Close[pos];
    //---- main calculation loop
       while(pos>=0)
    After the first run Pos is set to zero. the For sets it negitive (and sums a group of High[-n] bogus) the While loop never executes.
  3. No need for the decrement counter
  4. Do it like deVries or more like
    ExtMapBufferH[pos] = ExtMapBufferH[pos+1] + (High[pos] - High[pos + MA_Period])/MA_Period;
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. After the first run Pos is set to zero. the For sets it negitive (and sums a group of High[-n] bogus) the While loop never executes.
  3. No need for the decrement counter
  4. Do it like deVries or more like


Many thanks deVries & WHRoeder. I am using deVries' suggestion and it is working. I realise I was trying to reinvent the wheel. The iMA function already does what I manually wanted to compute.

WHRoeder: Please accept my apology for the long post. It is my first time posting here, and to make it more dificult, I am doing it from my iPad and can not upload the source document, so I just copied and pasted. I take note of your requirement and will conform to it in future. For now, I have edited it to only reflect the offending portion.

Once again, many thanks for the speedy help.

Reason: