Why aren't all positions closed out?
Author: daivd_leake
Creation Date: 11/25/2015 3:59 AM
profile picture

daivd_leake

#1
Hello,

I am considering purchasing WL but having had issues with exact backtesting logic in the past (e.g., with Multicharts) I am doing additional checks on WL against some separate backtesting code I have

I'm trying to understand why this simple strategy doesnt close out all its positions in WL, example code is below. It is simply buying / covering at a lower band and selling / shorting an upper band, it is almost always in the market.

I've tried a bunch of ways of coding this -- how can I ensure all positions are closed out before opening a new position?

Thanks
David

CODE:
Please log in to see this code.

profile picture

Eugene

#2
Hi David,

Because it's programmed to open multiple positions despite the single-position template (inadvertently?) To fix it:

A. If you intend to open multiple positions, check out see the WealthScript Programming Guide (Help menu) > Programming Trading Strategies > Multi-Position Strategies > MP Strategy Template on how to adjust the strategy.

B. If it's a single position strategy, make sure only one is being opened:

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

Cone

#3
Expanding the explanation...
In Eugene's code, he gave priority to a long position for the backtest because the BuyAtLimit signal is tested first. Only if it fails (returns null) then ShortAtLimit is tested. If you test both signals (as in your original code), it's possible to open both a long and short position on the same bar.

In live trading, when the last bar is processed, both signals will be executed and generate both a BuyAtLimit Alert and ShortAtLimit Alert. Alerts occur when the code attempts to evaluate a trading signal on a bar that does not exist, i.e., a future bar. Consequently, in live trading there is no priority; the signal whose trigger price is first attained would be executed by the broker. Depending on the broker integration, the other signal may have to be canceled manually in order not to open a second position on the same bar.
profile picture

daivd_leake

#4
Eugene, Cone, thanks very much you for taking the time to reply here

I have run this now and WL aligns exactly with my other code

My only other question is around filtering these trades using this single position logic

Could you give me a pointer on how I would filter entries and exits like this?

So, for example:

If condition_a BuyAtLimit
If condition_b ShortAtLimit
If condition_c SellAtLimit
If condition_d CoverAtLimit

Thanks again

Best
David
profile picture

Eugene

#5
For example:

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

daivd_leake

#6
Thanks - so seems straight-forward if you are happy to have all conditions mutually exclusive.

But wondering how you would code for the possibility of i) the entry condition_a and condition_b both co-occuring on a given bar, and
ii) wanting this to be a single position strategy?

Presumably this needs a last order object to test whether the conditionalised BuyAtLimit is null? Not sure if this is possible

Appreciate your input (last question, promise!)
profile picture

daivd_leake

#7
I guess the answer to my own question (!) is one just uses if else statements to assign priority to entry conditions

I think ive got it now! Thanks chaps

David
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).