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()

syl = qb.AddEquity("SPY", Resolution.Minute).Symbol
df_price = qb.History(syl, datetime(2018,11,1), datetime(2018,11,8), Resolution.Minute).loc[syl.Value]['close']

minutes=len(df_price)
ema = qb.Indicator(ExponentialMovingAverage(750), syl, minutes, Resolution.Minute)
print(type(ema))
close = qb.Indicator(SimpleMovingAverage(1), syl, minutes, Resolution.Minute)
for i in range(5):
    print(f'{ema.index[i]}: {close["simplemovingaverage"][i]/ema["exponentialmovingaverage"][i]}')
<class 'pandas.core.frame.DataFrame'>
2019-05-14 15:30:00: 0.9985924920044988
2019-05-14 15:31:00: 0.9989911132468985
2019-05-14 15:32:00: 0.9978895725572325
2019-05-14 15:33:00: 0.9983067245044367
2019-05-14 15:34:00: 0.9990785343833104