Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Unexpected results having adapted Calhoun's MACD Crossover

Hi,

Help! I'm pulling my hair out and I haven't much left.

I'm adapting this algo to run on a daily basis and I'm not getting the expected output, even on the debug logging that I've added. Could someone tell me where I'm going wrong please. Thanks.

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

#initialize the strategy  
def initialize(context):  
    context.price={}  
#    set_universe(universe.DollarVolumeUniverse(floor_percentile=98.0,ceiling_percentile=100.0))  
    set_symbol_lookup_date('2014-10-09')  
    context.positions = [symbol('ALU')]  
    context.max_notional = 10000.1  
    context.min_notional = 0  
    context.iterator = 0  
    context.day = None  

def handle_data(context, data):  
    if get_datetime().day == context.day:  
        return  
    context.day = get_datetime().day  
    for sid in data:  
        close = data[sid].price 

        daily_prices = history(bar_count=40, frequency='1d', field='price')  
        weekly_prices = daily_prices.resample('1W')  
        daily_close = daily_prices[sid]  
        weekly_close = weekly_prices[sid]  
        daily_sma30 = ta.SMA(np.array(daily_close), timeperiod=30)  
        weekly_sma30 = ta.SMA(np.array(weekly_close), timeperiod=30)

        daily_macd, _, _ = ta.MACD(daily_close, fastperiod=12, slowperiod=26, signalperiod=9)  
        daily_macd_sma9 = ta.SMA(np.array(daily_close), timeperiod=9)  
        weekly_macd, _, _ = ta.MACD(weekly_close, fastperiod=12, slowperiod=26, signalperiod=9)  
        weekly_macd_sma9 = ta.SMA(np.array(weekly_close), timeperiod=9)  
        budget = round(context.portfolio.starting_cash * 0.25)  
        qty = round((budget - 9.99)/close)  
        cost = (close * qty) + 9.99  
        if (( daily_macd[-1] > daily_macd_sma9[-1] ) and  
            ( daily_macd[-2] <= daily_macd_sma9[-2] ) and  
            ( context.portfolio.cash >= cost ) and  
            ( not (sid in context.positions ))):  
            order(sid, qty)  
            log.info("Buy {0} shares of {1} at {2}, MACD0  = {3}, Signal0 = {4}, MACD1  = {5}, Signal1 = {6},".format(qty, sid, close, daily_macd[-1], daily_macd_sma3[-1], daily_macd[-2], daily_macd_sma3[-2]))  
            context.positions.append(sid)

        if (( daily_macd[-1] > daily_macd_sma9[-1] ) and  
            ( daily_macd[-2] <= daily_macd_sma9[-2] ) and  
            ( not (sid in context.positions ))):  
            order(sid,-(context.portfolio.positions[sid].amount))  
            log.info("Sell {sid} at {c},  MACD0  = {m0}, Signal0 = {S0},  MACD1  = {m1}, Signal1 = {s1}, cost basis = {cb}, current price = {cp}".format(sid=sid, c=close, m0=daily_macd[-1], s0=daily_macd_sma3[-1], m1=daily_macd[-2], s1=daily_macd_sma3[-2], cb=context.portfolio.positions[sid].cost_basis, cp=context.portfolio.positions[sid].last_sale_price))  
            context.positions.remove(sid)

        context.iterator = context.iterator + 1  
        if context.iterator < 30:  
            log.debug("close={c}, MACD0={m0}, Signal0={s0}, MACD1={m1}, Signal1={s1}".format(c=daily_close, m0=daily_macd[-1], s0=daily_macd_sma9[-1], m1=daily_macd[-2], s1=daily_macd_sma9[-2]))  

