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

Can get_analysis() count multiple positions(trades)?



  • I want to count multiple trades.
    In my strategy, I buy() at time T and T+1, and I sell() at time T+20 and T+21.
    In this case, I desire the amount of tredes by get_analysis is 2.
    Otherwise, it return 0 and occuers keyError.
    Can't get_analysis() count multiple positions(trades)?
    plz help me.



  • get_analysis() is a method that returns a dictionary of results for a specific analyzer. You will get the data output for that analyzer. To get different results, you need to add different analyzers and then use get_analysis() for that analyzer.

    In your case, you seem to be looking for total trades? For this you could use TradeAnalyzer.

    You could add it to cerebro like this as an example, use any name you like.:

        cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="trades")
    

    Then at the end of the backtest extract the information from the strategy object returned by the back test as follows:

    ...
    reuslts = cerebro.run()
    results[0].analyzers.getbyname("trades").get_analysis()
    

    If we print the analyzer we get something like this:

    print(results[0].analyzers.getbyname("trades").get_analysis())
    
    AutoOrderedDict([('total', AutoOrderedDict([('total', 22), ('open', 1), ('closed', 21)])), ('streak', AutoOrderedDict([('won', AutoOrderedDict([('current', 0), ('longest', 2)])), ('lost', AutoOrderedDict([('current', 1), ('longest', 5)]))])), ('pnl', AutoOrderedDict([('gross', AutoOrderedDict([('total', -12.700000000000138), ('average', -0.6047619047619113)])), ('net', AutoOrderedDict([('total', -12.700000000000138), ('average', -0.6047619047619113)]))])), ('won', AutoOrderedDict([('total', 7), ('pnl', AutoOrderedDict([('total', 206.69999999999993), ('average', 29.528571428571418), ('max', 88.89999999999998)]))])), ('lost', AutoOrderedDict([('total', 14), ('pnl', AutoOrderedDict([('total', -219.40000000000006), ('average', -15.671428571428576), ('max', -31.10000000000003)]))])), ('long', AutoOrderedDict([('total', 21), ('pnl', AutoOrderedDict([('total', -12.700000000000138), ('average', -0.6047619047619113), ('won', AutoOrderedDict([('total', 206.69999999999993), ('average', 29.528571428571418), ('max', 88.89999999999998)])), ('lost', AutoOrderedDict([('total', -219.40000000000006), ('average', -15.671428571428576), ('max', -31.10000000000003)]))])), ('won', 7), ('lost', 14)])), ('short', 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)])), ('len', AutoOrderedDict([('total', 125), ('average', 5.9523809523809526), ('max', 27), ('min', 1), ('won', AutoOrderedDict([('total', 94), ('average', 13.428571428571429), ('max', 27), ('min', 1)])), ('lost', AutoOrderedDict([('total', 31), ('average', 2.2142857142857144), ('max', 7), ('min', 1)])), ('long', AutoOrderedDict([('total', 125), ('average', 5.9523809523809526), ('max', 27), ('min', 1), ('won', AutoOrderedDict([('total', 94), ('average', 13.428571428571429), ('max', 27), ('min', 1)])), ('lost', AutoOrderedDict([('total', 31), ('average', 2.2142857142857144), ('max', 7), ('min', 1)]))])), ('short', 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)]))]))]))])
    

    This clearly holds a lot of trade information. To get the total trades you need to get the 'total' section and then the total trades also labelled 'total'.

    results[0].analyzers.getbyname("trades").get_analysis()['total']['total']
    
    22
    


  • Thank you for your reply.
    I could fix my bag.
    The cause of my bag was tradeid.
    Thank you.
    https://community.backtrader.com/topic/622/multiple-order-managment


Log in to reply
 

});