Back

Coarse Universe Selection help

Hi, I'm new to quantconnect and migrating from quantopian. I have some troubles understanding the API; I am not sure how to use indicators and especially the coase universe selection in python. I am trying create a str-list of equities based on their 30 day momentum and then fire a scheduled event that uses an updated list every time it fires:

class MyAlgo(QCAlgorithm):
def Initialize(self):
self.SetCash(10000)
self.SetStartDate(2017,1,1)
self.SetEndDate(2017,12,14)
self.AddEquity("SPY")

self.AddUniverse(self.CoarseSelectionFunction)

self.Schedule.On(self.DateRules.MonthStart(),self.TimeRules.At(12, 35), Action(self.momentum))

def CoarseSelectionFunction(self, coarse):
#so far I have tried this
sortedByDollarVolume = sorted(coarse, key=lambda x: x.Volume, reverse=True)

vol_asset = [x.Symbol for x in sortedByDollarVolume[:100]]

slice1 = self.History(vol_asset, 30, Resolution.Daily)
price_history1 = slice1["close"].unstack(level=0)
pre_return = price_history1.pct_change()

search = pre_return.sort_values(axis=0, ascending=False, inplace=False, kind='quicksort', na_position='last')
global c_asset
c_asset = list(search.index[0:50])
return c_asset


def OnData(self, slice):
pass

def momentum(self):

global c_asset
asset = c_asset

number = len(asset)#asset should be a python list

I have tried to define helper classes as well but without success - does anybody know how I could go about it? Thanks

Update Backtest








Maybe I should add that the strategy works if I simply add a list to Initialize(): 

 

class MyAlgo(QCAlgorithm):
def Initialize(self):

self.asset = ['TRIP', 'GPS', 'COTY']

for i in range(0,len(self.asset)):
self.AddEquity(self.asset[i])

self.Schedule.On(self.DateRules.MonthStart(),self.TimeRules.At(12, 35), Action(self.momentum))

def momentum(self):

asset = self.asset
number = len(asset)

1

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed