Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
what is bar count in history really mean?

I have go thru the history function history(bar_count, frequency, field, ffill=True), bar_count: The int number of bars returned by the history call. This includes the current bar.
From what my understanding, if bar_count =40, means it queries the last 40 days of price history for a universe. If bar_count =90, means it queries 90 days of price history for a universe.
I use a sample MACD talib sample code, changed the history from 40 days to 90 days, the returns is 7.6% and 5.4% respectively. The calculation of talib. MACD just using at most 26 days data, so I can't understand why it will return a different result.

# This example algorithm uses the Moving Average Crossover Divergence (MACD) indicator as a buy/sell signal.

# When the MACD signal less than 0, the stock price is trending down and it's time to sell.  
# When the MACD signal greater than 0, the stock price is trending up it's time to buy.

# Because this algorithm uses the history function, it will only run in minute mode.  
# We will constrain the trading to once per day at market open in this example.

import talib as ta  
import numpy as np  
import pandas as pd

# Setup our variables  
def initialize(context):  
    context.stocks = symbols('MMM', 'SPY', 'GOOG_L', 'PG', 'DIA')  
    context.pct_per_stock = 1.0 / len(context.stocks)  
    # Create a variable to track the date change  
    context.date = None

def handle_data(context, data):  
    todays_date = get_datetime().date()  
    # Do nothing unless the date has changed and its a new day.  
    if todays_date == context.date:  
        return  
    # Set the new date  
    context.date = todays_date  
    # Load historical data for the stocks  
    prices = history(90, '1d', 'price')  
    # Create the MACD signal and pass in the three parameters: fast period, slow period, and the signal.  
    # This is a series that is indexed by sids.  
    macd = prices.apply(MACD, fastperiod=12, slowperiod=26, signalperiod=9)  
    # Iterate over the list of stocks  
    for stock in context.stocks:  
        current_position = context.portfolio.positions[stock].amount  
        # Close position for the stock when the MACD signal is negative and we own shares.  
        if macd[stock] < 0 and current_position > 0:  
            order_target(stock, 0)  
        # Enter the position for the stock when the MACD signal is positive and  
        # our portfolio shares are 0.  
        elif macd[stock] > 0 and current_position == 0:  
            order_target_percent(stock, context.pct_per_stock)  

    record(goog=macd[symbol('GOOG_L')],  
           spy=macd[symbol('SPY')],  
           mmm=macd[symbol('MMM')])  


# Define the MACD function  
def MACD(prices, fastperiod=12, slowperiod=26, signalperiod=9):  
    '''  
    Function to return the difference between the most recent  
    MACD value and MACD signal. Positive values are long  
    position entry signals 

    optional args:  
        fastperiod = 12  
        slowperiod = 26  
        signalperiod = 9

    Returns: macd - signal  
    '''  
    macd, signal, hist = ta.MACD(prices,  
                                    fastperiod=fastperiod,  
                                    slowperiod=slowperiod,  
                                    signalperiod=signalperiod)  
    return macd[-1] - signal[-1]
2 responses

MACD is probably using exponentially weighted moving averages, which are impacted by all the price data they are calculated on, not just the up to the period lengths.

I have try other indicator, ATR, which is same. It is also affected by the bar_count, different bar count yield different returns.