虽然没有 MQL4 错误,但为什么没有生成 GUI 绘图?

While there are no MQL4 errors, why there was no GUI drawing produced?

出于学习目的,我正在尝试编写一个简单的 MA 指标,该指标会在价格交叉时改变颜色。虽然没有错误,但它什么也没画。您能否查看随附的代码以指出我的错误?

#property indicator_chart_window
#property indicator_buffers 2

extern int    maperiod = 20;
extern int    maprice  = PRICE_CLOSE;
extern int    mamethod = MODE_SMA;
extern color  colorup  = Green;
extern color  colordn  = Red;

       double mamain[];
       double bufferup[];
       double bufferdn[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){
//--- indicator buffers mapping
   SetIndexBuffer( 0, bufferup );
   SetIndexStyle(  0, DRAW_LINE, 0, 2, colorup );
   SetIndexBuffer( 1, bufferdn );
   SetIndexStyle(  1, DRAW_LINE, 0, 2, colordn );
//---
   return( 0 );
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){
//---
   int counted_bars = IndicatorCounted();
   if ( counted_bars < 0 ) return( -1 );
   if ( counted_bars > 0 ) counted_bars--;
   int limit = Bars - counted_bars;
   for (   int i = limit; i >= 0 ; i-- )
   {    mamain[i]  = iMA(    NULL, 0, maperiod, 0, 0, 0, i );
   if ( mamain[i] >= iClose( NULL, 0, i ) ) bufferup[i] = mamain[i];
   if ( mamain[i] <= iClose( NULL, 0, i ) ) bufferdn[i] = mamain[i];
   }
//--- return value of prev_calculated for next call
   return( 0 );
  }
//+------------------------------------------------------------------+

你的缓冲区mamain[]没有初始化。

int init(){

    IndicatorBuffers(3);
    SetIndexBuffer(2,mamain);
}

rates_totalprev_calculated 似乎是首选,但您当然可以使用 IndicatorCounted() 但请记住第一根柱线的角落情况:当您第一次将指标附加到图表时,您的 counted_bars = 0 和 limit = Bars,但是 mamain[] 和其他指标缓冲区只有 Bars 个元素,从 0 到 Bars-1。所以更好用
int limit = Bars - counted_bars - 1;

关于解决问题 - 除了在这里询问之外,您始终可以尝试将您的指标附加到图表并查看它没有错误(终端 window - Experts 文件夹),这将使开发更快

"Old"-MQL4 可能会工作一段时间,但仍然要习惯新功能:

extern ENUM_APPLIED_PRICE MAprice  = PRICE_CLOSE; // AVOIDS incompatible values
extern ENUM_MA_METHOD     MAmethod = MODE_SMA;    // AVOIDS incompatible values
#define                   MAshift     0           // ADDS   code == intent match-robustness
extern int                MAperiod = 20;
extern color              colorUP  =  clrGreen;
extern color              colorDN  =  clrRed;

       double             bufferUP[];
       double             bufferDN[];

int init(){
    ArrayInitialize    bufferUP,  EMPTY_VALUE );
    SetIndexBuffer( 0, bufferUP );
    SetIndexStyle(  0, DRAW_LINE, EMPTY, 2, colorUP );
    SetIndexLabel(  0, "MA_ABOVE_Close" );

    ArrayInitialize    bufferDN,  EMPTY_VALUE );
    SetIndexBuffer( 1, bufferDN );
    SetIndexStyle(  1, DRAW_LINE, EMPTY, 2, colorDN );
    SetIndexLabel(  1, "MA_UNDER_Close" );

    return( 0 );
}

int start(){
    int  counted_bars = IndicatorCounted();
    if ( counted_bars < 0 ) return( -1 );
    if ( counted_bars > 0 ) counted_bars--;

    for ( int    i = Bars - counted_bars; i >= 0 ; i-- ){
          double C = iClose( _Symbol, PERIOD_CURRENT, i ),
                 A = iMA(    _Symbol, PERIOD_CURRENT,
                                      MAperiod,
                                      MAshift,
                                      MAmethod,
                                      MAprice,
                                      i
                                      );
          if ( A >= C ) bufferUP[i] = A;
          if ( A <= C ) bufferDN[i] = A;
    }
    return( 0 );
}

New-MQL4.56789 使用另一个调用签名如果 #property strict:

int OnCalculate( const int       rates_total,
                 const int       prev_calculated,
                 const datetime &time_______ARR[],
                 const double   &open_______ARR[],
                 const double   &high_______ARR[],
                 const double   &low________ARR[],
                 const double   &close______ARR[],
                 const long     &tick_volumeARR[],
                 const long     &volume_____ARR[],
                 const int      &spread_____ARR[]
                 ){
//--- the main loop of calculations 
   for( int i  = prev_calculated - 1;
         (  i <  rates_total
         &&     !IsStopped() );          // AVOID SHARED (!) solo-THREAD BLOCKING
            i++
        ){
     // -----------------------------------------------------------------
          double A = iMA(    _Symbol, PERIOD_CURRENT,
                                      MAperiod,
                                      MAshift,
                                      MAmethod,
                                      MAprice,
                                      i
                                      );
          if ( A >= close______ARR[i] ) bufferUP[i] = A;
          if ( A <= close______ARR[i] ) bufferDN[i] = A;
     // -----------------------------------------------------------------
   }
   return( rates_total );
}