To whom this may concern.
I'm having trouble with my algorithm.
On every market open, I want to add all the closeprices of the last 30 days. Which i do like so:
// Calling history data
var todayCloseHistory = History("SPY", todayDate, endDate);
// Adding data to lists
foreach (TradeBar bars in todayCloseHistory){
todayVolList.Add(bars.Close);
}
Later, I then want to create a variable that should be the mean value of the closeprices within the list which i do like so:
decimal mean = todayVolList.Average();
And here the backtest says that line of code contains ‘no elements’.
I have next to no experience coding, so forgive me if my method is all outta whack. But I would really appreciate some help with my issue.
Thanks in advance
:))
Varad Kabade
Hi Bertram Petersen,
We can use a RollingWindow/list to store the close prices of the last 30 days. We recommend the following snippet for using the History method for updating the window:
suppose we just want the average of the last 30 days. In that case, we recommend using the SMA indicator to receive automatic updates of the close prices and warming up the indicator with historical data:
Moving forward, we recommend going through the following docs to learn more about the RollingWindow. We have implemented a demo algorithm highlighting both methods.
Best,
Varad Kabade
Bertram Petersen
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!