Hi guys,
I just want to get the value of the MOMP to use in my algorithm.
I think (I am pretty sure) it has something to do with RollingWindow, but I am honestly a bit confused with the explanation in the documentation.
I have pulled/manipulated some code snippets below. Can anyone let me know if I am on the right track?
My big question is commented on the right of the first line after //OnData in the code below. Thanks!Alix//RollingWindow Class
private RollingWindow<IndicatorDataPoint> _mompWin;
//Initialize
.
.
//Indicators
MOMP(_symbol, 20).Updated += (sender, updated) => _mompWin.Add(updated);
_mompWin = new RollingWindow<IndicatorDataPoint>(20);
_selectorIndicators = new Indicators
{
MOMP = MOMP(_symbol, 20, Resolution.Daily, Field.Low)
}
//OnData
var currMomp = _mompWin[0]; //Does this give me the current MOMP value as a decimal?
decimal mompTrig = -2.5;
if (!Portfolio.Invested && currMomp < mompTrig)
{
SetHoldings(_symbol, 1);
}
Michael Manus
hi
you can take a look on many examples provided my the QC team on github:
When you open the rolling window example here:
you will see your answer at line 71 :)
Alix Claire Erie
Thanks Michael :-)
Yes I was using this file and also the IndicatorSuiteAlgorithm.cs to build my algo. (I cloned thed GitHub library already on my local.)
So, it seems like I am on the right track.
Let me just ask a few more questions so I make sure I understand the details, if you don't mind :-) (My background is C and C++, so switching gears here.)
1) A RollingWindow is basically like an object that lets you access a vector, and in the case of an indicatory it basically allows you to access the members of the current period of the specified indicator that the program is looping through. It is basically like accessing a slot in multi-dimensional vector array. (Right ?)
2) So, the number in parentheses at the end of the RollingWindow declaration should in normal circumstances match the size of the period of the indicator, right? I think this is what I was most confused about. Example from your code:
// Creates a Rolling Window indicator to keep the 2 TradeBar
_window = new RollingWindow<TradeBar>(2); // For other security types, use QuoteBar
// Creates an indicator and adds to a rolling window when it is updated
SMA("SPY", 5).Updated += (sender, updated) => _smaWin.Add(updated);
_smaWin = new RollingWindow<IndicatorDataPoint>(5);
3) Can you point me towards the (Lean/QC) documentation that tells me what is going on in this line, please?
SMA("SPY", 5).Updated += (sender, updated) => _smaWin.Add(updated);
Particularly the "+= (sender, updated) =>" part. Thanks for all the help!Best,Alix
Michael Manus
as i am a beginner and not that skilled as the others but:
1) lets take a look what happens in the rolling window:
it seems its an thread safe ( ReaderWriterLockSlim ) generic list ( List<T> )where you Add() stuff in it of the predefined length.
And it seems to deque stuff when your added item reaches the size....
2) yep bad example....window of size 2 wit 2 values....
SMA is simple moving average of length 5.....so (5+4+3+2+1)/5 would be the current value: 3
so in the SMA window _smaWin which tracks 5 values of the sma would had 3 as the most recent value i think
if SPY had 5,4,3,2,1 as last values so yes everything right
3) is much harder to explain as it is c# syntax for subscribing to an event handler which is raised everytime you get a value. So its microsoft c# 3.5 or simething like that:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events
answered Mar 17 '10 :)
Michael Manus
3) Its the same as defining a method. line 62 & 116
Jake Mitchell
Does the Updated event handler work in Python? I just tried it (because I only want to plot every time the indicator is updated, not on every data point) and it isn't working. It says that Updated is not an attribute of the indicator class.
Link Liang
Hi Jake,
Yes the updated event handler works in Python. In the python version of RollingWindowAlgorithm, you may find the updated event Handler is implemented in the exact same way (line 50), just in different style of syntax. Here is a backtest with plotting the SMA indicator.
Check here for more information about charting and ploting. Hope it helps!
Alix Claire Erie
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.
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!