Hi,
Been stuck over this probably-silly issues where I get a RUNTIME error saying there's no "top" attribute ...
What am I doing wrong here?
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
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.factors import SimpleMovingAverage
import datetime
import pandas as pd
def initialize(context):
# In this algo, we're looking at sector ETFs. vol data quoted on Nov 22, 2016 from http://etfdb.com/type/sector/all/#etfs__overview&sort_name=symbol&sort_order=desc&page=1
context.security_list = symbols(
'XLY', # XLY Consumer Discretionary SPDR Fund vol 5816k
'XLF', # XLF Financial SPDR Fund vol 67797k
'XLK', # XLK Technology SPDR Fund vol 10191k
'XLE', # XLE Energy SPDR Fund vol 15515k
'XLV', # XLV Health Care SPRD Fund vol 10854k
'XLI', # XLI Industrial SPDR Fund vol 11941k
'XLP', # XLP Consumer Staples SPDR Fund vol 14221k
'XLB', # XLB Materials SPDR Fund vol 5488k
'XLU', # XLU Utilities SPRD Fund vol 17645k
'XRT', # XRT SPDR Retail ETF vol 3809k
'XOP', # XOP Oil & Gas Exploration & Production ETF vol 17373k
'XHB', # XHB Homebuilders ETF vol 2517k
'XBI') # XBI Biotech ETF vol 6870k
# This variable is used to manage leverage
context.weights = 0.99/len(context.security_list)
# Building SMA factors
sma9 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=9)
sma21 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=21)
# Combined factors to create new factors
sma_quotient = sma9 / sma21
sma_rank = sma_quotient.rank
#filters
SMALongFilter = sma_rank.top(5) # pick top momentum
SMAShortFilter = sma_rank.bottom(5) # pick bottom momentum
#context.longs = sma_rank.top(5)
#context.longs = sma_rank.percentile_between(60, 100)
#context.shorts = sma_rank.bottom(5)
# These are the default commission and slippage settings. Change
# them to fit your brokerage fees. These settings only matter for
# backtesting. When you live trade this algorithm, they are moot -
# the brokerage and real market takes over.
set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1))
set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))
# Rebalance every day (or the first trading day if it's a holiday).
# At 11AM ET, which is 1 hour and 30 minutes after market open.
schedule_function(rebalance,
date_rules.week_start(days_offset=0),
time_rules.market_open(hours = 1, minutes = 30))
def rebalance(context, data):
# Do the rebalance. Loop through each of the stocks and order to
# the target percentage. If already at the target, this command
# doesn't do anything. A future improvement could be to set rebalance
# thresholds.
for sec in context.security_list:
if data.can_trade(sec):
order_target_percent(sec, context.weights)