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

Trouble interpreting TradeAnalyzer results.



  • Hi all, I am just a bit confused on how I can better format the results from running TradeAnalyzer. My output currently looks like this:

    Analysis: AutoOrderedDict([('total', AutoOrderedDict([('total', 18), ('open', 0), ('closed', 18)])), ('streak', AutoOrderedDict([('won', AutoOrderedDict([('current', 4), ('longest', 5)])), ('lost', AutoOrderedDict([('current', 0), ('longest', 1)]))])), ('pnl', AutoOrderedDict([('gross', AutoOrderedDict([('total', 7136.000000000002), ('average', 396.44444444444457)])), ('net', AutoOrderedDict([('total', 7136.000000000002), ('average', 396.44444444444457)]))])), ('won', AutoOrderedDict([('total', 15), ('pnl', AutoOrderedDict([('total', 12806.0), ('average', 853.7333333333333), ('max', 1928.999999999999)]))])), ('lost', AutoOrderedDict([('total', 3), ('pnl', AutoOrderedDict([('total', -5670.0), ('average', -1890.0), ('max', -4950.0)]))])), ('long', AutoOrderedDict([('total', 0), ('pnl', AutoOrderedDict([('total', 0.0), ('average', 0.0), ('won', AutoOrderedDict([('total', 0.0), ('average', 0.0), ('max', 0.0)])), ('lost', AutoOrderedDict([('total', 0.0), ('average', 0.0), ('max', 0.0)]))])), ('won', 0), ('lost', 0)])), ('short', AutoOrderedDict([('total', 18), ('pnl', AutoOrderedDict([('total', 7136.000000000002), ('average', 396.44444444444457), ('won', AutoOrderedDict([('total', 12806.0), ('average', 853.7333333333333), ('max', 1928.999999999999)])), ('lost', AutoOrderedDict([('total', -5670.0), ('average', -1890.0), ('max', -4950.0)]))])), ('won', 15), ('lost', 3)])), ('len', AutoOrderedDict([('total', 332), ('average', 18.444444444444443), ('max', 81), ('min', 4), ('won', AutoOrderedDict([('total', 232), ('average', 15.466666666666667), ('max', 81), ('min', 4)])), ('lost', AutoOrderedDict([('total', 100), ('average', 33.333333333333336), ('max', 59), ('min', 20)])), ('long', AutoOrderedDict([('total', 0), ('average', 0.0), ('max', 0), ('min', 9223372036854775807), ('won', AutoOrderedDict([('total', 0), ('average', 0.0), ('max', 0), ('min', 9223372036854775807)])), ('lost', AutoOrderedDict([('total', 0), ('average', 0.0), ('max', 0), ('min', 9223372036854775807)]))])), ('short', AutoOrderedDict([('total', 332), ('average', 18.444444444444443), ('max', 81), ('min', 4), ('won', AutoOrderedDict([('total', 232), ('average', 15.466666666666667), ('max', 81), ('min', 4)])), ('lost', AutoOrderedDict([('total', 100), ('average', 33.333333333333336), ('max', 59), ('min', 20)]))]))]))])
    

    I appreciate you taking the time.





  • I'm not sure if this is what you are looking for, but I use a recursive function to convert the auto dict into a more readable dictionary.

    tradean = <<<your autoordereddict above>>>
    def unnest_trade_analysis(d, trade_analysis_dict, pk=""):
        """
        Recursive function that will create layered key names and attach the values.
        Used by the trade_analysis function.
        """
        for k, v in d.items():
            if isinstance(v, dict):
                unnest_trade_analysis(v, trade_analysis_dict, pk + "_" + k)
            else:
                trade_analysis_dict[(pk + "_" + k)[1:]] = v
    
    
    trade_analysis_dict = {}
    unnest_trade_analysis(tradean, trade_analysis_dict, pk="")
    

    Results in trade_analysis_dict:

    {'total_total': 18,
     'total_open': 0,
     'total_closed': 18,
     'streak_won_current': 4,
     'streak_won_longest': 5,
     'streak_lost_current': 0,
     'streak_lost_longest': 1,
     'pnl_gross_total': 7136.000000000002,
     'pnl_gross_average': 396.44444444444457,
     'pnl_net_total': 7136.000000000002,
     'pnl_net_average': 396.44444444444457,
     'won_total': 15,
     'won_pnl_total': 12806.0,
     'won_pnl_average': 853.7333333333333,
     'won_pnl_max': 1928.999999999999,
     'lost_total': 3,
     'lost_pnl_total': -5670.0,
     'lost_pnl_average': -1890.0,
     'lost_pnl_max': -4950.0,
     'long_total': 0,
     'long_pnl_total': 0.0,
     'long_pnl_average': 0.0,
     'long_pnl_won_total': 0.0,
     'long_pnl_won_average': 0.0,
     'long_pnl_won_max': 0.0,
     'long_pnl_lost_total': 0.0,
     'long_pnl_lost_average': 0.0,
     'long_pnl_lost_max': 0.0,
     'long_won': 0,
     'long_lost': 0,
     'short_total': 18,
     'short_pnl_total': 7136.000000000002,
     'short_pnl_average': 396.44444444444457,
     'short_pnl_won_total': 12806.0,
     'short_pnl_won_average': 853.7333333333333,
     'short_pnl_won_max': 1928.999999999999,
     'short_pnl_lost_total': -5670.0,
     'short_pnl_lost_average': -1890.0,
     'short_pnl_lost_max': -4950.0,
     'short_won': 15,
     'short_lost': 3,
     'len_total': 332,
     'len_average': 18.444444444444443,
     'len_max': 81,
     'len_min': 4,
     'len_won_total': 232,
     'len_won_average': 15.466666666666667,
     'len_won_max': 81,
     'len_won_min': 4,
     'len_lost_total': 100,
     'len_lost_average': 33.333333333333336,
     'len_lost_max': 59,
     'len_lost_min': 20,
     'len_long_total': 0,
     'len_long_average': 0.0,
     'len_long_max': 0,
     'len_long_min': 9223372036854775807,
     'len_long_won_total': 0,
     'len_long_won_average': 0.0,
     'len_long_won_max': 0,
     'len_long_won_min': 9223372036854775807,
     'len_long_lost_total': 0,
     'len_long_lost_average': 0.0,
     'len_long_lost_max': 0,
     'len_long_lost_min': 9223372036854775807,
     'len_short_total': 332,
     'len_short_average': 18.444444444444443,
     'len_short_max': 81,
     'len_short_min': 4,
     'len_short_won_total': 232,
     'len_short_won_average': 15.466666666666667,
     'len_short_won_max': 81,
     'len_short_won_min': 4,
     'len_short_lost_total': 100,
     'len_short_lost_average': 33.333333333333336,
     'len_short_lost_max': 59,
     'len_short_lost_min': 20}
    
    
    


  • @Jonny8 This looks great, but I'm receiving this error:

        cerebro.addanalyzer(btanalyzers.BasicTradeStats)
    AttributeError: module 'backtrader.analyzers' has no attribute 'BasicTradeStats'
    


  • I think the BasicTradeStats exists only in rich-oregan fork of backtrader - AFAIU it wasn't merged to the main repo.

    You may find it here: https://github.com/rich-oregan/backtrader/blob/feature-TradeRecorder-DataFrameOfTrades-002/backtrader/analyzers/basictradestats.py


Log in to reply
 

});