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
spy = qb.AddEquity("SPY",Resolution.Daily)
ibm = qb.AddEquity("IBM",Resolution.Daily)

for slice in qb.History(10):
    print(type(slice))
    print(slice.Time)
    for symbol in qb.Securities:
        print(f"{symbol.Value} : {slice[str(symbol.Value)].Price}")
<class 'QuantConnect.Data.Slice'>
2019-04-11 00:00:00
IBM : 143.03
SPY : 288.29
<class 'QuantConnect.Data.Slice'>
2019-04-12 00:00:00
IBM : 143.77
SPY : 288.23
<class 'QuantConnect.Data.Slice'>
2019-04-13 00:00:00
IBM : 144.35
SPY : 290.19
<class 'QuantConnect.Data.Slice'>
2019-04-16 00:00:00
IBM : 143.92
SPY : 289.99
<class 'QuantConnect.Data.Slice'>
2019-04-17 00:00:00
IBM : 145.13
SPY : 290.18
<class 'QuantConnect.Data.Slice'>
2019-04-18 00:00:00
IBM : 139.08
SPY : 289.48
<class 'QuantConnect.Data.Slice'>
2019-04-19 00:00:00
IBM : 140.33
SPY : 290.02
<class 'QuantConnect.Data.Slice'>
2019-04-23 00:00:00
IBM : 138.87
SPY : 290.31
<class 'QuantConnect.Data.Slice'>
2019-04-24 00:00:00
IBM : 140.41
SPY : 292.85
<class 'QuantConnect.Data.Slice'>
2019-04-25 00:00:00
IBM : 139.98
SPY : 292.23

Historical Data Requests¶

We can use the QuantConnect API to make Historical Data Requests. The data will be presented as multi-index pandas.DataFrame where the first index is the Symbol.

For more information, please follow the link.

In [2]:
qb.History(["SPY","IBM"],10)
Out[2]:
close high low open volume
symbol time
SPY 2019-04-11 288.29 288.39 287.310 287.77 29398619.0
2019-04-12 288.23 288.84 287.570 288.83 30768949.0
2019-04-13 290.19 290.47 289.260 290.00 32705751.0
2019-04-16 289.99 290.35 289.080 290.24 26751109.0
2019-04-17 290.18 291.01 289.500 290.95 29505916.0
2019-04-18 289.48 291.42 288.990 291.40 33799989.0
2019-04-19 290.02 290.32 288.660 290.10 31674392.0
2019-04-23 290.31 290.43 289.070 289.17 20943744.0
2019-04-24 292.85 293.14 290.420 290.68 30157965.0
2019-04-25 292.23 293.16 292.070 292.77 25926948.0
IBM 2019-04-11 143.03 143.49 142.000 142.10 1365238.0
2019-04-12 143.77 144.11 143.080 143.76 1591750.0
2019-04-13 144.35 144.44 143.690 144.22 1525126.0
2019-04-16 143.92 144.50 143.269 144.47 1609330.0
2019-04-17 145.13 145.39 144.020 144.27 3308616.0
2019-04-18 139.08 141.98 136.270 137.37 6605559.0
2019-04-19 140.33 140.49 138.720 139.01 2416039.0
2019-04-23 138.87 140.59 138.480 139.27 2118827.0
2019-04-24 140.41 140.92 138.940 138.95 1894843.0
2019-04-25 139.98 141.29 139.810 140.60 1231415.0