Beginner's Guide to MultiCharts Programming: Difference between revisions
(Created page with "This article highlights some PowerLanguage formatting tips, as discussed in a forum topic <ref>[http://www.multicharts.com/discussion/viewtopic.php?f=1&t=9792 MC Programing - ...") |
No edit summary |
||
Line 17: | Line 17: | ||
1 17/12/2011 Start | 1 17/12/2011 Start | ||
} | } | ||
</syntaxhighlight> | |||
=== Script Inputs === | |||
By including comments in the section of the script where the inputs and variables are defined the comprehension of the variables will be greater. | |||
An example of poor formatting would be the following. | |||
<syntaxhighlight> | |||
Inputs : High_MA_Len(10),Low_MA_Len(8),MedLen(8),PointSize(0.0001), StopLossSize(5), BreakEvenSize(7); | |||
</syntaxhighlight> | |||
A better formatting, both cleaner and easier to understand, would be: | |||
<syntaxhighlight> | |||
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 | |||
</syntaxhighlight> | |||
==== Other Input formatting suggestions ==== | |||
* Start[[IntraBarPersist]] variables with a captial letter (e.g. ''MyDefinedVariable'') and start regular variables with a normal letter (e.g. ''myDefinedVariable''). This helps to keep track of which variables are regular and which ones are IntraBarPersist. | |||
== If else statements == | |||
When writing [[Comparisons_and_Loops|if else statements]] the use of '''tabs''' are highly recommend. | |||
Some examples of poor formatting would be: | |||
<syntaxhighlight> | |||
If UpTrend Then Begin | |||
Buy Next Bar Market; | |||
End | |||
Else Begin | |||
SellShort Next Bar Market; | |||
End; | |||
</syntaxhighlight> | |||
<syntaxhighlight> | |||
If UpTrend Then Buy Next Bar Market | |||
Else SellShort Next Bar Market; | |||
</syntaxhighlight> | |||
<syntaxhighlight> | |||
If UpTrend Then Buy Next Bar Market Else SellShort Next Bar Market; | |||
</syntaxhighlight> | |||
Better formatting would be: | |||
<syntaxhighlight> | |||
if (UpTrend = True) then begin | |||
Buy Next Bar Market; | |||
Initial_Stop = Low[1]; | |||
else begin | |||
SellShort Next Bar Market; | |||
Initial_Stop = High[1]; | |||
end; | |||
</syntaxhighlight> | |||
=== Closing of 'begin'... 'end' statements === | |||
A little tip for the closing of ''begin'' and ''end'' statements would be to include '//: ' after the end statement followed by a description of the if statement. This makes it relatively easy in debugging to see what the code block the 'end;' statement closes. | |||
For example: | |||
<syntaxhighlight> | |||
if (MarketPosition(0) = 0) then begin | |||
if (CurrentContracts < posSize) then begin | |||
if (enterLong = True) then | |||
Buy ("EL Limit") posSize contracts next bar at ELPrice limit; | |||
if (enterShort = True) then | |||
SellShort ("ES Limit") posSize contracts next bar at ESPrice limit; | |||
end; //: CurrentContracts < PosSize | |||
end; //: No MarketPosition Intrabar | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== References == | == References == | ||
<references /> | <references /> |
Revision as of 09:50, 27 January 2012
This article highlights some PowerLanguage formatting tips, as discussed in a forum topic <ref>MC Programing - What I’ve learned</ref>.
Layout
Some tips regarding the layout of a coding script.
Description
It's considered good programming practice to include a description, changelog and dates into your script. This helps with keeping track of changes or reverting any made changes. An example of a good description is the following.
{
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
Version Date Reason
1 17/12/2011 Start
}
Script Inputs
By including comments in the section of the script where the inputs and variables are defined the comprehension of the variables will be greater.
An example of poor formatting would be the following.
Inputs : High_MA_Len(10),Low_MA_Len(8),MedLen(8),PointSize(0.0001), StopLossSize(5), BreakEvenSize(7);
A better formatting, both cleaner and easier to understand, would be:
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
Other Input formatting suggestions
- StartIntraBarPersist variables with a captial letter (e.g. MyDefinedVariable) and start regular variables with a normal letter (e.g. myDefinedVariable). This helps to keep track of which variables are regular and which ones are IntraBarPersist.
If else statements
When writing if else statements the use of tabs are highly recommend.
Some examples of poor formatting would be:
If UpTrend Then Begin
Buy Next Bar Market;
End
Else Begin
SellShort Next Bar Market;
End;
If UpTrend Then Buy Next Bar Market
Else SellShort Next Bar Market;
If UpTrend Then Buy Next Bar Market Else SellShort Next Bar Market;
Better formatting would be:
if (UpTrend = True) then begin
Buy Next Bar Market;
Initial_Stop = Low[1];
else begin
SellShort Next Bar Market;
Initial_Stop = High[1];
end;
Closing of 'begin'... 'end' statements
A little tip for the closing of begin and end statements would be to include '//: ' after the end statement followed by a description of the if statement. This makes it relatively easy in debugging to see what the code block the 'end;' statement closes.
For example:
if (MarketPosition(0) = 0) then begin
if (CurrentContracts < posSize) then begin
if (enterLong = True) then
Buy ("EL Limit") posSize contracts next bar at ELPrice limit;
if (enterShort = True) then
SellShort ("ES Limit") posSize contracts next bar at ESPrice limit;
end; //: CurrentContracts < PosSize
end; //: No MarketPosition Intrabar
References
<references />