Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    How to get value from trade.history

    Indicators/Strategies/Analyzers
    1
    2
    51
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • LiverLove
      LiverLove last edited by

      Hello.
      I can't figure out how to work with trade history.
      When I do trade_data['history'].to_string() I get a dict:

      1     [{'status': {'status': 1, 'dt': 738415.125, 'b...
      2     [{'status': {'status': 1, 'dt': 738416.8333333...
      3     [{'status': {'status': 1, 'dt': 738418.4583333...
      4     [{'status': {'status': 1, 'dt': 738420.0833333...
      5     [{'status': {'status': 1, 'dt': 738420.5833333...
      6     [{'status': {'status': 1, 'dt': 738421.0416666...
      7     [{'status': {'status': 1, 'dt': 738421.7083333...
      

      But if I try to get a value trade_data.iloc[0]['history'], I get a list

      [TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738415.125), ('barlen', 0), ('size', -0.050256307), ('price', 19898.0), ('value', -999.999996686), ('pnl', 0.0), ('pnlcomm', -0.39999999867440006), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f7fc9d028f0>), ('size', -0.050256307), ('price', 19898.0), ('commission', 0.39999999867440006)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738415.25), ('barlen', 3), ('size', -0.100634395), ('price', 19873.970956808553), ('value', -2000.005043486), ('pnl', 0.0), ('pnlcomm', -0.8000020173944), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f7fc9d03010>), ('size', -0.050378088), ('price', 19850.0), ('commission', 0.40000201872)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738416.0416666666), ('barlen', 22), ('size', -0.15069948), ('price', 19911.919904985738), ('value', -3000.715975483), ('pnl', 0.0), ('pnlcomm', -1.2002863901932002), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f7fc9d34040>), ('size', -0.050065085), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f7fc9d701f0>), ('commission', 0.4002843727988)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738416.8333333334), ('barlen', 41), ('size', 0.0), ('price', 19911.919904985738), ('value', 0.0), ('pnl', 29.600377543000118), ('pnlcomm', 27.211644913630916), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f7fc9d35180>), ('size', 0.15069948), ('price', 19715.5), ('commission', 1.188446239176)]))])]
      

      What am I doing wrong?

      LiverLove 1 Reply Last reply Reply Quote 0
      • LiverLove
        LiverLove @LiverLove last edited by

        To get the object, you need to extract the result of command trade_data.loc[[0]]['history'].values twice. Like this

        hist = trade_data.loc[[0]]['history'].values
        hist = hist[0][0]
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors