I tried
method 1
## only show part of code here
def initialize(context):
global stock
stock = sid(8554) # We will be trading SPY
algo.schedule_function(
rebalance,
date_rules.every_day(),
algo.time_rules.market_open(),
)
def rebalance(context, data) :
prices = data.history(stock, 'close', 9+1, '1d')
print('price={}'.format(prices))
and get result 1 (partial result)
2004-03-18 09:31 PRINT price=
2004-03-05 00:00:00+00:00 116.29
2004-03-08 00:00:00+00:00 115.26
2004-03-09 00:00:00+00:00 114.66
2004-03-10 00:00:00+00:00 112.98
2004-03-11 00:00:00+00:00 111.30
2004-03-12 00:00:00+00:00 112.69
2004-03-15 00:00:00+00:00 111.08
2004-03-16 00:00:00+00:00 111.73
2004-03-17 00:00:00+00:00 113.00
2004-03-18 00:00:00+00:00 112.65
Freq: C, Name: Equity(8554 [SPY]), dtype: float64
2004-03-19 09:31 PRINT price=
2004-03-08 00:00:00+00:00 114.857
2004-03-09 00:00:00+00:00 114.259
2004-03-10 00:00:00+00:00 112.585
2004-03-11 00:00:00+00:00 110.911
2004-03-12 00:00:00+00:00 112.296
2004-03-15 00:00:00+00:00 110.692
2004-03-16 00:00:00+00:00 111.339
2004-03-17 00:00:00+00:00 112.605
2004-03-18 00:00:00+00:00 112.505
2004-03-19 00:00:00+00:00 112.380
Freq: C, Name: Equity(8554 [SPY]), dtype: float64
Here we can see that the close price at 2004-03-17 00:00:00+00:00
changed from 113.00 to 112.605
I also tried to use method 2 with minute data
def handle_data(context, data):
today = get_datetime().date()
# Do nothing unless the date has changed
if today == context.date:
return
# Set the new date
context.date = today
## daily historical close price
prices = data.history(stock, 'close', 9+1, '1d')
print('price={}'.format(prices))
But the result 2 was the same
I then tried method 3 pipeline
stocks = symbols('SPY')
def initialize(context):
global stock
stock = sid(8554) # We will be trading SPY and tlt
context.i = 0 # Set index for windows
context.prices = []
# Attach the pipeline defined in my_pipe so we have data to use
attach_pipeline(make_pipeline(context), name='my_data')
# Rebalance every day, right after market open.
algo.schedule_function(
rebalance,
date_rules.every_day(),
algo.time_rules.market_open(),
# algo.time_rules.market_close(hours = 1),
)
def make_pipeline(context):
universe = Filters.StaticAssets(stocks)
price = USEquityPricing.close.latest
pipe = Pipeline(
columns={
'price': price,
},
screen=universe
)
return pipe
def before_trading_start(context, data):
# Store our pipeline output DataFrame in context.
context.output = pipeline_output('my_data')
context.prices.append(*context.output.price.values)
def rebalance(context, data):
# Skip first 9 days to get full windows
context.i += 1
# print('i={}'.format(context.i))
if context.i < 9:
return
price = context.prices[-1]
print('price={}'.format(context.prices))
and I got result 3
2004-03-17 09:31 PRINT price=[116.05, 116.29000000000001, 115.26000000000001, 114.66, 112.98, 111.3, 112.69, 111.08, 111.73]
2004-03-18 09:31 PRINT price=[116.29000000000001, 115.26000000000001, 114.66, 112.98, 111.3, 112.69, 111.08, 111.73, 113.0]
Since I append the latest close price to a list, it won't happen a changing issue like data.hitory
The last value 113.0 in the list at 2004-03-18 09:31 is the close price at 2004-03-17, which is the same as the method 1 before the data.history changed its value
There are also another dates having the same issue around 1/24/2016 and 4/2/2017
From this result 3, I get different trading results since it doesn't have the same close price as method 1 on certain dates.
What's happened to the change?
Is there anything wrong with my code?