Hi,
For instance:
Alcatel Lucent (ALU) on the NYSE between periods 05/02/2013 and 02/28/2014.
My technical analysis software records the MACD and Signal to be the following:
05/06/2013 Close 1.31 MACD -0.021 Signal -0;018
05/07/2013 Close 1.44 MACD -0.013 Signal -0.017
Hence there is a cross over (a Buy signal)
But my algo is getting this values where MACD0/Signal0 = [-1] in the array and MACD1/Signal1 = [-2] in the array:
2013-05-06handle_data:116DEBUG 2013-05-06 02:00:00+02:00: close=1.33, MACD0=-0.0201, Signal0=-0.0186, MACD1=-0.0195, Signal1=-0.0188
2013-05-07handle_data:116DEBUG 2013-05-07 02:00:00+02:00: close=1.42, MACD0=-0.0131, Signal0=-0.0179, MACD1=-0.0201, Signal1=-0.0186
Not the same values at all.
Loading the values into an Excel spreadsheet using data from finance.yahoo.com I get:
06/05/2013 1.31 MACD -0.022342091 Signal -0.020220082
07/05/2013 1.44 MACD -0.013488888 Signal -0.018873843
In theory I'm using the same signal periods etc. Can you explain the difference?
My code:
```
import math
import talib as ta
import numpy as np
import pandas as pd
def initialize(context):
context.price={}
set_symbol_lookup_date('2014-10-09')
context.positions = [symbol('ALU')]
context.max_notional = 100000.1
context.min_notional = 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=365, frequency='1d', field='price')
daily_close = daily_prices[sid]
daily_macd, _, _ = ta.MACD(daily_close, fastperiod=12, slowperiod=26, signalperiod=9)
daily_macd_sma9 = ta.SMA(np.array(daily_macd), timeperiod=9)
daily_macd_1 = round(daily_macd[-1],4)
daily_macd_2 = round(daily_macd[-2],4)
daily_macd_sma9_1 = round(daily_macd_sma9[-1],4)
daily_macd_sma9_2 = round(daily_macd_sma9[-2],4)
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, daily_close[-1], daily_macd_1, daily_macd_sma9_1, daily_macd_2, daily_macd_sma9_2))
context.positions.append(sid)
if (( daily_macd_1 < daily_macd_sma9_1 ) and
( daily_macd_2 >= daily_macd_sma9_2 ) and
( ( 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=daily_close[-1], m0=daily_macd_1, s0=daily_macd_sma9_1, m1=daily_macd_2, s1=daily_macd_sma9_2, cb=context.portfolio.positions[sid].cost_basis, cp=context.portfolio.positions[sid].last_sale_price))
context.positions.remove(sid)
Let me know what you think!
Andrew