Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Kalman Filter multiple Pairs Trading

Hi, I built an strategy based on someone else post, which applies Kalman Filter to calculate the hedge ratio, but there is one problem of my algorithm. Also, I include more than one pair in my portfolio.

I manage to keep the leverage of my portfolio under 1, so I use the function computeHoldingsPct(yShares, xShares, yPrice, xPrice) and for each pair I keep the percentage as y_target_pct / float(context.num_pairs), but why my leverage still go over 1 all the time?

Thanks

6 responses

Ryan,

I'm not 100% on how that is happening, but I think it is some interaction of order_target and order_target_percent. If you reorder your logic so that you only every make one call to order_target_percent per security, per day, then that would potentially solve the problem.

If you replace lines 88-89 and 99-100 with...

                    qty_y=int((y_target_pct / float(context.num_pairs))*context.portfolio.portfolio_value/data[stock_y].price)  
                    qty_x=int((x_target_pct / float(context.num_pairs))*context.portfolio.portfolio_value/data[stock_x].price)  
                    order(stock_y, qty_y)  
                    order(stock_x, qty_x)  

...does it work like you would expect?

You may want to examine the following.

Clone the backtest and look at the position values and transactions details for the last day.

See log file:

2011-05-31 PRINT executing  
2011-05-31 PRINT Currently have: (0, 0) of (Equity(28320 [USO]), Equity(26807 [GLD]))  
2011-05-31 handle_data:107 INFO opening short  
2011-05-31 PRINT X: Ordered 0.0 of Equity(28320 [USO]) (TARGET qty=0.0)  
2011-05-31 PRINT Y: Ordered -0.5 of Equity(26807 [GLD]) (TARGET qty=-66.8717400027)  
2011-05-31 PRINT Currently have: (0, 0) of (Equity(14517 [EWC]), Equity(14516 [EWA]))  
2011-05-31 handle_data:107 INFO opening short  
2011-05-31 PRINT X: Ordered 0.0 of Equity(14517 [EWC]) (TARGET qty=0.0)  
2011-05-31 PRINT Y: Ordered -0.5 of Equity(14516 [EWA]) (TARGET qty=-376.081233546)  
2011-06-01 PRINT executing  
2011-06-01 PRINT Currently have: (0, -66) of (Equity(28320 [USO]), Equity(26807 [GLD]))  
2011-06-01 PRINT Currently have: (0, -376) of (Equity(14517 [EWC]), Equity(14516 [EWA]))  
2011-06-02 PRINT executing  
2011-06-02 PRINT Currently have: (0, -66) of (Equity(28320 [USO]), Equity(26807 [GLD]))  
2011-06-02 handle_data:82 INFO closing short: Equity(28320 [USO]),Equity(26807 [GLD])  
2011-06-02 handle_data:90 INFO opening long  
2011-06-02 PRINT X: Ordered -0.249924421497 of Equity(28320 [USO]) (TARGET qty=-127.141834195)  
2011-06-02 PRINT Y: Ordered 0.250075578503 of Equity(26807 [GLD]) (TARGET qty=33.803291719)  
2011-06-02 PRINT Currently have: (0, -376) of (Equity(14517 [EWC]), Equity(14516 [EWA]))  
2011-06-02 handle_data:82 INFO closing short: Equity(14517 [EWC]),Equity(14516 [EWA])  
2011-06-03 PRINT executing  
2011-06-03 PRINT Currently have: (-127, 99) of (Equity(28320 [USO]), Equity(26807 [GLD]))  
2011-06-03 handle_data:76 INFO closing long: Equity(28320 [USO]),Equity(26807 [GLD])  
2011-06-03 handle_data:107 INFO opening short  
2011-06-03 PRINT X: Ordered 0.249915937777 of Equity(28320 [USO]) (TARGET qty=127.676488314)  
2011-06-03 PRINT Y: Ordered -0.250084062223 of Equity(26807 [GLD]) (TARGET qty=-33.7519884182)  
2011-06-03 PRINT Currently have: (0, 0) of (Equity(14517 [EWC]), Equity(14516 [EWA]))  

Sorry for so many posts - I just remembered order_target_percent doesn't account for already submitted orders, so that's why.

How would you fix this then...i'm shooting a blank

"There's more than one way to do it"

@James, thanks, so what does the code below do exactly?

for sid in OrderTargetPct.keys():
order_target_percent(sid, OrderTargetPct[sid])