My strategy cannot correctly close out multiple positions
Author: MustPlayOptions
Creation Date: 3/4/2018 5:42 AM
profile picture

MustPlayOptions

#1
Hi,

I've gone through the documentation and searched on the forums and in the Wiki and can't find any example of this. The following code won't close most positions:

CODE:
Please log in to see this code.


It's the SellAtClose that isn't working. I tried the ExitAtClose too (commented out) but that didn't work either. Any time there are multiple positions one stay open until the end of the series.

The reason I'm working on this is I didn't want to exit and re-enter a position the same day on the same stock. Even without the if(all_true ==0) is taken out and I just try to re-create the strategy using LastPosition successfully, it still won't close most positions.

Any help is appreciated. Thank you.
profile picture

Eugene

#2
Hi,

It looks like your strategy may employ Limit (and Stop) orders on the same bar. If so please revisit this FAQ as you can't realistically do that without some prerequisite: I want to test a strategy that buys and sells with stop/limit orders on the same bar.

The most likely reason for the SellAtClose not working is all_true > 0 on the Bar. Recreating the strategy using LastPosition is applicable for single position systems whereas your code is presumably for a multi-position system. Anyway, without seeing the entire code it would hardly be possible to tell for sure what's wrong.
profile picture

MustPlayOptions

#3
Thank you for the quick reply. The all_true == 0 wasn't it because even without that it didn't work.

Anyways, I didn't realize that the combo limit/stop was a problem and was planning on just doing a contingent order. Sure enough the performance was a lot worse when I reversed them. It's still ok but not as ok without the stop-loss at all.

I'd like to try and implement the Setting Priority for AtStop/AtLimit Orders, but am a novice programmer in Wealthlab.

I tried adding this line:

CODE:
Please log in to see this code.

But got an error saying the definition of SetTimeOfDayPriority doesn't exist.

I tried using Community.Components but it couldn't find it and I think the wiki says that's the legacy way of doing things anyways so it's better not to use that.

Do you know the code please that I'm supposed to add if Fidelity is my data provider? It said this:

CODE:
Please log in to see this code.


but I got the same error. I'm also using daily data so not sure how that will work.

Thank you

profile picture

Eugene

#4
Marc, you have to install the Community Components library and restart WLP for SetTimeOfDayPriority to be recognized.
profile picture

MustPlayOptions

#5
Thank you Eugene. I installed it and it's working now but not exactly sure what it's doing.

To just test it I commented out the Stop-loss and only left the at close and the limit. So it shouldn't have any effect but it does. It changes the number of trades.

What exactly is this.SetTimeOfDayPriority(5); doing please?

Thanks
profile picture

Eugene

#6
Please refer to the online user guide for such questions.
profile picture

MustPlayOptions

#7
Sorry
I wasn't clear. I know it's using a 5 minute bar but wasn't sure how that would change the win/loss rate for a buy at limit vs buy at close. The limit will always be reached during the day and the close is the last price so it doesn't make sense that it would change the number of wins/losses.

Anyways, thanks.
profile picture

Eugene

#8
There's no mystery. The SetTimeOfDayPriority method applies to entries which in your case is still AtLimit (and consequently cannot be realistically combined with AtStop/AtLimit exits for backtesting if not using the method). If you see Wealth-Lab saying "Trades not included due to insufficient simulated capital" > 0 on the Trades tab of your backtest, you know there's a case when the Priority kicks in and there's some work to be done by SetTimeOfDayPriority.
profile picture

Cone

