Well I’ve been playing with the Multicharts system for a while now and I thought I would share the details of what I’ve learned so far.
This document has been broken into key areas defined by the *** area ***.
Multicharts crew, feel free to jump in and correct anything I post here.
To start with, for a nice tidy layout, I prefer the TAB size to be set to three (3) characters. This value can be located in the PowerLanguage editor menu Tools\Editor Options\Code\Tab Size.
Part 1 – Script Layouts
It’s always a good idea to layout your code in a logical order. The areas I use are Description, Initialization code for each loop, Entry Code, Exit Code, Plotting from within Signals and Exporting of information. Mind you, depending on what the script is, it may only have two areas, a Description and a Plot.
*** Description ***
This starts with the description at the beginning. This area helps to remember why you wrote the script in the first place and if you are like me, have multiple copies on various PC’s, having a version number also proves very useful.
Example
Code: Select all
{
Type - Indicator
Name - NWT SPI Day Session
Desc - Plot a ribbon show Green for the Day Session and Red for the Night Session
Just to enable quicker back viewing of the data
Written by Neil Wrightson
Version Date Reason
1 17/12/2011 Start
}
Most scripts require some kind of inputs. Ie the period of a moving average. Of course, the period of the moving average could be hard coded but by placing it as an input, it can now be fed into the Optimizer for further testing.
Sample Input structure –
Code: Select all
Inputs : High_MA_Len(10),Low_MA_Len(8),MedLen(8),PointSize(0.0001), StopLossSize(5), BreakEvenSize(7);
//Nicer Input structure with meaningful comments
Inputs :
High_MA_Len(10), Low_MA_Len(8), // Moving average lengths
MedLen(8), // Median Look back Length
PointSize(0.0001); // Actual Point size of the instrument
As an example –
Code: Select all
// This is my Entry code for an Entry based on a moving average cross.
Inputs : High_MA_Len(10), Low_MA_Len(8), // Moving average lengths
// Calc Entry code
Some code here
Some code here
Some code here
// This is my Exit code area
Inputs : // Exit Inputs
StopLossSize(5), // Maximum number of pips to risk on this trade
BreakEvenSize(7);// Adjust to a break even after this many pips
Some code here
Some code here
Some code here
Variables – There are some predefined variables (ie value1, value2 etc) already in the scripting language but when I see these being used in code, I have trouble trying to remember what their purpose is. So I find it better to create my own variables with meaningful names. As with the above inputs, the variables should also be declared in the area that they are to be used.
Code: Select all
Inputs :
High_MA_Len(10), Low_MA_Len(8), // Moving average lengths
Variables :
HighMA(0), // Variable usage comment here
LowMA(0); // Variable usage comment here
// Calc Entry code
HighMA = average(close, High_MA_Len);
*** Formating ***
As I mentioned at the beginning, I like my Tab/Indenting to be set to three (3) characters, some people like 4 or more but it is the act of indenting that makes the code much easier to read and debug.
Code: Select all
//To use an example from the help file
//In the below, it is not very easy to read.
If UpTrend Then Begin
Buy Next Bar Market;
End
Else Begin
SellShort Next Bar Market;
End;
//Because it is a very simple example, it could have also been written as
If UpTrend Then Buy Next Bar Market
Else SellShort Next Bar Market;
// or even
If UpTrend Then Buy Next Bar Market Else SellShort Next Bar Market;
//In the below, it is very easy to see the possible results from the IF statement.
// This is my preferred method of formatting my code.
If UpTrend Then
Begin
Buy Next Bar Market;
Initial_Stop = Low[1];
End
Else
Begin
SellShort Next Bar Market;
Initial_Stop = High[1];
End;
// Much easier to see, read and add additional lines of code.
So, if people are interested, I will continue to make additional parts to "MC Programing - What I’ve learned"
Let me know?
Neil.