Add Hedging with SPY to Strategy
Author: trader rog
Creation Date: 1/13/2012 9:43 AM
profile picture

trader rog

#1
I'm looking to do a 50% hedge on positions using the SPY that are executed at the same time as a trade. The hedge does not need to be paired to a certain position because all trades are equal dollar value. I use 20 positions max for the strategy (10 positions and 10 hedges), so I have the PosSizer setting for 2 positions max per day (the trade and the hedge), and 6.7 Pct of Equity (6.7*10 positions = 67% + 33% for hedges) so all trades would consume roughly 100% of equity. How can I incorporate the hedge into the strategy below?

CODE:
Please log in to see this code.


profile picture

Eugene

#2
For simplicity maybe you need just a single hedge Position in SPY with a variable size?
profile picture

trader rog

#3
That would be perfect.
profile picture

Eugene

#4
Oops, I was wrong thinking that a custom PosSizer could've help accomplish this hedging with SPY. That won't work unless you enter all those positions at the very same time (bar).

I believe you need to go your original direction and settle with entering a hedge trade after the primary trade has been created. In other words (pseudocode):
CODE:
Please log in to see this code.


Not exact but close:

ActiveTrader 2011-05 | Inverse ETF switching system
Pairs Trading

You'll find more pointers by searching the forums and the QuickRef.
profile picture

trader rog

#5
Thank you for your reply Eugene. I have 2 issues that I can't seem to figure out. 1) How to adjust position size for the hedge to 1/2 that of other positions on the entry and exit. Am I on the right track with Tag? If a position is exited, I would like only the portion of the hedge associated with the position exited, not the whole hedge. 2) How can I get the exit code to ignore the hedge so that exits are only triggered by positions? If the following line ignored the hedge, I believe it would work.

CODE:
Please log in to see this code.


This is the entry block:

CODE:
Please log in to see this code.


And the exit block:

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

Eugene

#6
QUOTE:
Am I on the right track with Tag?

If only I knew where you are heading. ;) I guess you're assigning that percentage to drive the Position Options PosSizer's "Use .Tag" option, right? In this case, that might do the trick.
QUOTE:
2) How can I get the exit code to ignore the hedge

Why would you want to ignore the hedge?
profile picture

trader rog

#7
Yes I was trying to use .Tag to drive the PosSizer. Is that the best way if the hedge will be half the equity of positions being hedged?

I only want the exit trigger to ingore the hedge. This way only positions trigger exits followed by the partial hedge automatically being covered.
profile picture

Eugene

#8
1 - That's a pretty straightforward way.
2 - Still doesn't ring a bell to me.
profile picture

trader rog

#9
I'm looking to exit the long positions based on this line. I do not want the short spy hedge to be exited on this line though. So, only the long positions trigger an exit, then and only then will the hedge be covered.

CODE:
Please log in to see this code.


profile picture

Eugene

#10
All positions of a kind (e.g. all long positions) could be exited with the neat AllPositions shortcut:
CODE:
Please log in to see this code.
profile picture

trader rog

#11
Thank you Eugene. How to handle closing out the hedge for long positions?

The strategy has 10 possible long positions, and I would like to close out the spy hedge as the long positions are exited. For example, if there are 10 long positions but only 5 of them are exited at the end of the day, then only the part of the hedge associated with those 5 positions should be covered.
profile picture

Eugene

#12
To tell one kind of Position from another, have you tried using the Position.Tag, Position.Bars.Symbol, Position.EntrySignal properties?
profile picture

trader rog

#13
I have not tried using those properties. Can you please point me in the right direction as to how to structure the code using these properties? Would it be possible to include a hedge in a future PosSizer update?

Also, would it be easier to exit the hedge based on dollar value instead of tagging positions? So, for example, if at the end of the day the total long positions is $100,000, just keep $50,000 of the hedge?
profile picture

Eugene

#14
QUOTE:
Can you please point me in the right direction as to how to structure the code using these properties?

Please look their examples in the QuickRef.
QUOTE:
Would it be possible to include a hedge in a future PosSizer update?

I already rejected this idea earlier as incorrect/inappropriate.
QUOTE:
Also, would it be easier to exit the hedge based on dollar value instead of tagging positions?

Whatever works for you is fine with me. ;)
profile picture

trader rog

#15
I decided it would make more sense to exit the hedge based on dollar value as this is how it would be used in practice. It looks like the size and position type properties will do the trick but I wasn't able to figure it out using the QuickRef examples and didn't find anything about this on the community board. I would like to structure the hedge based on the dollar value of remaining open positions at the end of the day. As an example, if there are 10 positions and 3 of them are exited at the end of the day, then the hedge is rebalanced based on the dollar value of the 7 remaining positions.
profile picture

trader rog

#16
Does someone know how to figure out a solution to the post above about hedging based on the dollar value of remaining open positions? This thread Multiple Hedge Trades seems to be most closely related to what I'm trying to do but I didn't find a workable solution. Please help!
profile picture

trader rog

#17
I haven't been able to figure out the dollar based hedge so have opted to do a positions based hedge instead. The goal is to have the # of long positions always equal the # of short positions (in this case the spy hedge). There are a few things I can't figure out with the code below:

