Please HELP! assign value to a buffer does not work

 

When I try to assign a value to a buffer element it does not work ... even though the value s different from zero the buffer element is always zero,

in the code below sprLow[i] is always equal to zero, even though it has been assigned to 8.0, why?


#property copyright ""
#property link ""

#property indicator_buffers 5

#property indicator_separate_window
#property indicator_color1 Black
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Lime
#property indicator_color5 Red


//---- buffers
double vc[],
ind1[],
ind2[],
bbu[],
bbl[],
sprOpen[],
sprClose[],
sprHigh[],
sprLow[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(9);

SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,vc);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ind1);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ind2);
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,bbu);
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,bbl);

SetIndexBuffer(5,sprOpen);
SetIndexBuffer(6,sprClose);
SetIndexBuffer(7,sprHigh);
SetIndexBuffer(8,sprLow);

//----
return(0);

int start()
{

for(int i=Bars-2; i>=0; i--)
{
sprOpen[i]=2.5;
sprClose[i] = 3.0;
sprHigh[i] = 6.0;
sprLow[i] = 8.0;

}

}

 

IndicatorBuffers(9);

8 is the limit

However, you can have as many arrays that you manage as you like.

manage = declare, initialize, allocate storage, and specify as series or not

 

Thank you for the reply!

I have a couple of questions and really hope that you can answer

1. Do you have a piece of code where I can see how to declare, initialize, allocate storage for an array? this must be done where? In the Start() function or outside?

A piece of code would be really useful!

2.1. A the beginning of the code I have posted, the declaration

#property indicator_buffers 5

the number 5 indicates the number of buffers that actually I intend to display as a graphical object, correct?

This number can be different form the total number of buffers (some of which I do not have to display), the total number of buffers MUST be stated with the instruction

IndicatorBuffers(9);

correct?

2.2. the instruction

#property indicator_color1 Black

this will be associated BY DEFAULT to the buffer with index 0, correct?

THANK YOU!

 

declare:

double array[];

Storage:

ArrayResize(array, sizeInteger); // if you size it as 10, it has elements 0-9. Too big is better than too small.

Initialize:

ArrayInitialize(array, value); // it is already initialized to zero, use this if you want different

Sometimes you'll need your array to be a series array ( like the index bufers ) sometimes not:

ArraySetAsSeries(array, true/false)

---

8 is the maximum for indicator buffers, displayed or not.

---

#property indicator_color1 Black

= index 0, yes

---

// dumb sample, maintain an array of close prices
double array[];
int start(){
   ArrayResize(array, Bars);
   for(int i = Bars-1; i >= 0; i--){
      array[i] = Close[i];
   }
   return(0);
}
 
Here is my first indicator on MQL4:

//+------------------------------------------------------------------+
//|                                                          TDI.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
// Идея из ForexMagazin #58 
#property copyright "Written by Rosh"
#property link      "http://www.metaquotes.net"
 
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DarkBlue
#property indicator_color2 Red
//---- input parameters
extern int PeriodTDI=20;
//---- buffers
double TDI_Buffer[];
double Direction_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TDI_Buffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Direction_Buffer);
   SetIndexLabel(0,"Trend Direction Index");
   SetIndexLabel(1,"Direction");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double MomBuffer[];
   double MomAbsBuffer[];
   double MomSumBuffer[];
   double MomSumAbsBuffer[];
   double MomAbsSumBuffer[];
   double MomAbsSum2Buffer[];
   double temp;
   bool First=true;
   int limit,i;
   int    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--;
   if (First){limit=Bars-PeriodTDI; First=false;} else limit=Bars-counted_bars;
//   limit=Bars-PeriodTDI;
   ArrayResize(MomBuffer,limit);
   ArrayResize(MomAbsBuffer,limit);
   ArrayResize(MomSumBuffer,limit);
   ArrayResize(MomSumAbsBuffer,limit);
   ArrayResize(MomAbsSumBuffer,limit);
   ArrayResize(MomAbsSum2Buffer,limit);
   ArraySetAsSeries(MomBuffer,true);
   ArraySetAsSeries(MomAbsBuffer,true);
   ArraySetAsSeries(MomSumBuffer,true);
   ArraySetAsSeries(MomSumAbsBuffer,true);
   ArraySetAsSeries(MomAbsSumBuffer,true);
   ArraySetAsSeries(MomAbsSum2Buffer,true);
   
//---- TODO: add your code here
   for(i=0; i<limit; i++)
      {
      MomBuffer[i]=Close[i]-Close[i+PeriodTDI];
      MomAbsBuffer[i]=MathAbs(MomBuffer[i]);
      }
   for(i=limit; i>=0; i--)
      {
      temp=0;
      MomSumBuffer[i]=iMAOnArray(MomBuffer,0,PeriodTDI,0,MODE_SMA,i)*PeriodTDI;
      MomSumAbsBuffer[i]=MathAbs(MomSumBuffer[i]);
      Direction_Buffer[i]=MomSumBuffer[i];
      }
 
   for(int l=limit; l>=0; l--)
      {
      MomAbsSumBuffer[l]=iMAOnArray(MomAbsBuffer,0,PeriodTDI,0,MODE_SMA,l)*PeriodTDI;
      MomAbsSum2Buffer[l]=iMAOnArray(MomAbsBuffer,0,2*PeriodTDI,0,MODE_SMA,l)*2*PeriodTDI;
      TDI_Buffer[l]=MomAbsSum2Buffer[l]-MomAbsSumBuffer[l];
      }
      
//---- done
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Reason: