发表于:2008-08-17 08:10只看该作者
2楼
//+------------------------------------------------------------------+
//| Bands.mq4 |
//| Copyright ?2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#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;
extern int SMA1=3;
extern int SMA2=6;
extern int SMA3=12;
extern int SMA4=24;
//---- buffers
double MovingBuffer;
double UpperBuffer;
double LowerBuffer;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MovingBuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,UpperBuffer);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,LowerBuffer);
//----
SetIndexDrawBegin(0,BandsPeriod+BandsShift);
SetIndexDrawBegin(1,BandsPeriod+BandsShift);
SetIndexDrawBegin(2,BandsPeriod+BandsShift);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
int start()
{
int t,k,counted_bars=IndicatorCounted();
double deviation;
double sum,oldval,newres;
//----
if(Bars<=BandsPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(t=1;t<=BandsPeriod;t++)
{
MovingBuffer[Bars-t]=EMPTY_VALUE;
UpperBuffer[Bars-t]=EMPTY_VALUE;
LowerBuffer[Bars-t]=EMPTY_VALUE;
}
//----
int limit=Bars-counted_bars;
if(counted_bars>0) limit++;
for(t=0; tBandsPeriod-1) t=Bars-counted_bars-1;
while(t>=0)
{
sum=0.0;
k=t+BandsPeriod-1;
oldval=MovingBuffer[t];
while(k>=t)
{
newres=Close[k]-oldval;
sum+=newres*newres;
k--;
}
deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
UpperBuffer[t]=oldval+deviation;
LowerBuffer[t]=oldval-deviation;
t--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
3楼
谢谢秃鹫老师您!!!
韬客社区www.talkfx.co