I see what you're saying. I managed to get this:
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.morningstar import earnings_ratios
from quantopian.algorithm import attach_pipeline, pipeline_output
def initialize(context):
pipe = Pipeline()
attach_pipeline(pipe, name='pipeline')
#SPY
context.market = sid(8554)
def handle_data(context, data):
for i in context.high3MoPE:
price = data[i].price
if i not in context.portfolio:
#negative for shorting
order(i, -(context.portfolio.portfolio_value/len(context.portfolio.positions)))
#buy to cover at a 1.05 limit above price
order(i, (context.portfolio.portfolio_value/len(context.portfolio.positions)), style=LimitOrder(price*1.05))
for eq in context.portfolio:
#hold until their P/E ratio crosses the market P/E ratio, then close out the position
if data[eq].pe_ratio < data[context.market].pe_ratio:
order_target_percent(eq, 0)
def before_trading_start(context, data):
results = pipeline_output('pipeline')
print results.head(10)
#find 10 stocks with highest 3 month P/E and put them in a list called high3MoPE
#doesn't do by 3 months... only does daily PE
context.high3MoPE = query(fundamentals.valuation_ratios.pe_ratio).order_by(fundamentals.valuation_ratios.pe_ratio).limit(10)
#context.high3MoPE = PEquery.all()
print(context.high3MoPE)
#rebalance portfolio when trading day starts every day by distributing positions equally
However, it looks like the .all() method is not whitelisted for the query, so I can't put the results into a list and iterate through them. Is there a known workaround?