Pyramid code for portfolio backtesting of multiple positions
Author: Jim0000076
Creation Date: 10/26/2012 3:59 AM
profile picture

Jim0000076

#1
Hello Eugene,

I thought I posted a code earier but it does not seem to have loaded so I have reloaded it.

I am attempting to write a code for a simple MA back-testing with pyramiding on a portfolio. What I am attempting acheive is:

1. Entry based on a simple 6 / 12 cross.
2. Exit based on a simple 6 / 12 cross
3. Adding to positions when price rizes by X % after entry (nominally 1%).
4. Max of 3 Pyramids
5. Exit all positions at once.

I wrote a code that to do this that appears to work fine on a single stock but when I run it on a portfolio it buys a stock with a pyramid entry even though it does not hold the primary stock. I am assuming that the 'LastActivePosition.EntryPrice' component is looking at the last active price for any of the holdings and is not looking at specific stocks.

I have searched long and hard for a solution with no luck, any help would be greatly appreciated.

Thanks in advance

Jim



Code:

CODE:
Please log in to see this code.

profile picture

Eugene

#2
QUOTE:
I wrote a code that to do this that appears to work fine on a single stock but when I run it on a portfolio it buys a stock with a pyramid entry even though it does not hold the primary stock.

Yes, it can and will happen in multi-symbol portfolio backtests. The workaround we developed is the Pyramiding PosSizer (sorry, extensions are available only to customers).
profile picture

Jim0000076

#3
Does the Pyramiding PosSizer allow the use of ATR based position sizing simulation?
profile picture

Eugene

#4
It allows to choose from the three default options (fixed dollar, percent equity and max % risk). Max % Risk can be used for that purpose:

FAQ > How do I implement volatility position sizing?
profile picture

Jim0000076

#5
I assume that stated % is a fraction of actual ATR? The documentation is not explicit in this regard. If so, that should work, but it would be easier and more efficient if it was code based. Just my opinion.

I looked at the other PosSizer options, I assume that the Pyramiding one looks similiar to these and works in combination with code based strategies.





profile picture

Eugene

#6
Please check out the User Guide > Reference > Data Panel > Position Size Contro > Portfolio Simulation Mode > Max Percent Risk

The idea behind using the Max Percent Risk sizing is to assign the initial stop loss price to the RiskStopLevel property (refer to the QuickRef) before taking the Position. You determine the stop level based on volatility (e.g. by subtracting some ATR units from projected entry price).
profile picture

Jim0000076

#7
OK, so Pyramiding PosSizer allows me to pyramid using the ATR setup that I am already using. That should suffice. Thank you.
profile picture

Jim0000076

#8
Eugene, I have paid and downloaded the Pyramiding PosSizer but can't get it working. I also suspect that the same problem that i have outlined in my first post may still flow through via using this PosSizer. For example I would like to:

1. Buy a MA cross
2. Pyramid the position only if the position moves up by X% (nominally 1% in the code below).
3. Sell all positions in the cross

To do this I have quickly made an example code which I have pasted below. I might add that this code does not work and i am not sure why but nonetheless I note:

1. I used a standard cross as the initial entry.
2. I defined a "pyramid" entry rule and used this name in the posSizer.
3. I used a nominal risk of 1.5%
4. I nominated a staring account value of $100,000 so there is plenty on money at 1.5% risk.
5. I then added the following code to define my entry criteria for a pyramid enty of up 1% from open if (Close[bar] >= LastActivePosition.EntryPrice * 1.01)

I note that the code at point 5 above is the part of the code that I was having a problem with as WL6 does not appear to be able to distinguish between an Active Position or an Active Position for a specific stock and therefore this problem will follow through with this poSizer.

Just wondering if you have a work around to address this issue?

Example code that does not work (not sure why either) is below:


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

Eugene

#9
1. Mind the curly brackets, please.

This:
CODE:
Please log in to see this code.

Should be:
CODE:
Please log in to see this code.

Otherwise your code just buys on every bar, and assigns RiskStopLevel after a crossover.
2. Now that you're taking multiple positions, your Strategy code can no longer use the single-position template:
CODE:
Please log in to see this code.

Please see the WealthScript Programming Guide, Programming Trading Strategies > Multi-Position Strategies, "MP Strategy Template".
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).