Newbie seeks for programming help *Momentum Indicator Strategy*

Back

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!

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.


Is this all there is in the algorithm?

The main module for running the algorithm is a `QCAlgorithm` class object, so you'd need to create something like:

class MyAlgorithm(QCAlgorithm):
def Initialize(self):
self.AddAlpha(MOMAlphaModel()) # Attach your Alpha Model

def OnData(self):
pass

class MOMAlphaModel(AlphaModel):
# your alpha model

If you already have this, could be helpful to click that "Open Stacktrace" button and paste it here

1

Adam W, Thanks for your help on my issue! .Current.Value was exactly what I was missing. 

Sorry for bombing this thread, I couldn't figure out how to DM you!

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...

To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!