Converting this Smoothing MA MT4 into AFL - Help Needed

#1
Please can anybody help converting this smoothing MA in MQ4 to AFL? Screen shot attached. Thank you.

Here is the code
============


// List of Prices:
// Price = 0 - Close
// Price = 1 - Open
// Price = 2 - High
// Price = 3 - Low
// Price = 4 - Median Price = (High+Low)/2
// Price = 5 - Typical Price = (High+Low+Close)/3
// Price = 6 - Weighted Close = (High+Low+Close*2)/4
// Price = 7 - Heiken Ashi Close
// Price = 8 - Heiken Ashi Open
// Price = 9 - Heiken Ashi High
// Price =10 - Heiken Ashi Low


#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Silver
#property indicator_width1 2
#property indicator_color2 DeepSkyBlue
#property indicator_width2 2
#property indicator_color3 Tomato
#property indicator_width3 2


//----
extern int TimeFrame = 0;
extern int Price = 0;
extern int MA_Period = 14;
extern int MA_Shift = 0;
extern int MA_Method = 0;
extern int Color_Mode = 0;
extern int Sound_Mode = 0; //0-off,1-on(works only with Color_Mode=1)
extern int Sound_Shift = 0; //0-open bar(multiple),1-closed bar(once)
extern string Buy_Sound = "alert.wav";
extern string Sell_Sound = "alert2.wav";

extern string PriceMode = "";
extern string _0 = "Close";
extern string _1 = "Open";
extern string _2 = "High";
extern string _3 = "Low";
extern string _4 = "Median";
extern string _5 = "Typical";
extern string _6 = "Weighted Close";
extern string _7 = "Heiken Ashi Close";
extern string _8 = "Heiken Ashi Open";
extern string _9 = "Heiken Ashi High";
extern string _10 = "Heiken Ashi Low";

double MA[];
double Up[];
double Dn[];
double aPrice[];
//----
double tmp[][2];
double haClose[2], haOpen[2], haHigh[2], haLow[2];
int draw_begin, arraysize;
string IndicatorName, TF, short_name;
int sUp = 0, sDn =0;
datetime prevtime, prevhatime;

int init()
{
//----
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

if(TimeFrame == 0 || TimeFrame < Period()) TimeFrame = Period();

//----
IndicatorBuffers(4);

SetIndexBuffer(0, MA); SetIndexStyle(0,DRAW_LINE); SetIndexShift(0,MA_Shift*TimeFrame/Period());
SetIndexBuffer(1, Up); SetIndexStyle(1,DRAW_LINE); SetIndexShift(1,MA_Shift*TimeFrame/Period());
SetIndexBuffer(2, Dn); SetIndexStyle(2,DRAW_LINE); SetIndexShift(2,MA_Shift*TimeFrame/Period());
SetIndexBuffer(3,aPrice);

draw_begin=2*MathCeil(0.5*(MA_Period+1))*TimeFrame/Period();
SetIndexDrawBegin(0,draw_begin);
SetIndexDrawBegin(1,draw_begin);
SetIndexDrawBegin(2,draw_begin);



short_name="SmoothMA(";
arraysize = 5;

switch(TimeFrame)
{
case 1 : TF = "M1" ; break;
case 5 : TF = "M5" ; break;
case 15 : TF = "M15"; break;
case 30 : TF = "M30"; break;
case 60 : TF = "H1" ; break;
case 240 : TF = "H4" ; break;
case 1440 : TF = "D1" ; break;
case 10080 : TF = "W1" ; break;
case 43200 : TF = "MN1"; break;
default : TF = "Current";
}

IndicatorName = WindowExpertName();

IndicatorShortName(short_name+MA_Period+")"+" "+TF);

SetIndexLabel(1,short_name+MA_Period+")"+" "+TF+" UpTrend");
SetIndexLabel(2,short_name+MA_Period+")"+" "+TF+" DnTrend");
SetIndexLabel(0,short_name+MA_Period+")"+" "+TF);

//----
ArrayResize(tmp,arraysize);

return(0);
}

int start()
{
int limit, y, i, shift, cnt_bars = IndicatorCounted();

if(cnt_bars > 0) limit = Bars - cnt_bars - 1;
if(cnt_bars < 0) return(0);
if(cnt_bars < 1)
{
limit = Bars - 1;

for(i=Bars-1;i>0;i--)
{
MA = EMPTY_VALUE;
Up = EMPTY_VALUE;
Dn = EMPTY_VALUE;
}
}


//----
if(TimeFrame != Period())
{
limit = MathMax(limit,TimeFrame/Period()+1);

for(shift = 0;shift < limit;shift++)
{
y = iBarShift(NULL,TimeFrame,Time[shift]);

MA[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Pe riod,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Soun d_Shift,Buy_Sound,Sell_Sound,0,y);

if(Color_Mode > 0)
{
Up[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Pe riod,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Soun d_Shift,Buy_Sound,Sell_Sound,1,y);
Dn[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Pe riod,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Soun d_Shift,Buy_Sound,Sell_Sound,2,y);
}
}

return(0);
}
else
{
for(shift=limit;shift>=0;shift--)
{
if(arraysize > 1 && prevtime != Time[shift])
{
for(i=0;i<arraysize;i++) tmp[1] = tmp[0];
prevtime = Time[shift];
}


if(Price <= 6) aPrice[shift] = iMA(NULL,0,1,0,0,Price,shift);
else
if(Price > 6 && Price <= 10) aPrice[shift] = HeikenAshi(Price-7,shift);

MA[shift] = SmoothMA(0,aPrice[shift],MA_Period,1,shift);


if(Color_Mode == 1)
{
Up[shift] = EMPTY_VALUE;
Dn[shift] = EMPTY_VALUE;

if(MA[shift] > MA[shift+1]) Up[shift] = MA[shift];
else
if(MA[shift] < MA[shift+1]) Dn[shift] = MA[shift];


if(Sound_Mode == 1 && shift == 0)
{
if(((Sound_Shift > 0 && sUp == 0) || Sound_Shift == 0) && MA[shift+Sound_Shift] > MA[shift+1+Sound_Shift] && MA[shift+1+Sound_Shift] <= MA[shift+2+Sound_Shift])
{
if(Sound_Shift > 0) {sUp = 1; sDn = 0;}
PlaySound(Buy_Sound);
}
else
if(((Sound_Shift > 0 && sDn == 0) || Sound_Shift == 0) && MA[shift+Sound_Shift] < MA[shift+1+Sound_Shift] && MA[shift+1+Sound_Shift] >= MA[shift+2+Sound_Shift])
{
if(Sound_Shift > 0) {sUp = 0; sDn = 1;}
PlaySound(Sell_Sound);
}
}
}
}
}

//----
return(0);
}

double SmoothMA(int num,double price,int per,double pow,int bar)
{
double beta = 0.45*(per-1)/(0.45*(per-1)+2);
double alpha = MathPow(beta,pow);
if(bar == Bars - 2) {tmp[num+4][0] = price; tmp[num+0][0] = price; tmp[num+2][0] = price;}
else
if(bar < Bars - 2)
{
tmp[num+0][0] = (1-alpha)*price + alpha*tmp[num+0][1];
tmp[num+1][0] = (price - tmp[num+0][0])*(1-beta) + beta*tmp[num+1][1];
tmp[num+2][0] = tmp[num+0][0] + tmp[num+1][0];
tmp[num+3][0] = (tmp[num+2][0] - tmp[num+4][1])*MathPow((1-alpha),2) + MathPow(alpha,2)*tmp[num+3][1];
tmp[num+4][0] = tmp[num+4][1] + tmp[num+3][0];
}
return(tmp[num+4][0]);
}


// HeikenAshi Price: 7 - Close,8 - Open,9 - High,10 - Low
double HeikenAshi(int price,int bar)
{
if(prevhatime != Time[bar])
{
haClose[1] = haClose[0];
haOpen [1] = haOpen [0];
haHigh [1] = haHigh [0];
haLow [1] = haLow [0];
prevhatime = Time[bar];
}

if(bar == Bars - 1)
{
haClose[0] = Close[bar];
haOpen [0] = Open [bar];
haHigh [0] = High [bar];
haLow [0] = Low [bar];
}
else
{
haClose[0] = (Open[bar] + High[bar] + Low[bar] + Close[bar])/4;
haOpen [0] = (haOpen[1] + haClose[1])/2;
haHigh [0] = MathMax(High[bar],MathMax(haOpen[0],haClose[0]));
haLow [0] = MathMin(Low [bar],MathMin(haOpen[0],haClose[0]));
}

switch(price)
{
case 0: return(haClose[0]); break;
case 1: return(haOpen [0]); break;
case 2: return(haHigh [0]); break;
case 3: return(haLow [0]); break;
}
}
 

Attachments

Similar threads