Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Two securities: ratio MACD?

Hi all,

I'm trying to develop a backtest where I use technical indicators derived off the ratio between two securities. In the attached test, I'm using SPY & IWM.

I generated a variable "ratio" based off spy['price']/iwm['price'] and confirmed the reported values with ratio.tail(). However, I'm a bit unsure how to generate a MACD value for each day. I tried ta.MACD(ratio,12,26,9) and the results are unclear.

To add context, if I can get the MACD (or another indicator) to generate a numerical value by date, then I would like to implement this into a backtest within the Quantopian Algorithm to use as trading signals.

Any help is much appreciated!
Thanks,
Chris

2 responses

This may help :

Try this in IDE:

# MACD indicator on price_relative ratio  
import talib  
# --------------------------------------------------------  
assets = symbols('SPY','IWM'); Fast, Slow, Sig = 12, 26, 9  
# --------------------------------------------------------  
def initialize(context):  
    schedule_function(macd_indicator, date_rules.every_day(), time_rules.market_close())      

def macd_indicator(context, data):  
    bars = (Fast + Slow + Sig)  
    prices = data.history(assets, 'price',  252, '1d')  
    price_relative = prices.divide(prices.ix[0])  
    ratio = price_relative[assets[0]]/price_relative[assets[1]]    

    macd, signal, hist = talib.MACD(ratio[-bars:], Fast, Slow, Sig)

    record(macd = macd[-1], signal = signal[-1], hist = hist[-1], zero = 0)  
    print(macd[-1], signal[-1], hist[-1])