I'm using the
handle_data()
function to access the price data of the last three days. All the data is stored inside a CSV file. However, only the last value is returned while all other values are returned as
inf
Note: I'm using zipline to code in my own IDE!
Code:
def initialize(context):
context.i = zipline.api.symbol("AAPL")
def handle_data(context, data):
x = data.history(context.i, "open", 3, "1d")
print(x)
def run_backtest():
start = pd.to_datetime("2017-01-10").tz_localize('US/Eastern') # Format: YYYY-MM-DD
end = pd.to_datetime("2017-03-29").tz_localize('US/Eastern')
zipline.run_algorithm(start=start, end=end, initialize=initialize, handle_data=handle_data,
bundle="apple_data", capital_base=1000)
run_backtest()
Output:
2017-01-06 00:00:00+00:00 inf
2017-01-09 00:00:00+00:00 inf
2017-01-10 00:00:00+00:00 118.769000
Freq: C, Name: Equity(0 [AAPL]), dtype: float64
2017-01-09 00:00:00+00:00 inf
2017-01-10 00:00:00+00:00 inf
2017-01-11 00:00:00+00:00 118.739000
Freq: C, Name: Equity(0 [AAPL]), dtype: float64
CSV file:
date,open,high,low,close,volume,dividend,split
2017-01-03,115.800003,116.330002,114.760002,116.150002,28781900,0,0
2017-01-04,115.849998,116.510002,115.750000,116.019997,21118100,0,0
2017-01-05,115.919998,116.860001,115.809998,116.610001,22193600,0,0
2017-01-06,116.779999,118.160004,116.470001,117.910004,31751900,0,0
2017-01-09,117.949997,119.430000,117.940002,118.989998,33561900,0,0
2017-01-10,118.769997,119.379997,118.300003,119.110001,24462100,0,0
2017-01-11,118.739998,119.930000,118.599998,119.750000,27588600,0,0
Sincerely,
Niklas