code to do a range bar

Questions about MultiCharts and user contributed studies.
Posts: 22
Joined: 29 Jul 2010
Has thanked: 1 time
Been thanked: 4 times

code to do a range bar

Postby tradersheldon » 28 Feb 2011

i would like to program one system, that take the high and the low of the first 30 minutes of the sesion but really i do not how to do, somebody can help me to write the code, how is possible to take the high and the low of the first 30 minutes of the session please, to do asystem that buy when we break that level...


User avatar
Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 215 times

Re: code to do a range bar

Postby furytrader » 02 Mar 2011

Hi TraderSheldon,

It's actually not that hard to program. The first question I would have is: what bar interval are you using? For example, are you using 1 minute bar charts? 5 minutes? This will be important, because it will affect how many bars back you look during the first 30 minute period.

Let's imagine that you have the following conditions:

1. You're trading the e-Mini S&P 500, which opens at 8:30 am Chicago Time and closes at 15:15 pm Chicago Time. This means that the first 30 minutes ends at 9:00 am.

2. You're using five minute bar charts;

You would write something like this:

Code: Select all

Vars: BuyLevel(0), SellLevel(0);

If Time = 900 Then Begin
   BuyLevel = Highest(High,6);
   SellLevel = Lowest(Low,6);

If Time >= 900 And Time < 1515 Then Begin
    Buy Next Bar At BuyLevel STOP;
    SellShort Next Bar At SellLevel STOP;


A few things to note:

1) The reason we use "6" in the Highest(high) and Lowest(Low) code is because 30 minutes / 5 minutes per bar = 6. If you were using a 2 minute bar chart, you would use "15" in place of "6" in the code above, since 30 / 2 = 15;

2) The reason we don't want the system to continue to enter stops at 1515 (3:15 pm) is that orders placed at that time could result in trades being taken either in the after-hours or on the first bar of the next day (depending on whether you're showing day session only). This code assumes you only want to take breakout trades during the day session;

3) The "SetExitOnClose" command gets you out of any open trades at the end of the day.

Hope this helps!

Return to “MultiCharts”