Using Custom Indicator buffer without declaration?

[Deleted]  

Hi, Generally for using custom indicator buffer, we need to declare it at first in following way.

#property indicator_buffers 3

But in my indicator this number of buffer is not fixed, it calculated with Chart High price & Low price / scale unit. to decide how many buffer it will required to plot data.

So such case how can I do that?

I don't want to draw graphical objects as It will be like a MA. So indicator buffer is my best option.

Please guide.

Thank you 

Keith Watford
Moderator
31035
Keith Watford  
Declare as many as you may likely need but use only as many as necessary.
[Deleted]  
GumRai:
Declare as many as you may likely need but use only as many as necessary.

Thank you for reply.

I am using following code for custom buffer number. But it showing array required error.

int OnInit()
  {
 
 //-----------------------------------------------------------//
   double CalcDigits = MarketInfo(Symbol(),MODE_DIGITS);
      {
      if(CalcDigits == 2 || CalcDigits == 3) CalcPoint = 100;
      else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 10000;
      } 
  
//----Getting buffer data---------------//
double   top =  WindowPriceMax();
double   bottom = WindowPriceMin();
double   range = floor(MathAbs(top-bottom)*CalcPoint);
int   buffscale = (int) floor(range / floor(Scale)) * 2; // Total number of buffer used for all type of indicator.
double P[10]; // Array declaration with any value at 1st.
ArrayResize(P,buffscale,10); // Then resizing it with buffscale
  
//--- indicator buffers mapping
  for(int sc = 0; sc <= buffscale; sc++ )
  {
   //---- 1st Indicator
   SetIndexBuffer(sc,P[sc]);
   SetIndexStyle(sc,DRAW_SECTION,STYLE_SOLID,1,Col1);
   SetIndexLabel(sc,"Resistance 1");
   
   //---- 2nd Indicator
   SetIndexBuffer(sc,P[sc]);
   SetIndexStyle(sc,DRAW_SECTION,STYLE_SOLID,1,Col2);
   SetIndexLabel(sc,"Resistance 2");
   
   
   }

   
//---
   return(INIT_SUCCEEDED);
  }

 Any correction in this code or any idea to allocate the buffer in reasonable way?

peter.MT4Web
157
peter.MT4Web  

Have you ever looked in the manual what an indicator buffer is and how it works?

Why are you writing code without knowing the basics?

William Roeder
25046
William Roeder  
You've written babble. sc is an array, P is an array, P[sc] is garbage. Start studying multi-line indicators.
[Deleted]  

Sorry my bad, I did a serious silly mistake, I overlooked indicator buffer as normal array at 1st.

int OnInit()
  {
 
 //-----------------------------------------------------------//
   double CalcDigits = MarketInfo(Symbol(),MODE_DIGITS);
      {
      if(CalcDigits == 2 || CalcDigits == 3) CalcPoint = 100;
      else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 10000;
      } 
  
//----Getting buffer data---------------//
double   top =  WindowPriceMax();
double   bottom = WindowPriceMin();
double   range = floor(MathAbs(top-bottom)*CalcPoint);
int   buffscale = (int) floor(range / floor(Scale)); 
double P[],N[]; // Array 

  
//--- indicator buffers mapping
  for(int sc = 0; sc <= buffscale; sc++ )
  {
   //---- 1st Indicator
   SetIndexBuffer(sc,P);
   SetIndexStyle(sc,DRAW_SECTION,STYLE_SOLID,1,Col1);
   SetIndexLabel(sc,"1st R");
  }
  for(int sc = 0; sc<= buffscale; sc++)
  {
   //---- 2nd Indicator
   SetIndexBuffer(sc,N);
   SetIndexStyle(sc,DRAW_SECTION,STYLE_SOLID,1,Col2);
   SetIndexLabel(sc,"2nd R");
   
   
   }

   
//---
   return(INIT_SUCCEEDED);
  }

