MQL4向数组分配值失败

时间:2018-07-11 09:02:24

标签: mql4 mt4

这是我的代码,试图生成ADC Cloud指标。 首先,如果我只是生成云,它就可以工作。

当前,我正在尝试使直方图在零以上时变为绿色,否则为红色。然后,我将Could数组分为两个缓冲区GreenBuffer和RedBuffer。在这一步,我陷入了未知错误。

我可以确定问题是由Sharp登录代码标记的错误部分引起的。

首先谢谢你!

#property strict
#property indicator_separate_window
#property indicator_buffers 2

//--- input parameters
input int ADX_period=14;

double Cloud[];
double GreenBuffer[];
double RedBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
    {
//--- indicator buffers mapping
    //SetIndexBuffer(0, GreenBuffer);
    SetIndexBuffer(0, Cloud);
    SetIndexLabel(0, "Cloud");
    SetIndexStyle(0, DRAW_HISTOGRAM, 0, 2, clrGreen);

    //SetIndexBuffer(1, Cloud);
    //SetIndexStyle(1, DRAW_HISTOGRAM, 0, 2, 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[])
    {
//---
    int Limit_calc = 0;
    int BarCnt = IndicatorCounted();

    Limit_calc = Bars - BarCnt;

    for (int i = Limit_calc-1; i >= 0 ; i--)
        {
        double output = iADX(NULL, 0, ADX_period, PRICE_CLOSE, MODE_PLUSDI, i)
                   - iADX(NULL, 0, ADX_period, PRICE_CLOSE, MODE_MINUSDI, i);

        Cloud[i] = output;
        // #########################################
        // ###### Error Part #######################
        //if (output > 0)
        //    {
        //    GreenBuffer[i] = output;
        //    RedBuffer[i] = 0.00;
        //    }
        //else
        //    {
        //    GreenBuffer[i] = 0.00;
        //    RedBuffer[i] = output;
        //    }
        // ##########################################
        }

    //Comment(Cloud[1]);

//--- return value of prev_calculated for next call
    return(rates_total);
    }

2 个答案:

答案 0 :(得分:0)

int OnInit()
   {
//--- indicator buffers mapping
    SetIndexBuffer(0, GreenBuffer);
    SetIndexLabel(0, "Green");
    SetIndexStyle(0, DRAW_HISTOGRAM, 0, 2, clrGreen);

    SetIndexBuffer(0, RedBuffer);
    SetIndexLabel(0, "Red");
    SetIndexStyle(0, DRAW_HISTOGRAM, 0, 2, clrRed);
//---
    return(INIT_SUCCEEDED);
   }

int OnCalculate( *** )
   {
    ...
    for (int i = Limit_calc-1; i >= 0 ; i--)
      {
       double output = iADX(NULL, 0, ADX_period, PRICE_CLOSE, MODE_PLUSDI, i)
               - iADX(NULL, 0, ADX_period, PRICE_CLOSE, MODE_MINUSDI, i);
       if (output > 0)
         {
          GreenBuffer[i] = output;
          RedBuffer[i] = 0.00;
         }
       else
         {
          GreenBuffer[i] = 0.00;
          RedBuffer[i] = output;
         }
      }
...
   }

答案 1 :(得分:0)

已修复和[PASS]-on-TESTED代码:

#property strict
#property indicator_separate_window
#property indicator_buffers 2

//--- input parameters
input int    ADX_period = 14;

      double GreenBuffer[],
               RedBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
    {
//--- indicator buffers mapping
    SetIndexBuffer( 0,  GreenBuffer );
    SetIndexLabel(  0, "Green" );
    SetIndexStyle(  0, DRAW_HISTOGRAM, 0, 2, clrGreen );

    SetIndexBuffer( 1,  RedBuffer );
    SetIndexLabel(  1, "Red" );
    SetIndexStyle(  1, DRAW_HISTOGRAM, 0, 2, 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[]
                 )
    {
//---
    int BarCnt     = IndicatorCounted();
    int Limit_calc = Bars - BarCnt;

    for ( int i = Limit_calc - 1; i >= 0 ; i-- )
    {
        double output = iADX( NULL, 0, ADX_period, PRICE_CLOSE, MODE_PLUSDI,  i )
                      - iADX( NULL, 0, ADX_period, PRICE_CLOSE, MODE_MINUSDI, i );

        if (   output > 0 )  { GreenBuffer[i] = output;  RedBuffer[i] = 0.00;   }
        else {                 GreenBuffer[i] = 0.00;    RedBuffer[i] = output; }
        }
//---
    return( rates_total );
    }