I am getting the following error on line 108, and am not sure why it is happening and what to fix. If anyone knows I would appreciate feedback!
Thank you!
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.filters.morningstar import Q1500US
from quantopian.algorithm import order_optimal_portfolio
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.factors import SimpleMovingAverage, VWAP, RSI, AverageDollarVolume
import quantopian.optimize as opt
from datetime import datetime
from quantopian.pipeline.data.psychsignal import stocktwits
from quantopian.pipeline.experimental import risk_loading_pipeline
def initialize(context):
context.max_leverage = 1.0
context.max_pos_size = 0.015
context.max_turnover = 0.95
algo.schedule_function(
rebalance,
algo.date_rules.week_start(), # Schedules frequency of rebalance
algo.time_rules.market_open(),# Hours offset
)
sentiment_score = SimpleMovingAverage(
inputs=[stocktwits.bull_minus_bear],
window_length=3,
)
algo.attach_pipeline(
risk_loading_pipeline(),
'risk'
)
"All stocks must be part of the Q500US Universe"
base_universe = Q1500US()
"Last minutes closing price"
last_close_price = USEquityPricing.close.latest
"Ensures that price of stock is over $1"
last_open_price = USEquityPricing.open.latest
pricefilter = (last_open_price > 1) & (last_open_price < 50)
"Calculates SMA5 for all Stocks in Q500US"
sma_5 = SimpleMovingAverage(
inputs=[USEquityPricing.close],
window_length=5
)
"Makes sure that LAST MINUTES BAR CLOSED ABOVE sma5, and OPENS OVER sma5"
sma5filter = (last_close_price > sma_5) & (last_open_price > sma_5)
"Calculates VWAP and Makes sure that VWAP is ABOVE CURRENT Price"
vwap = VWAP(window_length=14)
vwapfilter = (vwap > last_open_price)
"Calculates RSI14 and Makes Sure that RSI is below 60"
rsi = RSI(window_length=14)
rsifilter = rsi < 60
requirements = (rsifilter &
vwapfilter &
sma5filter &
base_universe &
pricefilter)
log.info("{}: ...".format(get_datetime('US/Eastern').time()), ...)
# Now create an instance of the pipeline and add the desired columns you want returned in the resulting output
pipe=Pipeline()
pipe.add(requirements, 'Confirmation')
pipe.add(sentiment_score, 'Sentiment')
attach_pipeline(pipe,'my_pipeline')
pipe.set_screen(requirements & sentiment_score.notnull())
def before_trading_start(context,data):
# Run the pipeline we previously defined. It will return the columns of data we requested.
context.output = algo.pipeline_output('my_pipeline')
context.risk_factor_betas = algo.pipeline_output(
'risk'
)
context.pipeline_data = algo.pipeline_output(
'my_pipeline'
)
print(context.output)
def rebalance(context, data):
log.info(context.pipeline_data.head(10))
alpha = context.pipeline_data.sentiment_score
if not alpha.empty:
objective = opt.MaximizeAlpha(alpha)
# Create position size constraint
constrain_pos_size = opt.PositionConcentration.with_equal_bounds(
-context.max_pos_size,
context.max_pos_size
)
# Constrain target portfolio's leverage
max_leverage = opt.MaxGrossExposure(context.max_leverage)
# Ensure long and short books
# are roughly the same size
dollar_neutral = opt.DollarNeutral()
# Constrain portfolio turnover
max_turnover = opt.MaxTurnover(context.max_turnover)
# Rebalance portfolio using objective
# and list of constraints
algo.order_optimal_portfolio(
objective=objective,
constraints=[
constrain_pos_size,
max_leverage,
dollar_neutral,
max_turnover,
]
)