Back

Update a rolling window inside a class without using an instantiated QC Algorithm object

Hi folks!

I have a strategy class with two indicators and one rolling window. I want the class to be as clean as possible, so it hasn’t an instance of QCAlgorithm. It only gives signals when the CheckSignals method is called it form the Main algorithm.

Specifically I have something like this in the strategy class:
public class TrendStrategy
{
#region Fields
public SimpleMovingAverage Trend;
private Momentum TrendMomentum;
private RollingWindow MomentumWindow;
#endregion

#region Constructors
public TrendStrategy(int period)
{
Trend = new SimpleMovingAverage(period);
TrendMomentum = new Momentum(2).Of(Trend);
MomentumWindow = new RollingWindow(2);
}
#endregion

#region Methods
public OrderSignal CheckSignal()
{
MomentumWindow.Add(TrendMomentum.Current.Value);
// do ultra-top-secret stuff
}
#endregion
}

And in the Main algorithm, something like this:
public override void Initialize()
{
SetStartDate(_startDate);
SetEndDate(_endDate);
SetCash(_portfolioAmount);

AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);

TrendStrategy strategy = new TrendStrategy(20);

var consolidator = new IdentityDataConsolidator();
SubscriptionManager.AddConsolidator(symbol, consolidator);
consolidator.DataConsolidated += (sender, consolidated) =>
{
strategy.Trend.Update(new IndicatorDataPoint(consolidated.Time, consolidated.Price));
};
}


So the Trend and the TrendMomentum are automatically updated from the Main Algorithm, but the momentumWindow is updated when the CheckSignal method is called. The question is; can I update automatically the MomentumWindow when the TrendMomentum is actualized without using a QCAlgorithm instance inside the strategy class?

Now, after reading it, seems a puzzle more than a question!

Thanks in advance, JJ
Update Backtest






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.



Tricky question. I think so but I think so but it depends what you want to pass through.

Registering an indicator object sets it up to receive data updates automatically through the consolidators. This is the default way of passing it data. If you manually are using indicator classes like this then it needs to be updated like you've done.

Perhaps you could make your TrendStrategy implement a few Indicator interface methods, and then register it to receive data? Or just pass in the data to a strategy.Update() method?

@MichaelH will have better thoughts on this.
1

In an airport, so this will be short, but if you desire to keep the components decoupled then I would do updates the way you have. Possibly moving the update logic to a method on trend strategy, then calling it from the consolidator event handler like you have.

You could also have an initialize in tend strategy which accepts qcalgorithm but doesn't save it, and he can wire everything up, this would minimize the coupling to that single method.
1

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.


Thank you very much @Daniel and @Michael!

I see again the question and I realize that every other possibility is more complex and in the end, will not add nothing to the algorithm. Basically because the CheckSignal method is called in every bar!

Note to myself: KISS

Cheers, JJ
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.


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