Back to Community
Difference in moving average values algorithm performance

Hi,

I am confused regarding one aspect of the behavior of moving average.

Since the moving average is calculated per day, I believe (please correct me if I'm wrong) that the value of moving average doesn't change during the day.

If that is correct, the back test should be same in both cases, calculating moving average in handle_data and also in a scheduled function rebalance running once a day 1 hour after the market opens.

However, it is not so.

I am getting different backtest results for:

1.

def initialize(context):  
    context.stocks = symbols('XLY','XLF','XLK','XLE','XLV','XLI','XLP','XLB','XLU')  
    #schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(hours = 1))  
def handle_data(context,data):  
    for stock in context.stocks:  
        k1 = data.history(stock,'price',50,'1d')  
        ma1 = k1.mean()  
        k2 = data.history(stock,'price',200,'1d')  
        ma2 = k2.mean()  
        price = data.current(stock,'price')  
        if ma1 > ma2:  
            log.info('Buying')  
            log.info(ma1)  
            log.info(ma2)  
            order_target_percent(stock,0.11)  
        elif ma1 < ma2:  
            log.info('Shorting')  
            log.info(ma1)  
            log.info(ma2)  
            order_target_percent(stock,-0.11)  

2.

def initialize(context):  
    context.stocks = symbols('XLY','XLF','XLK','XLE','XLV','XLI','XLP','XLB','XLU')  
    schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(hours = 1))  
def rebalance(context,data):  
    for stock in context.stocks:  
        k1 = data.history(stock,'price',50,'1d')  
        ma1 = k1.mean()  
        k2 = data.history(stock,'price',200,'1d')  
        ma2 = k2.mean()  
        price = data.current(stock,'price')  
        if ma1 > ma2:  
            log.info('Buying')  
            log.info(ma1)  
            log.info(ma2)  
            order_target_percent(stock,0.11)  
        elif ma1 < ma2:  
            log.info('Shorting')  
            log.info(ma1)  
            log.info(ma2)  
            order_target_percent(stock,-0.11)  

Could someone please help me understand why?

2 responses

Anish,

See https://www.quantopian.com/help#ide-history and note:

history(context.assets, "price", 6, "1d") returns the prices for the previous 5 days and the current price.

Since your computation of the mean includes the current price, the mean will change depending on when it is computed during the day. Is this what you are seeing?

Hi Grant,

Thanks I think that makes sense.

As per my understanding, in the first case where handle_data is used the last value returned by history is the latest price which changed every minute. This is different in case of second case where schedule_function is used where only the price at the point where the function is executed is considered.

I have one more doubt.

I have attached the back test for the first case (handle_data). For some strange reason, I observed that the stocks are traded at interval of 11 minutes in spite of handle_data supposed to run every minute.

I wanted to know if this is due to some internal Quantopian construct or just by coincidence?

Thanks.

Clone Algorithm
1
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: 57cbe51188548810198cde28
There was a runtime error.