1) The long trades are never exited and the short trades are always exited the next day. They should be synched and occur simultaneously.
2) There are many more short trades than long trades. There should be an equal number instead.
3) The equity is 6.7% for all trades. It should be 6.7% for long trades only and 3.3% for short trades.

Those are the main errors I am seeing in the results. Can someone please shed some light on what is wrong with the coding? It would be much appreciated.

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

Eugene

#18
1. It will not work as expected because you're opening multiple positions in single Position strategy template.
2. The equity percentage will be incorrect because for hedge positions, Position.Tag it's not assigned.
profile picture

trader rog

#19
Thank you for your reply.
1. When you say multiple positions in single position strategy template, do you mean longs and shorts? I currently use a similar template to this for just long and it works fine.
2. I thought this assigned the hedge:

CODE:
Please log in to see this code.


What am I missing?
profile picture

Eugene

#20
QUOTE:
What am I missing?

I don't see a conditional .Tag assignment after creating the short position.
profile picture

trader rog

#21
You lost me a little bit there. I'm not a programmer so this stuff can get confusing. Can you elaborate on your previous answer? Also, can you please give me your thoughts on the 1st point? Thank you Eugene.
profile picture

Eugene

#22
QUOTE:
Can you elaborate on your previous answer?

Here's what I meant:
CODE:
Please log in to see this code.

QUOTE:
Also, can you please give me your thoughts on the 1st point?

See Programming Trading Strategies > Multi-Position Strategies in the WealthScript Programming Guide.
profile picture

trader rog

#23
I got the Tag working correctly, but can't seem to get the exits right. I tried using the multi-position strategy template but the shorts never get covered. Can you please take a peek and give me a pointer as to what I'm doing wrong?

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

Eugene

#24
I took a quick peek and in this snippet, issues originate from your usage of brackets:

1. the check for PositionType == .Short was put inside the long exit block (never going to close any short trade by definition),
2. the activeLongs counter was decrementing even if the exit condition wasn't satisfied.

Without attempting to analyze the logic of your code, here's a revised version:
CODE:
Please log in to see this code.
profile picture

trader rog

#25
Thank you Eugene. My code is almost there. I am still trying to figure out how to cover the short trades simultaneously with the long trades. I inserted a couple lines of code into what you did above but am not sure how to apply this check to only the long positions so a short position is only covered if a long position is sold. Any suggestions?

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

Cone

#26
The script holds long and short positions in the same security? What's the point?

You're not exiting 'a' (1) Position, you're exiting 'AllPositions'. Is that the intention?

If so, get rid of the little block to Cover short Positions and instead just change SellAtClose to ExitAtClose to exit AllPositions, short or long.

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

trader rog

#27
Thank you for your reply Cone. It holds longs from a DataSet (which doesn't include SPY) and shorts only SPY (as a hedge).

My intention is to exit 1 position at a time. In a nutshell, I'm looking to hedge the long positions with SPY, so if a long is entered, a short in SPY is immediately entered, and when the long is sold, the short SPY is immediately covered.
profile picture

Cone

#28
This logic doesn't even come close then. Remember, the exit logic loops over the Positions that contain your long and hedge positions. You may sell a long position but already have iterated past all the hedge positions that you need to cover.

You have to think about these things logically. You can do it a dozen different ways, but almost certainly not with one loop. Does it matter which hedge you cover after exiting a long? If not, then forget about exiting the hedges and just count the number of longs exited the first time you loop through ActivePositions. Then, loop again, processing the short positions this time and exit the number of hedges required. If it matters which hedge you exit, then you need to tag Positions to exit the right one, but the logic could be similar.
profile picture

trader rog

#29
Thank you Cone for pointing out that I'll need more than one loop because I've been trying to do this within one loop and haven't had any luck at all.

It does not matter which hedge is covered after exiting a long, as long as it's consistent. Counting the number of longs exited then looping through the short positions would be perfect. I apologize for taking your time, but I'm not sure how to write the code to do this.
profile picture

Cone

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

trader rog

#31
Thank you very much Cone! The exits work as expected now.

I have 2 small issues with my entries after which this strategy will be complete:

1) Tag is inconsistent for the shorts. Sometimes the equity size is 6.7% and sometimes 3.3%. It should always be 3.3%.

2) A few shorts are entered without a corresponding long being entered first. When this happens, that short is never exited. Of course shorts should only be entered after a corresponding long is entered.

Can you please take a peek and see if any errors stand out to you? Thanks again for all your help.

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

Cone

#32
1. It's not realistic to use RSI priority based on an indicator for a Stop/Limit system. click here for the realistic options.

2. The short entry is not conditional to the long entry, so it will always be entered whether or not the BuyAtStop order triggers.

3. Re: 6.7%.. it's because of #2.. when the long is not entered, LastPosition.Tag = 6.7; is executed, but the LastPosition was a short.

This should fix 2 & 3.. #1 is up to you.

CODE:
Please log in to see this code.
Note that now it's not possible to enter a short without a corresponding long, however in a Portfolio Simulation it's possible that you enter a long position but not have enough buying power to enter the short.
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).