Pyramiding - Partial Exits
Author: gborrageiro
Creation Date: 7/2/2009 6:39 AM
profile picture

gborrageiro

#1
hello,

Can anyone point me in the direction of some sample code of a system for taking profits such as:
buy = fast ema crosses up over slow ema
short = slow ema crosses up over fast ema

exit strategies:
1. 3 contracts are traded
2. 1 contract is exited after 2 profitable points
3. 1 contract is exited after 4 profitable points
4. the final contract is exited with a 6 point trailing stop
5. all contracts are exited when the stoploss of 4 points is hit.

many thanks
GB
profile picture

Eugene

#2
profile picture

gborrageiro

#3
thanks Eugene. I'm new to Wealth-Lab and not a coder unfortunately. Below is the code I've converted via the rules-based strategy creater.
How do use splitposition to achieve the following:
1. 3 contracts are traded
2. 1 contract is exited after 0.02%
3. 1 contract is exited after 0.03%
4. the final contract is exited with a 0.06% trailing stop
5. all contracts are exited when the stoploss of 0.03% is hit.

many thanks
GB


CODE:
Please log in to see this code.
profile picture

Eugene

#4
I quickly modified the KB article code. I deliberately didn't present the full solution, just a short side example. Your price triggers in the 0.02%-0.06% ballpark are very small, is that correct? Example below has it coded as 2-6% respectively. Your task is to verify and modify it accordingly to suit your needs (trailing stops, long & short etc).

CODE:
Please log in to see this code.
profile picture

gborrageiro

#5
thanks Eugene.
I did purposely have small profit targets as the underlying security is highly geared.
take care.
profile picture

Eugene

#6
leeto asked:

Let say (Only long position ) I enter to the position with 300 shares.

1. I would like to close my first 100 share at + 20 cents and when my price hit the first target I would like to move up stop order on the remaining 200 sharers at +10 cents profits (or 10 cents less than target 1 ).

2. Then I would like to close another 100 shares at tarter #2 which is +30 cents (relatively to the entry bar) and simultaneously move up my stop order on the remaining 100 shares at 10 cents below the target #2.

3. Then the remaining 100 shares would be guided by trailing stop at the end of the market day.

I find out a related forum topic http://www.wealth-lab.com/Forum/Posts/pyramiding-partial-exits-29505
and the related articles
http://www2.wealth-lab.com/WL5Wiki/psPyramiding.ashx

http://www2.wealth-lab.com/WL5WIKI/kbPyramid.ashx

but I cannot figure out how to deal with moving stop orders when a price hits a target. The last article that I mention looks like to employ the idea about moving up stop ( after hitting a target ) but it is still difficult for me to connect the dots. Could you help me... please... Many thanks in advance !


CODE:
Please log in to see this code.
profile picture

leeto

#7
I think I solve the main core of the problem but the code working not quite correct because of the bug that I think I spot but don't know how to re-write the problematic portion of the code.

So first of all, I simplify my initial condition to make life easier a little bit. I use not 300 shares but 400 shares (it is easier to divide in mind ) everything else the same.



(Only long position ) I enter to the position with 400 shares.

1. Initial stop => -10 cents

2. Target #1 => + 20 cents and I close 200 shares (half of the position )
Stop #2 => +10 cents (close remaining 200 shares )

3. Target #2 => + 40 cents and closing 100 shares
Stop #3 => +35 cents (close remaining 100 shares )

4. If price didn't hit stop #3 after reaching target#2 => I close it at any price at the end of the day.
all data intraday

CODE:
Please log in to see this code.


The code has several problems that I spot.

Problem #1 : it keep going divide position. So in the results we can see not only 400, 200, 100 shares but 50, 25,13,6 ... even 0 shares
Even if I change the variable InitialPositionSize on the constant 400 (shares for example ). The function of splitting stop to work.

Problem #2: Function GetTime(int bar) doesn't work at all.

I think the core problem located here :

CODE:
Please log in to see this code.


But I dont know how to substitute
CODE:
Please log in to see this code.
expression in order to introduce target1, target2 and InitialPositionSize variables.

Please help me to solve this problems. Many thanks in advance !
profile picture

leeto

#8
QUOTE:
Problem #2: Function GetTime(int bar) doesn't work at all.


I figure out why GetTime function didn't work.

CODE:
Please log in to see this code.


Still working on the first problem.
profile picture

Cone

#9
I'll help with this... stand by.
profile picture

leeto

#10
Thank you Cone so much in advance !

This is my last update of the code maybe it will become useful. In this code version, I integrate SetShareSize function and change targetSwitcher to two bool target1Switcher / target2Switcher which seems better. However, my code still acting weird :((


CODE:
Please log in to see this code.
profile picture

Cone

#11
The solution is a little complex, but the logic is more easily managed with an object. The PosTracker object keeps track of the next target and stop, and it's stored in the Position Tag (see Position Object > Tag in the QuickRef). Additionally, note that exits can occur at multiple prices on the same bar, so multiple prices have to be checked and positions split, if required, on the same bar.

CODE:
Please log in to see this code.
profile picture

leeto

#12

You cannot imagining how grateful I am for the work that you done. For sure, I cannot write so advanced code. I'm pleased to read your code. This the true example of work of professional. But, unfortunately, I have noticed minor bug and I cannot spot the source of this problem. The problem is that in the result page I notice that amount of shares can be 50, 25, 13, 6 and etc so somehow the algorithm keep going to divide position even after remaining piece of position reach 100 shares ( In other words, 100 shares should be absolute minimum in the results Trades page ) .

QUOTE:

3. Target #2 => + 40 cents and closing 100 shares
Stop #3 => +35 cents (close remaining 100 shares )


Please help me to resove this bug, I will not be able to cope with this problem without you. In attachment, I put the printscreen with intances of the bugged trades.
profile picture

leeto

#13
In attachment, you can find the data for the several stocks that expose the bug.
profile picture

Cone

#14
Did you copy the code that I posted? For me this doesn't happen, not even when I use your data. I don't even think it's possible for it to happen with the code above unless you've modified the "targets" or "stops" array. You see, the number of elements in the targets array determines precisely how many times the initial Position is allowed to split. Note that the stops array is required to have one more element than targets (the initial stop offset).

Here's an example of the ACN run with your data -
CODE:
Please log in to see this code.
profile picture

Eugene

#15
QUOTE:
You cannot imagining how grateful I am for the work that you done.


Now that your trial has ended, purchase of a Wealth-Lab Developer license would be a reasonable acknowledgement for all the support effort. ;)
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).