How to trade an index outside my dataset?
Author: wbzhang
Creation Date: 5/12/2011 11:28 PM
profile picture

wbzhang

#1
I want to design some market-neutral strategies. For example, I create a dataset with 100 stocks, and I pick a stock to long. Simultaneously, I want to short market index "^GSPC" to make the portfolio market-neutral.

I tried:

SetContext("^GSPC", true);
ShortAtMarket(bar+1);
....
RestoreContext();

The trade is not successful. Please note "^GSPC" is an external symbol. How to trade the symbol correctly? thanks..
profile picture

wbzhang

#2
I tried it again, now it works... interesting.
profile picture

Eugene

#3
What's really interesting is how do you determine that "the trade is not successful". There's no clue.
profile picture

wbzhang

#4
I still have problems here.

I want to long stock and short index (^RUA) simultaneously to make the portfolio market neutral:
If condition 2 is satisfied, I will buy stock and short index;
if condition 1 is satisfied, I will sell stock and cover index.

However, after simulation, the index "short-cover" pairs are correct, but all the bought stocks are not sold (they keep open in the end). What's the reason? You know, actually I hope both individual stocks and index should exit at the same time.

Here is my code segmentation:
CODE:
Please log in to see this code.


The result trades are:

QUOTE:
Position Symbol Quantity Entry Date Entry Price Exit Date Exit Price Profit % Profit $ Bars Held Profit per Bar Entry Name Exit Name MAE % MFE %
Long AAPL 50 2009-10-21 199.52 Open Open 75.08 $7,489.50 342 $21.90 just buy -6.99 82.89
Short ^RUA 15 2009-10-21 638.64 2009-10-23 631.22 1.16 $111.30 3 $37.10 short index just sell -1.01 1.64
Long AAPL 49 2009-10-27 201.66 Open Open 73.22 $7,234.85 338 $21.40 just buy -7.98 80.95
Short ^RUA 16 2009-10-27 623.69 2009-11-04 609.16 2.33 $232.48 7 $33.21 short index just sell -0.47 4.03
Long AAPL 49 2009-11-10 201.02 Open Open 73.77 $7,266.21 328 $22.15 just buy -6.14 81.52
Short ^RUA 15 2009-11-10 636.65 2009-11-12 632.43 0.66 $63.30 3 $21.10 short index just sell -1.15 0.85
Long AAPL 50 2010-01-25 202.51 Open Open 72.49 $7,340.00 278 $26.40 just buy -6.05 80.19
Short ^RUA 15 2010-01-25 640.74 2010-04-15 716.36 -11.80 ($1,134.30) 57 ($19.90) short index just sell -12.02 4.62
Long AAPL 44 2010-04-21 258.80 Open Open 34.97 $3,982.44 218 $18.27 just buy -23.01 41.00
Short ^RUA 15 2010-04-21 713.61 2010-05-07 655.02 8.21 $878.85 13 $67.60 short index just sell -1.40 11.67
Long AAPL 43 2010-05-19 249.50 Open Open 40.00 $4,291.83 198 $21.68 just buy -7.27 46.25
Short ^RUA 16 2010-05-19 663.69 2010-06-02 650.34 2.01 $213.60 10 $21.36 short index just sell -0.34 7.40
Long AAPL 42 2010-06-29 264.12 Open Open 32.25 $3,577.98 170 $21.05 just buy -10.81 38.16
Short ^RUA 18 2010-06-29 634.53 2010-07-09 634.87 -0.05 ($6.12) 8 ($0.77) short index just sell -0.07 6.20
Long AAPL 43 2010-07-21 265.09 Open Open 31.77 $3,621.46 155 $23.36 just buy -11.14 37.65
Short ^RUA 17 2010-07-21 637.27 2010-08-04 664.27 -4.24 ($459.00) 11 ($41.73) short index just sell -4.34 1.69
Short ^RUA 16 2010-08-16 633.37 2010-08-20 630.25 0.49 $49.92 5 $9.98 short index just sell -2.13 1.31
Short ^RUA 17 2010-09-09 647.22 Open Open -20.30 ($2,233.46) 120 ($18.61) short index -23.95 0.00
profile picture

Eugene

