Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 559 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
[SOLVED]-Trades closing themselves?! (and opening in odd places) #467005
07/12/17 02:38
07/12/17 02:38
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Hi all,

OK, they are most likely not closing themselves but I can't put my finger on what the issue is.
The strategy is simple enough and based on price levels. Once price is inside the upper/lower zone, it places a sell/buy below/above the zone by a certain number of pips. Trades are meant to stay open until TP or the end of the test period.

The code:
Code:
//----globals
if(is(INITRUN))
{
	var sixthLen 	= 1682;
	var splitBy 	= 6;
	//var TP 			= 50*PIP; //takeprofit in pips converted to price value
	//var buffer 		= 10*PIP; //distance from market for pendings to be placed
	var tradeLim	= 2000;	
}

//----sixths functions
var sixthHigh(period, splitBy)
{
	return (HH(period) - ((HH(period)-LL(period))/splitBy));
}

var sixthLow(period, splitBy)
{
	return (LL(period) + ((HH(period)-LL(period))/splitBy));
}

//----order functions
function CheckForTradeOpportunity()
{
	var TP 			= 50*PIP; //takeprofit in pips converted to price value
        var buffer 		= 10*PIP; //distance from market for pendings to be placed
	
	vars Price = series(priceClose()); //create the price series for the selected asset
		
	EntryTime = 10000000;
			
	//check to see if we can enter long
	if(NumOpenLong + NumPendingLong < tradeLim) 
	{
		if(crossUnder(Price, sixthLow(sixthLen, splitBy))) //> need to change this to a comparative function or add another block for non cross events
		{
			enterLong(0, sixthLow(sixthLen, splitBy) + buffer, 0, TP);
			printf("nprice = %.5f, sixthsLow = %.5f, pending price = %.5f", price(), sixthLow(sixthLen, splitBy), sixthLow(sixthLen, splitBy) + buffer);		
		}	
	}
	
	//check to see if we can enter short
	if(NumOpenShort + NumPendingShort < tradeLim) 
	{
		if(crossOver(Price, sixthHigh(sixthLen, splitBy)))
		{
			enterShort(0, sixthHigh(sixthLen, splitBy) - buffer, 0, TP);
			printf("nprice = %.5f, sixthsHigh = %.5f, pending price = %.5f", price(), sixthHigh(sixthLen, splitBy), sixthLow(sixthLen, splitBy) + buffer);	
		}	
	}
}

//----main exectution block, comparable to OnTick()
function run()
{
	set(LOGFILE);
	BarPeriod = 60;
	LookBack = sixthLen;
	//PlotBars = 500;
	StartDate = 2012;
	EndDate = 2017;
	Hedge = 2; //allows full hedging, default = 0 -> no hedgintg	
	
	CheckForTradeOpportunity();
		
//--------------
	
	plot("Lowest", LL(sixthLen), 0, BLACK);
	plot("Highest", HH(sixthLen), 0, BLACK);
	plot("upperTZ", HH(sixthLen) - ((HH(sixthLen)-LL(sixthLen))/splitBy), 0, RED);
	plot("lowerTZ", LL(sixthLen) + ((HH(sixthLen)-LL(sixthLen))/splitBy), 0, RED);
	
	set(PLOTNOW);
}



Inspecting the resulting plot reveals there are trades closing at a loss before the simulation is over. I'm not sure why and it is likely some default global I'm not aware of despite having found and checked it in the manual. Any ideas?

Cheers,
BobbyT

EDIT: changed the start/end period to highlight another problem. There are trades being placed at what appears to be erroneous prices. Check the plot in the first half of July 2010. There is a trade that has opened at the peak of a small rally. This trade is well outside of the trade zone (both upper and lower zones). This is easily seen against the red channel which marks out the inner boundary of the trade zone.
I'm taken aback by the amount of teething problems I'm having switching over to Zorro. Having said that, the platforms I'm familiar with have massive user bases and chances are if you have a problem you can normally find info about it somewhere. Alas, Zorro is still growing. So I have to bother you poor sods if I need help. Sorry (these posts will get more interesting with time but at this stage I'm still learning to crawl before I can walk in Zorro land)

Last edited by BobbyT; 07/18/17 03:31.
Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467021
07/12/17 16:41
07/12/17 16:41
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
I learned that prices cannot be erroneous since the market is always right laugh.

I don't know how you wanted your trades to close, but if they behave odd, I can not give another advice than find the reason and fix it. Check the prices and your entry/exit conditions in detail in the log. Follow a trade from start to end. Use the debugger and watch your variables. Once you've got used to examine and debug your scripts systematically, all those issues will be gone.