Any Coding example of multi line indicator will be helpful. In this code, above code doesn't have any error. But logically its wrong. Because, its creating multiple buffer but with same array. Here I explain what I actually required to do.

I need P[] buffer also in P1[], P2[] way, each of the P1[] should be in same size buffscale also same number of buffscale. P1[]........Pn[] (n = buffscale) 

For example, 

If buffscale is 50 then I need indicator buffer array initialize from P1[] to P50[]  

How to do that? Using double array buffer?

Thank you in advance.

[Deleted]  

I figured that I need double array for each indicator type: 

So for buffer allocation: 

For IndicatorOne[i][ii] where i = buffscale & ii = Indicator Counted() Number of bar index

So I tried coding in following way,

struct dynamic_Buffscale
      {

       double IndicatorOne[];
       
      };

 
dynamic_Buffscale DB[];

int OnInit()
  {

int buffscale = Calculation; 
int CB = IndicatorCounted();
   if(CB<0) return(-1);
   if(CB>0) CB--;
int LT = Bars-CB;
//--- indicator buffers mapping
 ArrayResize(DB,buffscale,1);


   for(int sc = 0; sc <= buffscale; sc++ )
     {
        ArrayResize(DB[sc].IndicatorOne,LT,0);
        ArrayInitialize(DB[sc].IndicatorOne,0);
        
        //---- 1st Indicator
       SetIndexBuffer(sc,DB[sc].IndicatorOne);
       SetIndexStyle(sc,DRAW_SECTION,STYLE_SOLID,1,Col1);
       SetIndexLabel(sc,"IndicatorOne");
     }

   return(INIT_SUCCEEDED);
  }

// Inside the Oncalculate Part //

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

 for(int i=0; i<limit; i++)
 {
   Data = Calculation(iOpen(Symbol(), PERIOD_D1,i));

//------------2nd loop --------------------------//

       for(int ii = 0; ii<= buffscale; ii++)
      {
        
            
               DB[ii].IndicatorOne[i] = ((newsetbase + floor(Data * Scale)) + (ii*SK))/CalcPoint;
         
            
      }
     

}

Above code shows No Compilation Error . But While adding this indicator on chart showing out of range error at this line

ArrayResize(DB[sc].IndicatorOne,LT,0);

 Plus error of No initialization. sc is the buffscale in correct size then whats is the problem here?

Please help I coded upto this. 

Keith Watford
Moderator
31035
Keith Watford  

I have no idea what you are trying to do.

You seem to be mixing structs and buffers.

IndicatorCounted probably is not assigned a value in Init.

I can't make any suggestions because I am totally baffled

[Deleted]  
GumRai:

I have no idea what you are trying to do.

You seem to be mixing structs and buffers.

IndicatorCounted probably is not assigned a value in Init.

I can't make any suggestions because I am totally baffled

Sorry to make you baffled. I am trying my best to explain it to you. 

My Indicator counts resulted data for each daily bar & for each daily bar it counts N number of  Data (BuffScale).  That means I need N number of buffer for each day data.

My Indicator required double array for indicator buffer as it seems.

Indicator[Bar Index][ Buffer Scale aka. N number ]

If Buffscale is 5 then it will allocate like indicator[0,1], [0,2],[0,3],[0,4],[0,5] in following way. for next bar indicator[1,1],[1,2] ....etc.

In side main function we will run loop in following way (Example). Can you please help now? How to setup the array then without stuct

 for(int i=0; i<limit; i++)
 {
   Data = Calculation with i;

       for(int j = 0; j<= buffscale; j++)
      {
        
           Indicator[i][j] = Calculation with Data & j;       
            
      } 

}
[Deleted]  
Can we add 2D array with indicator buffer anyhow or it will be always one dimensional?
William Roeder
25046
William Roeder  
Perhaps you should read the manual. SetIndexBuffer - Custom Indicators - MQL4 Reference The very first line:
The function binds a specified indicator buffer with one-dimensional dynamic array of the double type.