The log output snippet that I'm receiving is this:
2011-01-04handle_data:73DEBUGclose=2010-11-08 00:00:00+00:00 3.21 2010-11-09 00:00:00+00:00 3.09 2010-11-10 00:00:00+00:00 3.14 2010-11-11 00:00:00+00:00 3.02 2010-11-12 00:00:00+00:00 2.94 2010-11-15 00:00:00+00:00 2.95 2010-11-16 00:00:00+00:00 2.93 2010-11-17 00:00:00+00:00 2.95 2010-11-18 00:00:00+00:00 2.93 2010-11-19 00:00:00+00:00 2.92 2010-11-22 00:00:00+00:00 2.82 2010-11-23 00:00:00+00:00 2.78 2010-11-24 00:00:00+00:00 2.92 2010-11-26 00:00:00+00:00 2.84 2010-11-29 00:00:00+00:00 2.76 2010-11-30 00:00:00+00:00 2.73 2010-12-01 00:00:00+00:00 2.81 2010-12-02 00:00:00+00:00 2.84 2010-12-03 00:00:00+00:00 2.97 2010-12-06 00:00:00+00:00 2.92 2010-12-07 00:00:00+00:00 2.95 2010-12-08 00:00:00+00:00 2.96 2010-12-09 00:00:00+00:00 3.01 2010-12-10 00:00:00+00:00 3.04 2010-12-13 00:00:00+00:00 3.05 2010-12-14 00:00:00+00:00 3.07 2010-12-15 00:00:00+00:00 2.98 2010-12-16 00:00:00+00:00 2.99 2010-12-17 00:00:00+00:00 3.00 2010-12-20 00:00:00+00:00 2.9... 2011-01-05handle_data:73DEBUGclose=2010-11-09 00:00:00+00:00 3.09 2010-11-10 00:00:00+00:00 3.14 2010-11-11 00:00:00+00:00 3.02 2010-11-12 00:00:00+00:00 2.94 2010-11-15 00:00:00+00:00 2.95 2010-11-16 00:00:00+00:00 2.93 2010-11-17 00:00:00+00:00 2.95 2010-11-18 00:00:00+00:00 2.93 2010-11-19 00:00:00+00:00 2.92 2010-11-22 00:00:00+00:00 2.82 2010-11-23 00:00:00+00:00 2.78 2010-11-24 00:00:00+00:00 2.92 2010-11-26 00:00:00+00:00 2.84 2010-11-29 00:00:00+00:00 2.76 2010-11-30 00:00:00+00:00 2.73 2010-12-01 00:00:00+00:00 2.81 2010-12-02 00:00:00+00:00 2.84 2010-12-03 00:00:00+00:00 2.97 2010-12-06 00:00:00+00:00 2.92 2010-12-07 00:00:00+00:00 2.95 2010-12-08 00:00:00+00:00 2.96 2010-12-09 00:00:00+00:00 3.01 2010-12-10 00:00:00+00:00 3.04 2010-12-13 00:00:00+00:00 3.05 2010-12-14 00:00:00+00:00 3.07 2010-12-15 00:00:00+00:00 2.98 2010-12-16 00:00:00+00:00 2.99 2010-12-17 00:00:00+00:00 3.00 2010-12-20 00:00:00+00:00 2.99 2010-12-21 00:00:00+00:00 2.9... 2011-01-06handle_data:73DEBUGclose=2010-11-10 00:00:00+00:00 3.14 2010-11-11 00:00:00+00:00 3.02 2010-11-12 00:00:00+00:00 2.94 2010-11-15 00:00:00+00:00 2.95 2010-11-16 00:00:00+00:00 2.93 2010-11-17 00:00:00+00:00 2.95 2010-11-18 00:00:00+00:00 2.93 2010-11-19 00:00:00+00:00 2.92 2010-11-22 00:00:00+00:00 2.82 2010-11-23 00:00:00+00:00 2.78 2010-11-24 00:00:00+00:00 2.92 2010-11-26 00:00:00+00:00 2.84 2010-11-29 00:00:00+00:00 2.76 2010-11-30 00:00:00+00:00 2.73 2010-12-01 00:00:00+00:00 2.81 2010-12-02 00:00:00+00:00 2.84 2010-12-03 00:00:00+00:00 2.97 2010-12-06 00:00:00+00:00 2.92 2010-12-07 00:00:00+00:00 2.95 2010-12-08 00:00:00+00:00 2.96

You can see that 'Close' is being printed out but it's not clear as to which other values are being printed out!

Am I addressing the daily_macd and daily_macd_sma9 arrays correctly. It appears not as nothing is being displayed.

Where am going wrong.

Thanks in advance for your help.
Andrew

2 responses

Hi Andrew,

What's currently happening now is that when you call 'c=daily_close', you're actually setting 'c' to reference the Pandas Series that 'daily_close' belongs to ('daily_prices[sid]') which means that you're trying to log all the daily closes for the past 40 days. Instead, in order to get the most recent 'daily_close', you have to set 'c=daily_close[-1]' which gets the most recent row of data (in other words: today's daily_close).

The line below should do the trick:

log.debug("close={c}, MACD0={m0}, Signal0={s0}, MACD1={m1}, Signal1={s1}".format(c=daily_close[-1], m0=daily_macd[-1], s0=daily_macd_sma9[-1], m1=daily_macd[-2], s1=daily_macd_sma9[-2]))  

Let me know if you have any further questions!

Seong

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.

Thanks Seong