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