Hi, I am relatively new, and I'm working on my first real trading algo right now.
I would like to retrieve the previous day's closing price for a security after I have selected it in the mornnig. As I am adding new securities everyday, I don't think a Rolling Window will help here, because I will be actively looking for YESTERDAY's data, while the security was not necessairally in my universe yesterday (this is why I think I need to use History). Any help would be awesome, thank you!
Cesare Augustus
Here is the best I could come up with for now:
self.yesterdays_close = None one_day_history = self.History(symbol, 1, Resolution.Daily) for bar in one_day_history.itertuples(): self.yesterdays_close.Update(bar.Index[1], bar.close)
Derek Melchin
Hi Cesare,
Yesterday's closing price is provided within the CoarseFundamental objects passed to the universe selection method. Therefore, instead of making a History call each day, we can just save the price in a dictionary when we are selecting the universe. This approach is more efficient than making daily History calls.
See the attached backtest for reference.
Best,
Derek Melchin
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.
Cesare Augustus
def CoarseSelection(self, coarse):
sorted_by_dollar_volume = sorted(coarse, key=lambda x: x.DollarVolume, reverse = True)
## This just looks like an empty dictionary to me
self.yesterday_close_by_symbol = {}
for c in sorted_by_dollar_volume[:3]:
## Where does c.Price come from? It looks to me like we are just adding
## a key value pair for the symbol in the blank dictionary
self.yesterday_close_by_symbol[c.Symbol] = c.Price
return list(self.yesterday_close_by_symbol.keys())
Thank you for your prompt reply Derek.
Forgive me, but I don't understand. Where did your code call yesterday's closing price? It looks to me like it just created a blank dictionary (named self.yesterday_close_by_symbol) then added an arbitrary price (c.Price) for the first 3 symbols in sorted_by_dollar_volume. What am I missing?
Cesare Augustus
By the way, I know my indentation is off in my last comment, sorry for that. It appears that I've edited it the maximum number of times.
Derek Melchin
Hi Cesare,
The CoarseSelection method above is called each day at midnight in the backtest. When it's called, for each security in our universe, we save yesterday's closing price (`c.Price` or `c.AdjustedPrice`). The universe was limited 3 securities to speed up the backtest.
Best,
Derek Melchin
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.
Cesare Augustus
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!