Beginner's Guide to MultiCharts Programming: Difference between revisions
From MultiCharts
Beginner's Guide to MultiCharts Programming (view source)
Revision as of 16:07, 27 April 2017
, 27 April 2017no edit summary
(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 - ...") |
Abeloglazova (talk | contribs) No edit summary |
||
(4 intermediate revisions by one other user not shown) | |||
Line 3: | Line 3: | ||
== Layout == | == Layout == | ||
Some tips regarding the layout of a coding script. | 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 === | === Description === | ||
Line 17: | Line 25: | ||
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 [[:Category:Comparisons and Loops|If Else statements]] the use of '''tabs''' is highly recommended. | |||
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 /> |