I have been working on a project to test the weekend effect between large cap, mid cap, and small cap stocks. And how the top 20 from each perform using the weekend effect approach. However, the problem I am facing is getting the SID in equity type. I have tried to sort the stocks and get simply the SID by splitting but it isn't in the equity type when I try to buy/sell the stocks.
Here is the code I used along with comments explaining what I am doing. Thanks for the help I have tried approaching this in multiple ways but as I am a beginner with quantopian I can't seem to get it.
#this algorithm tests the weekend effect and how it compares between large cap, mid cap, and small cap. It then takes the top 20 traded by volume from the previous day and splits up the portfolio into those 20 stocks evenly. We hold that portfolio for the whole week and sell the stocks saturday and hold only cash through the weekend and then redo the cycle come the new week.
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.filters.morningstar import Q500US
def initialize(context):
#this schedules the buy function to buy the stocks at the start of the week as the market opens
schedule_function(func=buy,date_rule=date_rules.week_start(),time_rule=time_rules.market_close()
#this schedules the sell function to sell the stocks at the end of the week 3 hours before the market closes
schedule_function(func=sell,date_rule=date_rules.week_end(),time_rule=time_rules.market_close(hours=1))
def before_trading_start(context,data):
#this filters the stocks into large cap, mid cap, and small cap
#large_cap_filter= get_fundamentals(
#query(fundamentals.valuation.market_cap)
#.filter(fundamentals.valuation.market_cap>5000000000)
#)
mid_cap_filter=get_fundamentals(
query(fundamentals.valuation.market_cap
)
.filter(fundamentals.valuation.market_cap>2000000000)
.filter(fundamentals.valuation.market_cap<5000000000)
)
#small_cap_filter=get_fundamentals(
#query(fundamentals.valuation.market_cap
#)
#.filter(fundamentals.valuation.market_cap>300000000)
#.filter(fundamentals.valuation.market_cap<2000000000)
#)
#small_cap=[stock for stock in small_cap_filter]
#gets each stock in the filter
mid_cap=[stock for stock in mid_cap_filter]
#large_cap=[stock for stock in large_cap_filter]
#volume_small=data.current(small_cap,"volume")
#gets the volume for each stock in
volume_mid=data.current(mid_cap,"volume")
#volume_large=data.current(large_cap,"volume")
#context.large_cap = volume_large.sort_values(axis=0,inplace=False,ascending=False).head(20)
#context.small_cap = volume_small.sort_values(axis=0,inplace=False,ascending=False).head(20)
#this sort the volumes by the top values and returns only the top 20 values
context.mid_cap = volume_mid.sort_values(axis=0,inplace=False,ascending=False).head(20)
print context.mid_cap
#this isn't necessary
value = context.mid_cap
context.stocks = [stock for stock in context.mid_cap]
#this gets out the sids after they have been sorted but it does it in a way in which the sids are no longer in the asset type(instead they are integers) and that is the main problem I am running into. Because when I try to go to purchase the stocks in the buy function it says the type is wrong
context.sids = []
v = value.to_dict().keys()
for i in range(len(v)):
context.sids.append(int(str(v[i]).split(" ")[0][7:]))
#this was just something I was trying but it doesn't work
#context.stocks = [stock for stock in context.sids]
#print context.stocks
print context.sids
#the sell function that puts every stock in my portfolio at the time back to 0 shares so I hold cash over the weekend
def sell(context,data):
for c in context.portfolio.positions:
order_target(c,0)
print context.portfolio.cash
#the buy function that is supposed to purchase the stocks we found in context.sids but it will not. It should split the stocks evenly into my portfolio.
def buy(context,data):
for c in context.sids:
if data.can_trade(c):
amount=1.0/20
order_percent(c,amount)
print context.portfolio.positions