Averaging when position profitable
Author: kyokushin
Creation Date: 3/29/2014 4:57 AM
profile picture

kyokushin

#1
I just want to average down a stock when my open position with this particular stock is profitable. How would I have to code this?
profile picture

Eugene

#2
You could take this code as a guideline:

Average down strategy
profile picture

kyokushin

#3
Thanks but I'm not very familiar with coding. What I asked for shall be one condition of a multicondition group. I just want to buy more shares of a particular stock when my already open position of that stock is profitable. Could you provide me that code?
profile picture

Eugene

#4
For Rule-based strategies, you have the option to add a condition from the "Position-Based" group called "Current position is profitable".
profile picture

kyokushin

#5
You're right. But as far as I understood it the rule takes the most recent position which is not necessarily the same stock it buys some more of. Or am I wrong?
profile picture

Cone

#6
In general [and unless you're doing something more advanced like pairs trading or symbol rotation] scripts operate on one symbol at a time. So in a script "LastPosition" will refer to the LastPosition entered for the "clicked" symbol, or the one currently running in a MSB (Multi Symbol Backtest).

Make sure you add that condition with an "OR" (or as a separate entry condition) since it requires that you have an active position already.
profile picture

kyokushin

#7
I'm performing MSBs and using the position sizer "Avg Down with Pct Equity Limit." The position size is 5% and the max. % of equity is 20.

When filling up a position to the 20% and buying a stock a second or third time I want the rule to do this only when this particular position is not more than X% on the negative side.

How would I have to code this? Sorry for being not precise enough in the beginning.
profile picture

Eugene

#8
In this case, you have to open the code in a new Strategy window and manually edit it. By default, "Current open position is profitable" creates this code behind:
CODE:
Please log in to see this code.

I think this should do it:
CODE:
Please log in to see this code.

Where "-X" is your X% on the negative side

UPDATE: code edited (NetProfitAsOfBar > NetProfitAsOfBarPercent), thanks Cone
profile picture

kyokushin

#9
Does

if (LastActivePosition.NetProfitAsOfBar(bar) >= -15)

mean not more than 15% on the negative side?

profile picture

Cone

#10
You should be using NetProfitAsOfBarPercent.
profile picture

kyokushin

#11
Earlier you wrote "in a script "LastPosition" will refer to the LastPosition currently running in a Multi Symbol Backtest."

What I want the script to do is to just buy more shares of a particular stock if the already bought are not more than X% on the negative side. As far as I got you the script as it is now just looks if the last position entered is not more than X% negative. Regardless what stock it is.

Let me give you a small example to make it more clear: One day the script buys AXP. On the next day it buys XOM. On the third day it attempts to buy AXP again. But this shall only be done if AXP is not more than X% negative.
profile picture

Eugene

#12
If AXP is the symbol currently processed by your MSB, LastPosition refers to that symbol's last Position, i.e. AXP.
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).