Beginner's Guide to MultiCharts Programming: Difference between revisions
(→Layout) |
Abeloglazova (talk | contribs) No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 48: | Line 48: | ||
== If else statements == | == If else statements == | ||
When writing [[ | When writing [[:Category:Comparisons and Loops|If Else statements]] the use of '''tabs''' is highly recommended. | ||
Some examples of poor formatting would be: | Some examples of poor formatting would be: |
Revision as of 16:07, 27 April 2017
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.
General layout tips
Some general layout tips are:
- Use tabs and empty lines to make the code better readable - don't forget: if you want to edit your script a half-year from now, you'll still need to be able to comprehend it.
- Make use of comments.
- While it isn't needed in EasyLanguage to use parentheses ( & ) after an 'if' statement, it makes the code better readable and prevents errors.
- Make your code explicit. For example, instead of writing if LastBarOnChart then write if (LastBarOnChart = True) then. While both statements work, the latter makes it easier to debug and makes the implicit assumptions you're making explicit.
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
- 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 If Else statements the use of tabs is highly recommended.
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 />