รบกวน Admin อีกครั้งครับ ผมลองทำตามที่ Admin แนะนำแล้วครับ(แบบปูๆปลาเท่าที่ผมรู้นะครับ) ติดตรงที่ไม่มีError แต่มีแค่ค่าเดียวที่แสดงครับ ก็เลย งง ครับว่ามันผิดตรงไหน รบกวน Adminอีกครั้งนะครับ
ขอบคุณครับ
//+------------------------------------------------------------------+
//| Custom Moving Average.mq4 |
//| Copyright ฉ 2004, MetaQuotes Software Corp. |
//|
http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright ฉ 2004, MetaQuotes Software Corp."
#property link "
http://www.metaquotes.net/"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 clrRed
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color2 clrBlue
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color3 clrMagenta
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color4 clrDarkOrange
//---- indicator parameters
extern int MA_Period1=5;
extern int MA_Shift1=0;
extern int MA_Method1=0;
extern int MA_Period2=25;
extern int MA_Shift2=0;
extern int MA_Method2=0;
extern int MA_Period3=50;
extern int MA_Shift3=0;
extern int MA_Method3=0;
extern int MA_Period4=100;
extern int MA_Shift4=0;
extern int MA_Method4=0;
//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars1=0;
int ExtCountedBars2=0;
int ExtCountedBars3=0;
int ExtCountedBars4=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
int draw_begin1,draw_begin2,draw_begin3,draw_begin4;
string short_name1,short_name2,short_name3,short_name4;
//---- drawing settings
SetIndexStyle(1,DRAW_LINE,draw_begin1,2,clrRed);
SetIndexShift(1,MA_Shift1);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
if(MA_Period1<2) MA_Period1=13;
draw_begin1=MA_Period1-1;
SetIndexStyle(2,DRAW_LINE,draw_begin2,2,clrBlue);
SetIndexShift(2,MA_Shift2);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
if(MA_Period2<2) MA_Period2=13;
draw_begin2=MA_Period2-1;
SetIndexStyle(3,DRAW_LINE,draw_begin3,2,clrMagenta);
SetIndexShift(3,MA_Shift3);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
if(MA_Period3<2) MA_Period3=13;
draw_begin3=MA_Period3-1;
SetIndexStyle(4,DRAW_LINE,draw_begin4,2,clrDarkOrange);
SetIndexShift(4,MA_Shift4);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
if(MA_Period4<2) MA_Period4=13;
draw_begin4=MA_Period4-1;
//---- indicator short name
switch(MA_Method1)
{
case 1 : short_name1="SMA("; draw_begin1=0; break;
case 2 : short_name1="SMMA("; break;
case 3 : short_name1="LWMA("; break;
default :
MA_Method1=0;
short_name1="EMA(";
}
IndicatorShortName(short_name1+MA_Period1+")");
SetIndexDrawBegin(0,draw_begin1);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer1);
switch(MA_Method2)
{
case 1 : short_name2="SMA("; draw_begin2=0; break;
case 2 : short_name2="SMMA("; break;
case 3 : short_name2="LWMA("; break;
default :
MA_Method2=0;
short_name2="EMA(";
}
IndicatorShortName(short_name2+MA_Period2+")");
SetIndexDrawBegin(0,draw_begin2);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer2);
switch(MA_Method3)
{
case 1 : short_name3="SMA("; draw_begin3=0; break;
case 2 : short_name3="SMMA("; break;
case 3 : short_name3="LWMA("; break;
default :
MA_Method3=0;
short_name3="EMA(";
}
IndicatorShortName(short_name3+MA_Period3+")");
SetIndexDrawBegin(0,draw_begin3);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer3);
//---- initialization done
//---- indicator short name
switch(MA_Method4)
{
case 1 : short_name4="SMA("; draw_begin4=0; break;
case 2 : short_name4="SMMA("; break;
case 3 : short_name4="LWMA("; break;
default :
MA_Method4=0;
short_name4="EMA(";
}
IndicatorShortName(short_name4+MA_Period4+")");
SetIndexDrawBegin(0,draw_begin4);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer4);
IndicatorShortName(short_name1+MA_Period1+")");
SetIndexDrawBegin(0,draw_begin1);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer1);
IndicatorShortName(short_name2+MA_Period2+")");
SetIndexDrawBegin(0,draw_begin2);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer2);
IndicatorShortName(short_name3+MA_Period3+")");
SetIndexDrawBegin(0,draw_begin3);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer3);
IndicatorShortName(short_name4+MA_Period4+")");
SetIndexDrawBegin(0,draw_begin4);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer4);
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
if(Bars<=MA_Period1&&MA_Period2&&MA_Period3&&MA_Period4) return(0);
ExtCountedBars1=IndicatorCounted();
ExtCountedBars2=IndicatorCounted();
ExtCountedBars3=IndicatorCounted();
ExtCountedBars4=IndicatorCounted();
//---- check for possible errors
if(ExtCountedBars1<0&&ExtCountedBars2<0&&ExtCountedBars3<0&&ExtCountedBars4<0) return(-1);
//---- last counted bar will be recounted
if(ExtCountedBars1>0) ExtCountedBars1--;
if(ExtCountedBars2>0) ExtCountedBars2--;
if(ExtCountedBars3>0) ExtCountedBars3--;
if(ExtCountedBars4>0) ExtCountedBars4--;
//----
switch(MA_Method1)
{
case 0 : sma(); break;
case 1 : ema(); break;
case 2 : smma(); break;
case 3 : lwma();
}
switch(MA_Method2)
{
case 0 : sma(); break;
case 1 : ema(); break;
case 2 : smma(); break;
case 3 : lwma();
}
switch(MA_Method3)
{
case 0 : sma(); break;
case 1 : ema(); break;
case 2 : smma(); break;
case 3 : lwma();
}
switch(MA_Method4)
{
case 0 : sma(); break;
case 1 : ema(); break;
case 2 : smma(); break;
case 3 : lwma();
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
void sma()
{
double sum1=0;
double sum2=0;
double sum3=0;
double sum4=0;
int i,pos1=Bars-ExtCountedBars1-1;
int a,pos2=Bars-ExtCountedBars2-1;
int s,pos3=Bars-ExtCountedBars3-1;
int d,pos4=Bars-ExtCountedBars4-1;
//---- initial accumulation
if(pos1<MA_Period1) pos1=MA_Period1;
for(i=1;i<MA_Period1;i++,pos1--)
sum1+=Close[pos1];
if(pos2<MA_Period2) pos2=MA_Period2;
for(a=1;a<MA_Period2;a++,pos2--)
sum2+=Close[pos2];
if(pos3<MA_Period3) pos3=MA_Period3;
for(s=1;s<MA_Period3;s++,pos3--)
sum3+=Close[pos3];
if(pos4<MA_Period4) pos4=MA_Period4;
for(d=1;d<MA_Period4;d++,pos4--)
sum4+=Close[pos4];
//---- main calculation loop
while(pos1>=0)
{
sum1+=Close[pos1];
ExtMapBuffer1[pos1]=sum1/MA_Period1;
sum1-=Close[pos1+MA_Period1-1];
pos1--;
}
while(pos2>=0)
{
sum2+=Close[pos2];
ExtMapBuffer2[pos2]=sum2/MA_Period2;
sum2-=Close[pos2+MA_Period2-1];
pos2--;
}
while(pos3>=0)
{
sum3+=Close[pos3];
ExtMapBuffer3[pos3]=sum3/MA_Period3;
sum3-=Close[pos3+MA_Period3-1];
pos3--;
}
while(pos4>=0)
{
sum4+=Close[pos4];
ExtMapBuffer4[pos4]=sum4/MA_Period4;
sum4-=Close[pos4+MA_Period4-1];
pos4--;
}
//---- zero initial bars
if(ExtCountedBars1<1)
for(i=1;i<MA_Period1;i++) ExtMapBuffer1[Bars-i]=0;
if(ExtCountedBars2<1)
for(a=1;a<MA_Period2;a++) ExtMapBuffer2[Bars-a]=0;
if(ExtCountedBars3<1)
for(s=1;s<MA_Period3;s++) ExtMapBuffer3[Bars-s]=0;
if(ExtCountedBars4<1)
for(d=1;d<MA_Period4;d++) ExtMapBuffer4[Bars-d]=0;
}
//+------------------------------------------------------------------+
//| Exponential Moving Average |
//+------------------------------------------------------------------+
void ema()
{
double pr1=2.0/(MA_Period1+1);
double pr2=2.0/(MA_Period2+1);
double pr3=2.0/(MA_Period3+1);
double pr4=2.0/(MA_Period4+1);
int pos1=Bars-2;
int pos2=Bars-2;
int pos3=Bars-2;
int pos4=Bars-2;
if(ExtCountedBars1>2) pos1=Bars-ExtCountedBars1-1;
if(ExtCountedBars2>2) pos2=Bars-ExtCountedBars2-1;
if(ExtCountedBars3>2) pos3=Bars-ExtCountedBars3-1;
if(ExtCountedBars4>2) pos4=Bars-ExtCountedBars4-1;
//---- main calculation loop
while(pos1>=0&&pos2>=0&&pos3>=0&&pos4>=0)
{
if(pos1==Bars-2) ExtMapBuffer1[pos1+1]=Close[pos1+1];
ExtMapBuffer1[pos1]=Close[pos1]*pr1+ExtMapBuffer1[pos1+1]*(1-pr1);
pos1--;
if(pos2==Bars-2) ExtMapBuffer2[pos2+1]=Close[pos2+1];
ExtMapBuffer2[pos2]=Close[pos2]*pr2+ExtMapBuffer2[pos2+1]*(1-pr2);
pos2--;
if(pos3==Bars-2) ExtMapBuffer3[pos3+1]=Close[pos3+1];
ExtMapBuffer3[pos3]=Close[pos3]*pr3+ExtMapBuffer3[pos3+1]*(1-pr3);
pos3--;
if(pos4==Bars-2) ExtMapBuffer4[pos4+1]=Close[pos4+1];
ExtMapBuffer4[pos4]=Close[pos4]*pr4+ExtMapBuffer4[pos4+1]*(1-pr4);
pos4--;
}
}
//+------------------------------------------------------------------+
//| Smoothed Moving Average |
//+------------------------------------------------------------------+
void smma()
{
double sum1=0;
double sum2=0;
double sum3=0;
double sum4=0;
int i,k,pos1=Bars-ExtCountedBars1+1;
int q,w,pos2=Bars-ExtCountedBars2+1;
int a,s,pos3=Bars-ExtCountedBars3+1;
int z,x,pos4=Bars-ExtCountedBars4+1;
//---- main calculation loop
pos1=Bars-MA_Period1;
pos2=Bars-MA_Period2;
pos3=Bars-MA_Period3;
pos4=Bars-MA_Period4;
if(pos1>Bars-ExtCountedBars1) pos1=Bars-ExtCountedBars1;
while(pos1>=0)
{
if(pos1==Bars-MA_Period1)
{
//---- initial accumulation
for(i=0,k=pos1;i<MA_Period1;i++,k++)
{
sum1+=Close[k];
//---- zero initial bars
ExtMapBuffer1[k]=0;
}
}
else sum1=ExtMapBuffer1[pos1+1]*(MA_Period1-1)+Close[pos1];
ExtMapBuffer1[pos1]=sum1/MA_Period1;
pos1--;
}
if(pos2>Bars-ExtCountedBars2) pos2=Bars-ExtCountedBars2;
while(pos2>=0)
{
if(pos2==Bars-MA_Period2)
{
//---- initial accumulation
for(q=0,w=pos2;q<MA_Period2;q++,w++)
{
sum2+=Close[w];
//---- zero initial bars
ExtMapBuffer2[w]=0;
}
}
else sum2=ExtMapBuffer2[pos2+1]*(MA_Period2-1)+Close[pos2];
ExtMapBuffer2[pos2]=sum2/MA_Period2;
pos2--;
}
if(pos3>Bars-ExtCountedBars3) pos3=Bars-ExtCountedBars3;
while(pos3>=0)
{
if(pos3==Bars-MA_Period3)
{
//---- initial accumulation
for(a=0,s=pos3;a<MA_Period3;a++,s++)
{
sum3+=Close
;
//---- zero initial bars
ExtMapBuffer3=0;
}
}
else sum3=ExtMapBuffer3[pos3+1]*(MA_Period3-1)+Close[pos3];
ExtMapBuffer3[pos3]=sum3/MA_Period3;
pos3--;
}
if(pos4>Bars-ExtCountedBars4) pos4=Bars-ExtCountedBars4;
while(pos4>=0)
{
if(pos4==Bars-MA_Period4)
{
//---- initial accumulation
for(z=0,x=pos4;z<MA_Period4;z++,x++)
{
sum4+=Close
//---- zero initial bars
ExtMapBuffer4
}
}
else sum4=ExtMapBuffer4[pos4+1]*(MA_Period4-1)+Close[pos4];
ExtMapBuffer4[pos4]=sum4/MA_Period4;
pos4--;
}
}
//+------------------------------------------------------------------+
//| Linear Weighted Moving Average |
//+------------------------------------------------------------------+
void lwma()
{
double sum1=0.0,lsum1=0.0;
double sum2=0.0,lsum2=0.0;
double sum3=0.0,lsum3=0.0;
double sum4=0.0,lsum4=0.0;
double price1;
double price2;
double price3;
double price4;
int i,weight1=0,pos1=Bars-ExtCountedBars1-1;
int a,weight2=0,pos2=Bars-ExtCountedBars2-1;
int s,weight3=0,pos3=Bars-ExtCountedBars3-1;
int d,weight4=0,pos4=Bars-ExtCountedBars4-1;
//---- initial accumulation
if(pos1<MA_Period1) pos1=MA_Period1;
for(i=1;i<=MA_Period1;i++,pos1--)
{
price1=Close[pos1];
sum1+=price1*i;
lsum1+=price1;
weight1+=i;
}
if(pos2<MA_Period2) pos2=MA_Period2;
for(a=1;a<=MA_Period2;a++,pos2--)
{
price2=Close[pos2];
sum2+=price2*a;
lsum2+=price2;
weight2+=a;
}
if(pos3<MA_Period3) pos3=MA_Period3;
for(s=1;s<=MA_Period3;s++,pos3--)
{
price3=Close[pos3];
sum3+=price3*s;
lsum3+=price3;
weight3+=s;
}
if(pos4<MA_Period4) pos4=MA_Period4;
for(d=1;d<=MA_Period4;d++,pos4--)
{
price4=Close[pos4];
sum4+=price4*d;
lsum4+=price4;
weight4+=d;
}
//---- main calculation loop
pos1++;
i=pos1+MA_Period1;
while(pos1>=0)
{
ExtMapBuffer1[pos1]=sum1/weight1;
if(pos1==0) break;
pos1--;
i--;
price1=Close[pos1];
sum1=sum1-lsum1+price1*MA_Period1;
lsum1-=Close;
lsum1+=price1;
}
pos2++;
a=pos2+MA_Period2;
while(pos2>=0)
{
ExtMapBuffer2[pos2]=sum2/weight2;
if(pos2==0) break;
pos2--;
a--;
price2=Close[pos2];
sum2=sum2-lsum2+price2*MA_Period2;
lsum2-=Close[a];
lsum2+=price2;
}
pos3++;
s=pos3+MA_Period3;
while(pos3>=0)
{
ExtMapBuffer3[pos3]=sum3/weight3;
if(pos3==0) break;
pos3--;
s--;
price3=Close[pos3];
sum3=sum3-lsum3+price3*MA_Period3;
lsum3-=Close;
lsum3+=price3;
}
pos4++;
d=pos4+MA_Period4;
while(pos4>=0)
{
ExtMapBuffer4[pos4]=sum4/weight4;
if(pos4==0) break;
pos4--;
d--;
price4=Close[pos4];
sum4=sum4-lsum4+price4*MA_Period4;
lsum4-=Close[d];
lsum4+=price4;
}
//---- zero initial bars
if(ExtCountedBars1<1)
for(i=1;i<MA_Period1;i++) ExtMapBuffer1[Bars-i]=0;
if(ExtCountedBars2<1)
for(a=1;a<MA_Period2;a++) ExtMapBuffer2[Bars-a]=0;
if(ExtCountedBars3<1)
for(s=1;s<MA_Period3;s++) ExtMapBuffer3[Bars-s]=0;
if(ExtCountedBars4<1)
for(d=1;d<MA_Period4;d++) ExtMapBuffer4[Bars-d]=0;
}
//+------------------------------------------------------------------+