I have removed a section of code and modified the code as given below.
Could any one please tell me, what is the significance of the removed code. I am new to MQL4 and would really be thankful if anyone tell me removed code.
Play videoPlease edit your post.
For large amounts of code, attach it.
- Do you really expect us to go line by line to figure out what you removed and what you added? Comment out the removed code and add comments bracketing the added code.
Really sorry for my crude way of presentation. And thanks for the fast reply. For an easier formulation of my problem, I have divided the original code (which I downloaded from METAQUOTES site) in various blocks as given below
#property indicator_chart_window #property indicator_buffers 3 #property indicator_color1 LightSeaGreen #property indicator_color2 LightSeaGreen #property indicator_color3 LightSeaGreen //---- indicator parameters extern int BandsPeriod=20; extern int BandsShift=0; extern double BandsDeviations=2.0; //---- buffers double MovingBuffer[]; double UpperBuffer[]; double LowerBuffer[]; int init() { //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,MovingBuffer); SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,UpperBuffer); SetIndexStyle(2,DRAW_LINE); SetIndexBuffer(2,LowerBuffer); //-----------------------------------------------------------------------------------------BLOCK 1 SetIndexDrawBegin(0,BandsPeriod+BandsShift); SetIndexDrawBegin(1,BandsPeriod+BandsShift); SetIndexDrawBegin(2,BandsPeriod+BandsShift); //-----------------------------------------------------------------------------------------BLOCK 2 return(0); } int start() { int i,k,counted_bars=IndicatorCounted(); double deviation; double sum,oldval,newres; //---- if(Bars<=BandsPeriod) return(0); //---- initial zero //-----------------------------------------------------------------------------------------BLOCK 3 if(counted_bars<1) for(i=1;i<=BandsPeriod;i++) { MovingBuffer[Bars-i]=EMPTY_VALUE; UpperBuffer[Bars-i]=EMPTY_VALUE; LowerBuffer[Bars-i]=EMPTY_VALUE; } //-----------------------------------------------------------------------------------------BLOCK 4 int limit=Bars-counted_bars; if(counted_bars>0) limit++; for(i=0; i<limit; i++) MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i); //-----------------------------------------------------------------------------------------BLOCK 5 i=Bars-BandsPeriod+1; if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1; //-----------------------------------------------------------------------------------------BLOCK 6 while(i>=0) { sum=0.0; k=i+BandsPeriod-1; oldval=MovingBuffer[i]; while(k>=i) { newres=Close[k]-oldval; sum+=newres*newres; k--; } deviation=BandsDeviations*MathSqrt(sum/BandsPeriod); UpperBuffer[i]=oldval+deviation; LowerBuffer[i]=oldval-deviation; i--; } //------------------------------------------------------------------------------------------------- return(0); } //+------------------------------------------------------------------+
The blocks I removed are
//-----------------------------------------------------------------------------------------BLOCK 1 SetIndexDrawBegin(0,BandsPeriod+BandsShift); SetIndexDrawBegin(1,BandsPeriod+BandsShift); SetIndexDrawBegin(2,BandsPeriod+BandsShift); //-----------------------------------------------------------------------------------------BLOCK 3 if(counted_bars<1) for(i=1;i<=BandsPeriod;i++) { MovingBuffer[Bars-i]=EMPTY_VALUE; UpperBuffer[Bars-i]=EMPTY_VALUE; LowerBuffer[Bars-i]=EMPTY_VALUE; } //-----------------------------------------------------------------------------------------BLOCK 4 int limit=Bars-counted_bars; if(counted_bars>0) limit++; for(i=0; i<limit; i++) MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);
And I made modification for BLOCK 5 in the following line
i=Bars-BandsPeriod+1;
The new code for the above line is
i=Bars-BandsPeriod-1;
By this, what I have done is, the indicator drawing starts only after n bars, where n= the period for the bollinger bands. This helps to remove the codes given in BLOCK 1
Also I shifted the line in the BLOCK 4
MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);
to BLOCK 6 as given below
//-----------------------------------------------------------------------------------------BLOCK 6 i=Bars-BandsPeriod-1; if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1; while(i>=0) { sum=0.0; k=i+BandsPeriod-1; MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i); oldval=MovingBuffer[i]; while(k>=i) { newres=Close[k]-oldval; sum+=newres*newres; k--; } deviation=BandsDeviations*MathSqrt(sum/BandsPeriod); UpperBuffer[i]=oldval+deviation; LowerBuffer[i]=oldval-deviation; i--; }
The modified code for the whole program looks as given below
#property indicator_chart_window #property indicator_buffers 3 #property indicator_color1 LightSeaGreen #property indicator_color2 LightSeaGreen #property indicator_color3 LightSeaGreen //---- indicator parameters extern int BandsPeriod=20; extern int BandsShift=0; extern double BandsDeviations=2.0; //---- buffers double MovingBuffer[]; double UpperBuffer[]; double LowerBuffer[]; int init() { //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,MovingBuffer); SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,UpperBuffer); SetIndexStyle(2,DRAW_LINE); SetIndexBuffer(2,LowerBuffer); //-----------------------------------------------------------------------------------------BLOCK 1 //REMOVED FROM THE ORIGINAL //-----------------------------------------------------------------------------------------BLOCK 2 return(0); } int start() { int i,k,counted_bars=IndicatorCounted(); double deviation; double sum,oldval,newres; //---- if(Bars<=BandsPeriod) return(0); //---- initial zero //-----------------------------------------------------------------------------------------BLOCK 3 //REMOVED FROM THE ORIGINAL //-----------------------------------------------------------------------------------------BLOCK 4 //REMOVED FROM THE ORIGINAL //-----------------------------------------------------------------------------------------BLOCK 5 i=Bars-BandsPeriod-1; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<MODIFIED CODE if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1; //-----------------------------------------------------------------------------------------BLOCK 6 while(i>=0) { sum=0.0; k=i+BandsPeriod-1; MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<INSERTED CODE oldval=MovingBuffer[i]; while(k>=i) { newres=Close[k]-oldval; sum+=newres*newres; k--; } deviation=BandsDeviations*MathSqrt(sum/BandsPeriod); UpperBuffer[i]=oldval+deviation; LowerBuffer[i]=oldval-deviation; i--; } //------------------------------------------------------------------------------------------------- return(0); } //+------------------------------------------------------------------+
MY PROBLEM
1. I am a new bie. Even though the code seems to work well, I'm sure there is a reason for the deleted codes' existence.
2. I do not understand why the codes in the BLOCK 3 are there for, in the original code.
PLEASE DO HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- What part of "EDIT your post" was unclear?
//{ removed // SetIndexDrawBegin(0,BandsPeriod+BandsShift); // SetIndexDrawBegin(1,BandsPeriod+BandsShift); // SetIndexDrawBegin(2,BandsPeriod+BandsShift); //} removed
These simply say do not draw the first per+shift bars on the left. Most of the indicators calculate all bars even though the first n are invalid. This hides the invalid ones.if(counted_bars<1)
for(i=1;i<=BandsPeriod;i++) { MovingBuffer[Bars-i]=EMPTY_VALUE; UpperBuffer[Bars-i]=EMPTY_VALUE; LowerBuffer[Bars-i]=EMPTY_VALUE; } This hides the initial lines. Unnecessary since they are already hidden by #2 AND if you don't write to a element that is what it already contains. Probably 2 & 3 were coded like that because either coded didn't understand properly or there were bugs in mt4 10 years ago
//{changed // i=Bars-BandsPeriod+1; i=Bars-BandsPeriod-1; //} if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1;
It probably was i = Bars - (BandsPeriod+1). Yours is correct. But it doesn't matter since the extra line segment is hidden (2&3)
You moved the calculation of moving buffer[i] and rms[i] into one loop. But rms looks at BandsPeriod bar back, there is no bars back when count=0 and i = Bars-0-1. You have two look backs and should handle each independently.int limit=Bars-counted_bars; if(counted_bars>0) limit++; for(i=0; i<limit; i++) MovingBuffer[i]=iMA(.. i=Bars-BandsPeriod+1; if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1; while(i>=0) { sum=0.0; k=i+BandsPeriod-1; oldval=MovingBuffer[i]; while(k>=i) { newres=Close[k]-oldval; sum+=newres*newres; k--; }
while(i>=0) { sum=0.0; k=i+BandsPeriod-1; MovingBuffer[i]=iMA(.. oldval=MovingBuffer[i]; while(k>=i) { newres=Close[k]-oldval; sum+=newres*newres; k--; }
int LBma = BandsPeriod + BansShift, // Lookback LBstd= LBma + BandsPeriod; if(counted_bars < LBma) counted_bars = LBma; for(int i= Bars - 1 - countedBars; i >= 0; i--) MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i); if(counted_bars < LBstd) counted_bars = LBstd; for(i= Bars - 1 - countedBars; i >= 0; i--){ : deviation=BandsDeviations*MathSqrt(sum/BandsPeriod); UpperBuffer[i]=oldval+deviation; LowerBuffer[i]=oldval-deviation; }
Also the decrement of counted is unnecessary
- What part of "EDIT your post" was unclear?
-
These simply say do not draw the first per+shift bars on the left. Most of the indicators calculate all bars even though the first n are invalid. This hides the invalid ones.
-
This hides the initial lines. Unnecessary since they are already hidden by #2 AND if you don't write to a element that is what it already contains. Probably 2 & 3 were coded like that because either coded didn't understand properly or there were bugs in mt4 10 years ago
-
It probably was i = Bars - (BandsPeriod+1). Yours is correct. But it doesn't matter since the extra line segment is hidden (2&3)
You moved the calculation of moving buffer[i] and rms[i] into one loop. But rms looks at BandsPeriod bar back, there is no bars back when count=0 and i = Bars-0-1. You have two look backs and should handle each independently. Also the decrement of counted is unnecessary
Again sorry for not editing the post. ( This is my first thread ) so didnt understand how to edit. And thanks for the reply. AndI see that, the algo you mentioned last is more efficient. Again thanx a ton.
Play video | Please edit your post. For large amounts of code, attach it. |
Then why didn't you click on the link I provided?
Thanx. Now I got it. Didnt notice the link earlier. No excuse though.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Below is the code from the MetaQoutes site for Bollinger Bands custom indicator
Could any one please tell me, what is the significance of the removed code. I am new to MQL4 and would really be thankful if anyone tell me removed code.