Hi All!
This is my first post in the community! Thank you for taking the time to read this question!
Problem statement:
I have a SimpleMovingAverage Indicator with Daily Resolution. It is registered on a ticker [e.g. SPY] with minutely resolution. On each trading day @ 15:43 [using Scheduled events], I perform an analysis using the SimpleMovingAverage on that day as calculated at 15:43 using the price observed at 15:43 as well as all the previous days' closing prices.
To achieve this I am utilizing ComputeNextValue. However, for the SMA, ComputeNextValue(IndicatorDataPoint input) actually changes the internal state, and registers the supplied intraday point as part of the private window of the SMA object. This means that the SMA calcluated at the end of that day [after 15:43] is incorrect.
Background:
For some indicators [e.g. RSI or ATR] supplying an intraday data point does not change the internal state of the indicator, and ComputeNextValue works as “a what-if” calculator. I.e. supplying the intraday point, does not alter the calculation at the end of day.
Question:
Is there any built-in method which allows this intraday “what-if” calculation? [I can do it by hand, but would like to know if there is a built-in function or method]
Thank you in advance for any responses!
Fred Painchaud
Hi Corvin,
Even though in some instances it might be possible, calling ComputeNextValue is not expected by design. I do not recommend it.
I would recommend you using you SMA, updating it during the day with Update, as usual.
For your “all previous days' closing prices”, I would recommend using a RollingWindow (with the number of previous days close you want passed as param), and use daily bars to Add the close for each day to it.
Then, at 15:43, with your scheduled event, you can use the current SMA for the day and the X previous days close to compute you are computing.
And no, there is no built-in method to compute “what-if” intraday calculations. Sorry about that.
Fred
Corvin Codirla
Hi Fred!
Thank you for getting back!
Understood.
Just to clarify: given that this isn't the designed purpose for ComputeNextValue, you wouldn't recommend using it for the other indicators, but implement the intraday calc manually, similar to the SMA you outline above. Is that correct?
All the very best,
Corvin
Fred Painchaud
Hi Corvin,
I'm not 100% clear on what exactly is your intraday calc 😊 so I am not 100% positive here but I'd say yes to your question.
“Manually” does not mean that you need to start from scratch, that's something I'd like to point out - except if your intraday calc is totally unknown/new.
Fred
Corvin Codirla
Hi Fred!
Thank you for your reply. Yes, I assume I should be more clear on what I mean by intraday calc, etc.
The use case is “end-of-day” trading. The constraint is that you can't calculate anything with end of day prices and trade on end of day prices. So you have to calculate something 15 minutes before [or whatever time you choose].
Let's say you need SMA(5) at end of day. So you would have [defining P[-n]:=end of day price n days ago] at end of day today:
SMA := (P[-4] + P[-3]+P[-2]+P[-1]+P[0] ) / 5
However, P[0] is not known until end of day, so we calculate SMA 15 minutes before close.
So we calculate instead: SMA' := (P[-4] + P[-3] + P[-2] + P[-1] + P@15 minutes before close) / 5
For other indicators ComputeNextValue does this [e.g. RSI]. For SMA ComputeNextValue updates the internal window of the indicator so spoils all future calculations.
It appears that the only way to really implement this, in absence of a “what-if" for the indicator, is to do it manually, i.e. implement my own code [which is what I meant by manual], which is what I understood you meant.
Thank you very much for getting back on all this!
All the best,
C.
Fred Painchaud
Hi Corvin,
Crystal clear 😊. So now I can say a 100% yes. You need to do this manually. Your SMA' is SMA(4) Daily (the usual one, built-in), and then just do, 15 minutes before the end of the day (assuming 15h45):
(SMA(4) + close (minute or second)) / 5
so you'll have your SMA' (SMA of last 4 days close + close at 15h45 today).
Using the normal SMA(4) is what I meant by not having to do it all from scratch…
Cheers!
Fred
Vladimir
Fred Painchaud
Both formulas you provided in the previous post are wrong.
It should be like this
SMA'= (4*SMA(4) + P@15 minutes before close)/5
Fred Painchaud
Hi Vladimir,
You're right. The ‘*4’ slipped out…
Corvin: please note…
Fred
Corvin Codirla
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!