Closing trades partially

Posted By: ZorroTradeAA

Closing trades partially - 03/11/21 18:31

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!
Posted By: kalmar

Re: Closing trades partially - 03/11/21 23:14

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
Posted By: ZorroTradeAA

Re: Closing trades partially - 03/12/21 19:23

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?
Posted By: ZorroTradeAA

Re: Closing trades partially - 03/18/21 17:53

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)?
© 2024 lite-C Forums