[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [Metastockusers] Better coding in MetaStock



PureBytes Links

Trading Reference Links

Title: Message
very professional. thanks jose..
-----Original Message-----
From: Metastockusers@xxxxxxxxxxxxxxx [mailto:Metastockusers@xxxxxxxxxxxxxxx] On Behalf Of Jose Silva
Sent: Saturday, April 16, 2005 11:03 AM
To: Metastockusers@xxxxxxxxxxxxxxx
Subject: [Metastockusers] Better coding in MetaStock



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 the formula, assign these values to
parameters so they may be easily changed as required;

3) Always use meaningful names for variables.  The 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 code that ignores the above four points:

========================
3% envelope 14 MA Expert
========================
---8<--------------
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<--------------

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 this code;

2) No parameters assigned to global variables - it would take some
effort to change the 16 values, and there is always the risk of
introducing errors;

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 code below plots the same signals, and:

1) Avoids PREV functions;

2) Assigns global variables to variables at the easy-to-get head of
the formula;

3) Uses names for variables that reflect their intended use;

4 {Comments}, {Comments}, {Comments}.

===========================
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=1

---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(binary<1,2);
long

---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 - first signal }
short:=binary<1 AND Alert(binary>0,2);
short

---8<----------

Life is so much easier with clean, annotated code... ;)


jose '-)
http://www.metastocktools.com







Yahoo! Groups Links