import pandas
import numpy as np
import talib
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import SimpleMovingAverage
class AvgDailyDollarVolume(CustomFactor):
inputs = [USEquityPricing.close, USEquityPricing.volume]
window_length = 30
def compute(self, today, assets, out, close_price, volume):
dollar_volume = close_price * volume
avg_dollar_volume = np.mean(dollar_volume, axis=0)
out[:] = avg_dollar_volume
#class Income_statement(CustomFactor):
# Pre-declare inputs and window_length
#inputs = [morningstar.income_statement]
#window_length = 1
# Compute market cap value
#def compute(self, today, assets, out, income):
#out[:] = income
class MarketCap(CustomFactor):
# Pre-declare inputs and window_length
inputs = [USEquityPricing.close, morningstar.valuation.shares_outstanding]
window_length = 1
# Compute market cap value
def compute(self, today, assets, out, close, shares):
out[:] = close[-1] * shares[-1]
def initialize(context):
#context.size = float(context.portfolio.cash / 20) & (sma_long ) & (sma_short)
context.shorting = True
context.shorts = []
context.max_positions = 26
pipe = Pipeline()
attach_pipeline(pipe, 'mypipe')
sma_200 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=200)
sma_100 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=100)
sma_50 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50)
sma_long = ( (sma_100 > sma_200) & (sma_50 > sma_100))
sma_short = ( (sma_100 < sma_200) & (sma_50 < sma_100))
#pipe.add(sma_200, 'sma_200')
#pipe.add(sma_115, 'sma_115') # and sma_long and sma_short & (sma_long) & (sma_short )
#pipe.add(sma_35, 'sma_35') # (sma_35 > sma_115) & & (sma_35 < sma_115))
pipe.add(sma_short, 'sma_short')
pipe.add(sma_long, 'sma_long')
mkt_cap = MarketCap()
pipe.add(mkt_cap, 'mkt_cap')
#income = Income_statement()
#pipe.add(income, 'income')
dollar_volume = AvgDailyDollarVolume()
pipe.add(dollar_volume, 'dollar_volume')
dv_filter = (dollar_volume > 10*10**6) & (dollar_volume < 30 * 10**6)
mkt_cap_fltr= (mkt_cap > 10*10**8) & (mkt_cap < 20*10**8)
pipe.set_screen (sma_long & sma_short & dv_filter & mkt_cap_fltr)
schedule_function(logic,date_rules.every_day(),time_rules.market_close())
#schedule_function(logic,date_rules.every_day(),time_rules.market_close(minutes=380))
def before_trading_start(context, data):
context.output = pipeline_output('mypipe')
#log.info(len(context.output))
context.sma_long = context.output.sort(['sma_long'], ascending=True).iloc[:100]
log.info(len(context.sma_long))
log.info("\n" + str(context.sma_long.head(10)))
context.sma_short = context.output.sort(['sma_short'], ascending=True).iloc[:100]
log.info(len(context.sma_short))
log.info("\n" + str(context.sma_short.head(10)))
update_universe(context.sma_long.index.union(context.sma_short.index))
def logic(context, data):
pass
def handle_data(context, data):
pass