0

This is my code trying to generate the ADC Cloud indicator. At first, it can work if I just generate the cloud.

Currently, I am trying to make the histogram green when it is above zero, otherwise red. Then, I separate the Could array into two buffers, GreenBuffer and RedBuffer. At this step, I stuck in an unknown error.

I can make sure the problem is coming from the ERROR PART, marked by Sharp Sign in Code.

Thank you first!

#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);
    }
Carl Zheng
  • 708
  • 2
  • 6
  • 20

2 Answers2

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;
         }
      }
...
   }
Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
  • Sorry, I know you change the output variable, which is not the source of the error. I forget to change back when I test. Now, I edit my code to fix this. And currently, it still cannot work. I appreciate your help! – Carl Zheng Jul 12 '18 at 01:47
  • Have you checked OnInit()? Are you sure your one is same as I mentioned? If you had three buffers, one is not shown, and first one (Cloud) may be confusing – Daniel Kniaz Jul 12 '18 at 18:07
0

REPAIRED & [PASS]-on-TESTED CODE :

#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 );
    }
user3666197
  • 1
  • 6
  • 50
  • 92