I am trying to display the total profit / return per symbol when working with strategies with multiple symbols in order to realise for which symbol the strategy works better. I use the below code, to display the total profit per symbol, but it prints the total profit every once per day for every symbol, resulting to huge logs. I only want to display the total profit per symbol once in the end of the backtest.
def OnEndOfDay(self, symbol):
if self.IsWarmingUp:
return
for sec in self.cryptos:
if self.Portfolio[sec].Invested:
returns = (self.Securities[sec].Price - self.Portfolio[sec].AveragePrice) / self.Portfolio[sec].AveragePrice
self.Debug(f"Symbol: {sec}, Total Profit: {returns:.4%}")
Mia Alissi
You can use the
OnEndOfAlgorithmmethod to print the total profit per symbol at the end of the backtest. This method is called once at the end of the algorithm execution. Here is how you can modify your code:This way, you will only get the total profit per symbol once at the end of the backtest, reducing the size of your logs.
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.
Stamatis
It works.
Mia you are far better than ChatGPT!
Stamatis
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!