Back to Community
before_trading_start price values not as expected.

Hi,
All I am doing is printing close price values from before_trading_start, but for the previous day, it gives me the close price values at the first minute of market open, and I am expecting the close price from the end of day? Why is this behaving like this? or am I misreading it?
So on 2016-01-05, from before_trading_start() the close price for 2016-01-04 is being given as 102.04, whereas on 2016-01-06, from before_trading_start(), the close price for 2016-01-04 is given as 105.33
Could someone please explain this?

Thanks.

Clone Algorithm
2
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
#from pandas import Series, DataFrame
#import pandas as pd
#import numpy as np

# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.stocks = symbols('AAPL')
    context.i = 0
    context.close_prices = None
    context.date = None
    
def before_trading_start(context,data):
    if context.i == 0:
        return
    print context.close_prices

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    todays_date = get_datetime().date()
    if todays_date == context.date:
        return
    context.date = todays_date
    context.i = context.i + 1  
    
    context.close_prices = history(5, '1d', 'close_price')
        
There was a runtime error.
5 responses

The last three nonempty lines are only executed the first time handle_data is called each day, ie. at 9:31am EST/EDT. At that time, your call to history returns the latest price from that minute, and the closing prices from the previous 4 trading days. The previous day's close is in history[-2]. Read the help page on history.

Thanks André,
I was under the impression that before_trading_start() fires before the open, so a print command from within it would never have access to current day's values, but apparently I was wrong in assuming that.

@Ashish - before_trading_start does run before the trading start, but it prints in line 16 what you saved in context.close_prices the most recent time line 26 in handle_data was executed, and that's 9:31am EST/EDT of the previous trading day.

For example, if you're running this live, the latest call to before_trading_start (early this morning, late last night, or at midnight) should have printed the day closing prices from last Friday, Monday, Tuesday, and Wednesday, and the latest prices from 9:31am yesterday (Thursday).

I have inserted some print statements to help you see what happens where when. Run it and see the log.

Clone Algorithm
2
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
#from pandas import Series, DataFrame
#import pandas as pd
#import numpy as np

# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.stocks = symbols('AAPL')
    context.i = 0
    context.close_prices = None
    context.date = None
    
def before_trading_start(context,data):
    if context.i == 0:
        return
    print "before_trading_start "+`get_datetime()`+" UTC"
    print context.close_prices

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    todays_date = get_datetime().date()
    if todays_date == context.date:
        return
    context.date = todays_date
    context.i = context.i + 1  
    
    context.close_prices = history(5, '1d', 'close_price')
    print "handle_data "+`get_datetime()`+" UTC"
    print context.close_prices
There was a runtime error.

Thanks André,
I think I get it now, I am updating close_prices once a day at the open so for that day I only get the price for the opening minute and that is the price that is given to before_trading_start for the next day. I was incorrectly expecting the close price for the day to be sent to before_trading_start.