In [1]:
# Import. Not sure if all these are required. They were in the QC template.
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Jupyter")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Jupyter import *

# Import pandas and Numpy because they rock.
import pandas as pd
import numpy as np
In [2]:
# Create an instance
qb = QuantBook()
In [3]:
# Add SPY to the list of data subscriptions
qb.AddEquity("SPY")
Out[3]:
<QuantConnect.Securities.Equity.Equity at 0x7f690012a510>
In [4]:
# Get the last 50 datapoints with second resolution
history = qb.History('SPY', 50, Resolution.Second)
In [6]:
# Select the close values and use the unstack method to get a column for each security
close_df = history.close.unstack(level=0)
close_df
Out[6]:
symbol SPY
time
2017-11-15 15:59:10 256.440
2017-11-15 15:59:11 256.455
2017-11-15 15:59:12 256.450
2017-11-15 15:59:13 256.450
2017-11-15 15:59:14 256.450
2017-11-15 15:59:15 256.440
2017-11-15 15:59:16 256.450
2017-11-15 15:59:17 256.460
2017-11-15 15:59:18 256.480
2017-11-15 15:59:19 256.475
2017-11-15 15:59:20 256.480
2017-11-15 15:59:21 256.480
2017-11-15 15:59:22 256.510
2017-11-15 15:59:23 256.520
2017-11-15 15:59:24 256.515
2017-11-15 15:59:25 256.510
2017-11-15 15:59:26 256.500
2017-11-15 15:59:27 256.490
2017-11-15 15:59:28 256.481
2017-11-15 15:59:29 256.480
2017-11-15 15:59:30 256.440
2017-11-15 15:59:31 256.440
2017-11-15 15:59:32 256.420
2017-11-15 15:59:33 256.420
2017-11-15 15:59:34 256.410
2017-11-15 15:59:35 256.420
2017-11-15 15:59:36 256.430
2017-11-15 15:59:37 256.460
2017-11-15 15:59:38 256.460
2017-11-15 15:59:39 256.469
2017-11-15 15:59:40 256.470
2017-11-15 15:59:41 256.460
2017-11-15 15:59:42 256.440
2017-11-15 15:59:43 256.450
2017-11-15 15:59:44 256.435
2017-11-15 15:59:45 256.440
2017-11-15 15:59:46 256.460
2017-11-15 15:59:47 256.450
2017-11-15 15:59:48 256.460
2017-11-15 15:59:49 256.450
2017-11-15 15:59:50 256.450
2017-11-15 15:59:51 256.450
2017-11-15 15:59:52 256.460
2017-11-15 15:59:53 256.460
2017-11-15 15:59:54 256.450
2017-11-15 15:59:55 256.450
2017-11-15 15:59:56 256.450
2017-11-15 15:59:57 256.470
2017-11-15 15:59:58 256.420
2017-11-15 15:59:59 256.430
In [7]:
# Use the 'resample' method to get closes at 5 sec increments
# The 'last' method simply gets the last value in each bucket
close_df.resample('5S', closed='right', label='right').last()
Out[7]:
symbol SPY
time
2017-11-15 15:59:10 256.44
2017-11-15 15:59:15 256.44
2017-11-15 15:59:20 256.48
2017-11-15 15:59:25 256.51
2017-11-15 15:59:30 256.44
2017-11-15 15:59:35 256.42
2017-11-15 15:59:40 256.47
2017-11-15 15:59:45 256.44
2017-11-15 15:59:50 256.45
2017-11-15 15:59:55 256.45
2017-11-15 16:00:00 256.43
In [8]:
# One could also get the mean or max or other vlue from each bucket
# For more methods and better explanation see https://www.quantopian.com/posts/prices-in-algo
close_df.resample('5S', closed='right', label='right').mean()
Out[8]:
symbol SPY
time
2017-11-15 15:59:10 256.4400
2017-11-15 15:59:15 256.4490
2017-11-15 15:59:20 256.4690
2017-11-15 15:59:25 256.5070
2017-11-15 15:59:30 256.4782
2017-11-15 15:59:35 256.4220
2017-11-15 15:59:40 256.4578
2017-11-15 15:59:45 256.4450
2017-11-15 15:59:50 256.4540
2017-11-15 15:59:55 256.4540
2017-11-15 16:00:00 256.4425
In [11]:
# Lets use the mean of each bucket and continue
close_5s = close_df.resample('5S', closed='right', label='right').mean()

# To get specific values of a dataframe use the 'iloc' method along with column reference (ie SPY)
close_5s.iloc[0].SPY
Out[11]:
256.44
In [15]:
# The above dataframe syntax is a bit unwieldly so one could turn the whole thing into a list first 
close_5s_list = close_5s.SPY.tolist()
close_5s_list
Out[15]:
[256.44,
 256.449,
 256.469,
 256.507,
 256.4782,
 256.422,
 256.4578,
 256.445,
 256.454,
 256.454,
 256.44250000000005]
In [16]:
# Now it's easy to index the list to get specific values
close_5s_list[0]
Out[16]:
256.44
In [17]:
# One can compare different values
close_5s_list[0] > close_5s_list[3]
Out[17]:
False