Selling increments of position
Author: giorgos
Creation Date: 2/7/2009 6:49 PM
profile picture

giorgos

#1
I understand that I can control buying size by setting the percentage to buy. However when the sell condition is met each time I would like to sell 1/5 of the position held. Can you please help with how to code this. Thanks




CODE:
Please log in to see this code.

profile picture

Cone

#2
Note that you can't just exit 20% of the remaining position each time (try the math). Instead, the percentage rises with each subsequent exit to get rid of a fixed position 20% of the original position as an example with 100 shares shows...

100 * 0.2 = 20
80 * 0.25 = 20
60 * 0.333 = 20
40 * 0.5 = 20
20 - last lot

Anyway, there are a few ways to do this, but here's one that uses SplitPosition to split up the original Position in 5 parts immediately. That makes it easier so that you can simply decide how many of 20% lots you want to exit when the time comes.

CODE:
Please log in to see this code.


In your case, you need only to exit the "LastActivePosition" in the exit logic -
CODE:
Please log in to see this code.

profile picture

giorgos

#3
You are right actually every time the buy condition is met I would buy one lot (its just happens I have set each purchase at 20% of my trading capital) and if the buying condition is met with no sells I will keep adding a lot. Now if a sell condition is met I only want to sell one lot each time.

Here is my entry and exit code

CODE:
Please log in to see this code.

Thanks
profile picture

Cone

#4
First, please use the CODE button when posting code (I fixed if for you above).

It now sounds like you're not really interested in splitting a position and selling 20%. Rather, you're sizing is 20% of equity. Well look, this is fine. A position is a position. It doesn't matter how many shares it contains, the code only deals with "a position". So, if you're only selling one position at a time, and you don't care which one it is, just use the exit logic that I gave you already.
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).