#5
No wonder that positions are not being closed: the default exit logic pattern, applicable to single position systems, is not appropriate here where multiple positions are opened at the same time.

CODE:
Please log in to see this code.


Don't forget to execute in single symbol mode.

P.S. In entry rules, you could try this to avoid situations when the hedge position is being purchased but the primary position wasn't taken for whatever reason i.e. capital limitations:
CODE:
Please log in to see this code.
profile picture

wbzhang

#6
thanks Eugene, that solves my problem.

However, now I have significant performance problem. As you said, this is single symbol simulation. If I backtest the strategy with a universe (300 symbols, 2.5 years, daily time scale):

If there is no hedging, it will take only < 0.5 minutes to finish the simulation.
If I add hedging, it will take 10+ minutes.

The performance downgrade seems unreasonably dramatic, anything wrong here?

For your reference, here is the code:

CODE:
Please log in to see this code.


I just need to remove below sentence, the simulation could be finished in < 0.5 minute; otherwise, it takes 10+ minutes.
CODE:
Please log in to see this code.
profile picture

Eugene

#7
You might want to post the whole code, I can't see how this call could lead to a drastic perfomance decline.
profile picture

wbzhang

#8
Thanks! Here is the completed code. Actually it is quite simple:

CODE:
Please log in to see this code.


It spent a long time on "executing strategy..." phase.
If I remove the hedging part, "executing strategy" could be done in seconds.
profile picture

Eugene

#9
Runs pretty fast for me.

Please use this checklist to supply additional information.
profile picture

wbzhang

#10
Please note here my "^RUA" is an external symbol.
If I change "^RUA" to one of my internal symbols, the simulation would be as fast as usual.

I can move "^RUA" into an internal symbol, but that needs some dataset/code change. Could you think of any other idea to solve this problem?
profile picture

Eugene

#11
I realize that it's a symbol from a different DataSet but that's not enough.

Start with the data providers involved, symbols, data loading range... anything that helps us reproduce what you're seeing and what I can not reproduce.
profile picture

wbzhang

#12
Hi Eugene, thanks!

I found the problem here. Because "^RUA" is a symbol inside system pre-defined dataset "Indices", each access to "^RUA" will need data transfer over network I guess. Even I update "Indices" dataset, it still quite slow if "^RUA" is involved.

Now I solved this problem by below solution:
1. Export ^RUA to a local file, e.g., MY-RUA.csv
2. Create an ASCII dataset contain MY-RUA
3. Then I can trade MY-RUA instead of ^RUA, which guarantees all the data are in local.

Now the speed is as fast as usual. I think that's good enough for me.

thanks again. One thing I am curious is that, is that true each access to indices will need network data transfer?
For example, you can try to run strategies on system defined dataset "S&P 100", that will be extremely slow.
profile picture

Eugene

#13
Wenbin,

Glad you got it working.

Certainly, first thing I'd do when my static data is loading slowly is turn off On demand data update feature. This is the prominent case of accessing data over a network.

Also when using Yahoo data, turn off "Always return the partial bar" unless you're using it for live charting/trading.
profile picture

wbzhang

#14
That's correct. thanks!

One more problem is regarding hedging. For example, I buy some stock, and short index. Possibly the "buy" trade was executed successfully, but the hedging trade wasn't because of various reasons (insufficient capital, etc.). Is that possible to make the two trades as an atomic operation? If the hedging trade is not successful, I want to undo the previous trade.

That is, we want them either both executed, or both un-executed. Any solutions?

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

Eugene

#15
No, an already created Position can't be undone; especially because of insufficient capital, as position sizing in portfolio simulation context is an "overlay", a post-processing event.
profile picture

Cone

#16
There are no easy solutions for this one (until Eugene comes up with one). The same problem applies to pyramiding - say you enter 3 Positions in a script, but when the sizing is applied, it rejects the first (or second) entry. The simulation is happy using the last 2 trades, but in live trading, the logic would have been different had the first Position been rejected.

The solution probably needs to be in Wealth-Lab's backtesting logic. A new "trade group" Position property that functions as an "All or None" in the simulation might work.

That aside, the only other way I can think to avoid this problem is to using SetShareSize sizing and keep an estimate of cash and equity so that you can ensure that both trades will be successful. It's more complicated than it sounds though.
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).