Hi Everyone,
as you might have guessed it I am brand new to the Quant World and also new to programming. So every help is more than welcome.
I started with the bootcamp and thought I´ll give the momentum strategy my own spin with my limited programing skills. Unfortunatelly I ran in an ->
Runtime Error: Object reference not set to an instance of an object (Open Stacktrace)
and I am not able to fix it on my own.
I can´t attach the backtest because it didnt start. So I just paste the code here ->
***The Idea is to split the self.mom List into two list. One with positiv momentum and one with negativ momentum. The first positiv symbol I will go long and the top negativ I want to go short***
from datetime import timedelta
from QuantConnect.Indicators import *
class MOMAlphaModel(AlphaModel):
def __init__(self):
self.mom = []
def OnSecuritiesChanged(self, algorithm, changes):
for security in changes.AddedSecurities:
symbol = security.Symbol
self.mom.append({"symbol":symbol, "indicator":algorithm.MOM(symbol, 1, Resolution.Daily)})
def Update(self, algorithm, data):
ordered = sorted(self.mom, key=lambda kv: kv["indicator"].Current.Value, reverse=True)
pos_mom = filter(lambda kv: kv["indicator"].Current.Value > 0, ordered)
neg_mom = filter(lambda kv: kv["indicator"].Current.Value < 0, ordered)
pos_mom_sort = sorted(pos_mom, key=lambda kv: kv["indicator"].Current.Value, reverse=True)
neg_mom_sort = sorted(neg_mom, key=lambda kv: kv["indicator"].Current.Value, reverse=True)
if len(pos_mom_sort) > 0 and len(neg_mom_sort) > 0:
return Insight.Group([Insight.Price(pos_mom_sort[0]['symbol'], timedelta(1), InsightDirection.Up), Insight.Price(neg_mom_sort[0]['symbol'], timedelta(1), InsightDirection.Down) ])
if len(pos_mom_sort) > 0 and len(neg_mom_sort) == 0:
return Insight.Group([Insight.Price(pos_mom_sort[0]['symbol'], timedelta(1), InsightDirection.Up), Insight.Price(pos_mom_sort[1]['symbol'], timedelta(1), InsightDirection.Up) ])
if len(pos_mom_sort) == 0 and len(neg_mom_sort) > 0:
return Insight.Group([Insight.Price(neg_mom_sort[0]['symbol'], timedelta(1), InsightDirection.Down), Insight.Price(neg_mom_sort[1]['symbol'], timedelta(1), InsightDirection.Down) ])
Thank You for your help in advance!