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...
Backtest from to with initial capital
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
# Backtest ID: 56d58b55ce47e50de46f3f61
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...
Backtest from to with initial capital
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
# Backtest ID: 56e3230409fc0b0df6058412
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.