Code help: Range of 1st hours trading.

Posted By: Geek

Code help: Range of 1st hours trading. - 11/05/13 23:51

I'm trying to code a simple trading system:


Quote:
Find the high low range of first hours trading:

Buy on the breakout of the lowest low or the highest high of that first hour.

Profit target is the range between the high and low of first hour.

Stop set in opposite of trade direction equal to first hours trading range.

Exit at end of day, if no stops or limits get hit.

Don’t trade if range is higher than 40 points:


Quote:

function run()
{

BarPeriod = 5;

LookBack = 30;

vars Price = series(price());

if(hour(UTC) >= 16.30) // Exit trade at end of the day if stop or target have not been met.
exitLong();


Stop = 40*PIP; // - Change this to the amount of the range in first hours trading.

TakeProfit = 40*PIP: // - Change this to the amount of the range in first hours trading.


Dont trade if range is > 40 PIP; // Set dont trade if the rage in higher than 40*PIP;


if (*Price > HH(30) && NumOpenLong <1 && hour(UTC) >9 && hour(UTC) < 16.30) // Buy on breakout outside of the first hours range.
enterLong();

else if (*Price > LL(30) && NumOpenShort <1 && hour(UTC) > 9 && hour(UTC) < 16.30) // Sell on breakout outside of the first hours range.

enterShort();


}






But am finding it difficult to get right code for finding out the high low range of first hours trading as the system uses this for stop and profit targets and my coding skills are not that great.

So looking to find the range of the first hours trading of hour(UTC) 8 to 9..

How so you do this?

???

Profit target is set from the the range (PIPS between the high and low of first hour trading)

TakeProfit = ???


Stop to be set in opposite of trade direction equal to first hours trading range..

Stop = ???


Also don’t trade if first hour range is higher than 40 points:

???


Would appreciate any help on the above as i'm stuck.

Thanks.







Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/11/13 09:21

For the high-low range of the first hour of a trading day, you can use the dayHigh and dayLow functions. In that case, set the EndMarket variable to 1030 so that the "market day" ends at 10:30, one hour after its begin.

http://manual.zorro-trader.com/day.htm
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/12/13 22:14

Originally Posted By: jcl
For the high-low range of the first hour of a trading day, you can use the dayHigh and dayLow functions. In that case, set the EndMarket variable to 1030 so that the "market day" ends at 10:30, one hour after its begin.

http://manual.zorro-trader.com/day.htm


Thanks JCL.

I think the below code works for the Stop and TakeProfit, the only thing I am having trouble with is asking Zorro not to trade when the High and Low range exceeds 40*PIP; I'm getting an "illegal indirection" error?

Quote:


