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
2 registered members (EternallyCurious, howardR), 646 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Closing trades partially #482637
03/11/21 18:31
03/11/21 18:31
Joined: Mar 2021
Posts: 6
Z
ZorroTradeAA Offline OP
Newbie
ZorroTradeAA  Offline OP
Newbie
Z

Joined: Mar 2021
Posts: 6
Hi guys,

I'm new in this forum while I used Zorro already for a while for backtesting.

I faced an issue that I really don't understand why this happen and I could not find any solution in that forum.
My goal is to include in my strategy partial exits of my positions.
When I enter the trade, I cannot say how many lots (in this case stocks) I need to sell - this depends on some factors like volatility.

I find in the manual the function exitTrade. The function has as input a pointer to a trade and optional the price and the amount of lots I like to exit.
Tha manuel tells me the following: "Exits a particular trade completely or partially at market or at a given price limit. Returns 0 when the order failed, otherwise nonzero."

To make a more simple example, I coded a strategy which enteres the first two Wednesdays of my trading period the market, and shall start to exit them in quarters afterwards.
Unfortunately it exits my complete trades ones.

Here is the code:

Code
int counter = 0;

void run()
{	
	set(PLOTNOW|LOGFILE);
	
	LookBack = 0;
	Fill = 3;
	Verbose = 2;
	StartDate = 20200101;
	EndDate = 20200228;
	Slippage = 0;
	asset("AAPL_EOD");
	
	//Entering position
	if(dow()==3 && counter<2){
		enterLong(100*(counter+1));
	}
	
	//Counter
	if(dow()==3)
		counter ++;
	
	//Closing position
	if(dow()==3 && counter>2){
		for(open_trades){
			exitTrade(ThisTrade, 0, TradeLots/4);
		}
	}

}


And here is the part of the log file that shows the problem:

[4: Wed 20-01-08 16:00] 73.54/75.35\73.54/75.04
[AAPL_EOD::L00501] Long 100@76.04 at 15:00:00
...
Wednesday 20-01-15 Loss -33.30 ----
Current DD: 106$ (44.2%) (new depth)
[AAPL_EOD::L01001] Long 200@77.61 at 15:00:00
...
Wednesday 20-01-22 Profit +83.70 ----
Current DD: 161$ (30.0%) (new depth)
[AAPL_EOD::L00501] Sell 100@78.68: +263 Bid 78.67
[AAPL_EOD::L01001] Sell 200@78.68: +212 Bid 78.67

I would expect to sell only a quarter of the position, meaning 25 for the first trade and 50 for the second. But as you can see it sells the whole position.

Any hints or alternative implementation are welcome.
Thank you!

Re: Closing trades partially [Re: ZorroTradeAA] #482641
03/11/21 23:14
03/11/21 23:14
Joined: Oct 2017
Posts: 56
Munich
K
kalmar Offline
Junior Member
kalmar  Offline
Junior Member
K

Joined: Oct 2017
Posts: 56
Munich
Is this thread of help for you?
https://opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=482190&Searchpage=1&Main=59003&Words=%2Bpartial&Search=true#Post482190

Re: Closing trades partially [Re: kalmar] #482649
03/12/21 19:23
03/12/21 19:23
Joined: Mar 2021
Posts: 6
Z
ZorroTradeAA Offline OP
Newbie
ZorroTradeAA  Offline OP
Newbie
Z

Joined: Mar 2021
Posts: 6
Hi - thanks for your response.
I already read this forum but could not really find an answer. Out of a couple of reasons:
I cannot open half a position or similar as I don't know in the beginning of my trade how many shares I would like to sell.
I don't use the TICK flag as this is not the focus of my strategy.
The solution would be probably a TMF - but I could not find the way to implement it. I can close the position in a TMF, that's fine. But I cannot say how to close a quarter or half of the postion.

I'm really interesting why the function exitTrade is not working as desribed in the manual. Maybe I got it wrong?

Re: Closing trades partially [Re: ZorroTradeAA] #482706
03/18/21 17:53
03/18/21 17:53
Joined: Mar 2021
Posts: 6
Z
ZorroTradeAA Offline OP
Newbie
ZorroTradeAA  Offline OP
Newbie
Z

Joined: Mar 2021
Posts: 6
Hi everyone,

maybe my example was not so easy to read. So I searched in the manual and in the chapter Tips&Tricks I find something that should close half the position.
Example from the manual:
Code
...
TRADE* MyTrade = enterlong();
...ThisTrade = MyTrade; // connect trade variables to MyTrade
var MyResult = TradeProfit; // evaluate trade variables
...
exitTrade(MyTrade,0,TradeLots/2); // exit half the trade

For unknown reasons it does not work for me.
Here is my more simple code:
Code
int counter = 0;
TRADE* myTrade;

void run()
{	
	set(PLOTNOW|LOGFILE);
	
	LookBack = 0;
	Fill = 3;
	Verbose = 0;
	StartDate = 20200101;
	EndDate = 20200131;
	Slippage = Commission = Spread = 0;
	asset("AAPL_EOD");
	
	//Counter
	counter ++;
	
	//Entering position
	if(counter<2){
		Lots = 100;
		myTrade = enterLong();
	}
	
	//Closing position
	if(counter>2){
		exitTrade(myTrade, 0, TradeLots/2);
	}

}


And here the log file:

Test: partialTakeProfits AAPL_EOD 2020
[AAPL_EOD::L00104] Long 100@73.14 at 15:00:00
[1: Fri 20-01-03 16:00] 0000 +6.00 1/0 (73.20)
Friday 20-01-03 Loss -59.00 ----
[2: Mon 20-01-06 16:00] 0000 +65.00 1/0 (73.79)
Monday 20-01-06 Profit +59.00 ----
[AAPL_EOD::L00104] Sell 100@73.82: +68.00 Bid 73.82

Why does it sell 100 instead of half the position (in this case 50)?


Moderated by  Petra 

Gamestudio download | chip programmers | 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