Navigation

    Backtrader Community

    • Register
    • 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/

    Trade "opened"

    General Code/Help
    2
    2
    413
    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.
    • T
      tw00000 last edited by tw00000

      In: https://www.backtrader.com/docu/trade.html?highlight=trade

      It reads:

      isopen (bool): records if any update has opened the trade

      I'm wondering which of these options this refers:

      1. Trade was filled
      2. Trade was sent to broker
      3. Trade was accepted

      Here's what I'm trying to achieve:

      At the end of a backtest, I'm saving all the trades to a pickle file so that I can examine them

      I would like to know, for each order (buy / sell / close): when the order was sent to the broker and when the order was filled.

      Here's what I have so far, but I'm not sure what I'm getting from trade.dtopen:

      for index, data in enumerate(strategy.datas):
              dataname = data._name
      
              data_trades = strategy._trades[data][0]
              trades = []
              for trade in data_trades:
                  try: 
                      trades.append({'ticker' : trade.data._name,
                                      'bar_closed' : len(trade.data),
                                      'trade_ref' : trade.ref,
                                      'pnl' : round(trade.pnl, 2),
                                      'bar_opened' : trade.baropen,
                                      'date_opened' : trade.data.num2date(trade.dtopen),
                                      'bar_closed' : trade.barclose,
                                      'date_closed' : trade.data.num2date(trade.dtclose),
                                      'n_bars_open_for' : trade.barlen,
                                      'entry_trade' : trade.history[0]['event']['order'].info,
                                      'exit_trade' : trade.history[1]['event']['order'].info,
                                      'entry_price' : trade.history[0]['event']['price'],
                                      'exit_price' : trade.history[1]['event']['price']
                                      })
                  except Exception as e:
                      pass
      

      Thanks for any assistance!

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @tw00000 last edited by backtrader

        @tw00000 said in Trade "opened":
        I'm wondering which of these options this refers:

        1. Trade was filled
        2. Trade was sent to broker
        3. Trade was accepted

        None of them, because a Trade is not an Order. A trade refers only to the position in an asset and can comprise multiple orders in different directions.

        • Docs - Trade

        It is clearly defined at the beginning:

        Definition of a trade:
        
            - A Trade is open when the a position in a instrument goes from 0 to a size X which may positive/negative for long/short positions
            - A Trade is closed when a position goes from X to 0.
        

        @tw00000 said in Trade "opened":

        I would like to know, for each order (buy / sell / close): when the order was sent to the broker and when the order was filled.

        You should then work with Order instances and not with Trade instances.

        • Docs - Orders
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors