Back to Community
Why the price is different between research and back test?

In the back test, I logged the price. The prices are different, some of them is due to display issue, but some are not. Also, the volume is massively different.

2017-07-05 09:31  PRINT                             open    high      low    close    price     volume  
2017-07-03 00:00:00+00:00  125.0  125.04  124.020  124.418  124.418  7512425.0  
2017-07-05 00:00:00+00:00  124.2  124.20  124.135  124.150  124.150    33861.0  
2017-07-06 09:31  PRINT                              open    high      low   close   price      volume  
2017-07-05 00:00:00+00:00  124.20  124.65  124.115  124.47  124.47  10028780.0  
2017-07-06 00:00:00+00:00  123.52  123.53  123.490  123.50  123.50    115198.0  
Loading notebook preview...
4 responses

This is the code used in back test.

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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.filters.morningstar import Q1500US
 
def initialize(context):
    """
    Called once at the start of the algorithm.
    """   
    # Rebalance every day, 1 hour after market open.
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open())
     
    # Record tracking variables at the end of each day.
     
def my_assign_weights(context, data):
    """
    Assign weights to securities that we want to order.
    """
    pass
 
def my_rebalance(context,data):
    """
    Execute orders according to our schedule_function() timing. 
    """
    df = data.history(symbol('TLT'),fields=['open', 'high', 'low', 'close', 'price', 'volume'], bar_count=2, frequency='1d')
    print(df[['open', 'high', 'low', 'close', 'price', 'volume']])
    
    pass
 
There was a runtime error.

Keep an eye on the bar_count when calling the data history, I forgot exactly how it worked but refer to quantopians help page with regard to that. You may have to call the "df" object to get what you want.

Try writing

print(df[0])  

& checking it against a known value..

Also if you want the my_rebalance function to run 1 hour after market open don't leave the market_open() field blank but rewrite it to say

time_rules.market_open(hours = 1)  

So the code knows to rebalance 1 hour after market open

@ John Wu

The reason the numbers don't match (in places) is that you are fetching them at different times of the day. Specifically, all the daily data returned by 'get-pricing' in a notebook are end of day or close values. The data fetched by the algorithm using 'data.history' are as of the current minute.

So, looking at your algorithm output, this makes perfect sense...

2017-07-05 09:31  PRINT                             open    high      low    close    price     volume  
2017-07-03 00:00:00+00:00  125.0  125.04  124.020  124.418  124.418  7512425.0  
2017-07-05 00:00:00+00:00  124.2  124.20  124.135  124.150  124.150    33861.0  

As of 2017-07-05 09:31 the volume for the current day (ie 07-05) was 33861 shares. This is how many shares had traded through the first minute of trading that day (ie as of 9:31 am). The volume for the previous day (ie 07-03) was 7512425 shares and represent the total shares traded for that day.

Now let's look at the notebook price and volumes for those same two days...

open_price  high    low close_price volume  price  
2017-07-03 00:00:00+00:00   125.00  125.04  124.020 124.418 7512425.0   124.418  
2017-07-05 00:00:00+00:00   124.20  124.65  124.115 124.470 10028780.0  124.470

Remember notebook data is end of day. So 2017-07-05 end of day volume was 10028780 shares. This doesn't match the algorithm volume because the algorithm volume was printed at 9:31am and NOT the end of day. However, the volume for the previous day (ie 07-03) was 7512425 shares and represent the total shares traded for that day and also matches exactly the value in the algorithm.

A lot of people would like at 9:31 in the morning to know the closing price for the day but no can do. Can't look into the future. There's no crystal ball. No lookahead bias.

@Dan Whitnable
This is helpful.
Thanks for clarification.