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"



  • 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!


  • administrators

    @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.

    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.


Log in to reply
 

});