Problem with custom indicator not refreshing

 

Hi There.

I made this indicator which works OK, but will not continue after new bars appear. The only way I can get it to refresh is to switch between time frames. 

It references the differential between 2 different charts 

Any help would be greatly appreciated .

 

Kind Regards 

 David

 

 

 #property  copyright "Copyright © 2016, MetaQuotes Software Corp."

#property  link      "http://www.metaquotes.net/"


#property  indicator_separate_window

#property  indicator_buffers 1

#property  indicator_color1  Silver

#property  indicator_width1  2


double     Buffer[];


 int period = 15;

 int Mode = 0;           // 0= MA, 1=EMA, 2=SMMA, 3=LWMA

 int Price = 5;    //0=Close, 4=Median, 5=Typical, 6=Weighted

 int init()

  {

   IndicatorBuffers(1);

      SetIndexStyle(0,DRAW_HISTOGRAM);

   SetIndexDrawBegin(0,9);

   IndicatorDigits(Digits+2);

   SetIndexBuffer(0,Buffer);

   return(0);

  }

   int deinit()

 {

   return(0);

  }

  int start()

  {

     int limit;

   int counted_bars=IndicatorCounted(); 

   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;

   for(int i=0; i<limit; i++)

                  {

          Buffer[i]= iClose("EURJPY",0,i) - (iClose("AUDUSD",0,i) *     

                (iMA("EURJPY",0,period,0,Mode,Price,i+1) / iMA("AUDUSD",0,period,0,Mode,Price,i+1)));

                   }

      return(0);

     }

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

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

  2. This is pre build 600 type code. Don't use ints when you can use the enumeration.
    int Mode = 0;           // 0= MA, 1=EMA, 2=SMMA, 3=LWMA
    int Price = 5;    //0=Close, 4=Median, 5=Typical, 6=Weighted
    Especially if you make these inputs.
    ENUM_MA_METHOD     Mode  = MODE_SMA;
    ENUM_APPLIED_PRICE Price = PRICE_TYPICAL;
    
    See Smoothing Methods and Price Constants.

  3. if(counted_bars>0) counted_bars--;
    No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum 
  4. iMA(period) has a lookback of period, so your first iMA(period,Bars-1) is likely zero and you divide by zero and the indicator stops. Handle your lookback properly.
    int counted = IndicatorCounted();
    int lookback = ... // iMA(period) has look back of period.
                       // buffer[i+2] has look back of 2
                       // use maximum of all.
    for(int iBar = Bars - 1 - MathMax(lookback, counted); iBar >= 0; --iBar) ...
    
  5. Veeb_d: but will not continue after new bars appear.
    Add a print statement before the for and show Bars, counted_bars and limit and find out why.
 

Hey, 

Thanks for the info. 

It is a great help. It looks like there are a variety of ways to achieve the outcome. Your idea is good.

 

Many thanks

 

David