Here is a sample of the code I'm running:
Separating the two different groups
def initialize(context):
context.stock = [sid(.............)]
context.bond = [sid(............)]
I assume I am good so far as I have identified my trade-able universe and have them separated according to what they are...stocks or bonds.
Next I will make my pipeline. This is where my issue comes about because I have to choose my trade-able universe, either my context.stocks or context.bonds. I don't want to group them together beforehand because as I stated previously I want to analyze them differently.
def make_pipeline(context):
universe=Filters.StaticAssets(context.stock)
# Volatility measure
# vol = Annualized_vol_1yr()
close = recent_close()
# Factor of returns for each period at close price.
returns_5=Factors.Returns(inputs=[USEquityPricing.close],
window_length=5,
mask=universe)
returns_21=Factors.Returns(inputs=[USEquityPricing.close],
window_length=21,
mask=universe)
returns_63=Factors.Returns(inputs=[USEquityPricing.close],
window_length=63,
mask=universe)
returns_126=Factors.Returns(inputs=[USEquityPricing.close],
window_length=126,
mask=universe)
returns_1yr=Factors.Returns(inputs=[USEquityPricing.close],
window_length=252,
mask=universe)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 5 day weight & scores
weight_5 = 0
score_5d = (weight_5*returns_5)
# 21 day weight & scores
weight_21 = 1
score_21d = (weight_21*returns_21)
sma21 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=21)
ma21 = ((close-sma21)/sma21)
# 63 day weight & scores
weight_63 = 0
score_63d = (weight_63*returns_63)
# 126 day weight & scores
weight_126 = 0
score_126d = (weight_126*returns_126)
# 1yr weight & scores
weight_1yr = 0
score_1yr = (weight_1yr*returns_1yr)
score = (score_5d + score_21d + score_63d + score_126d + score_1yr)
score_rank = score.rank(ascending=False)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Screen out any ETFs that have posted a loss in the last 5 days
gain_5d = (returns_5 > -.005)
pipe = Pipeline(
columns = {
'score': score,
"score_rank": score_rank,
'ma21': ma21,
},
screen = (gain_5d)
)
return pipe
So as you could probably have guessed, at this point I have effectively analyzed and screened for the context.stocks but the context.bonds have been left behind. Before adding the context.stocks to my rebalancing function I will edit one last time to get the data nice a neat.
def before_trading_start(context, data):
context.output = pipeline_output('My_pipeline')
# ascending=false for 'higher is better'
# ascending=true for 'lower is better'
# Choose the best ETF from the screen based on rank
context.output = context.output.sort_values(['ma21'],ascending=False).iloc[:3]
context.output = context.output.sort_values(['score_rank'],ascending=True).iloc[:1]
And that's it. The context.stocks have been analyzed, screened, and are now singled out down to one equity...but the bonds are still at step 1.
Since I can't run two "make_pipeline" functions but I don't want to analyze the bonds under the same criteria/at the same time as the stocks, what should I do?
Any help would be much appreciated!