Test momentumSP500Select in Python


I am trying to test the MonmentumSP500Select in Python, but experience a error as shown below.


tkr = alphien.data.getTickersSP500().ticker

port = alphien.portfolio.Portfolio(tkr)

data = alphien.data.getHistoryData(tkr, field = ‘bb_live’, zoom=‘2007::2016’)

port.payout(alphien.tutorial.momentumSP500Select,features = data,n=32)




TypeError Traceback (most recent call last)
<ipython-input-20-6fe72d4328ac> in <module>
3 data = alphien.data.getHistoryData(tkr, field = ‘bb_live’, zoom=‘2007::2016’)
4 port.payout(alphien.tutorial.momentumSP500Select,features = data,n=32)
----> 5 port.evaluate(zoom=“2007::2016”)
6 port.backtest()

/mnt/public/IT/Libs/Python/alphien/portfolio/portfolio.py in evaluate(self, startDate, endDate, zoom, period, lastN, computationDataPoints, rollEval, alignAndFillDates)
397 if rollEval is None:
398 self.singleEval(startDate=startDate, endDate=endDate, zoom=zoom, period=period, lastN=lastN, computationDataPoints=computationDataPoints,
–> 399 rollEval=False, alignAndFillDates=alignAndFillDates)
400 else:
401 self.rollEval(startDate=startDate, endDate=endDate, zoom=zoom, period=period, rollingWindow=rollEval, computationDataPoints=computationDataPoints)

/mnt/public/IT/Libs/Python/alphien/portfolio/portfolio.py in singleEval(self, startDate, endDate, zoom, period, lastN, computationDataPoints, rollEval, alignAndFillDates, composeLogic)
448 if self.payoutDataParamName[i] in self.payoutParams[1][i]:
449 # data arg keyword is present but value is None -> replace with data features
–> 450 if self.payoutParams[1][i][self.payoutDataParamName] is None:
451 loadFromFeatures = ‘keyword’
452 # case user passed in argument as positional argument

TypeError: unhashable type: ‘list’


It is better to use a DataFeatures object in momentumSP500Select. Also, do not call a positional argument by passing its keyword.

This works on my end:

tkr = alphien.data.getTickersSP500().ticker.unique().tolist()#
data = alphien.data.DataFeatures(tkr)

port = Portfolio(tkr)
port.payout(alphien.tutorial.momentumSP500Select,data ,n=32)

Please let me know if not the case for you. Thanks

Hi Herve,

Thank you. It works.


TypeError: unhashable type: ‘list’ usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error.

For ex. when you use a list as a key in the dictionary , this cannot be done because lists can’t be hashed. The standard way to solve this issue is to cast a list to a tuple . So, in order to fix unhashable type: ‘list’, you’ll have to change your list into tuples if you want to put them as keys in your dictionary .

my_dict = {'name': 'John', tuple([1,2,3]):'values'}

The hash() is a built-in python method, used to return a unique number . This can be applied to any user-defined object which won’t get changed once initialized. This property is used mainly in dictionary keys .