For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Everyone's opinion needed :-) Are these statistics better looking/more helpful?

  • I created a simple Analyzer that calculates 'bread and butter' popular statistics, all in one place.

    TradeAnalyzer holds alot of information, just when I print output, it scrolls through several pages and therefore difficult to read.

    I also wanted to add some other basic stats like Win:Loss ratio and Reward:Risk (also called 'Risk/reward')

    I have completed the 'BasicTradeStats' Analyzer code.
    I'm now experimenting with subclassing the print() method to make the output easier to read.

    So this..


    Instead of this..


    My Questions
    It's going to take me half a day to complete the improved print output and my time is short.

    1. Will this improved output be more helpful to you guys? don't be shy, let me know :)

    2. (more for Backtrader) Are you comfortable I'm overriding the print method? The output is different than the standard output for Analyzers.

    3. Are there any other essential basic statistics that I have missed that you feel belong here? [Drawdown and Sharpe are useful, but already an Analyzer for them, and the output is fine.. so unnecessary to add here.]

    Please let me know asap, I may be able to complete this pull request with my free time tomorrow.
    Cheers guys

  • @Richard-O'Regan thank you for your work. Would be great to introduce MAE and MFE per trade.

  • Hey Ab_trader, thanks for that.
    Funny, a guy wrote a post a month ago asking for MFE&MAE and I wrote down a rough algorithm for calculating.
    Two approaches are

    1. Simple & rough: We can calculate it roughly, with trade data. e.g. for each trade open, for each period, look at the pnl value. Store highest and lowest pnl before trade closed.
      But we are limited to bar period. If daily, the close may be very different than high/low values, so MFE&MAE are at best an approximation (of course approximation gest better as granuality of bar decreases, i.e. from daily -> hourly -> minute data etc)

    2. The better way to do it: would be to read the underlying data, and find high and low for each period, and assuming no stop or target was hit, we can use the high and low for MFE&MAE etc. This is much more accurate than just the close.

    I think it would be a great feature to add, tho my main reason to not add here, is that to use properly, it would need a chart not just a table with couple of averages. e.g.

    I personally feel this is more advanced and should be a separate Analyzer dedicated to MFE&MAE with chart features.

    These stats above are more for simple basic obvious stats.

    This is just my opinion mate , if you disagree let me know, everything is doable.

    Thanks again for replying - if anyone else thinks a major basic popular statistic that fits in here, e.g. like Risk/Reward let me know. Thanks

  • administrators

    @Richard-O'Regan said in Everyone's opinion needed :-) Are these statistics better looking/more helpful?:

    1. (more for Backtrader) Are you comfortable I'm overriding the print method? The output is different than the standard output for Analyzers.

    In Python you can do monkey-patching and override the default behavior of the print method for all analyzers, or just keep it for the analyzers you want. Nothing to be comfortable or uncomfortable with. You can do it.

  • @backtrader said in Everyone's opinion needed :-) Are these statistics better looking/more helpful?:


    Noted, just I thought it would be helpful for everyone, hence why I'm considering making public in the code of my Analyzer pull-request (rather than just a monkey-patch on my local computer). Just it is breaking the default convention for BT and wanted to check would be ok by you:)

  • administrators

    The monkey-patching statement is not only addressed at your local code. Your open source code can monkey-patch the print method for any user. It may be named as: extension.

    Example usage (there are for sure many patterns)

    import backtrader as bt
    import bt_analyzer_mega_print
    # just work with your code as usual ...

    During the import bt_analyzer_mega_print your code patches the print method.

    You may try something more advance like

    import bt_analyzer_mega_print as bt

    Here your module is imported, which in turn imports backtrader internally. Before returning your replace the entry in sys.modules for your module with the imported backtrader

    Note: unsure if as bt will be a direct reference to the module in sys.modules or a variable referencing your original module. But it's worth a try.


  • Hey backtrader, I read and clicked links, didn't really understand, buts its late here and I'm tired, at computer all day. I will take a look with fresher eyes tomorrow - always something new I can learn :) cheers mate

  • administrators

    The key is here, from Van Rossum

      # module
      import sys
      class Foo:
        def funct1(self, <args>): <code>
        def funct2(self, <args>): <code>
      sys.modules[__name__] = Foo()

    This works because the import machinery is actively enabling this
    hack, and as its final step pulls the actual module out of
    sys.modules, after loading it. (This is no accident. The hack was
    proposed long ago and we decided we liked enough to support it in the
    import machinery.)

    You can then (according to the description) try:

      # module
      import backtrader
      sys.modules[__name__] = backtrader

    And if everything works as expected, importing your module has effectively imported backtrader for the external user after monkey patching.

  • Thank-you for extra explanation, just finishing off some code for BT then I will investigate your links thoroughly. Kind Regards

  • @richard-o-regan Is this code available somewhere?

  • @richard-o-regan Good afternoon. I just joined BackTrader. Long-time futures trader; technical. Years ago with aid of programmers created own linux-based backtesting and then real time trading. Now looking for back testing platform. Found QuantConnect on IB site, but others recommended Backtrader. I will be working with programmer, but based on number of years experience backtesting tens of thousands of iterations, what we did was to rather than merely take output, and rearrange, place all output into a large spd sheet. That included say all 500 iterations for a test, along with all the indiv vars, and columns such as you suggest, % pft tdes, etc. However, with spd sheet, could easily sort on columns, as well as sub-sorts. Regards,

Log in to reply