Re: Trades closing themselves?! (and opening in odd places) [Re: jcl] #467025
07/12/17 18:34
07/12/17 18:34
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Hi JCL,

That is always true. until it isn't laugh

That's exactly the thing, the trades are never meant to exit. It's a bad strategy but one that I though would allow me to become familiar with the platform.

After the finding that pending orders are deleted after one bar if they aren't filled, I thought maybe there was something that I had missed in the default global variables that may be causing the issue.

Regardless, I have added print statements which return the entry price and indicator price for each trade and the indicator price always seems to be correct. So I am at a loss as to what the issue is.

I will check out the debug mode to see if I can pick up the issue as it steps through the simulation. But...don't be surprised if you see me back here in the next couple of days whining about the same problem tongue

Cheers,
BobbyT


Last edited by BobbyT; 07/12/17 18:36.
Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467029
07/12/17 20:52
07/12/17 20:52
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Well, I'm back much sooner than anticipated.

I'm having problems with print statements not appearing in the log file.

They are entered to the log for ~2 months and then they just stop! Makes debugging pretty difficult if I can't get the information I need from the terminal.

Any suggestions as to what's going on? I'm all for helping myself but this is beyond what information I can get my hands on.

Cheers,
BobbyT

Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467031
07/12/17 22:52
07/12/17 22:52
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Further to the above, you suggest tracking individual trades through the simulation.

How is that done before a pending trade is opened? Pending orders are not assigned a trade number until they are filled. In some instances (and this is where I think the problem trades on the plot are coming from), trades do not open for several months. It is impossible to trace them without having a trade number/ID

Cheers,
BobbyT

Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467032
07/13/17 00:47
07/13/17 00:47
Joined: Nov 2013
Posts: 123
Mithrandir77 Offline
Member
Mithrandir77  Offline
Member

Joined: Nov 2013
Posts: 123
BobbyT, you can try set(STEPWISE) and use print(TO_LOG) and/or print(TO_HTML) immediately after

Code:
enterLong(0, sixthLow(sixthLen, splitBy) + buffer, 0, TP);



for instance to log a long pending trade. Check

http://zorro-project.com/manual/en/printf.htm
and
http://zorro-project.com/manual/en/mode.htm

Last edited by Mithrandir77; 07/13/17 00:47.
Re: Trades closing themselves?! (and opening in odd places) [Re: Mithrandir77] #467034
07/13/17 02:22
07/13/17 02:22
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Hi Mithrandir,

Thanks for the reply. I tried
Code:
printf(TO_LOG,"nStuff", SomeVariables);


but it throws a crash error (#111) when it tries to print those statements.

I tried replacing my desired statements with
Code:
printf(TO_LOG, "ntest message");


which results in the same crash error.

I had also previously tried short-cutting it to the log with
Code:
printf(TO_LOG, "#ntest message");


this runs, but results in messages not being printed to the log after a couple of months.

The above examples were appended immediately after the enterLong/Short calls.

Cheers,
BobbyT

EDIT: forgot about the forum editing out slashes from text. In all the examples there is a backslash before the 'n' (between the # and 'n' in the third example)

Last edited by BobbyT; 07/13/17 02:23.
Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467036
07/13/17 06:56
07/13/17 06:56
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
Important advice for writing scripts:

When something does not work, find out why _before_ you attempt to fix it - not the other way around. When you wildly modify commands, you`ll end up with nonsense and crashes. There are plenty printf examples in the manual and in C books.

If you don't find your printf in the log, the right reaction is not `ugh, it doesn't work`, but `why is this part of my code not executed?`

Re: Trades closing themselves?! (and opening in odd places) [Re: jcl] #467037
07/13/17 08:06
07/13/17 08:06
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Hi JCL,

The above print statements were tested based on what is in the manual (with the backslashes missing due to the forum system).

The print statements are contained within a conditional that also sends the trade. For roughly two months both the trade is sent and the print statements make it to the log. After that, only the trade is sent.

This tells me the conditional is being met as the trades are still sending. But printf miraculously stops printing to the log. It does however print to the terminal.

Is the stack full? Do I have to dip my toes into memory allocation?

Cheers,
BobbyT

Re: Trades closing themselves?! (and opening in odd places) [Re: BobbyT] #467040
07/13/17 09:19
07/13/17 09:19
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
Hmm. Is it so difficult to at least replicate the syntax of a command from 1000 examples in the manual? Printf is the most often used function in C. This prints to the window and to the log:

printf("\nStuff");

And this only to the log;

printf("#\nStuff");

You got it right in your first code, so why the problems now? But if you definitely can't get familiar with all those commands, syntax, and tiny quotation marks, the last resort would be contacting Support and let them program your script.

Page 1 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1