Hi, When I run this code in 'research' it works like a charm, when I run it in the IDE, the context.output is empty. Any help?
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.classifiers.morningstar import Sector
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.data.builtin import USEquityPricing
import pandas
def initialize(context):
# Schedule our rebalance function to run at the start of each week.
schedule_function(my_rebalance, date_rules.week_start(), time_rules.market_open(hours=3))
schedule_function(my_other_rebalance, date_rules.week_end(), time_rules.market_open(hours=3))
# Create our pipeline and attach it to our algorithm.
my_pipe = make_pipeline()
attach_pipeline(my_pipe, name='my_pipeline')
def make_pipeline():
base_universe = Q500US()
exchange = morningstar.share_class_reference.exchange_id.latest
morningstar_sector = Sector()
nyse_filter = exchange.eq('NYS')
mean_close_7 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=7, mask=base_universe)
latest_close = USEquityPricing.close.latest
percent_difference = (((mean_close_7 - latest_close) / (mean_close_7)) * 100)
myfilter = (percent_difference > 1.00)
return Pipeline(columns={"% Diff" : percent_difference,
"Over" : myfilter,
"Exchange" : exchange,
"Sector Code" : morningstar_sector}, screen=(myfilter & nyse_filter))
def before_trading_start(context, data):
context.output = pipeline_output(name='my_pipeline')
context.movers = context.output[context.output["Over"]].index.tolist()
length = len(context.movers)
print length
print context.movers
def my_rebalance(context, data):
for security in context.movers:
print (security)
if data.can_trade(security):
order_target_percent(security, (1/length))
log.info("Buying %s" % (security.symbol))
def my_other_rebalance(context, data):
for security in context.movers:
print (security)
current_price = data.current(security, 'price')
hist_price = data.history(asset=security, fields='price', bar_count=1, frequency='1w')
if data.can_trade(security) and current_price > hist_price:
order_target_percent(security, 0)
log.info("Buying %s" % (security.symbol))
else:
continue