function run()
{

BarPeriod = 60;
LookBack = 100;
set(TICKS);
set(PEEK);

StartMarket = 800;
EndMarket = 900;

vars Price = series(price());

if(hour(UTC) >= 16.30)
exitLong();

if(hour(UTC) >= 16.30)
exitShort();

var DH = dayHigh (UTC,0);
var DL = dayLow (UTC,0);


asset("UK100");
Stop = DH-DL;
TakeProfit = DH-DL;

if (*DH-*DL > 40*PIP);
Lots = 0;


if (*Price > DH && NumOpenLong <1 && hour(UTC) > 9 && hour(UTC) < 16.30)
enterLong();

else if (*Price < DL && NumOpenShort <1 && hour(UTC) > 9 && hour(UTC) < 16.30)

enterShort();
Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/13/13 15:00

A little puzzle to the users - who can find all mistakes in this code? Solution tomorrow.
Posted By: DdlV

Re: Code help: Range of 1st hours trading. - 11/13/13 18:18

The illegal indirections are the *DH and *DL. What are you trying to accomplish?

After that, there's an extra ";" leading to no statement for the if.

Then, you haven't ended the function. laugh

HTH.
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/13/13 20:14

Originally Posted By: DdlV
The illegal indirections are the *DH and *DL. What are you trying to accomplish?


HTH.



Thanks,

I'm trying to accomplish - if the range is higher than 40 Pips then do not trade.


Updated code below which runs but no trades are entered?

Quote:

function run()
{

BarPeriod = 60;
LookBack = 100;
set(TICKS);
set(PEEK);

StartMarket = 800;
EndMarket = 900;

vars Price = series(price());

if(hour(UTC) >= 16.30)
exitLong();

if(hour(UTC) >= 16.30)
exitShort();

var DH = dayHigh (UTC,0);
var DL = dayLow (UTC,0);

var Range = DH-DL;

asset("UK100");

Stop = Range;

TakeProfit = Range;

if (Range > 40*PIP)
Lots = 0;


if (*Price > DH && NumOpenLong <1 && hour(UTC) > 9 && hour(UTC) < 16.30)
enterLong();

else if (*Price < DL && NumOpenShort <1 && hour(UTC) > 9 && hour(UTC) < 16.30)

enterShort();

}
Posted By: DdlV

Re: Code help: Range of 1st hours trading. - 11/14/13 11:59

Best next step, then, is to add printf's so you can see what various values are and what the code is consequently doing.

HTH.
Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/14/13 15:22

Some other problems that I noticed:

set(PEEK) - can render your results useless. NEVER set this flag unless you have a reason.

hour(UTC) - makes no sense, you probably mean lhour()?

16.30 - you have 60 minutes bars and hour() returns an integer anyway, so you can't compare with 16.30.

Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/14/13 17:24

Thanks guys, appreciate you pointing these things out!

When I do not set the PEEK flag, i get "error 045: Negative price offset at bar 1" ?

I know this is due to the following: It goes away when Peek is set.
Quote:

var DH = dayHigh (UTC,0);
var DL = dayLow (UTC,0);


I have set StartMarket and EndMarket - from 8am-9am.. As that's the first hours trading that this script is based on that the dayHigh and dayLow should work from. i do not want to set dayLow (UTC,1); for instance as this will based on yesterdays low..?

Anyway, I made some more changes to the code, i'd be interested to see how incorrect this is. I know it will be, but all this help me and any other learners to get to grips with the code.

Edit: i just noticed another error, I'm not sure what this error means exactly?

"Error in 'line 26:
Syntax error: Can't go to FALSEGOTO:DOUBLE::.
< if (Close) >

Thanks for your patience and for pointing out any inaccuracies.

Quote:

function run()
{

BarPeriod = 15;
LookBack = 1000;
set(TICKS);

StartDate = 2010;

vars Price = series(price());

StartMarket = 800;
EndMarket = 900;

var DH = dayHigh (UTC,0);
var DL = dayLow (UTC,0);

var Range = DH-DL;
var Close = timeOffset(UTC,0,16,30);
var Open = timeOffset(UTC,0,8,00);
var StartTrade = timeOffset(UTC,0,9,00);
var Now = timeOffset(UTC,0,0,00);

if (Close)
exitLong();

if (Close)
exitShort();

asset("UK100");

Stop = Range;

TakeProfit = Range;

if (Range > 40*PIP)
Lots = 0;

if (*Price > DH && NumOpenLong <1 && Now > StartTrade && Now < Close)
enterLong();

else if (*Price < DL && NumOpenShort <1 && Now > StartTrade && Now < Close)
enterShort();

}
Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/15/13 11:38

PEEK means that your system is peeking into the future. This in turn means that you will need a time machine for trading it. Therefore setting the PEEK flag is a bad idea if you don't have such a machine at hand.

Better find out the reason of the error 45, and fix it. In your case it's calling dayHigh/Low before the market day is over. You cannot know the high of a day already in the morning. Thus do not call those functions until the EndMarket hour is over.

The syntax error is a wrong condition in the if clause. "If" checks if something is true or false. A var has a floating point value and cannot be true or false. For doing something at a certain time, use the hour or lhour functions.
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/17/13 17:26

Originally Posted By: jcl

Better find out the reason of the error 45, and fix it. In your case it's calling dayHigh/Low before the market day is over. You cannot know the high of a day already in the morning. Thus do not call those functions until the EndMarket hour is over.


Thanks for the explanations.

I think i have sorted the other problems just still not understanding why the the dayHigh/dayLow is not working? I know you have explained it but my apologise, I am still unable to figure it out, although i know its probably something quite simple..

I thought that if i have set the start and end market for the dayhigh and daylow then when using (UTC,0) then this should work as it reverts to the first hour of trading 8 till 9 am and calculates the high/low of the day(first hour trading)?

Could you point out specifically in the code what is incorrect and where am i calling the functions before EndMarket = 900; is over? And possibly if you don't mind the solution this so i can finally "get it".

Many Thanks.

Quote:

function run()
{

BarPeriod = 15;
LookBack = 1000;
set(TICKS);

vars Price = series(price());
var DH = dayHigh (UTC,0);
var DL = dayLow (UTC,0);

StartMarket = 800;
EndMarket = 900;

var Range = DH-DL;
var Close = timeOffset(UTC,0,16,30);
var Open = timeOffset(UTC,0,8,00);
var StartTrade = timeOffset(UTC,0,9,00);
var Now = timeOffset(UTC,0,0,00);

if (Now > Close)
exitLong();

if (Now > Close)
exitShort();

asset("UK100");

Stop = Range;

TakeProfit = Range;

if (Range > 40*PIP)
Margin = 0;

if (*Price > DH && NumOpenLong <1 && Now > StartTrade && Now < Close)
enterLong();

else if (*Price < DL && NumOpenShort <1 && Now > StartTrade && Now < Close)
enterShort();

}
Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/18/13 09:46

This is what you probably wanted to program:

Code:
var DH = 0, DL = 0;
if(lhour(UTC) > EndMarket) {
  DH = dayHigh (UTC,0);
  DL = dayLow (UTC,0);
}


Do you understand why you need the if?
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/18/13 10:34

Thank you, appreciated.

Yes i understand this now,

The 0 = current?

The Start and End Market give me from 8 till 9am.

So, If the current hour is more than 9am the high and low from 8-9am will be Yes or No..

I noticed when the { } have been added it removes the negative price offset error.

I've run the script but no trades are entered, just wondering what could be causing this....I think it relates to these lines
Quote:
&& NumOpenLong <1 && Now > StartTrade && Now < Close)
..
Will try and sort this out now.

