i used the quantopian for macd of SPY,
and i tried to print the results and compare them to the macd(12,26), in nasdaq
in http://www.nasdaq.com
for example the macd(12,26) in nasdaq in 13/05/2016 is 0.151261
and in the code the result is -0.2988
and if i change the number of days in data.history to 200 days instead of 40 days i get the result
of -0.34
why is that? , is the result should be constant? like in the nasdaq table..
what i am trying to get is the same result in the code, like the same result in nasdaq.
how to do it?,
log info of the code 40 days
2016-05-06 16:31 PRINT -0.717896611672
2016-05-09 16:31 PRINT -0.562740007029
2016-05-10 16:31 PRINT -0.410930520325
2016-05-11 16:31 PRINT -0.202623920898
2016-05-12 16:31 PRINT -0.21576104177
2016-05-13 16:31 PRINT -0.298872598933
2016-05-16 16:31 PRINT -0.451957435914
2016-05-17 16:31 PRINT -0.326623323108
2016-05-18 16:31 PRINT -0.484773835961
2016-05-19 16:31 PRINT -0.501470073136
log info of the code 200 days
2016-05-10 16:31 PRINT -0.537455894185
2016-05-11 16:31 PRINT -0.304118791863
2016-05-12 16:31 PRINT -0.310129256988
2016-05-13 16:31 PRINT -0.345350938546
2016-05-16 16:31 PRINT -0.45822481994
2016-05-17 16:31 PRINT -0.290026098983
2016-05-18 16:31 PRINT -0.428585926489
2016-05-19 16:31 PRINT -0.455168738045
2016-05-20 16:31 PRINT -0.388553685498
2016-05-23 16:31 PRINT -0.295147126269
2016-05-24 16:31 PRINT -0.197016032683
nasdaq info..
Date Open High Low Close/Last 20 Day Moving Avg MACD (12,26) MACD EMA (9) Divergence
05/20/2016 204.92 206.1 204.86 205.49 206.443835 -0.235284 0.107187 -0.342471
05/19/2016 204.06 204.54 202.78 204.2 206.617835 -0.230253 0.192805 -0.423058
05/18/2016 204.44 206.3 203.63 204.91 206.856335 -0.089137 0.298569 -0.387707
05/17/2016 206.46 206.8 204.23 204.8508 207.115835 0.019983 0.395496 -0.375513
05/16/2016 204.96 207.34 204.89 206.78 207.368295 0.164998 0.489374 -0.324376
05/13/2016 206.21 206.86 204.38 204.76 207.491255 0.151261 0.570468 -0.419207
05/12/2016 207.29 207.4899 205.37 206.56 207.642255 0.337201 0.67527 -0.338069
05/11/2016 207.91 208.54 206.5 206.5 207.714755 0.386351 0.759787 -0.373436
05/10/2016 206.72 208.5 206.64 208.45 207.789795 0.449573 0.853146 -0.403573
my code is
# When the MACD signal less than 0, the stock price is trending down and it's time to sell.
# When the MACD signal greater than 0, the stock price is trending up it's time to buy.
# Because this algorithm uses the history function, it will only run in minute mode.
# We will constrain the trading to once per day at market open in this example.
import talib
import numpy as np
import pandas as pd
# Setup our variables
def initialize(context):
context.stocks = symbols('SPY')
# context.pct_per_stock = 1.0 / len(context.stocks)
schedule_function(rebalance, date_rules.every_day())
# Rebalance daily.
def rebalance(context, data):
# Load historical data for the stocks
prices = data.history(context.stocks, 'close',40,'1d')
macds = {}
# Iterate over the list of stocks
for stock in context.stocks:
# Create the MACD signal and pass in the three parameters: fast period, slow period, and the signal.
macd_raw, signal, hist = talib.MACD(prices[stock], fastperiod=12,
slowperiod=26, signalperiod=9)
macd = macd_raw[-1] - signal[-1]
macds[stock] = macd
current_position = context.portfolio.positions[stock].amount
#def make_pipeline():
mean_close_10 = 5
mean_close_30 = -5
if macd > mean_close_30 and macd < mean_close_10 :
print macd