I started learning how to use pipeline yesterday but I keep running into the same problem. I always get this:
KeyError: 'tradeable_securities'
There was a runtime error on line 63.
My understanding is that this means there are no securities in that dictionary. I feel like at least one of the 1,500 stocks should pass the filter I've created. I think I followed the pipeline tutorial pretty closely but my algorithm doesn't work and theirs does. Can anyone point out what I'm doing wrong?
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume, SimpleMovingAverage
from quantopian.pipeline.filters.morningstar import Q1500US
def initialize(context):
schedule_function(my_rebalance, date_rules.week_start(), time_rules.market_open(hours=1))
my_pipeline = make_pipeline()
attach_pipeline(make_pipeline(), 'my_pipeline')
def make_pipeline():
base_universe = Q1500US()
#---------------------------------------------------
# Factors
#---------------------------------------------------
meanHigh_A = SimpleMovingAverage(inputs=[USEquityPricing.high], window_length=9, mask=base_universe)
meanLow_A = SimpleMovingAverage(inputs=[USEquityPricing.low], window_length=9, mask=base_universe)
meanHigh_B = SimpleMovingAverage(inputs=[USEquityPricing.high],window_length=26, mask=base_universe)
meanLow_B = SimpleMovingAverage(inputs=[USEquityPricing.low], window_length=26, mask=base_universe)
meanHigh_C = SimpleMovingAverage(inputs=[USEquityPricing.high], window_length=52, mask=base_universe)
meanLow_C = SimpleMovingAverage(inputs=[USEquityPricing.low], window_length=52, mask=base_universe)
tenkanSen = (meanHigh_A - meanLow_A) / 2
kijunSen = (meanHigh_B - meanLow_B) / 2
senkouSpanA = (tenkanSen + kijunSen) / 2
senkouSpanB = (meanHigh_C - meanLow_C) / 2
current_price = SimpleMovingAverage(inputs=[USEquityPricing.open], window_length=2, mask=base_universe)
#---------------------------------------------------
# Filters
#---------------------------------------------------
price_over_green_cloud = current_price > senkouSpanA > senkouSpanB
price_over_red_cloud = current_price > senkouSpanB > senkouSpanA
uptrend_cloud = senkouSpanA > senkouSpanB
price_over_base = current_price > kijunSen
base_over_conversion = tenkanSen > kijunSen
tradeable_securities = (
(price_over_green_cloud | price_over_red_cloud)
& uptrend_cloud
& price_over_base
& base_over_conversion
)
#---------------------------------------------------
# Pipeline Settings
#---------------------------------------------------
return Pipeline(
screen=(tradeable_securities)
)
def before_trading_start(context, data):
context.output = pipeline_output('my_pipeline')
context.weight = my_compute_weights(context, data)
def my_compute_weights(context, data):
weight = 1/len(context.tradeable_securities)
return weight
def my_rebalance(context,data):
for security in context.portfolio.positions:
if security not in context.tradeable_securities and data.can_trade(security):
order_target_percent(security, 0)
for security in context.tradeable_securities:
if data.can_trade(security):
order_target_percent(security, context.weight)