Adding a hedge to DrKoch's Basic HiLoLimit
Author: cmatthews
Creation Date: 2/22/2010 1:52 PM
profile picture

cmatthews

#1
DrKoch's Basic HiLoLimit strategy is one of best in class in terms of tradeable dip-buyers from what i can tell. Of course one of the problems with dip-buyers in general is the "1987" style crash where there is no time to get out of or hedge trades, and no short term rebounds before "the big one".

To this end, i'd like to add the ability to add a market hedge in the form of SPY ETF such that every long taken in hedged at end of day with $ neutral SPY. I suspect that somewhere around 1/2 the gross profit will be sacrificed, but should allow for the strategy to be more comfortably levered. I tried adding setcontext code to the Bandtrader just below the BuyAtLimit code as per below, but I am clearly misunderstanding the way setcontext works since this results in many SPY trades being taken independently of the primary symbol trades. I presume i need to identify WHEN the primary symbol has traded to then code the system to short the SPY hedge, but i am unclear as to how to "link" this execution.

CODE:
Please log in to see this code.

profile picture

Eugene

#2
Hello,

For future reference: each downloadable Strategy has its own discussion forum. This one is no exception, and its forum has been already populated. Now, how to get there: go to "Public strategies", find a Strategy, note a link at the right that says "N topics" ("Discussions" column), click on it, and you're in the right place:

BasicHiLoLimit discussions

Please discuss downloaded strategies in their forums. tia.


QUOTE:
but I am clearly misunderstanding the way setcontext works since this results in many SPY trades being taken independently of the primary symbol trades.

You're taking a hedge trade without checking first if the BuyAtLimit has executed or not.
profile picture

cmatthews

#3
ok - so as a non-programmer i've looked through the code of the various strategies that use SetContext (Rotation and Pair Trading strategies) and have tried various combinations of LastPosition, IsLastPositionActive, EntrySignal, etc, in an effort to identify when the BuyAtLimit has executed so i can in turn take the hedge....all to no avail. Is this a relatively easy piece of code? If so would anyone be so kind as to give me a hint?

much thanks in advance...
profile picture

Eugene

#4
Although I haven't yet tried creating a hedge for HiLoLimit, my understanding is that in a multi-symbol, portfolio simulation mode backtest, those dip buyers typically have tons of trades skipped due to insufficient funds. Please see Strategy Window > Backtesting Strategies > 100% of Equity Sizing > Basis price in the WLP User Guide. So, when a BuyAtLimit order was skipped due to capital constraints, an AtClose order could've slipped in, resulting "in many SPY trades being taken independently of the primary symbol trades". So, my idea was to try this:
CODE:
Please log in to see this code.

profile picture

ss161

#5
I agree with Eugene's comment. In addition, when you go to exit the trade, you have to know which of your hedge trades to cover along with the exit. I'm sure many ways exist to do it, I create a collection object to hold both trades (I call it "PairPosition"), that way you always keep together both trades. I posted the object in another current post on PairTrading.
profile picture

cmatthews

#6
thank you Eugene that is helpful. And thank you ss161 you are correct of course - the code as added does help initiate the first "leg" of the pair, but the hedge trade is exiting independently as opposed to being tied to the long position. Also it appears that the initial long entry is entered more than once on the same day at the same price without a corresponding hedge - not sure why.

In practice you would probably not actually execute the pairs like this, rather you would "group together" all the longs you have taken less the sells made during the day and execute one "net hedge" at the close (ie just hedge up your net dollar exposure either shorting or covering a larger SPY position). You *could* execute hedge trades in real time each time a long is entered or exited, but for backtesting purposes with EOD data the single hedge adjustment at end of day would be sufficient (and cheaper on commissions of course). Maybe this is a better / easier approach to code? I still suspect that in the long run the approach would actually improve sharpe ratio over the long-only dipbuyer - particularly in 2008-style or 1987-style markets.

I took a quick look at the "PairPosition" logic but suspect it is beyond my very limited ability to code but thank you i will try it as well.
profile picture

Eugene

#7
QUOTE:
Also it appears that the initial long entry is entered more than once on the same day at the same price without a corresponding hedge - not sure why.

It's because the strategy is coded to do just that: taking multiple position entries, and then closing them out, looping by ActivePosition. You should try the single positon logic (i.e. using IsLastPositionActive) instead.
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).