QuantConnect Logo

Welcome to The QuantConnect Research Page¶

Refer to this page for documentation https://www.quantconnect.com/docs#Introduction-to-Jupyter¶

Contribute to this template file https://github.com/QuantConnect/Lean/blob/master/Jupyter/BasicQuantBookTemplate.ipynb¶

QuantBook Basics¶

Start QuantBook¶

  • Add the references and imports
  • Create a QuantBook instance
In [1]:
%matplotlib inline
# Imports
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Jupyter")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Market import TradeBar, QuoteBar
from QuantConnect.Jupyter import *
from QuantConnect.Indicators import *
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import pandas as pd

# Create an instance
qb = QuantBook()

# Select asset data
goog = qb.AddOption("GOOG")
goog.SetFilter(-2, 2, timedelta(0), timedelta(180))

option_history = qb.GetOptionHistory(goog.Symbol, datetime(2017, 1, 11, 10, 10), datetime(2017, 1, 11, 10, 15))
data = option_history.GetAllData()
print(type(data))
data.head(5)
<class 'pandas.core.frame.DataFrame'>
Out[1]:
askclose askhigh asklow askopen asksize bidclose bidhigh bidlow bidopen bidsize close high low open openinterest volume
expiry strike type symbol time
2017-01-20 00:00:00 800.0 Call GOOG 170120C00800000 2017-01-11 10:11:00 9.3 9.3 9.2 9.2 10.0 8.9 8.9 8.6 8.6 19.0 9.1 9.10 8.90 8.90 NaN NaN
2017-01-11 10:12:00 9.3 9.3 9.3 9.3 10.0 8.9 8.9 8.9 8.9 12.0 9.1 9.10 9.10 9.10 NaN NaN
2017-01-11 10:13:00 9.3 9.3 9.3 9.3 10.0 8.9 8.9 8.9 8.9 37.0 9.1 9.10 9.10 9.10 NaN NaN
2017-01-11 10:14:00 9.3 9.3 9.3 9.3 10.0 8.7 9.0 8.6 9.0 28.0 9.0 9.15 8.95 9.15 NaN NaN
2017-01-11 10:15:00 9.3 9.3 9.3 9.3 10.0 8.7 8.7 8.7 8.7 29.0 9.0 9.00 9.00 9.00 NaN NaN
In [2]:
data = data.sort_values(by=['askclose'])
data.head(5)
Out[2]:
askclose askhigh asklow askopen asksize bidclose bidhigh bidlow bidopen bidsize close high low open openinterest volume
expiry strike type symbol time
2017-01-20 00:00:00 812.5 Call GOOG 170120C00812500 2017-01-11 11:31:00 3.3 3.6 3.3 3.6 91.0 2.85 3.00 2.65 3.00 5.0 3.075 3.300 2.975 3.300 NaN NaN
2017-01-11 11:32:00 3.3 3.3 3.3 3.3 23.0 2.85 2.85 2.85 2.85 65.0 3.075 3.075 3.075 3.075 NaN NaN
2017-01-11 11:33:00 3.3 3.3 3.3 3.3 57.0 2.90 2.90 2.85 2.85 14.0 3.100 3.100 3.075 3.075 NaN NaN
2017-01-12 10:10:00 3.4 3.4 3.4 3.4 78.0 2.90 2.90 2.80 2.80 16.0 4.590 4.590 4.590 4.590 89.0 0.0
2017-01-12 10:09:00 3.4 3.6 3.4 3.6 83.0 2.80 2.90 2.80 2.90 19.0 4.590 4.590 4.590 4.590 89.0 0.0