Hello,
As an exercise in Basic Metastock Programming, is it possible to use
the MSFL to create the Parabolic SAR from scratch?
Yes, I do realize MSFL has the SAR() function, MS has the SAR indicator,
and there are probably many DLLs out there, and I have studied the MSFL functions in the User's Guide until I am cross-eyed.
However, I would like to play with variations on the basic SAR algorithm;
and, just for the nut of it, determine whether or not such an indicator can be
done solely with the MSFL.
If it is possible to program the SAR, perhaps someone in this forum
might use this as an example to demonstrate the power of the MSFL to us
less-experienced basic programmers? I
think my problem is understanding the initialization and propagation of
parameters with the MSFL, along with the roles of PREV and Ref()? If it
cannot be done, perhaps this would a good place to explain the restrictions?
If anyone is interested, here is the attempt with lots of commented documentation. I checked the syntax with the formula editor,
so it should be cut-and-paste ready. I shall be happy to post the final results if anyone can get me past the fundamental glitches.
Thanks!
=======================================================
{ 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 don’t 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 yesterday’s 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;
Joe