Back to Community
Question: performing calculations on recorded variables

I'd like to perform a few simple calculation on the variables in record_vars, for example calculating the average number of positions held during the backtest period. Is there a quick way to do that?

2 responses

One can retrieve the data stored in a backtest, including the recorded variables, through attributes of the AlgorithmResult object in the research environment. The first step is to get a reference to this object using the get_backtest method. This returns a AlgorithmResult object from which one can then reference the various attributes. For example, if one wanted a dataframe of the recorded variables something like this should work (make sure you use your own backtest ID) .

my_backtest_id = '5d6551e306688560ea8bb80c'  
my_backtest = get_backtest(my_backtest_id)

# Display a dataframe of the backtest's recorded variables.  

The above will return a dataframe with all recorded variable points. One can also get a list of all positions by referencing the positions attribute. The dataframes can then be copied, manipulated, plotted, and analyzed (for example the number of positions could be grouped by day and the count could be plotted over the duration of the backtest). There is a complete list of algorithm result attributes and methods in the docs (

Hope that helps.


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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

Dan, thank you so much for your clear explanation and for pointing that document out to me. Everything is clear now.