指标永远比图表慢几个bar

 

请问如何让指标的运算和图表保持同步?谢谢,功力不够,想不到应该如何再改了


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

//|                                                         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
//--- indicator buffers
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);
  }
//+------------------------------------------------------------------+

 

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

//|                                                         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
//--- indicator buffers
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=rates_total-prev_calculated;

  limit++;

  
   for(int i=0 ;i<limit; 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);
  }
//+------------------------------------------------------------------+
 

rates_total :总K线数

prev_calculated:为指标计算赋值过的K线

 
Shaofei Chen:

rates_total :总K线数

prev_calculated:为指标计算赋值过的K线

谢谢,但情况没有改变,仍然不能实时的和图表同步我每次要重启这个指标才出现同一刻
 

需要初始化

 
不错
 
指标的底层原理是每次新柱产生进行一次收线柱计算,所以Tick实时更新在目前框架下没法实现,除非重写一个类,但显示在逻辑上也存在问题,当前柱的连线会不断变化位置,需要精密的逻辑设计
原因: