The following sample code runs over the period from 2016-08-31 to 2016-10-30 and uses AAPL US as an example.
The schedule_function() runs every [email protected] minute before market close and it prints out the last 4 daily close prices using data.history() function.
Please refer to the log message @2016-08-31 03:59
First, why does the closing price of 2016-08-30 (data.history()) and 2016-08-31 (data.current()) show the same value (I have verified from other data source they should not be)?
Second, the closing price of 2016-08-30 should have known when the schedule_function() run at 2016-08-31 03:59, but it does not show the correct value until 2016-09-31 03:59. It does not seem to be a single data point issue. It consistently shows the same issue/feature throughout the back-test period.
Any suggestion? Thank you.
Anthony
def initialize(context):
context.sma_lookback = 4
# AAPL US
context.security = sid(24)
schedule_function(test_price_1, date_rules.every_day(), time_rules.market_close(minutes=1))
def test_price_1(context,data):
price_history = data.history( context.security, fields='price', bar_count=context.sma_lookback, frequency='1d')
print(price_history)
current_price = data.current(context.security, 'price')
log.info("{}, current_price:{}".format(get_datetime('US/Eastern').time(),current_price))
############# Log Message ######################
2016-08-31 03:59 PRINT
2016-08-25 00:00:00+00:00 107.580
2016-08-26 00:00:00+00:00 106.940
2016-08-29 00:00:00+00:00 106.820
2016-08-30 00:00:00+00:00 105.895
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-08-31 03:59 test_price_1:46 INFO 15:59:00, current_price:105.895
2016-09-01 03:59 PRINT
2016-08-26 00:00:00+00:00 106.94
2016-08-29 00:00:00+00:00 106.82
2016-08-30 00:00:00+00:00 105.99 <- only at 2016-09-01 it shows the correct close price of 2016-08-30
2016-08-31 00:00:00+00:00 106.20
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-01 03:59 test_price_1:46 INFO 15:59:00, current_price: 106.2
2016-09-02 03:59 PRINT
2016-08-29 00:00:00+00:00 106.82
2016-08-30 00:00:00+00:00 105.99
2016-08-31 00:00:00+00:00 106.11 <- only at 2016-09-02 it shows the correct close price of 2016-08-31
2016-09-01 00:00:00+00:00 106.70
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-02 03:59 test_price_1:46 INFO 15:59:00, current_price:106.7
2016-09-03 03:59 PRINT
2016-08-30 00:00:00+00:00 105.990
2016-08-31 00:00:00+00:00 106.110
2016-09-01 00:00:00+00:00 106.725
2016-09-02 00:00:00+00:00 107.750
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-03 03:59 test_price_1:46 INFO 15:59:00, current_price:107.75
2016-09-07 03:59 PRINT
2016-08-31 00:00:00+00:00 106.110
2016-09-01 00:00:00+00:00 106.725
2016-09-02 00:00:00+00:00 107.730
2016-09-06 00:00:00+00:00 107.690
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-07 03:59 test_price_1:46 INFO 15:59:00, current_price:107.69
2016-09-08 03:59 PRINT
2016-09-01 00:00:00+00:00 106.725
2016-09-02 00:00:00+00:00 107.730
2016-09-06 00:00:00+00:00 107.700
2016-09-07 00:00:00+00:00 108.330
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-08 03:59 test_price_1:46 INFO 15:59:00, current_price:108.33
2016-09-09 03:59 PRINT
2016-09-02 00:00:00+00:00 107.73
2016-09-06 00:00:00+00:00 107.70
2016-09-07 00:00:00+00:00 108.38
2016-09-08 00:00:00+00:00 105.46
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-09 03:59 test_price_1:46 INFO 15:59:00, current_price:105.46
2016-09-10 03:59 PRINT
2016-09-06 00:00:00+00:00 107.700
2016-09-07 00:00:00+00:00 108.380
2016-09-08 00:00:00+00:00 105.510
2016-09-09 00:00:00+00:00 103.185
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-10 03:59 test_price_1:46 INFO 15:59:00, current_price:103.185
2016-09-13 03:59 PRINT
2016-09-07 00:00:00+00:00 108.38
2016-09-08 00:00:00+00:00 105.51
2016-09-09 00:00:00+00:00 103.14
2016-09-12 00:00:00+00:00 105.64
Freq: C, Name: Equity(24 [AAPL]), dtype: float64
2016-09-13 03:59 test_price_1:46 INFO 15:59:00, current_price:105.64