History Test¶

Verify the 'History' method returns a dataframe. Also verify what forms the method can be called with.

In [1]:
# Imports - not certain all these are required.
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Jupyter")
from System import *
from QuantConnect import *
from QuantConnect.Data.Market import TradeBar, QuoteBar
from QuantConnect.Jupyter import *

from datetime import datetime, timedelta

import pandas as pd

# Create an instance
qb = QuantBook()

Subscribe to some data¶

In [2]:
qb.AddEquity("AAPL")
qb.AddEquity("IBM")
Out[2]:
<QuantConnect.Securities.Equity.Equity at 0x7fc3719d66d0>

Request Historical data¶

First use the plain form with just the number of days (in this case) of data we want. This should return data for all assets with data subscriptions (eg added with the 'AddEquity' method as above). This should return data for AAPL and IBM.

In [7]:
# Gets the last 10 days historical data for the subscribed assets.
history_plain_form = qb.History(10)
In [8]:
# Let's see what we've got
history_plain_form
Out[8]:
close high low open volume
symbol time
AAPL 2017-09-29 15:50:00 153.960 154.080 153.670 153.670 408560.0
2017-09-29 15:51:00 153.960 153.960 153.900 153.955 73108.0
2017-09-29 15:52:00 153.980 153.980 153.910 153.960 91107.0
2017-09-29 15:53:00 153.991 154.010 153.970 153.975 74310.0
2017-09-29 15:54:00 154.060 154.060 153.980 154.000 67332.0
2017-09-29 15:55:00 154.050 154.060 154.010 154.055 73385.0
2017-09-29 15:56:00 153.990 154.080 153.900 154.050 197477.0
2017-09-29 15:57:00 154.070 154.110 153.990 153.990 137711.0
2017-09-29 15:58:00 154.035 154.075 154.025 154.070 124224.0
2017-09-29 15:59:00 154.000 154.130 153.980 154.035 595823.0
IBM 2017-09-29 15:50:00 145.020 145.020 144.990 145.010 13405.0
2017-09-29 15:51:00 145.020 145.030 145.000 145.030 8497.0
2017-09-29 15:52:00 144.980 145.020 144.970 145.020 7712.0
2017-09-29 15:53:00 144.970 144.990 144.960 144.970 10186.0
2017-09-29 15:54:00 144.975 144.980 144.965 144.965 8076.0
2017-09-29 15:55:00 144.980 145.010 144.970 144.980 18554.0
2017-09-29 15:56:00 144.950 145.020 144.930 144.990 21358.0
2017-09-29 15:57:00 144.980 145.000 144.940 144.940 20600.0
2017-09-29 15:58:00 145.010 145.010 144.980 144.980 22688.0
2017-09-29 15:59:00 145.000 145.080 144.990 145.010 73168.0
In [9]:
# Looks like a Dataframe of the data but let's be sure. 
type(history_plain_form)
Out[9]:
pandas.core.frame.DataFrame
In [10]:
# Good. It's a Dataframe.
# Note that this form does NOT seem to work in an algorithm
# Let' try another form where we specify a list of assets
history_list_form = qb.History(['AAPL', 'IBM'], 10)

TypeErrorTraceback (most recent call last)
<ipython-input-10-295d4fd660c4> in <module>()
      2 # Note that this form does NOT seem to work in an algorithm
      3 # Let' try another form where we specify a list of assets
----> 4 history_list_form = qb.History(['AAPL', 'IBM'], 10)

TypeError: No method matches given arguments
In [11]:
# OOPS. This form does NOT work in notebooks.
# However, it does in an algorithm. Which is good because we often just have a list of assets.
In [12]:
# Also doesn't work with a variable list
my_list = ['AAPL', 'IBM']
history_list_form = qb.History(my_list, 10)

TypeErrorTraceback (most recent call last)
<ipython-input-12-69be1fcd7b91> in <module>()
      1 # Also doesn't work with a variable list
      2 my_list = ['AAPL', 'IBM']
----> 3 history_list_form = qb.History(my_list, 10)

TypeError: No method matches given arguments
In [ ]:
# Oh well....