Having some issues getting the following to backtest.
I'm assuming it's some syntax errors and code in wrong spots/order.
Hoping to get some community help so I can move forward.
In short, I upload two custom data points: weekly data for 1. eroc and 2. eez. The algo rebalances monthly, puts eroc and eez into pipeline, builds a list for both and then uses the lists to reference historic data points. It then looks to go long certain stocks and short the sector ETF (industrials in my algo) and then short certain stocks and long the respective sector ETF.
Thanks all!
import quantopian.algorithm as algo
import quantopian.optimize as opt
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import Returns
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data.user_53bc0c4831bb052ad000002f import roc
def initialize(context):
#1 rebalance at beggining of month
schedule_function(rebalance,
date_rule=date_rules.month_start(days_offset=1),
time_rule=time_rules.market_open())
attach_pipeline(make_pipeline(), 'my_pipeline')
def make_pipeline():
#1 add custom data, eroc and eez, to pipeline columns:
universe = QTradableStocksUS()
pipe = Pipeline(
columns={
'eroc': roc.eroc.latest,
'eez': roc.eez.latest
},
screen=roc.eroc.latest.notnull()
)
return pipe
def before_trading_start(context, data):
#1 add eroc and eez to lists:
context.output = pipeline_output('my_pipeline')
context.eroc = context.output[context.output['eroc']].index.tolist()
context.eez = context.output[context.output['eez']].index.tolist()
def run_this(context, data):
#1 get 26 week history for eroc and eez
#2 if stock meets eroc and eez criteria, add to long list or short list
#1 build history:
histeroc = data.history(context.eroc, 'price', 26, '1w')
histeez = data.history(context.eez, 'price', 26, '1w')
eroc1 = histeez.ix[1]
eroc2 = histeez.ix[2]
eroc3 = histeez.ix[3]
eroc4 = histeez.ix[4]
eroc5 = histeez.ix[5]
eroc6 = histeez.ix[6]
eroc7 = histeez.ix[7]
eroc13 = histeez.ix[13]
eroc14 = histeez.ix[14]
eroc15 = histeez.ix[15]
eroc16 = histeez.ix[16]
eroc17 = histeez.ix[17]
eroc18 = histeez.ix[18]
eroc19 = histeez.ix[19]
eroc20 = histeez.ix[20]
eroc21 = histeez.ix[21]
eroc22 = histeez.ix[22]
eroc23 = histeez.ix[23]
eroc24 = histeez.ix[24]
eroc25 = histeez.ix[25]
eroc26 = histeez.ix[26]
eez1 = histeez.ix[1]
eez2 = histeez.ix[2]
eez3 = histeez.ix[3]
eez4 = histeez.ix[4]
eez5 = histeez.ix[5]
eez6 = histeez.ix[6]
eez7 = histeez.ix[7]
eez12 = histeez.ix[12]
eez13 = histeez.ix[13]
eez14 = histeez.ix[14]
eez15 = histeez.ix[15]
eez16 = histeez.ix[16]
eez17 = histeez.ix[17]
eez18 = histeez.ix[18]
eez19 = histeez.ix[19]
eez20 = histeez.ix[20]
eez21 = histeez.ix[21]
eez22 = histeez.ix[22]
eez23 = histeez.ix[23]
eez24 = histeez.ix[24]
eez25 = histeez.ix[25]
eez26 = histeez.ix[26]
current_price_eez = data.current(context.eez, 'price')
current_price_eroc = data.current(context.eroc, 'price')
#2 test for criteria and add to lists:
for stock in context.eroc:
if eroc26[stock]*.99 > eroc21[stock] and eez26[stock] < -1:
context.longs = context.longs[stock]
if eroc26[stock]*1.01 < eroc21[stock] and eez26[stock] > 1:
context.shorts = context.shorts[stock]
def rebalance(context, data)
#1 create trades
#2 determine number of stocks to go long
#3 determine number of stocks to go short
#4 order long security and go short sector ETF
#5 order short security and go long sector ETF
my_positions = context.portfolio.positions
if (len(context.longs) > 0) or (len(context.shorts) > 0):
#2/3 determine percent of portfolio that is long or short stocks:
long_weight = len(context.longs)/(len(context.longs)+len(context.shorts))
short_weight = len(context.shorts)/(len(context.longs)+len(context.shorts))
#4 go long stock and short sector ETF the same ammount:
for security in context.longs:
if data.can_trade(security):
order_target_percent(security, long_weight) & order_target_percent(sid(19657), long_weight*(-1))
#5 go short stock and long sector ETF the same ammount:
for security in context.shorts:
if data.can_trade(security):
order_target_percent(security, short_weight)
#close positions if not in lists
for security in my_positions:
if security not in context.longs and security not in context.shorts \
and data.can_trade(security):
order_target_percent(security, 0)