Any MQL4 programmers? What is wrong with this code

2019-06-11 22:51发布

When I try to divide the two doubles in a buffer my indicator blacks out, and the values go extreme in the second window -90000000 and 90000000

    #property indicator_separate_window // Îòîáðàæåíèå â îòäåëüíîì îêíå
    #property indicator_buffers 3       // Êîëè÷åñòâî áóôåðîâ
    #property indicator_color1 Red     // Öâåò ïåðâîé ëèíèè
    #property indicator_color2 Blue     // Öâåò âòîðîé ëèíèè
    #property indicator_color3 Green

    double FillBuffer[];
    double DBuffer[];
    double AverageBuffer[];

    double H,L;
    double point=Point();

   int init()                          // Ñïåöèàëüíàÿ ôóíêöèÿ init()
   {  
      int period =  _Period;  
      string symbol =  Symbol();
      int digits =  _Digits ;   
      point =  _Point ;

     if(digits == 5 || digits == 3) { digits = digits - 1 ; point = point * 10 ; } 


     SetIndexBuffer(0,DBuffer);   
     SetIndexBuffer(1,FillBuffer);
     SetIndexBuffer(2,AverageBuffer);    
     SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,1);
     SetIndexLabel(0, "ADR");


     return(INIT_SUCCEEDED);                       
     }

     int start()
     {


     int i, limit, counted_bars;

     counted_bars = IndicatorCounted();
     //---- check for possible errors
     if (counted_bars<0) return(-1);
     //---- last counted bar will be recounted
     if (counted_bars>0) counted_bars--;
     limit = Bars - counted_bars;

     for (i = limit; i >= 0; i--)
     {
     double dbuff= iHigh(NULL,0,i)- iLow(NULL,0,i);
     double D0  = iHigh(NULL,0,i+1)- iLow(NULL,0,i+1);
     double D1  = iHigh(NULL,0,i+2)- iLow(NULL,0,i+2);
     double D2  = iHigh(NULL,0,i+3)- iLow(NULL,0,i+3);
     double D3  = iHigh(NULL,0,i+4)- iLow(NULL,0,i+4);
     double D4  = iHigh(NULL,0,i+5)- iLow(NULL,0,i+5);
     double Average = ((D0+D1+D2+D3+D4)/5)/point;
     FillBuffer[i]=dbuff/Average;

     }

     return(0);

When I try to divide the two values in FillBuffer[] my indicator blacks out. But if I just have either the dbuff or Average in the buffer it will show lines but I want the percentage one is of the other to be printed.

标签: mql4
1条回答
唯我独甜
2楼-- · 2019-06-11 23:13

There is not much wrong with the code, but it does not paint the line:

Some polishing may help, but the core logic of the MQL4 Custom Indicator is forgotten in the code. If one does not assign a way, how to plot ( paint ) the line on screen, the GUI will remain "blacked-out" even though the values may have gotten calculated.


Performance warning:

Custom Indicators ( all Custom Indicators ) share one common solo-thread (!!), so proper performance tuning is warmly recommended in this type of MQL4-code-execution blocks.

Some further acceleration might be achieved by reducing / avoiding all the repetitive re-averaging via a sliding-window implementation replacement. While this is not so risky at 5-BARs deep re-processing, for deeper TimeSeries convolutions, the effect is significant and impressive in Strategy Tester accelerated mode of computing's run-times ( getting down to minutes instead of hours ). Worth one's time and efforts.

#property indicator_separate_window
#property indicator_buffers         3
#property indicator_color1          Red
#property indicator_color2          Blue
#property indicator_color3          Green

      double    FillBuffer[];
      double       DBuffer[];                         // NEVER FILLED IN
      double AverageBuffer[];                         // NEVER FILLED IN

      double point = Point();

int   init()
{     int    period =  _Period;                       // NEVER CONSUMED    
      string symbol =   Symbol();                     // NEVER CONSUMED
      int    digits =  _Digits ;                      // LOCAL SCOPE ONLY VISIBLE INSIDE init(){...}
             point  =  _Point ;

      if (  digits == 5 || digits == 3 ) { digits -=  1;
                                           point  *= 10;
                                           }

      SetIndexBuffer( 0,       DBuffer );
      SetIndexBuffer( 1,    FillBuffer );             // 1: ASSIGNED, BUT NEVER SET TO HAVE ANY { DRAW_LINE | DRAW_ ... } GUI OUTPUT
      SetIndexBuffer( 2, AverageBuffer );             // 2: ASSIGNED, BUT NEVER SET TO HAVE ANY { DRAW_LINE | DRAW_ ... } GUI OUTPUT
      SetIndexStyle ( 0, DRAW_LINE, STYLE_SOLID, 1 ); // 0: SET AS DRAW_LINE ( BUT NEVER FILLED IN WITH DATA )
      SetIndexLabel(  0, "ADR" );

      return( INIT_SUCCEEDED );
   }

int   start()
{     int i, limit, counted_bars;
      counted_bars = IndicatorCounted();
//----check for possible errors
      if ( counted_bars < 0 ) return( -1 );

//----last counted bar will be recounted
      if ( counted_bars > 0 ) counted_bars--;

      limit = ( Bars - 5 ) - counted_bars;            // AVOID 1st 5 BARS,
      int   i0, i1, i2, i3, i4, i5;                   // WHERE (i+5) WILL OVERFLOW THE TIMESERIES LEFT EDGE
      for ( i  = limit,
            i1 = i + 1,
            i2 = i + 2,
            i3 = i + 3,
            i4 = i + 4,
            i5 = i + 5; i >= 0; i--,
                                i1--,
                                i2--,
                                i3--,
                                i4--,
                                i5--
                                )
      {     FillBuffer[i]  = (   High[i]
                                - Low[i]
                                )
                           / ( ( High[i1] + High[i2] + High[i3] + High[i4] + High[i5] )
                             - (  Low[i1] +  Low[i2] +  Low[i3] +  Low[i4] +  Low[i5] )
                               )
                           / 5.
                           / point;

         /* double dbuff   = iHigh( NULL, 0, i   ) - iLow( NULL, 0, i   );
            double D0      = iHigh( NULL, 0, i+1 ) - iLow( NULL, 0, i+1 );
            double D1      = iHigh( NULL, 0, i+2 ) - iLow( NULL, 0, i+2 );
            double D2      = iHigh( NULL, 0, i+3 ) - iLow( NULL, 0, i+3 );
            double D3      = iHigh( NULL, 0, i+4 ) - iLow( NULL, 0, i+4 );
            double D4      = iHigh( NULL, 0, i+5 ) - iLow( NULL, 0, i+5 );
            double Average = ( ( D0 + D1 + D2 + D3 + D4 ) / 5 ) / point;
            FillBuffer[i]  = dbuff / Average;
            */
      }
      return( 0 );
 }
查看更多
登录 后发表回答