View Single Post
  #6 (permalink)  
Old 07-20-2006, 04:08 AM
RJames5541 RJames5541 is offline
Junior Member
 
Join Date: Jul 2006
Posts: 15
RJames5541 is on a distinguished road
I am trying to write a program to plot the bandwidth between upper and lower bollinger bands. The program seems to be executing all of the math correctly since when I load the indicator into the trading terminal the value of the indicator (bandwidth) is displayed in the upper left corner of the indicator window. Unfortunately, no line is charted in the indicator window. Why might this be happening? any help would be great! ..... The code I have written so far is below:

Thanks,
Ryan

//+------------------------------------------------------------------+
//| Band Width of Bollinger Bands .mq4 |
//| Copyright © 2006, Ghostrider Capital LLC. |
//| ryanjmcgregor@tds.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ghostrider Capital LLC."
#property link "ryanjmcgregor@tds.net"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_maximum 1.5
#property indicator_minimum -0.5


//---- indicator parameters
extern int BandsPeriod=20;
extern int BandsShift=0;
extern double BandsDeviations=2.0;
//---- buffers
//---- Moving, Upper, and Lower are for counting to draw Band Width 60
double BandWidth60Buffer[];
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,DodgerBlue );
SetIndexBuffer(0,BandWidth60Buffer);
SetIndexLabel(0,"Band Width 20");

SetIndexBuffer(1,MovingBuffer);
SetIndexBuffer(2,UpperBuffer);
SetIndexBuffer(3,LowerBuffer);

//----
//---- 4 Indicators Buffers Mapping
SetIndexDrawBegin(0,0);
// SetIndexDrawBegin(1,BandsPeriod+BandsShift);
// SetIndexDrawBegin(2,BandsPeriod+BandsShift);
// SetIndexDrawBegin(3,BandsPeriod+BandsShift);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2 );
IndicatorShortName("Band Width of 20 2.0 BB");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
int start()
{
int i,k,counted_bars=IndicatorCounted();
double deviation;
double sum,oldval,newres,upper,lower,middle;

//----
if(Bars<=BandsPeriod) return(0);
//---- initial zero
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;
BandWidth60Buffer[Bars-i]=EMPTY_VALUE;
}
//----
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);
middle=MovingBuffer[i];
//----
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--;
}
deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
UpperBuffer[i]=oldval+deviation;
LowerBuffer[i]=oldval-deviation;
upper=UpperBuffer[i];
lower=LowerBuffer[i];
//---- Formula Band Width = (Upper - Lower) / Middle Band moving sma
BandWidth60Buffer[i]=(upper-lower)/middle;

i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Reply With Quote