Back

QC-IB Webinar: Pairs Trading with Python

Hey All!

I did a non-technical introduction to pairs trading today with Interactive Brokers. I always felt most pairs trading tutorials dived straight into the numbers so I wanted to make this one as simple as possible. You can see the Notebook and Backtest we used attached here. The slides are available in PDF format on Slide Share: 

https://www.slideshare.net/quantconnect/quantconnect-introduction-to-pairs-trading
Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



IB has shared the video for this webinar; you can see it here:

https://www.youtube.com/watch?v=cZFqYJmQoTM
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Very Nice Jared!

1

Hi there, I'm new to quantconnect.  I really enjoyed the video and wanted to play around with the notebook.  I have cloned the notebook and tried to run it but it keeps giving me an error in the cell with the following code.

syls = ["XOM","CVX"]
qb.AddEquity(syls[0])
qb.AddEquity(syls[1])
start = datetime(2003,1,1)
end = datetime(2009,1,1)

x = qb.History(syls[0], start, end, Resolution.Daily).loc[syls[0]]['close']
y = qb.History(syls[1], start, end, Resolution.Daily).loc[syls[1]]['close']

when assigning the history to x I get an attribute error no attribute loc because qb.History is not returning a Pandas Dataframe.

I tried the following to see what type was being returned by History

syls = ["XOM","CVX"]
qb.AddEquity(syls[0])
qb.AddEquity(syls[1])
start = datetime(2003,1,1)
end = datetime(2009,1,1)

x = qb.History(syls[0], 10, Resolution.Daily)

type(x)

 

and the type below is returned

QuantConnect.Util.1, Culture=neutral, PublicKeyToken=null]]

I wasnt able to find the api documentation to try and determine what QuantConnect.Util.1 can anyone point me in the correct direction, or let me know if I'm making a complete Noob mistake.

 

Thanks

1

Hi Brandon,

The research notebook is supporting Python3 now. We've modified this file. You just need to add "[ ]" to symbol list when requesting the history price.

https://github.com/QuantConnect/Research/blob/master/Notebooks/05%20Pairs%20Trading%20Strategy%20Based%20on%20Cointegration.ipynb
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thanks, that did it!

0

I have done automated trading before, and have a lot of pair trading experience, but only on Multicharts, Tradestation, and Quantopian, so I am a little unclear how your live trading code works. I cloned your notebook and upgraded to the $20/month Quantconnect account, and then as a test ran your strategy live in my IB account (it is Sunday so there were no trades) and then turned it off.

Everything looks good, the only thing I don't understand is how it knows how much money to trade. If I have $30,000 in my IB acccount (all cash, no stock holdings) and your notebook has the setting self.SetCash(10000) I assume that means it would do a $10,000 trade. But for pair trading, does that mean it would trade $10,000 of each stock in the pair, or $5,000 of each stock for a total of $10,000?

0

Never mind, I see now from the backtest. It trades 100 shares of each.

0

Thank you for sharing. Is " max.lag = 1 " in the ADF test a restrictive assumption? Will it be different if we choose another value?

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed