Disable trade exclusion due to insufficient capital
Author: sburgener
Creation Date: 9/19/2012 11:48 AM
profile picture

sburgener

#1
Hi,

I have built a strategy on a portfolio that is rebalanced on a regular basis (being always fully invested). On every day I manually keep track of the equity value, in order I fully reinvest the available cash. Also, I need this equity value because my SetShareSize depends on the latter.

1) Am I correct by saying that it is not possible to close out only part of the shares for one of the stocks (in my portfolio), i.e. we cannot specify a specific number of shares (through SetShareSize) for closing commands (such as SellAtClose)? I tried everything but it seems like I can't close out only part of the shares for a single stock.

2) Assuming that I am correct in (1), I work around this issue by fully closing out the position on each rebalancing date and reentering the same stock with a new number of shares. However, as this hypothetical closing and opening of the new number of shares must be done at the same time (to reflect that it is just a rebalancing), some of my trades are not executed.
The reason is the following: In my code (which I run on the full portfolio), I only set orders for the stock which is currently being proceded (I am thus not setting buy/sell orders for other stocks through DataSetSymbols[xxx]). Even though I get the trade signals I expect, there is insufficient capital available. I reckon this comes from the fact that because sell and buy happens at the same time (say on the close, and given that I always want my portfolio to be fully invested), we don't exit all positions before entering any of the new ones (but for the stock which is currently being processed the WealthLab algorithm closes out the old share number and buys the new share number, before proceding to the next stock in the portfolio ). This causes that the cash is not totally available before entering into the new positions (but still locked in some former positions), and this causes some trades not to go through.
In my code I set the portfolio equity to 1million initially. To work around the above issue, I can set the Starting Capital in Portfolio Simulation Mode to a much higher number, say 5 million (note that this won't affect my strategy as the portfolio equity is internally set and kept track of). However, the performance indicators in WealthLab are then computed on an equity level of 5 million rather than 1 million.
Is there a way to work around the above and relax the condition that some trades are automatically excluded if there is not available cash?

Thank you in advance for your help!

Best,
S.
profile picture

Eugene

#2
1) You can employ partial exits with SplitPosition.

2) Bumping up the starting capital will not help.
profile picture

Cone

#3
If you're not using DataSetSymbols, how are you estimating the Portfolio equity value? I don't think it's possible, so I'm very interested to know what you're doing!

Note: Split Position doesn't work well for simulations that are rejecting positions frequently. See Open Issues.
profile picture

sburgener

#4
Thanks for your prompt reply.

Regarding SplitPosition I think it won't be that useful, as it only allows to increase but not decrease the number of positions (which I need as well, given that the allocation may completely change on rebalancing).
So, given your answers, I reckon there is no easy way around the issue except the one I proposed?

Eugene, regarding the bumping of start capital it does actually help. In my code, I hardcode the initial value to 1 million and all consequent orders are based on the evolution of this 1 million. Then, when setting the Starting Capital in the wealthlab GUI to 1 million as well, some positions are not executed (even if they should) as not all stocks have been closed before we enter the new positions as explained above. However, when substantially increasing the Starting Capital (but leaving it at 1 million in my code), all positions get executed, because even if not all positions are closed out before entering new ones, there is still enough "hypotetical" cash available to enter the new position. The automatically displayed performance measure will however be wrong, as they are based on starting capital of say 5 million, whereas they should be on a value of 1 million (which is set in my code and drives the orders and the full strategy).