#9
The first thing I see is incorrect syntax (it's impossible that your script can even execute):

CODE:
Please log in to see this code.


Next, the order of signal precedence is incorrect - you should execute stop orders before limit orders for a more pessimistic (probably more realistic) backtest. Furthermore, pos is not a Position object, it's an integer. You need to get a reference to the Position object.
CODE:
Please log in to see this code.


After you make these corrections, I think it would behoove you to post your code so that we can help you with more details like these.
profile picture

MustPlayOptions

#10
Thank you both. I fixed the syntax errors and with the SellAtStop first, as you suggested, the performance got a lot worse. I just took it out and it did better, assuming that the limit gets reached, so now it's just the:

CODE:
Please log in to see this code.


I appreciate the help as always.
profile picture

superticker

#11
And you need the -1 after ActivePositions.Count; otherwise, you're out of bounds on the ActivePositions collection.
CODE:
Please log in to see this code.

CODE:
Please log in to see this code.

I think if enough of the code was actually posted so we could actually compile and run it for "real", silly mistakes like this wouldn't be happening.

Also, it's not clear what the intent of all_true is in the code. Are the stop and limit orders intended to be mutually exclusive to the all_true condition or not? And why not declare all_true as Boolean rather than integer? (all_true should work okay declared as integer, but the code would execute slightly faster as a Boolean flag since you could skip the comparison test with zero.)
profile picture

MustPlayOptions

#12
I apologize because I was posting the pseudocode for a strategy and obviously didn't do a very good job of it.

I am not anywhere near an expert programmer and it was just as easy to make it 1/0 as True/False and I had no idea that one would be faster than the other. Thanks for pointing that out. I can switch it easily enough.

The reason I started down this path is because I can't find a good way to actually combine strategies into 1. I have 2-3 strategies I wanted to try and combine (not just run independently). I.e. I wanted to do the following for each symbol:

1) For each sub-strategy determine the buy/sell alerts.

2) Go through them and figure out the most conservative and use that. E.g. if a limit and atmarket alert to buy the same stock are both present then use the limit. This part isn't in the pseudocode above - I was starting "simple".

3) Consider limiting buys to only those with multiple strategies saying buy and don't sell the stock at the close if the buy signal is still present (but it's ok to sell if stop or limit is reached), thus the all_true checks.

The sell limit and sell at stop actually compiled without errors but it was obviously wrong. Thanks Cone for pointing out that prob.

Anyways, this is a whole other discussion and I'm trying to figure as much as I can out myself but appreciate all the help when I get stuck. I haven't posted the actual code because I haven't actually combined multiple strategies yet. I did get the one I was trying to start with working though with the activepositions loop rather than just a lastposition thanks to the help above.

After running a correlation check I'm not convinced I want to combine the strategies I was looking at as much now so may hold off on this until I find another strategy I'm comfortable actually investing with that's less correlated. I may still try to combine them to see if that improves the performance, we'll see.

Thanks
profile picture

Eugene

#13
QUOTE:
Anyways, this is a whole other discussion

That's right. A new forum thread would be optimal.
profile picture

superticker

#14
QUOTE:
a ... way to actually combine strategies into one.
I have a strategy with multiple entry points and multiple exit points with the signal message annotating each of them uniquely. But some comments:

1) All the entry and exit points are based on the same primary metrics. (I trade against the decorrelated line.)

2) Entry and exit points are mutually exclusive to each other because I only allow one position for each stock.

What you want to avoid is creating "multiple" positions controlled by highly related entry or exit points. You might be able to get that working if you have a highly defined pecking order so one entry point doesn't conflict with another's criteria. But I would be very careful.

Building strategies is a little like building linear models. Each term in the linear model must be orthogonal to all others; otherwise, it becomes impossible to uniquely solve for independent parameters for each term in the model. We say the system matrix goes "singular" if terms are too highly correlated. For example, to uniquely define a plane you need three points so you have three equations and three unknowns. But if the three points all fall on the same line (i.e. the three equations are not linearly independent), then you can no longer uniquely solve for the plane; your system matrix goes singular.

Wealth-Lab has a Performance Visualizer to test the independence of combined strategies so you can remove ones that are highly correlated to improve stability.
profile picture

MustPlayOptions

#15
Thank you for the comments superticker.
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).