MC Programing - What I’ve learned – Part 1 (Script Layouts)

Questions about MultiCharts and user contributed studies.
NW27
Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 83 times

MC Programing - What I’ve learned – Part 1 (Script Layouts)

Postby NW27 » 08 Jan 2012

MC Programing - What I’ve learned – Part 1 (Script Layouts)
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
}


*** Script Inputs ***
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


It is a good idea to place the Input definitions directly above the code area they are to be used in.
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 ***
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);


A good reason for declaring inputs and variables in the same area of the script that they are being used, is that it makes it much easier to copy and paste this area of code into a new script. Ie copying and pasting the Exit area of the code.

*** 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.
These users thanked the author NW27 for the post (total 4):
bowlesj3SPJoshMSwap

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby bowlesj3 » 08 Jan 2012

It is always good to pass on any formatting ideas.

There are some formatting ideas in this thread.
viewtopic.php?f=16&t=7665

Useful for moving around if statements (use it a lot)
viewtopic.php?f=5&t=7142


Even with good methods (habits) being in a rush can cause lapses (it happens).

NW27
Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 83 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby NW27 » 08 Jan 2012

Hi bowlesj3,

Good information in those links, in particular the first link.

I think you will find that the EL is based on the Pascal language, which happens to be my native programming language. The syntax is 99% the same as Pascal, which uses the Begin / End statements in exactly the same way. The only difference appears to be the assignment operator, in Pascal it is a := b in EL it is just a=b.
The critical thing in regard to formating, is to make it easier on yourself. And if you share your code, on others.

However, it was my intention to go beyond formating and move into actual coding of a Signal/Strategy.

It takes time to put these informational things together. If no one shows interest in me continuing then I won't worry about it.

Neil.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby bowlesj3 » 08 Jan 2012

I think anyone brand new to programming would be interested. I do not know the balance here.

Spaceant
Posts: 252
Joined: 30 May 2009
Has thanked: 1 time
Been thanked: 3 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby Spaceant » 12 Jan 2012

Hi,

As John commented, there should be a lot of people who are interested in this topic. Me too!

Sa

User avatar
JoshM
Posts: 2089
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1485 times
Contact:

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby JoshM » 27 Jan 2012

Already some good tips for beginners in this thread, but to add a little tip: after each 'end;' statement I type '//: ' followed by a description of the if statement. For example:

Code: Select all

else 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


This makes it relatively easy in debugging to see what the code block the 'end;' statement closes.

Also, as the example above shows: don't be afraid to use white lines to make the code easier to read instead of making it one big block.

While it isn't needed in EasyLanguage to use parentheses ( & ) after an 'if' statement, for me it makes the code better readable, just as using the ' = True' in an if statement (better make it explicit - easier to debug).

User avatar
Stan Bokov
Posts: 963
Joined: 18 Dec 2009
Has thanked: 367 times
Been thanked: 300 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby Stan Bokov » 27 Jan 2012

Josh,

We should be opening up our Wiki within a week (filling it up with content now). This post would be a good start to an article, a "Starter's Guide to MC programming" for instance.

User avatar
JoshM
Posts: 2089
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1485 times
Contact:

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby JoshM » 27 Jan 2012

Stan Bokov wrote:We should be opening up our Wiki within a week (filling it up with content now). This post would be a good start to an article, a "Starter's Guide to MC programming" for instance.

Already registered Stan. ;) Funny that I was thinking about the same thing.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby bowlesj3 » 27 Jan 2012

It is too bad EL (or MC-EL) could not be adjusted to have a good
VB / VBA format

IF .... then
else
End If

alignment (unloading the begin/end combo) with a method of putting the cursor on the IF or the Else or the End If and having the tab key make it jump to the next corresponding part of the 3 of them.

Even if the format was not changed and the tab jump was put into the Editor that would be just an amazing help with those very large if statements especially when they are heavily indented. It would need to be such that if the cursor was not directly on the "IF then begin end else begin end" key words the special tab jumps did not take place.

I guess this idea should go in the bug tracker. I would assume this is not that easy to program (probably very challenging to program). It would probably match the difficulty level of some of the features of a really good debugger.

If this tab feature was done the programmer could use the proper indenting of the begin/end format statements of EL and not have to worry about the fact that the completion of the statement does not line up with the IF statement. In fact all sorts of misalignment styles that we see all the time would be easy to navigate and figure out after the fact.

User avatar
Stan Bokov
Posts: 963
Joined: 18 Dec 2009
Has thanked: 367 times
Been thanked: 300 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby Stan Bokov » 27 Jan 2012

John,

I think you would also make a very good Wiki contributor. Some of the posts you've done on this forum are already pretty much ready articles that can be posted in there. Good job!
These users thanked the author Stan Bokov for the post (total 3):
JoshMbowlesj3TJ

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby bowlesj3 » 28 Jan 2012

Thanks Stan,

If there is something I have written the MC staff like, feel free to move it into that medium. I have no idea what is good and not so good. Maybe a bit of editing would be in order since I am not an English instructor (nor a trained technical writer).

I am assuming there will be an announcement when the Wiki is available along with a few ways to link to it.

John

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Re: MC Programing - What I’ve learned – Part 1 (Script Layou

Postby bowlesj3 » 29 Jan 2012

So there it is, I put the feature mentioned above in this PM entry.
https://www.multicharts.com/pm/viewissue ... _no=MC-801


Return to “MultiCharts”