extern int SlowPeriod=100; extern int FastPeriod=15; extern int MaMethod=MODE_SMA; #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 Blue //---- buffers double L1[]; double Price[]; int indexbegin = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorBuffers(2); SetIndexBuffer(0,L1); SetIndexLabel(0, "L1"); SetIndexStyle( 0, DRAW_HISTOGRAM); SetIndexBuffer(1,Price); indexbegin = Bars - 20; if (indexbegin < 0) indexbegin = 0; return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int i,j,k; int counted_bars = IndicatorCounted(); //---- check for possible errors if (counted_bars < 0) counted_bars = 0; //---- last counted bar will be recounted if (counted_bars > 0) counted_bars--; if (counted_bars > indexbegin) counted_bars = indexbegin; for (i = indexbegin-counted_bars; i >= 0; i--) { ArrayCopySeries(Price,MODE_CLOSE,Symbol(),Period()); L1[i]=-iMAOnArray(Price,0,SlowPeriod,0,MaMethod,i)+iMAOnArray(Price,0,FastPeriod,0,MaMethod,i); //if instead of the above line I use this: //L1[i]=Price[i]; //a histogram is ploted. so there is not any problem with other parts of code. Only iMAOnArray has a problem. } return(0); } //+------------------------------------------------------------------+Two additional lines bolded in the init function
AR78 wrote:
Thanks stringo
Do you mean iMAOnArray only works on indicator buffers and not on ordinary arrays?
even without those two lines array Price were containing values and
L1[i]=Price[i];
were functioning.
In fact, I prefere to use iMAOnArray on arrays in my code.
Parameters:
Sample: double macd_buffer[300]; double signal_buffer[300]; int i,limit=ArraySize(macd_buffer); ArraySetAsSeries(macd_buffer,true); for(i=0; i<limit; i++) macd_buffer[i]=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,i); for(i=0; i<limit; i++) signal_buffer[i]=iMAOnArray(macd_buffer,limit,9,0,MODE_SMA,i); | ||||||||
Thanks CockeyedCowboy
but this doesn't solve the problem. Please pay attention to the bold lines:
extern int SlowPeriod=100; extern int FastPeriod=15; extern int MaMethod=MODE_SMA; #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 Blue //---- buffers double L1[]; double Price[]; int indexbegin = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { SetIndexBuffer(0,L1); SetIndexLabel(0, "L1"); SetIndexStyle( 0, DRAW_HISTOGRAM); ArraySetAsSeries(Price,true); //Adding or removing the following line also has no effect: //ArrayResize(Price,Bars); indexbegin = Bars - 20; if (indexbegin < 0) indexbegin = 0; return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int i,j,k; int counted_bars = IndicatorCounted(); //---- check for possible errors if (counted_bars < 0) counted_bars = 0; //---- last counted bar will be recounted if (counted_bars > 0) counted_bars--; if (counted_bars > indexbegin) counted_bars = indexbegin; for (i = indexbegin-counted_bars; i >= 0; i--) { ArrayCopySeries(Price,MODE_CLOSE,Symbol(),Period()); L1[i]=-iMAOnArray(Price,0,SlowPeriod,0,MaMethod,i)+iMAOnArray(Price,0,FastPeriod,0,MaMethod,i); //if instead of the above line I use this: //L1[i]=Price[i]; //a histogram is ploted. so there is not any problem with other parts of code. Only iMAOnArray has a problem. } return(0); } //+------------------------------------------------------------------+
Result is the same
Excuse me stringo
no new Idea?
tumbleweed rolls by...
Your Price array is not allocated. 3 ways:
1. Assign it to index buffer (my example)
2. Define it with non-zero size (double Price[10000];)
3. Resize it on the every start (very slow solution).
Thanks very much stringo.
and one more thing:
why
3. Resize it on the every start (very slow solution)?
And why not resizing it only once at init() ?

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I had a problem with one of my custom indicators. after many hours of debuging and summarizing I reached to this piece of code. But this code plots nothing. can you please help?