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()
In [5]:
# This simple call has been running endlessly and still no results
ng = qb.AddFuture("NG")
df = qb.GetFutureHistory(ng.Symbol, datetime(2019, 2, 25), datetime(2019, 2, 26)).GetAllData()
In [6]:
subset=df.index.get_level_values(1).isin(["NG26H19","NG27J19"])
df[subset]
Out[6]:
askclose askhigh asklow askopen asksize bidclose bidhigh bidlow bidopen bidsize close high low open openinterest volume
expiry symbol time
2019-03-27 NG27J19 2019-02-25 00:01:00 2.767 2.770 2.767 2.769 5.0 2.764 2.768 2.760 2.766 3.0 2.768 2.768 2.768 2.768 NaN 11.0
2019-02-25 00:02:00 2.767 2.767 2.767 2.767 6.0 2.764 2.764 2.764 2.764 6.0 2.768 2.768 2.768 2.768 NaN 0.0
2019-02-25 00:03:00 2.767 2.767 2.766 2.766 8.0 2.764 2.764 2.764 2.764 3.0 2.768 2.768 2.768 2.768 NaN 0.0
2019-02-25 00:04:00 2.765 2.770 2.765 2.767 6.0 2.761 2.764 2.761 2.764 4.0 2.764 2.764 2.764 2.764 NaN 2.0
2019-02-25 00:05:00 2.765 2.767 2.765 2.765 7.0 2.761 2.762 2.761 2.761 4.0 2.761 2.761 2.761 2.761 NaN 1.0
2019-02-25 00:06:00 2.765 2.765 2.765 2.765 12.0 2.761 2.761 2.761 2.761 4.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:07:00 2.765 2.765 2.765 2.765 0.0 2.761 2.761 2.761 2.761 4.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:08:00 2.764 2.764 2.764 2.764 1.0 2.761 2.761 2.761 2.761 4.0 2.761 2.761 2.761 2.761 NaN 2.0
2019-02-25 00:09:00 2.765 2.765 2.764 2.765 7.0 2.761 2.761 2.761 2.761 2.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:10:00 2.765 2.765 2.765 2.765 7.0 2.761 2.761 2.761 2.761 3.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:11:00 2.762 2.765 2.761 2.765 9.0 2.760 2.761 2.760 2.761 5.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:12:00 2.762 2.762 2.761 2.762 8.0 2.760 2.761 2.760 2.761 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:13:00 2.762 2.762 2.762 2.762 6.0 2.760 2.761 2.760 2.761 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:14:00 2.762 2.762 2.762 2.762 5.0 2.760 2.761 2.760 2.761 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:15:00 2.763 2.763 2.762 2.762 3.0 2.760 2.760 2.760 2.760 5.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:16:00 2.763 2.764 2.763 2.763 1.0 2.760 2.760 2.760 2.760 5.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:17:00 2.763 2.764 2.763 2.764 1.0 2.760 2.760 2.760 2.760 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:18:00 2.763 2.764 2.763 2.764 1.0 2.760 2.760 2.760 2.760 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:19:00 2.763 2.764 2.762 2.764 3.0 2.760 2.760 2.760 2.760 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:20:00 2.762 2.763 2.762 2.762 2.0 2.760 2.760 2.760 2.760 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:21:00 2.763 2.763 2.762 2.762 2.0 2.760 2.760 2.760 2.760 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:22:00 2.763 2.763 2.762 2.762 2.0 2.760 2.760 2.760 2.760 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:23:00 2.763 2.764 2.761 2.764 3.0 2.760 2.760 2.760 2.760 0.0 2.761 2.761 2.761 2.761 NaN 0.0
2019-02-25 00:24:00 2.763 2.764 2.763 2.764 3.0 2.759 2.761 2.759 2.760 8.0 2.761 2.761 2.760 2.760 NaN 4.0
2019-02-25 00:25:00 2.762 2.763 2.761 2.763 1.0 2.759 2.761 2.759 2.760 0.0 2.761 2.761 2.760 2.760 NaN 0.0
2019-02-25 00:26:00 2.763 2.764 2.761 2.764 2.0 2.759 2.761 2.759 2.760 0.0 2.761 2.761 2.760 2.760 NaN 0.0
2019-02-25 00:27:00 2.763 2.764 2.762 2.762 3.0 2.759 2.760 2.759 2.760 3.0 2.760 2.760 2.760 2.760 NaN 3.0
2019-02-25 00:28:00 2.763 2.763 2.763 2.763 3.0 2.759 2.761 2.759 2.759 2.0 2.760 2.760 2.760 2.760 NaN 0.0
2019-02-25 00:29:00 2.763 2.763 2.763 2.763 2.0 2.759 2.759 2.759 2.759 8.0 2.760 2.760 2.760 2.760 NaN 0.0
2019-02-25 00:30:00 2.764 2.765 2.763 2.763 1.0 2.760 2.761 2.759 2.759 10.0 2.760 2.760 2.760 2.760 NaN 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2019-02-25 23:31:00 2.842 2.842 2.842 2.842 1.0 2.837 2.837 2.837 2.837 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:32:00 2.842 2.842 2.842 2.842 1.0 2.837 2.837 2.837 2.837 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:33:00 2.842 2.843 2.842 2.843 1.0 2.837 2.838 2.837 2.838 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:34:00 2.842 2.843 2.842 2.843 0.0 2.836 2.836 2.836 2.836 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:35:00 2.842 2.843 2.842 2.843 0.0 2.837 2.837 2.837 2.837 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:36:00 2.842 2.843 2.842 2.843 0.0 2.837 2.837 2.836 2.836 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:37:00 2.842 2.843 2.842 2.843 0.0 2.837 2.837 2.836 2.836 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:38:00 2.843 2.843 2.841 2.841 1.0 2.837 2.837 2.836 2.836 0.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:39:00 2.843 2.843 2.841 2.841 0.0 2.837 2.838 2.837 2.838 1.0 2.841 2.841 2.841 2.841 22964.0 0.0
2019-02-25 23:40:00 2.842 2.842 2.842 2.842 1.0 2.837 2.838 2.837 2.838 0.0 2.837 2.837 2.837 2.837 22964.0 2.0
2019-02-25 23:41:00 2.842 2.842 2.842 2.842 0.0 2.837 2.837 2.832 2.832 1.0 2.837 2.837 2.837 2.837 22964.0 0.0
2019-02-25 23:42:00 2.840 2.843 2.840 2.843 1.0 2.837 2.837 2.832 2.832 0.0 2.841 2.841 2.840 2.840 22964.0 2.0
2019-02-25 23:43:00 2.838 2.842 2.838 2.842 1.0 2.837 2.837 2.832 2.832 0.0 2.841 2.841 2.840 2.840 22964.0 0.0
2019-02-25 23:44:00 2.842 2.842 2.842 2.842 1.0 2.837 2.837 2.832 2.832 1.0 2.837 2.837 2.837 2.837 22964.0 1.0
2019-02-25 23:45:00 2.842 2.842 2.842 2.842 0.0 2.837 2.837 2.836 2.836 2.0 2.837 2.837 2.837 2.837 22964.0 0.0
2019-02-25 23:46:00 2.842 2.842 2.842 2.842 0.0 2.837 2.837 2.836 2.837 1.0 2.837 2.837 2.837 2.837 22964.0 0.0
2019-02-25 23:47:00 2.842 2.843 2.842 2.843 1.0 2.837 2.838 2.837 2.837 1.0 2.837 2.837 2.837 2.837 22964.0 0.0
2019-02-25 23:48:00 2.843 2.843 2.842 2.843 14.0 2.837 2.838 2.836 2.836 1.0 2.843 2.843 2.842 2.842 22964.0 4.0
2019-02-25 23:49:00 2.843 2.843 2.842 2.843 14.0 2.837 2.838 2.836 2.836 1.0 2.843 2.843 2.842 2.842 22964.0 0.0
2019-02-25 23:50:00 2.842 2.842 2.842 2.842 1.0 2.837 2.838 2.836 2.836 0.0 2.843 2.843 2.842 2.842 22964.0 0.0
2019-02-25 23:51:00 2.842 2.842 2.842 2.842 1.0 2.837 2.838 2.836 2.836 0.0 2.843 2.843 2.842 2.842 22964.0 0.0
2019-02-25 23:52:00 2.843 2.843 2.842 2.843 1.0 2.838 2.840 2.838 2.838 1.0 2.843 2.843 2.842 2.842 22964.0 0.0
2019-02-25 23:53:00 2.843 2.843 2.842 2.843 0.0 2.838 2.838 2.837 2.837 2.0 2.843 2.843 2.842 2.842 22964.0 0.0
2019-02-25 23:54:00 2.843 2.843 2.843 2.843 15.0 2.837 2.838 2.837 2.838 2.0 2.843 2.843 2.842 2.842 22964.0 0.0
2019-02-25 23:55:00 2.843 2.843 2.843 2.843 0.0 2.838 2.838 2.838 2.838 2.0 2.843 2.843 2.842 2.842 22964.0 0.0
2019-02-25 23:56:00 2.843 2.843 2.843 2.843 14.0 2.837 2.838 2.837 2.838 2.0 2.843 2.843 2.843 2.843 22964.0 1.0
2019-02-25 23:57:00 2.843 2.843 2.843 2.843 14.0 2.837 2.838 2.837 2.838 2.0 2.843 2.843 2.843 2.843 22964.0 0.0
2019-02-25 23:58:00 2.843 2.843 2.843 2.843 0.0 2.838 2.838 2.838 2.838 2.0 2.843 2.843 2.843 2.843 22964.0 0.0
2019-02-25 23:59:00 2.843 2.843 2.843 2.843 0.0 2.838 2.838 2.838 2.838 2.0 2.843 2.843 2.843 2.843 22964.0 0.0
2019-02-26 00:00:00 2.842 2.842 2.842 2.842 1.0 2.837 2.839 2.837 2.837 6.0 2.843 2.843 2.843 2.843 22964.0 0.0

1380 rows × 16 columns

In [ ]: