I'm trying to work through some basics for my first trial algorithm. Questions:
- I'm trying to do a simple SPY/TLT basket. I set up SPY and TLT exactly the same. SPY works; TLT says it has no data and I should warm it up. I tried but it's not working. Why does TLT act differently than SPY, and what am I doing wrong? Why does an equity symbol require warmup? How should I handle the warmup requirement? Code & output below.
- lean cloud backtest --open gives you a nice backtest report (is that what can be attached to a post here?) and a simple tearsheet. But local backtests don't produce any report at all, except a couple of log files and JSON files that aren't very human-readable. I can pick out some stats but I'm not very good at interpreting a JSON equity curve. I thought maybe lean research <project> might do it, but that just opens a basically-empty notebook, with no connection to your project as far as I can see. Is there a better way to view local backtest results?
+ Expand
class Test1(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2013, 1, 1) # Set Start Date
self.SetEndDate(2021, 1, 1) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.spy = self.AddEquity("SPY", Resolution.Daily)
self.tlt = self.AddEquity("TLT", Resolution.Daily)
self.storeYear = self.Time.year
self.SetWarmUp(200) ### Just picked a random value to try to satisfy the warmup requirement
def OnData(self, slice):
if self.IsWarmingUp: ### Should I use slice.HasData? Or slice.ContainsKey? Or ... ??
return
if not self.Portfolio.Invested:
self.SetHoldings("SPY", .5)
self.SetHoldings("TLT", .5)
self.Debug(f'Opened position on {self.Time:%Y-%m-%d}: {self.Portfolio["SPY"].Quantity} SPY, {self.Portfolio["TLT"].Quantity} TLT')
# Rebalance basket every year
if self.Time.year != self.storeYear:
self.storeYear = self.Time.year
self.SetHoldings("SPY", .5)
self.SetHoldings("TLT", .5)
self.Debug(f'Rebalance on {self.Time:%Y-%m-%d}: {self.Portfolio["SPY"].Quantity} SPY, {self.Portfolio["TLT"].Quantity} TLT')
It doesn't buy any TLT at all, and in fact the initial position doesn't buy SPY either. ?? Debug output: (with IsWarmingUp)
2021-06-28T15:45:33.8638945Z TRACE:: Debug: Algorithm finished warming up.
Opened position on 2013-01-01: 0.0 SPY, 0.0 TLT
2021-06-28T15:45:33.8653526Z ERROR:: TLT: The security does not have an accurate price as it has not yet received a bar of data. Before placing a trade (or using SetHoldings) warm up your algorithm with SetWarmup, or use slice.Contains(symbol) to confirm the Slice object has price before using the data. Data does not necessarily all arrive at the same time so your algorithm should confirm the data is ready before using it. In live trading this can mean you do not have an active subscription to the asset class you're trying to trade. If using custom data make sure you've set the 'Value' property.
2021-06-28T15:45:34.0162317Z TRACE:: Debug: Rebalance on 2014-01-01: 410.0 SPY, 0.0 TLT
2021-06-28T15:45:34.1172608Z TRACE:: Debug: Rebalance on 2015-01-01: 358.0 SPY, 0.0 TLT
2021-06-28T15:45:34.2180536Z TRACE:: Debug: Rebalance on 2016-01-01: 337.0 SPY, 0.0 TLT
2021-06-28T15:45:34.3188668Z TRACE:: Debug: Rebalance on 2017-01-04: 335.0 SPY, 0.0 TLT
2021-06-28T15:45:34.4225254Z TRACE:: Debug: Rebalance on 2018-01-03: 316.0 SPY, 0.0 TLT
2021-06-28T15:45:34.5233399Z TRACE:: Debug: Rebalance on 2019-01-01: 288.0 SPY, 0.0 TLT
2021-06-28T15:45:34.6242421Z TRACE:: Debug: Rebalance on 2020-01-01: 295.0 SPY, 0.0 TLT
2021-06-28T15:45:34.6954721Z TRACE:: Synchronizer.GetEnumerator(): Exited thread.
I tried using slice.ContainsKey("TLT") and it didn't get the error, but it didn't enter any trades either …
Jasper van Merle
Hi Gary,
The first issue is most likely a data issue. When running a local backtest you need to have local data for all securities your algorithm subscribes to. We've got extensive documentation on local data and generally recommend downloading the required data from the QuantConnect Data Library using the CLI or using LEAN. This allows you to use the same data locally as is used in the cloud.
Regarding the second issue, we currently don't have a GUI for local backtest results like we have for backtests ran in the cloud. This does not leave you without options though:
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.
Gary Fritz
Thanks Jasper! I probably should have figured this out on my own, but I'm suffering from a bit of overwhelm …
Re: data. The Pricing page says “Free Plan Includes: Equity, Forex, Crypto, and Futures Datasets.” So I thought I would have free access to daily equity data. But apparently not? lean data download says "Error: Your organization needs to have an active Security Master subscription to download Equity data." I assume that's one of the paid monthly plans? Then in addition I pay 10 QCC/symbol for daily data? (Also, it would be nice if it gave a more helpful error message, instead of sending me down the warmup rabbit-hole …)
lean report – yes, that looks promising. I'll give it a more thorough test when I actually get a successful backtest. 😊 https://www.quantconnect.com/docs/v2/lean-cli/tutorials/research has code to retrieve local backtests, but I believe it's C#. Do you have the Python equivalent somewhere? I found many requests for a Python implementation, but no code. I would just translate it, but I don't know what to include &etc.
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.
Gary Fritz
Duh! I totally missed the Python button. Apparently I'm not the only blind one, given by how many requests I saw for Python code. 😊
Thanks! I think that tells me what I need for now. I'm sure I'll be back for more!
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!