Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
4 registered members (AndrewAMD, TipmyPip, NewbieZorro, Grant), 14,196 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Toolbox functions for opening/closing restrictions #430086
09/23/13 17:16
09/23/13 17:16
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
I learned some new things this past week with bitwise operators. They are like dip switches I think, and those could be great for lots of things!

Anyways, I'm a n00b scripter but wanted to share these functions I wrote. I am building a toolbox to help with rapid development of new bots for my legion. Once I get a good handful of polished tools, I plan to extract them into an include-able set that will be easy to tack onto new bots.

As part of my process, I first identify a working trade logic (ie, "the edge") and somewhere further in that process, I can use optimizer to test these combinations of open/close times.

I've done a lot of thinking about whether or not controlling the time for executing trades would be considered "curve fitting". My current belief is that it is not, or maybe "not much", because I am simply filtering days or time segments that the logic is known to work best. This seems reasonable, as discretionary traders do it all the time. These functions do not alter trade logic, they only filter it at some level (hopefully reducing poor performance windows and improving cream-of-the-crop windows).

Any improvements or contributions are welcome!
Code:
function fridayClose(int fridayclose)
{
	//allows Friday trading up until NYSE 3pm; close trades and don't allow after this
	if(fridayclose && dow() == FRIDAY && lhour(ET) >= 15) 
		{
			exitLong("*");
			exitShort("*");
			return 1; //condition met; indicate no further trades
		}
	return 0; //condition not met; safe to take new trades
}

function todayOpenCombo(var dayopencombo)
{
	//allows optimizer to specify the best combo of days for opens
	//bit position 0 = Monday
	//bit position 1 = Tuesday
	//bit position 3 = Wednesday
	//bit position 4 = Thursday
	//bit position 5 = Friday
	//bit position 6 = Sunday
	//given a combination #, the function will return whether
	//current dow() is in the combination

	int dayopencombobits = dayopencombo+.5; //truncate to rounded int
	int today = dow() - 1; //Mon is 0
	if (today == 6) today = 5; //bump Sun to 5 (no Sat, keep binary range 0-63)

	if (dayopencombobits & (1 << today)) return 1; //current dow() is in the combo
		else return 0; //current dow() not in combo, do not allow trade opens
}

function todayCloseCombo(var dayclosecombo)
{
	//allows optimizer to specify the best combo of days to close by NYSE 4pm
	//bit position 0 = Monday
	//bit position 1 = Tuesday
	//bit position 3 = Wednesday
	//bit position 4 = Thursday
	//bit position 5 = Friday
	//bit position 6 = Sunday
	//given a combination #, the function will determine if we are beyond
	//a combo close time, close all trades if necessary, and return 1
	//if no further trades allowed today

	int dayclosecombobits = dayclosecombo+.5; //truncate to rounded int
	int today = dow() - 1; //Mon is 0
	if (today == 6) today = 5; //bump Sun to 5 (no Sat, keep binary range 0-63)

	if ((dayclosecombobits & (1 << today)) && lhour(ET) >= 16) 
	{
		exitLong("*");
		exitShort("*");
		return 1; //current dow() is in the combo; indicate no further trades
	}
	else return 0; //current dow() not in combo, safe to take new trades
}

function marketOpenCombo(var marketopencombo)
{
	//allows optimizer to specify best markets to initiate trades
	//bit position 0 = New York 8am-5pm Eastern
	//bit position 1 = Sydney 5pm-2am Eastern
	//bit position 2 = Tokyo 7pm-4am Eastern
	//bit position 3 = London 3am-12pm Eastern
	//given a combination #, the function will determine if current time is within
	//a market part of the combination (returns 1 to allow trading if true)
	
	int marketcombobits = marketopencombo+.5; //truncate to rounded int
	if ( (lhour(ET) >=8) && (lhour(ET) <17) && (marketcombobits & (1 << 0)) ) return 1; //inside New York
	if ( (lhour(ET) >=17) || (lhour(ET) <2) && (marketcombobits & (1 << 1)) ) return 1; //inside Sydney
	if ( (lhour(ET) >=19) || (lhour(ET) <4) && (marketcombobits & (1 << 2)) ) return 1; //inside Tokyo
	if ( (lhour(ET) >=3) && (lhour(ET) <12) && (marketcombobits & (1 << 3)) ) return 1; //inside London
	return 0; //default - current market not in combination, don't allow trade opens
}

function hourOpen(int hourblockstart, int hourblockend)
{
	//blocks new open trades between selected hours
	//uses NYSE time, including DST
	if ( (lhour(ET) >= hourblockstart) && (lhour(ET) < hourblockend) )
		return 0; //between blocked hours, do not allow trade opens
	else
		return 1; //no conditions met, allow trades by default
}



These are the flags I use in my logic section:
Code:
int fridayclose = 0; //enforce auto-close and no trades after NYSE 3pm Friday
	int hourblockstart = 0; //block trade opens beginning at NY hour
	int hourblockend = 0; //block trade opens ending at NY hour
	int dayopencombo = 63; //optimize(22,1,63,1); //combo of days to open; 63=every day
	int dayclosecombo = 0; //optimize(1,1,63,1); //combo of days to close after NYSE 4pm; 0=none; 63=every day
	int marketopencombo = 15; //optimize(1,1,15,1); //combo of markets to allow trade opens; 15=every market

...
	//check if ANY trade restrictions before evaluating for new signals
	if ( (!fridayClose(fridayclose) //close NYSE 3pm on Friday
		|| !todayCloseCombo(dayclosecombo) ) //close NYSE 4pm on selected days
		&& todayOpenCombo(dayopencombo) //open on selected days only
		&& marketOpenCombo(marketopencombo) //open during selected markets only
		&& hourOpen(hourblockstart,hourblockend) ) //open during selected hours only
		{
			//OK to trade, let's evaluate signals then



PS: this page helps with thinking through the bit combinations: http://ascii.cl/conversion.htm

Re: Toolbox functions for opening/closing restrictions [Re: dusktrader] #434169
12/15/13 22:35
12/15/13 22:35
Joined: Dec 2013
Posts: 11
J
Jeff_Raven Offline
Newbie
Jeff_Raven  Offline
Newbie
J

Joined: Dec 2013
Posts: 11
Thanks for these, dt. These will let the optimizer do easily some things that I have done laboriously by hand before. Great! And I can easily see some extensions of the bit flags to some other things I'm planning on translating from MT4.

Re: Toolbox functions for opening/closing restrictions [Re: Jeff_Raven] #434170
12/16/13 00:29
12/16/13 00:29
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
Glad you found them useful, and please let me know if you think up any other uses that we can build new tools with.

I would recommend to take the latest code from this post since there may have been fixes made since this thread.

Re: Toolbox functions for opening/closing restrictions [Re: dusktrader] #434171
12/16/13 01:10
12/16/13 01:10
Joined: Dec 2013
Posts: 11
J
Jeff_Raven Offline
Newbie
Jeff_Raven  Offline
Newbie
J

Joined: Dec 2013
Posts: 11
Thanks for the heads-up. That post is actually where I found them. I've been reading through the threads looking for examples and felt like I'd struck gold. laugh I'll definitely let you know if I get some new stuff working.


Moderated by  Petra 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1