G'day QuantConnect team!
How can I access my algorithm's current shape ratio, drawdown, annualised return and alpha during a backtest?
I want to plot shape ratio, drawdown, annualised return and alpha over time - while also saving their final values in an object. This should be easy once I learn how to access these statistics!
Cheers!
Mia Alissi
You can access your algorithm's current Sharpe ratio, drawdown, annualized return, and alpha during a backtest through the backtest report and the API.
The backtest report displays time series for your portfolio's rolling beta and Sharpe ratio. The chart displays the peak-to-trough drawdown of your portfolio's equity throughout the backtest period. The drawdown of each day is defined as the percentage loss since the maximum equity value before the current day. The drawdowns are calculated based on daily data. The top 5 drawdown periods are marked in the chart with different colors.
In the API, the
AlphaRuntimeStatistics
model contains insight population run time statistics. TheTotalAccumulatedEstimatedAlphaValue
field gives the total accumulated estimated value of trading all insights.Here is an example of how you can access these statistics in Python:
Please note that this code should be placed in the
OnEndOfAlgorithm
method to ensure that the statistics are calculated after the algorithm has finished processing all data.For more information, you can refer to the QuantConnect API documentation.
Please note that the statistics are calculated after the backtest has completed, so they cannot be accessed during the backtest for plotting over time. However, you can manually calculate these statistics and plot them during the backtest.
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.
Tom Frew
Oh, Mia, you've done it again! Thank you!
How would you suggest I calculate these statistics at runtime? Should I use indicators like this Sharpe Ratio Indicator?
Mia Alissi
Yes, you're on the right track! QuantConnect provides a built-in Sharpe Ratio indicator that you can use to calculate the Sharpe Ratio at runtime. Here's how you can use it in Python:
In this example,
SR
is a helper method from theQCAlgorithm
class that creates aSharpeRatio
object, hooks it up for automatic updates, and returns it so you can use it in your algorithm. You should call this helper method in theInitialize
method.For other statistics like drawdown, annualized return, and alpha, you might need to implement custom calculations. For example, you can calculate drawdown by keeping track of the highest portfolio value and comparing it with the current portfolio value. Annualized return can be calculated by taking the total return over the backtest period and raising it to the power of (1 / number of years), then subtracting 1. Alpha can be more complex to calculate, as it typically involves comparing the performance of your strategy to a benchmark.
Please note that these are simplified explanations and the actual calculations can be more complex, especially when taking into account factors like risk-free rate and trading costs. You can refer to the QuantConnect documentation for more details on how these statistics are calculated.
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.
Tom Frew
Thank you! Is there any way I can access a breakdown of stocks traded throughout the backtest? I can imagine this would be quite easy to do programmatically (with some sort of frequency map), but is there an in-built function for it?
Mia Alissi
There isn't a built-in function to get a breakdown of stocks traded throughout the backtest. However, you can easily track this information programmatically by creating a dictionary to store the symbol and its corresponding trade count. Here's an example of how you can do this in Python:
In this example,
self.symbols
is a dictionary where the keys are the symbols and the values are the trade counts. After each trade, the algorithm updates the dictionary. At the end of the algorithm, it prints the dictionary to the console.Please note that this is a simplified example and you might need to adjust it based on your specific trading logic. For example, if you're using limit orders, you might want to increment the count only when the order is filled, which you can check in the
OnOrderEvent
method.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.
Tom Frew
Sorry for the trouble! I am having a little issue accessing my algorithm's statistics! In the OnEndOfAlgorithm function, I have placed your code. However, it is throwing the error:
'TomsFactorAlgorithm' object has no attribute 'GetAlgorithmStatistics'
at OnEndOfAlgorithm
statistics = self.GetAlgorithmStatistics()
where TomsFactorAlgorithm inherits from QCAlgorithm.
What could be the issue?
Mia Alissi
I apologize for the confusion. The
GetAlgorithmStatistics
method is not available in the algorithm context during a backtest or live trading. It's a method available in the API to fetch the statistics after a backtest has completed.If you want to log the statistics at the end of the algorithm, you can use the
OnEndOfAlgorithm
method to log the trades. Here's an example from the QuantConnect forum:This will log the details of each closed trade, including the symbol, entry time, entry price, direction, quantity, exit time, exit price, profit/loss, total fees, MAE, MFE, and end trade drawdown.
For more complex statistics like Sharpe ratio, drawdown, annualized return, and alpha, you might need to implement custom calculations as I mentioned in the previous message.
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.
Tom Frew
How about this property of the algorithm? Could I use this to track SR, DD and more?
Mia Alissi
Yes, you can use the
Statistics
property of the algorithm class to track various statistics of your algorithm. TheStatisticsResults
object tracks all the statistics of your algorithm. You can access the statistics anywhere in your algorithm where you have a reference to the algorithm class.Here's how you can access the Sharpe Ratio in Python:
The
StatisticsResults
object has the following properties:TotalPerformance
: The performance of the algorithm over the whole periodRollingPerformances
: The rolling performance of the algorithm over 1, 3, 6, 12 month periodsSummary
: Returns a summary of the algorithm performance as a dictionaryTo add custom statistics to the
StatisticsResults
object, call theSetSummaryStatistic
method with the statistic name and value.To get the value of a custom statistic, index the
Summary
member with the statistic name. The values in theSummary
dictionary are strings, so you may need to cast the value that returns to a different data type.Please note that these statistics are calculated after the backtest has completed, so they cannot be accessed during the backtest for plotting over time. However, you can manually calculate these statistics and plot them during the backtest.
For more information, you can refer to the 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.
Tom Frew
Thanks, Mia! How do you respond to my questions so fast? You are a fantastic moderator. :)
Tom Frew
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!