Back to Community
Help needed with momentum rotation strategy

I'm just learning python and cannot get this code to run properly.
Can anyone identify what I am doing incorrectly in the code

Heres what I am trying to do

  1. take the daily log returns of XLF and XLU
  2. subtract XLU daily log return from XLF log return. (how much more XLF has returned each day)
  3. take the cumulative sum
  4. get the 200 day moving average of the cumulative sum
  5. if the cumulative sum is greater than its 200 day moving average then buy XLF otherwise buy XLU

edit: fyi, the problem in the attached algorithm is that it doesn't rotate between ETFs. I think something is wrong with the IF statement, I cant figure out what I need to do to fix the code though

Clone Algorithm
5
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
"""
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
import quantopian.pipeline.filters as Filters
from datetime import datetime, timedelta
import numpy as np
import pandas as pd


def initialize(context):

    #context.spy = sid(8554)
    #context.shy = sid(23921) 
    context.xlf = [sid(19656)]
    context.xlu = [sid(19660)]
    
    """
    Called once at the start of the algorithm.
    """
    # Rebalance every day, 1 hour after market open.
    schedule_function(
        rebalance,
        date_rules.every_day(),
        time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    schedule_function(
        record_vars,
        date_rules.every_day(),
        time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    attach_pipeline(make_pipeline(context), 'pipeline')


def make_pipeline(context):
    """
    A function to create our dynamic stock selector (pipeline). Documentation
    on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """

    # Base universe set to the QTradableStocksUS
    universe = Filters.StaticAssets(context.xlf)

    # Factor of yesterday's close price.
    yesterday_close = USEquityPricing.close.latest

    pipe = Pipeline(
        columns={
            'close': yesterday_close,
        },
        screen=universe
    )
    return pipe


def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = pipeline_output('pipeline')

    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index.tolist()  


def rebalance(context, data):
 
    XLF = data.history(context.xlf, "price", 600, "1d")
    XLU = data.history(context.xlu, "price", 600, "1d")
    

    XLF_pct_change = XLF.iloc[[0, -1]].pct_change() + 1
    XLU_pct_change = XLU.iloc[[0, -1]].pct_change() + 1
      
    XLF_log_change = np.log(XLF_pct_change)
    XLU_log_change = np.log(XLU_pct_change)

    XLF_XLU = XLF_log_change.ix[-1] - XLU_log_change.ix[-1]   
    
    price = XLF_XLU.cumsum(axis = 0)
    ma_price = price.rolling(5).mean()
    above_ma = np.where(price>=ma_price, True, False)
    
    if above_ma[-1]:
        order_target_percent(sid(19656), 1.0)
        log.info("Buy XLF")
    else:
        order_target_percent(sid(19660), 1.0)
        log.info("Buy XLU")
        return

    """
    Execute orders according to our schedule_function() timing.
    """
    pass


def record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    pass


def handle_data(context, data):
    """
    Called every minute.
    """
    pass
There was a runtime error.
3 responses

this notebook shows what I am trying to do.

Loading notebook preview...
Notebook previews are currently unavailable.

Hi Patrick,

It looks like the issue occurs when you subtract XLU_log_change from XLF_log_change. In the way it's currently implemented, it will return NaNs for every day -- you can verify this by setting a breakpoint after line 87 and checking XLF_XLU.

I've attached a modified version of your algorithm that fixes this problem. (You'll probably still need to tweak it a bit to get exactly what you want, though.)

Clone Algorithm
12
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
"""
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
import quantopian.pipeline.filters as Filters
from datetime import datetime, timedelta
import numpy as np
import pandas as pd


def initialize(context):

    #context.spy = sid(8554)
    #context.shy = sid(23921) 
    context.xlf = [sid(19656)]
    context.xlu = [sid(19660)]
    
    """
    Called once at the start of the algorithm.
    """
    # Rebalance every day, 1 hour after market open.
    schedule_function(
        rebalance,
        date_rules.every_day(),
        time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    schedule_function(
        record_vars,
        date_rules.every_day(),
        time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    attach_pipeline(make_pipeline(context), 'pipeline')


def make_pipeline(context):
    """
    A function to create our dynamic stock selector (pipeline). Documentation
    on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """

    # Base universe set to the QTradableStocksUS
    universe = Filters.StaticAssets(context.xlf)

    # Factor of yesterday's close price.
    yesterday_close = USEquityPricing.close.latest

    pipe = Pipeline(
        columns={
            'close': yesterday_close,
        },
        screen=universe
    )
    return pipe


def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = pipeline_output('pipeline')

    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index.tolist()  


def rebalance(context, data):
 
    # Get series of XLF/XLU prices
    XLF = data.history(context.xlf, "price", 600, "1d")
    XLU = data.history(context.xlu, "price", 600, "1d")
    
    # Calculate percent change (and add 100%)
    XLF_pct_change = XLF.pct_change()
    XLF_pct_change += 1
    XLU_pct_change = XLU.pct_change()
    XLU_pct_change += 1
      
    # Calculate log returns
    XLF_log_change = np.log(XLF_pct_change)
    XLU_log_change = np.log(XLU_pct_change)

    # Rename columns so we can subtract properly
    XLF_log_change.columns = ['log_change']
    XLU_log_change.columns = ['log_change']
    
    # Calculate difference in log returns
    XLF_XLU = XLF_log_change.subtract(XLU_log_change)

    # Calculate cumulative sum
    price = XLF_XLU.cumsum(axis = 0)
    ma_price = price.rolling(5).mean()
    above_ma = np.where(price>=ma_price, True, False)
    
    if above_ma[-1]:
        order_target_percent(sid(19656), 1.0)
        log.info("Buy XLF")
    else:
        order_target_percent(sid(19660), 1.0)
        log.info("Buy XLU")
        return


def record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    pass


def handle_data(context, data):
    """
    Called every minute.
    """
    pass
There was a runtime error.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Lucy, thank you for taking the time to help. This is the syntax I was looking for