ActivePositions includes potential transactions rejected due to capital constraints
Author: kmckiou
Creation Date: 1/1/2020 9:05 PM
profile picture

kmckiou

#1
I have read the posts on ActivePositions and have a follow up question on use of Active Positions within a strategy.

I have a simple RSI strategy that generates a buy signal when the RSI series pass up (crossover) through a lower threshold and generates a sell signal when the RSI series passes above an upper threshold.

I am trading a single symbol and limiting the number of positions through the Percent of Equity option in the Portfolio Simulation Mode. In this case, the limit is 20%.

I decided to extend the strategy by including a "circuit breaker" if an open position should incur a loss of a certain magnitude (param) for a certain number of days (param). In order to implement this circuit breaker I added the following code (somewhat simplified for clarity):

CODE:
Please log in to see this code.


So, if circuiteBreaker = true, I exit all positions immediately:

CODE:
Please log in to see this code.



What I found when testing this code is that the list of trades in ActivePositions not only includes the positions that were generated from signals that were accepted and executed for the portfolio, but also includes a position for every buy signal for potential transactions that were rejected because of portfolio capital limitations.

Thus, when looping through the ActivePositions to determine if a circuit breaker is warranted, I was testing the profitability of positions that were never actually executed. I found this when looking at the backtest results and saw that the circuit breaker had triggered an exit when all the active transactions were profitable. So, I printed the active transactions prior to the exit and found far more active transactions in ActivePositions than there should have been.

Is this a bug or am I misusing the ActivePositions collection?

If I am misusing ActivePositions collection, how should I be evaluating positions that have actually been accepted and executed (allowing for capital limitations)

Thanks,
- Kevin
profile picture

Eugene

#2
Your Strategy does not have a way to know if a Position was skipped due to insufficient funds. That's because a Portfolio Simulation works as follows. First, Wealth-Lab's backtesting engine pre-executes all signals in Raw Profit mode with 1 share. Here the ActivePositions collection gets populated. Then WL applies the position sizing overlay which may skip signals due to capital constraints. Thus, the real number of active positions in a Portfolio Simulation backtest is in the hands of PosSizer.
profile picture

kmckiou

#3
Hi Eugene,
I bet you are getting tired of answering this and similar questions judging from the number of Position Sizing posts.

Thanks for confirming what I was seeing. I don't really want to create my own PosSizer as I really don't yet need it. In fact, at this time, I find it advantageous to examine all potential trades in the circuit breaker.

It appears, in my case, a simple solution to assure I only have positions representing actual trades is to limit the number of ActivePositions to 5 (20% of portfolios per trade) by putting a check before executing buy logic. This however does not solve a more general problem of how to implement a trading algorithm with dynamic position sizing...then it occurred to me that it can be done.

A general position sizing algorithm would start by setting the position size to 1% (or 5%, 10%, whatever divides evenly into 100%) in the Portfolio Simulation Mode. Then, the position size can be controlled by simply executing multiple buy positions when a signal occurs. For example, in a mean reversion strategy, the number of positions per signal might increase as volatility increases. Making a trade position size 1% makes the chart pretty messy, but a size of 10% is not bad. Just a thought.

Thanks,
- Kevin
profile picture

Eugene

#4
Hi Kevin,

On the surface, multiple buy positions looks like a good and creative approach. But make sure to allow for some leeway by setting a Margin Factor or using other workarounds as per this FAQ: I'm using 100% Equity position sizing and strategy doesn't seem to use all capital and/or there are trades not included due to insufficient capital.
profile picture

kmckiou

#5
Hi Eugene,

Right. In my case, a margin factor of just 1.02 to 1 has allowed me to use 100% of the portfolio capital when setting the position size to 20% of equity.

Happy New Year!

- Kevin
profile picture

Eugene

#6
Happy New Year to you too!
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).