Quote:
function run()
{

BarPeriod = 15;
LookBack = 100;
set(TICKS);

vars Price = series(price());

StartMarket = 800;
EndMarket = 900;

var DH = 0, DL = 0;

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

var Range = DH-DL;
var Close = timeOffset(UTC,0,16,30);
var Open = timeOffset(UTC,0,8,00);
var StartTrade = timeOffset(UTC,0,9,00);
var Now = timeOffset(UTC,0,0,00);

if (Now > Close)
exitLong();

if (Now > Close)
exitShort();

asset("UK100");

Stop = Range;

TakeProfit = Range;

if (Range > 40*PIP)
Margin = 0;

if (*Price > DH && NumOpenLong <1 && Now > StartTrade && Now < Close)
enterLong(1);

else if (*Price < DL && NumOpenShort <1 && Now > StartTrade && Now < Close)
enterShort(1);

}
Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/18/13 10:55

Sorry, I just see that I made a mistake - this is (hopefully) the correct code:

if(lhour(UTC) > EndMarket/100)
...
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/18/13 11:40

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);

}
Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/18/13 11:45

Margin = 0 does not prevent trades. You must set Lots = 0 for this. And your comparisons make no sense. You're comparing a hour with a bar offset. You can compare hours with hours and offsets with offsets, but mixing both won't work.

For developing a strategy, start with a very simple version. Look if trades are entered at the proper time. Then step by step add more conditions and stop/profit distances, and test after every step. Do not start already with the final version with complicated entries and exits.
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/18/13 12:13

Thanks again and for your patience laugh

One last thing, Is this correct for the current time so i can compare the offset correctly?

var Now = timeOffset(UTC,0,0,00);

I changed the code, and things just got even worse...

I think i will ditch this attempt as you say and start from scratch again..
Posted By: jcl

Re: Code help: Range of 1st hours trading. - 11/18/13 12:42

The current time is lhour(UTC) and minute(). timeOffset returns not a time, but a bar offset. Better forget about timeOffset altogether.

This is the code for checking if it's 9:30 or later:

