Download MetaTrader 5

Problem with custom indicator not refreshing

To add comments, please log in or register
All articles are available from MetaTrader 5. Contribute your own one!
David 2016.02.10 09:20 

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 




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

#property  link      ""

#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()









   int deinit()




  int start()


     int limit;

   int counted_bars=IndicatorCounted(); 

   if(counted_bars>0) 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)));





whroeder1 2016.02.10 14:04  
  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;
    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.
David 2016.02.11 09:03  


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



To add comments, please log in or register