Share a Hello,
I am trying to obtain the moment-in-time equity while a back test is running. I followed the instructions outlined here.
The Python example in the instructs are:
value = self.RuntimeStatistic[name]
The directions have a typo, and it should have an “s” at the end (RuntimeStatistics).
But, while the back test is running, RuntimeStatistics dictionary doesn't contain any entries. self.RuntimeStatistic.Count=0.
I am attempting to use the code:
value = self.RuntimeStatistic["Equity"]
Am I doing this wrong?
Mia Alissi
Hello Scott,
You're correct that the
RuntimeStatistics
dictionary should contain entries while the backtest is running. However, it's important to note that these statistics are updated at the end of each loop of the algorithm, so if you're trying to access them at the very start of the algorithm, they might not be populated yet.Here's the correct way to access the "Equity" runtime statistic in Python:
If you're still having trouble, it might be helpful to check if the "Equity" key is in the
RuntimeStatistics
dictionary before trying to access it:If the "Equity" key is not in the dictionary, you might want to check if your algorithm has made any trades yet, as the "Equity" statistic might not be populated until after the first trade.
If you're still having issues, please provide more details about when and how you're trying to access the
RuntimeStatistics
dictionary.Source: QuantConnect Documentation
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.
Scott Taylor
Sorry, that didn't answer it. The code gives the error: equity = self.RuntimeStatistics["Equity"]
Here is the code:
Mia Alissi
I see, thank you for providing the code. The
RuntimeStatistics
dictionary does not contain an "Equity" key by default. The default runtime statistics are: Equity, Fees, Holdings, Net Profit, PSR, Return, Unrealized, and Volume.However, you can add your own custom runtime statistics using the
SetRuntimeStatistic
method. If you want to track the equity, you can add a custom runtime statistic for it. Here's how you can do it:In this code, we're setting a custom runtime statistic called "Equity" to the current total portfolio value. Then, we're accessing this custom statistic and logging it.
Please note that the
SetRuntimeStatistic
method expects the value to be a string, so you might need to convert the portfolio value to a string before setting the statistic.Source: QuantConnect Documentation
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.
Scott Taylor
Arguing with a computer. 😊 Issue still remains. The code references “Equity”, which is a runtime statistic.
Mia Alissi
I apologize for the confusion, Scott. You're correct that "Equity" is a default runtime statistic. However, it seems there might be an issue with accessing it directly from the
RuntimeStatistics
dictionary in the middle of a backtest.The "Equity" statistic represents the total portfolio value if all of the holdings were sold at current market rates. You can directly access this value using
self.Portfolio.TotalPortfolioValue
in your algorithm.Here's how you can modify your code:
In this code, we're directly accessing the total portfolio value and logging it.
I hope this helps! If you have any other questions, feel free to ask.
Source: QuantConnect Documentation
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.
Scott Taylor
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!