Cone, sorry but I was not totally clear in my first post. What I meant is that in my Execute loop I do not use DataSetSymbols (and the script thus runs through all bars for one stock before proceding with the next stock, so that I don't have to explicitely use DataSetSymbols). I do however use DataSetSymbols in another class where I compute my moving equity level, which is totally independent from the Execute routine (but only called from within the latter).
profile picture

Eugene

#5
Sebastian, I understood that. I just meant to say that Wealth-Lab is not to be "fooled" by the shadow equity: its performance visualizer calculations are driven by the starting capital figure.
profile picture

sburgener

#6
Sorry Eugene, didn't mean to offend you! Thanks again for your support to both of you!
profile picture

Eugene

#7
None taken. Glad to help (when I can).
profile picture

sburgener

#8
Hi,

I would just have an additional question to the above.
1) When there are signals to close and open trades on a same date and time (e.g. in the evening on close), is there a way to force all positions to close first before entering any new positions?
2) I tried to set priorities when closing trades (through Sell or Cover), but this seems not to work, as I have the feeling that the position priority for closing can't be specifically defined but is inherited from (and the same as) when we openend/initiated the position. Or is it the case that priority does not at all affect closing of positions, but only their initiation?
3) As all positions in my strategy are always reset on every rebalancing (I close ALL positions and reopen new ones - see above), I tried to achieve what I wanted by setting the priority level for early trades (i.e. for low bar numbers) to be lower than for late trades (i.e. with higher bar numbers). Thus, assuming that the close priority is inherited from the position initialization, on a rebalancing date, the positions we close will all have a higher priority than the positions we open. When running my script on the portfolio, the code should close all positions before entering any new ones. However, this seems not to be the case as I run into the problem of insufficient capital for trades highlighted in the previous posts.

Could you please shed some light on priorities for closing positions?

Thanks!
Seb
profile picture

Eugene

#9
2, 3) Just to be on the safe side: priorities are only meant to work with entry signals. If what you said actually means inheriting the .Priority from a closed trade, then please make sure you're assigning them in a reliable manner:
CODE:
Please log in to see this code.
profile picture

Eugene

#10
1) Generally, one exits all positions with Position.AllPositions (see QuickRef). Also, the order of processing exits and entries is determined in your code. Does exit logic code precedes entry logic in your Strategy?
profile picture

sburgener

#11
QUOTE:
...please make sure you're assigning them in a reliable manner:..
Yes, I do indeed use LastActivePosition.Priority = 1 - bar/ Bars.Count.
QUOTE:
Does exit logic code precedes entry logic in your Strategy?
Yes exit logic precedes entry of new positions. However, as I am running my code on a portfolio, I only set buy and sell signals for the underlying (stock) which is currently being proceded. In this sense, (as I guess the code loops through all bars for one underlying, before it procedes to the next stock), the enter and exit signals for the 1st underlying will all be placed before we switch to the second underlying.
This is why I can't use closeAll as it will only close the positions hold for the respective underlying (currently being proceded in the code) and not for the other ones.

Is this correct or is the script on a portfolio running differently?
profile picture

Eugene

#12
QUOTE:
Yes, I do indeed use LastActivePosition.Priority

My comment was related to the usage of boolean condition preceding the priority assignment: it's important to verify that Position has been successfully created, before assigning a priority.
QUOTE:
This is why I can't use closeAll as it will only close the positions hold for the respective underlying (currently being proceded in the code) and not for the other ones.

You could loop by ActivePositions the first thing, exiting everything?
profile picture

sburgener

#13
Sorry, I misunderstood your point. Answering correclty: Yes, I indeed have a boolean condition in my code checking whether the position has been correctly created.

Regarding the loop over ActivePositions, it seems like this is not achievable in my current setting. Let me explain why.

In the Execute() routine, I only set buy/sell signals for the underlying which is currently being processed. Hence, I have NO loop on the various underlying instruments of the portfolio (in the Execute() routine). As I understand I don't need the above loop, given that the strategy backtests automatically runs through the Execute() routine (which loops over all bars) for each underlying, one after the other. Thus, when using the command ActivePositions it will only point to the currently active underlying (but not the positions for the other underlyings, as these will be created at the next runs of Execute() only).

The reason why I proceed this way (i.e. without a loop over individual instruments) is because if I set a loop over the underlying instruments of the portfolio in the Execute() routine, either
1) I create the positions for all underlyings when the Execute() routine runs the first time (i.e. for the first underlying in the portfolio) and thus the Charts displayed for all except the first underlying will have no signals displayed (which annoys me as I would like to look at these charts)
2) I create the positions for all underlyings every time I run through the Execute() routine, which will then create too many positions (the strategy will buy several positions for some underlyings and none for others due to insufficient funds).

Let me know if I am wrong on the above reasoning.
If not, I am getting back to the initial question whether it is possible to set a priority when closing instruments (in order to close all before opening new ones)?
profile picture

Eugene

#14
QUOTE:
If not, I am getting back to the initial question whether it is possible to set a priority when closing instruments (in order to close all before opening new ones)?

The concept of Position.Priority applies to entering new positions (only). The order of exiting positions is in your hands and is governed by your code.
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).