When I try test one cloned MACD strategy, there's one skip of order .
no any order put when crossover happen on 10Jan.
When I try test one cloned MACD strategy, there's one skip of order .
no any order put when crossover happen on 10Jan.
@tao,
Try this simplified version it trades crossover:
# MACD - SIGNAL crossover
import talib
# ------------------------------------------------------
STOCK = symbol('XOM'); FAST = 12; SLOW = 26; SIGNAL = 9;
# ------------------------------------------------------
def initialize(context):
schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())
schedule_function(record_vars, date_rules.every_day(), time_rules.market_close())
set_commission(commission.PerShare(cost = 0.005, min_trade_cost = 1.0))
set_slippage(slippage.FixedSlippage(spread = 0.01))
context.bought = False
def rebalance(context,data):
bars = SLOW*6
prices = data.history(STOCK, 'close', bars, '1d')
macd, signal, hist = talib.MACD(prices, FAST, SLOW, SIGNAL)
record(SIG = signal[-1], MACD = macd[-1])
if (macd[-1] >= signal[-1]) and not context.bought:
order_target_percent(STOCK, 1.0);
context.bought = True
elif (macd[-1] < signal[-1]) and context.bought:
order_target_percent(STOCK, 0);
context.bought = False
def record_vars(context, data):
record(leverage = context.account.leverage)