Rank: Advanced Member
Groups: Registered, Registered Users Joined: 1/19/2005(UTC) Posts: 1,065 Location: Koh Pha-Ngan, Earth
Was thanked: 2 time(s) in 2 post(s)
|
Here are some poor MetaStock coding practices worth avoiding:
1) MetaStock's PREV function slows down processing dramatically - its use should be avoided wherever possible. Placing unnecessary PREV functions in a formula generally reflects poor programming skills;
2) Rather than create a zillion formulae with different hard-coded parameters scattered inside a formula, assign these values to variables or user-inputs (in an easy-to-find code location) so that they may be easily changed as required;
3) Always use meaningful names for variables. Your code may need to be followed and understood by others, or worse still, Alzheimer's may just be around the corner;
4) Use {comments} wherever possible, for the same reasons as #3.
Here is a recent example of MS code that ignores the above four points:
[code:1:ee4a5480b7] ======================== 3% envelope 14 MA Expert ========================
---8<--------------
Highlights tab:
Long
A:=C>1.03*Mov(C,14,S); B:=C<.97*Mov(C,14,S); D:=If(A,1,If(B,0,PREV)); D=1
Short
A:=C>1.03*Mov(C,14,S); B:=C<.97*Mov(C,14,S); D:=If(A,1,If(B,0,PREV)); D=0
Symbol tab:
Long
A:=C>1.03*Mov(C,14,S); B:=C<.97*Mov(C,14,S); D:=If(A,1,If(B,0,PREV)); D=1 AND Ref(D,-1)=0
Short
A:=C>1.03*Mov(C,14,S); B:=C<.97*Mov(C,14,S); D:=If(A,1,If(B,0,PREV)); D=0 AND Ref(D,-1)=1 ---8<-------------- [/code:1:ee4a5480b7]
The code above is an example of poor coding in MetaStock, because:
1) Four (unnecessary) PREV function slows down the plotting of the Expert signals, and would dramatically slow down an exploration based on the same code;
2) There are no parameters assigned to global variables - it would take some effort to change all 16 values, and there is always the risk of introducing errors whilst doing so;
3) No meaningful names assigned to variables. Try following the code of some of the longer formulae, and the problem becomes apparent;
4) No {comments} used. The author may not need them, but others could do with some.
The MS code below plots the same signals, and:
1) Avoids PREV functions;
2) Assigns values to global variables at the easy-to-get head of the formula;
3) Uses names for variables that reflect their intended use;
4) {Comments}, {Comments}, {Comments}.
[code] =========================== 14 SMA / 3% envelope Expert ===========================
Highlights tab:
Long ---8<----------
{ Variables } SMAperiods:=14; EnvPercent:=3;
{ Envelope signals } Env:=EnvPercent/100; upper:=C>(1+Env)*Mov(C,SMAperiods,S); lower:=C<(1-Env)*Mov(C,SMAperiods,S);
{ Trade binary flag } binary:=ValueWhen(1,upper-lower<>0,upper-lower);
{ Long trade flag } binary>0
---8<----------
Short ---8<----------
{ Variables } SMAperiods:=14; EnvPercent:=3;
{ Envelope signals } Env:=EnvPercent/100; upper:=C>(1+Env)*Mov(C,SMAperiods,S); lower:=C<(1-Env)*Mov(C,SMAperiods,S);
{ Trade binary flag } binary:=ValueWhen(1,upper-lower<>0,upper-lower);
{ Short trade flag } binary<0
---8<----------
Symbols tab:
Long ---8<----------
{ Variables } SMAperiods:=14; EnvPercent:=3;
{ Envelope signals } Env:=EnvPercent/100; upper:=C>(1+Env)*Mov(C,SMAperiods,S); lower:=C<(1-Env)*Mov(C,SMAperiods,S);
{ Trade binary flag } binary:=ValueWhen(1,upper-lower<>0,upper-lower);
{ Long trade - first signal } long:=binary=1 AND Alert(bi
|