I'm trying to do a SMA algorithm on sectors, but once the sector index experiences a crossover, I want to actually trade the top 2 stocks in the sector. For right now, I am just using a single sector, but plan to expand it to pick the currently bullish sectors to decide where I want to actively trade.
def initialize(context):
context.investment_size = (context.portfolio.cash / 2.0)
context.stop_loss_pct = 0.995
context.limit = 2
context.sector = symbol('XLK')
def before_trading_start(context):
#need help with the 50 and 200 SMA's for the following lines
tech_shortMA = context.sector
tech_longMA = context.sector
if tech_shortMA > tech_longMA:
context.fundamentals = get_fundamentals(
query(
fundamentals.asset_classification.morningstar_sector_code
)
.filter(
fundamentals.asset_classification.morningstar_sector_code == 311
)
.order_by(
fundamentals.valuation.market_cap.desc()
)
.limit(context.limit)
)
update_universe(context.fundamentals.columns.values)
record(Sec_ShortMA=tech_shortMA, Sec_LongMA=tech_longMA)