Thanks, i have made a few changes and the code seems to work but the results are terrible and much worse than expected. I am not sure if something in my code is wrong ( i think it must be) and is causing such terrible result. i can see many more trades being entered than what i thought. It averages 32 per day when it should be only 1-2 and sometimes 3 per day..

This is my junior coding attempt, i am still very much learning, so apologise if its rubbish, if there is a simpler way or coding the below, or some more mistakes please feel free to point them out.

The system is:

Find the range of the first hours trading. (8am till 9am)
Go long or short after 9am when the price has broken out of the high or low or the first hours trading.
Stop and Profit targets are based on the "range" either way.
Stop trading at close (4.30pm) if stop or profit target have not been hit
Do not trade overnight.
If there is a large range > 40 in the first hour its best Not to open a trade for the day and come back the following day.

[img=http://s24.postimg.org/hbllwch1t/1st_hour_range_trading_system.jpg]

Quote:
function run()
{

BarPeriod = 5;
LookBack = 200;
set(TICKS);

StartDate = 2013;

vars Price = series(price());

StartMarket = 800; // Daily market opening time (used for calcuating first hours trading range)
EndMarket = 900; // Daily market closing time.

var DH = 0, DL = 0;

if(lhour(UTC) > EndMarket/100) {
DH = dayHigh (UTC,0);
DL = dayLow (UTC,0);
}

var Range = DH-DL;

var Close = timeOffset(UTC,0,16,30); // 4.30pm Close UTC
var Open = timeOffset(UTC,0,8,00); // 8.00am Open UTC
var StartTrade = timeOffset(UTC,0,9,00); // 9am UTC
var Now = timeOffset(UTC,0,0,00); // Current Time UTC?

if (Range > 40*PIP) // If the first hours trading range is higher than 40 pips do not enter trade.
Margin = 0;


if (lhour(UTC) < StartTrade) // If less than 9am, do not enter trade.
Margin = 0;
if (lhour(UTC) > Close) // if more than 4.30pm do not enter trade.
Margin = 0;
if (lhour(UTC) > StartTrade) // if more than 9am enter trade.
Margin = 50;
if (lhour(UTC) < Close) // if less than 4.30pm enter trade.
Margin = 50;

if (lhour(UTC) > Close) // Exit Trade at Close (4.30pm)
exitLong();
if (lhour(UTC) > Close) // Exit Trade at Close (4.30pm)
exitShort();

asset("UK100");

Stop = Range; // Stop Loss based on the first hours trading range.

TakeProfit = Range; // TakeProfit on the first hours trading range.


if (*Price > DH && NumOpenLong <1) // If current price is more than the first hours trading high enter Long
enterLong(1);

else if (*Price < DL && NumOpenShort <1) // if current price is less than the first hors trading low enter Short.
enterShort(1);

}