0

I'm having trouble getting my code to work. Im coding python in a backtesting environment called "Quantopian". Regardless, the .apply(), series, .pd or whatever terminology is beyond my skill level. (assuming I'm even on the right track lol)

What I'm trying to accomplish: Taking a couple stocks and constantly calculating the MACD. Then when the indicator meets a certain condition, the algo purchases or sells that specific stock.

What the MACD is simplistically: A momentum indicator that looks at historical data, using 12, 26 and 9 day Exponential Moving Averages and comparing them with each other. I've designed my own function, thats not my problem....

Help: I'm trying to apply it to the pool of stocks in my universe to constantly calculate the MACD every minute.

Where I'm specifically confused: I defined a MACD function but don't know how to get it to calculate every minute for whatever stocks are in my pool.


CODE:

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

def initialize(context):
set_commission(commission.PerTrade(cost=10))
context.stocks = symbols('AAPL', 'GOOG_L')

def handle_data(context, data):
    for stock in context.stocks:
        prices_fast = data.history(context.stocks, "close", 390, "1m").resample("30min").dropna()
        prices_slow = data.history(context.stocks, "close", 390, "1m").resample("30min").dropna()
        prices_signal = data.history(context.stocks, "close", 390, "1m").resample("30min").dropna()
        curr_price = data.history(context.stocks, "price", 30, "1m").resample("30min")[-1:].dropna()

        series = pd.Series([stock]).dropna()
        macd = series.apply(MACD)

        macd_func = stock.apply(MACD)
        if macd_func[stock] > 0:
            order(stock, 1)

    print macd_func
    record(macd=macd_func[stock])

def MACD(prices_fast, prices_slow, prices_signal, curr_price):        
    # Setting MACD Conditions:
    slow = 26
    fast = 12
    signal = 9

    # Calcualting Averages:
    avg_fast = pd.rolling_sum(prices_fast[:fast], fast)[-1:] / fast
    avg_slow = pd.rolling_sum(prices_slow[:slow], slow)[-1:] / slow
    avg_signal = pd.rolling_sum(prices_signal[:signal], signal)[-1:] / signal

    # Calculating the Weighting Multipliers:
    A = 2 / (fast + 1)
    B = 2 / (slow + 1) 
    C = 2 / (signal + 1)

    # Calculating the Exponential Moving Averages:
    EMA_fast =  (curr_price * A) + [avg_fast * (1 - A)]
    EMA_slow =  (curr_price * B) + [avg_slow * (1 - B)]
    EMA_signal = (curr_price * C) + [avg_signal * (1 - C)]

    # Calculating MACD Histogram:
    macd = EMA_fast - EMA_slow - EMA_signal

If someone could give me a handle, I would GREATLY appreciate it!

Thank you very VERY much,

Mike

Alexander
  • 105,104
  • 32
  • 201
  • 196
Mike
  • 41
  • 2
  • Why are you resampling your minute data to 30 min intervals? – Alexander May 03 '16 at 23:28
  • On my real trading platform with my bank I use this tool and look at 30 minute intervals rather than minutely hourly or daily etc. I have an analysis technique that seems to work so I'm trying to create an algorithm to mimic the exact conditions. – Mike May 03 '16 at 23:54
  • You may want to take a look at [similar questions](http://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python) that have [already](http://stackoverflow.com/questions/8600161/executing-periodic-actions-in-python) been [answered](http://stackoverflow.com/questions/3393612/run-certain-code-every-n-seconds). – ptrj May 04 '16 at 04:24

0 Answers0