if(lhour(UTC) >= 9 and minute() >= 30) ...
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/18/13 12:53

Oh, god, I've so much work to do, hopefully in 5 years time i will be able to swiftly code and understand it. laugh

I think I need to also re-read the manual many times..

Appreciate the help here. laugh
Posted By: swingtraderkk

Re: Code help: Range of 1st hours trading. - 11/18/13 15:00

Geek,

I've had a little go at this to try to make it simpler for you. I think you might be getting confused by the Day High and Day Low functions, so I'm simply looking at the max & min value of priceHigh & pricelow series over the first hour trading and setting pending orders for those levels. I've commented out a simple One Cancels Other check, I was not sure if it was part of the strategy to take one or both breakouts.

I have also commented out some print lines to help you see what is going on. Uncomment them and check your log if you want to see what's going on bar by bar.

Code:
function run()
{
	
	set(TICKS+LOGFILE);
	
	BarPeriod = 30;
	LookBack  = 3;
	Hedge = 2;
	
	
	StartDate = 20100101;
//	EndDate   = 20100201;

	asset("UK100");
	
	int marketstarthour		= 8;
	int marketstartminute	= 0;
	int marketendhour			= 16;
	int marketendminute		= 30;
	
	vars hi				= series(priceHigh());
	vars lo				= series(priceLow());
	
	//Find the high low range of first hours trading
	vars enthigh		= series(MaxVal(hi,2)); 
	vars entlow 		= series(MinVal(lo,2));
	vars Range			= series(enthigh[0]-entlow[0]);
	
	

	
	
//	printf("\n%2.0d:%2.0d High %4.1f Low %4.1f enthigh %4.1f entlow %4.1f range %4.1f",hour(),minute(),hi[0],lo[0],enthigh[0],entlow[0],Range[0]);
//	
	
//	if (NumPendingTotal == 1)   // Take only one trade per day OCO one cancels other Not sure if this was part of strategy or not
//	{
//		for(open_trades)
//		{
//			if (TradeIsPending) exitTrade(ThisTrade);
//		}
//	} 
	
		if ((hour() == marketstarthour+1)
				and (minute() == marketstartminute))
		{
			
//			printf("\nenthigh = %4.1f; entlow = %4.1f; Range = %4.1f; PIP = %4.1f",enthigh[0],entlow[0],Range[0],PIP);
//			
			
			if (Range[0] < 40*PIP) 		// Don’t trade if range is higher than 40 points
			{
//				printf("\nRange %4.1f < 40 pips, entering pending trades at enthigh %4.1f & entlow %4.1f,price %4.1f",Range[0],enthigh[0],entlow[0],priceClose());

				EntryTime = 15;											// Expire pending trades at 4:30
				
				enterLong(0,enthigh[0],Range[0],Range[0]);
				enterShort(0,entlow[0],Range[0],Range[0]);		//Buy on the breakout of the lowest low or the highest high of that first hour
			}
			
		}
		
		if (((hour() >= marketendhour)
				and (minute() >= marketendminute)) and (NumOpenTotal > 0))
		{
//			printf("\nExiting Open Trades");
			exitLong();
			exitShort();
		}
}

Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/18/13 15:27

Thanks very much swingtraderkk appreciated, this is great to compare with mine, I envy your's and others coding skills..

I will have a look right now laugh
Posted By: Geek

Re: Code help: Range of 1st hours trading. - 11/18/13 16:06

thanks KK, things seem to be working properly using your code, when checking the log file etc as far as i can see...Will look into it more..
Posted By: swingtraderkk

Re: Code help: Range of 1st hours trading. - 11/18/13 16:06

Don't envy, just keep at it. I'm only a few months ahead of you.
Posted By: dr_panther

Re: Code help: Range of 1st hours trading. - 09/12/16 21:20

The thread is very old, still I want comment, that dates are in UTC so to find the first hour of a trading session, you HAVE to use the localized functions, like lhour(). Please think of day light saving, most timezones use that, but UTC not.

Jcl said that in the middle of this tread, but it was ignored, so watch for that if you are using this kind of strategy.
© 2024 lite-C Forums