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

时间:2017-07-16 16:07:26

标签: mql4

出于学习目的,我试图编写一个简单的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 );
  }
//+------------------------------------------------------------------+

2 个答案:

答案 0 :(得分: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;

关于解决问题 - 除了在这里询问之外,你总是可以尝试将指标附加到图表上,并在那里看到它没有错误(终端窗口 - 专家文件夹),这将使得更快的发展

答案 1 :(得分:0)

&#34;旧&#34; -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 );
}

-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 );
}