Indicator always slower then current chart

 

Can someone tell me or help me edit this, how to make the indicator synchronize with current chart? Thanks


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

//|                                                         Test.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Chiu Yin"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot UpClose
#property indicator_label1  "UpClose"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot LowClose
#property indicator_label2  "LowClose"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrSpringGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
double         UpCloseBuffer[];
double         LowCloseBuffer[];

input int   InpMAPeriod=60;// Period
input double   Line1;
input double   Line2;
input double   Line3;
input double   Line4;
input double   Line5;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorDigits(Digits);
   IndicatorShortName("YUANLI");
//--- indicator buffers mapping
   SetIndexBuffer(0,UpCloseBuffer);
   SetIndexBuffer(1,LowCloseBuffer);

   SetIndexDrawBegin(0,InpMAPeriod);
   SetIndexDrawBegin(1,InpMAPeriod);

   ObjectCreate(0,"Line1",OBJ_HLINE,ChartWindowFind(0,"YUANLI"),Time[0],Line1);
   ObjectSet("Line1",OBJPROP_COLOR,clrRed);

   ObjectCreate(0,"Line2",OBJ_HLINE,ChartWindowFind(0,"YUANLI"),Time[0],Line2);
   ObjectSet("Line2",OBJPROP_COLOR,clrRed);

   ObjectCreate(0,"Line3",OBJ_HLINE,ChartWindowFind(0,"YUANLI"),Time[0],Line3);
   ObjectSet("Line3",OBJPROP_COLOR,clrRed);

   ObjectCreate(0,"Line4",OBJ_HLINE,ChartWindowFind(0,"YUANLI"),Time[0],Line4);
   ObjectSet("Line4",OBJPROP_COLOR,clrRed);

   ObjectCreate(0,"Line5",OBJ_HLINE,ChartWindowFind(0,"YUANLI"),Time[0],Line5);
   ObjectSet("Line5",OBJPROP_COLOR,clrRed);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- check for bars count
   if(rates_total<=InpMAPeriod)
      return(0);
     
   int limit;

   limit=prev_calculated;

   for(int i=limit; i<rates_total; i++)
     {
      double ma=iMA(NULL,0,InpMAPeriod,0,MODE_SMMA,PRICE_CLOSE,i);
      if(ma!=0)
        {
         UpCloseBuffer[i]=(high[i]+ma)*ma /10000;
         LowCloseBuffer[i]=(low[i]+ma)*ma /10000;
        }
     }

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

 

Use the following </> button to post your program.


int limit;

if (prev_calculated == 0)
   limit = InpMAPeriod;
else
   limit = prev_calculated - 1;
 
Naguisa Unada:

Use the following </> button to post your program.


Thanks, but this code doesn't work, I still need to refresh the indicator to get latest, do I missing some code?
 

I thought it's for MT5 because this is not MT4 section.

Post questions about MT4 in https://www.mql5.com/en/forum/mql4

int i,limit;

if(prev_calculated==0)
   limit=rates_total-InpMAPeriod;
else
   limit=rates_total-prev_calculated;

for(i=limit; i>=0; i--)
{
 
 UpCloseBuffer[i]=(high[i]+ma)*ma /10000;

The number is too small

 UpCloseBuffer[i]=(high[i]+ma)*ma;
Reason: