parabolic Sar in metastock Step by Step

#1
hello ! guys ! just likely to invite those guys interesting in metastock parabolic programing code & try to correct the codes belows :-

(p/s: the code seems correct but not working ... have a look ?)

{PARABOLIC SAR IN METASTOCK FORMULA LANGUAGE }
{Using an Acceleration Factor Step of 0.02 with a max of 0.20}

{POS=1 means Long Position; 0 means Short. }

{We start with Long}
POS:=If(Cum(1)=1,1,PREV);

{Use Long and Short Booleans to improve code readability }
Long:=If(Cum(1)=1, 1, PREV);
Short:=If(Cum(1)=1, 0, PREV);

{ Start with the maximum Acceleration Factor}
AF := If(Cum(1)=1, 0.20, PREV);

{Start with SAR equal to the Low}
MySAR:=If(Cum(1)=1, L, PREV);

{ Start with the Extreme Price equal to the High}
ExtPrc:=If(Cum(1)=1, H, PREV);

{Calculate the Acceleration Factor First}
{ If the position changes, reset AF to 0.02}
{ If the position does not change:}
{ - increment AF if long and higher highs}
{ - increment AF if short and lower lows }
{ - otherwise, AF does not change.}
AF:=If(POS<>Ref(POS,-1),0.02,
If((Long AND H>Ref(H,-1) ) OR (Short AND L<Ref(L,-1)),
If(PREV+0.02<0.20, PREV+0.02,0.20),{increment, but dont exceed max AF}PREV));

{If Long, the Extreme Price is the highest high }
{If Short, it is the lowest low. }
ExtPrc:= If(long AND H>PREV, H, If(short AND L<PREV, L, PREV));

{Calculate SAR from yesterdays values}
{ This had to be broken down into NewSAR }
{ to avoid MSFL binary overflow. }
NewSAR := (Ref(ExtPrc,-1) - Ref(MySAR,-1))*Ref(AF,-1) + Ref(MySAR,-1);
MySAR := If(POS <> Ref(POS,-1), Ref(ExtPrc,-1), NewSAR);

{Now we have a new SAR, do we need a new position?}
POS:= If(Long AND MySAR>L,0,{ Switch to Short}
If(Short AND MySAR<H,1,{ Switch to Long}
PREV));{ Otherwise don't change }

{ Change Short and Long as appropriate for new position. }
Short:=If(POS,0,1);
Long:=If(POS,1,0);
MySAR
 

rkkarnani

Well-Known Member
#2
hello ! guys ! just likely to invite those guys interesting in metastock parabolic programing code & try to correct the codes belows :-

(p/s: the code seems correct but not working ... have a look ?)

{PARABOLIC SAR IN METASTOCK FORMULA LANGUAGE }
{Using an Acceleration Factor Step of 0.02 with a max of 0.20}

{POS=1 means Long Position; 0 means Short. }

{We start with Long}
POS:=If(Cum(1)=1,1,PREV);

{Use Long and Short Booleans to improve code readability }
Long:=If(Cum(1)=1, 1, PREV);
Short:=If(Cum(1)=1, 0, PREV);

{ Start with the maximum Acceleration Factor}
AF := If(Cum(1)=1, 0.20, PREV);

{Start with SAR equal to the Low}
MySAR:=If(Cum(1)=1, L, PREV);

{ Start with the Extreme Price equal to the High}
ExtPrc:=If(Cum(1)=1, H, PREV);

{Calculate the Acceleration Factor First}
{ If the position changes, reset AF to 0.02}
{ If the position does not change:}
{ - increment AF if long and higher highs}
{ - increment AF if short and lower lows }
{ - otherwise, AF does not change.}
AF:=If(POS<>Ref(POS,-1),0.02,
If((Long AND H>Ref(H,-1) ) OR (Short AND L<Ref(L,-1)),
If(PREV+0.02<0.20, PREV+0.02,0.20),{increment, but dont exceed max AF}PREV));

{If Long, the Extreme Price is the highest high }
{If Short, it is the lowest low. }
ExtPrc:= If(long AND H>PREV, H, If(short AND L<PREV, L, PREV));

{Calculate SAR from yesterdays values}
{ This had to be broken down into NewSAR }
{ to avoid MSFL binary overflow. }
NewSAR := (Ref(ExtPrc,-1) - Ref(MySAR,-1))*Ref(AF,-1) + Ref(MySAR,-1);
MySAR := If(POS <> Ref(POS,-1), Ref(ExtPrc,-1), NewSAR);

{Now we have a new SAR, do we need a new position?}
POS:= If(Long AND MySAR>L,0,{ Switch to Short}
If(Short AND MySAR<H,1,{ Switch to Long}
PREV));{ Otherwise don't change }

{ Change Short and Long as appropriate for new position. }
Short:=If(POS,0,1);
Long:=If(POS,1,0);
MySAR
Too complicated it seems!! :)
BTW what do you wish to achieve from this?
 

Similar threads