Hi,
I would like to backtest
SPY crosses over SMA(200says) close all positions and buy SPY
SPY crosses bellow SMA(200days), close out all positions and buy SHY
Thanks
Brandon
Hi,
I would like to backtest
SPY crosses over SMA(200says) close all positions and buy SPY
SPY crosses bellow SMA(200days), close out all positions and buy SHY
Thanks
Brandon
@Brandon,
Try something like this:
# PMAC
equity, bill = symbol('SPY'), symbol('SHY')
ma = 200
def initialize(context):
schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 60))
def trade(context,data):
if get_open_orders(): return
price = data.current(equity, 'price')
mavg = data.history(equity, 'price', ma , '1d').mean()
wt_eqt = 1.0 if price > mavg else 0
wt_bill = 1.0 - wt_eqt
if all(data.can_trade([equity, bill])):
order_target_percent(equity, wt_eqt)
order_target_percent(bill, wt_bill)
record(wt_eqt = wt_eqt, wt_bill = wt_bill, Leverage = context.account.leverage )
@Q,
For some unknown reason during last 2 weeks I unable to attach backtest to post reply.
Please respond.