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

TradeAnalyzer: missing some trades, incorrect results



  • Hello,

    I am added bt.analyzers.TradeAnalyzer for my strategy. In the next of my strategy, I make buy, sell(take profit) and sell(stop loss) orders, though they are not specified like a bracket order. It looks like the following in the strategy:

    class StrategyX(Strategy):
        def __init__(self):
            self.order = None
    
        def next(self):
            if self.order:
                if takeProfitConditionIsMet:
                    self.sell(exectype=bt.Order.Market,
                                    transmit=False, parent=self.order)
                    self.order = None
                elif stopLossConditionIsMet:
                    self.sell(exectype=bt.Order.Stop,
                        transmit=True, parent=self.order)
    
            else:
                if entryConditionIsMet:
                    self.order = self.buy(exectype=bt.Order.Market, transmit=False)
    

    What might be the reason that some of the entries are actually missing in the TradeAnalyzer ( as in the amount of trades do not match correctly ) and that some stop losses show in the analyzer results as a won trade?

    EDIT: stop loss order type fixed.


  • administrators

    @eduedix said in TradeAnalyzer: missing some trades, incorrect results:

    What might be the reason that some of the entries are actually missing in the TradeAnalyzer ( as in the amount of trades do not match correctly ) and that some stop losses show in the analyzer results as a won trade?

    How are you expecting anyone to analyze anything and assume that Trades are missing if no information is provided? ( for example a log of the Trades and the information provided by the Analyzer)

    A simple inspection of the plot of the strategy could already clear out many things.



  • Should I assume that for the StrategyX above, everything looks OK? I will provide logs asap.



  • @backtrader here it is.

    I normally expect to make only one buy entry and one stop loss.

    -------------------------------------------------- ORDER BEGIN 2018-05-25 15:24:01.451527
    Ref: 1
    OrdType: 0
    OrdType: Buy
    Status: 1
    Status: Submitted
    Size: 10
    Price: None
    Price Limit: None
    TrailAmount: None
    TrailPercent: None
    ExecType: 0
    ExecType: Market
    CommInfo: None
    End of Session: 736445.9999999999
    Info: AutoOrderedDict()
    Broker: None
    Alive: True
    -------------------------------------------------- ORDER END
    -------------------------------------------------- ORDER BEGIN 2018-05-25 15:24:01.451645
    Ref: 2
    OrdType: 1
    OrdType: Sell
    Status: 1
    Status: Submitted
    Size: -10
    Price: None
    Price Limit: None
    TrailAmount: None
    TrailPercent: None
    ExecType: 3
    ExecType: Stop
    CommInfo: None
    End of Session: 736445.9999999999
    Info: AutoOrderedDict()
    Broker: None
    Alive: True
    -------------------------------------------------- ORDER END
    -------------------------------------------------- ORDER BEGIN 2018-05-25 15:24:01.451731
    Ref: 1
    OrdType: 0
    OrdType: Buy
    Status: 2
    Status: Accepted
    Size: 10
    Price: None
    Price Limit: None
    TrailAmount: None
    TrailPercent: None
    ExecType: 0
    ExecType: Market
    CommInfo: None
    End of Session: 736445.9999999999
    Info: AutoOrderedDict()
    Broker: None
    Alive: True
    -------------------------------------------------- ORDER END
    -------------------------------------------------- ORDER BEGIN 2018-05-25 15:24:01.451830
    Ref: 2
    OrdType: 1
    OrdType: Sell
    Status: 2
    Status: Accepted
    Size: -10
    Price: None
    Price Limit: None
    TrailAmount: None
    TrailPercent: None
    ExecType: 3
    ExecType: Stop
    CommInfo: None
    End of Session: 736445.9999999999
    Info: AutoOrderedDict()
    Broker: None
    Alive: True
    -------------------------------------------------- ORDER END
    -------------------------------------------------- ORDER BEGIN 2018-05-25 15:24:01.451932
    Ref: 1
    OrdType: 0
    OrdType: Buy
    Status: 4
    Status: Completed
    Size: 10
    Price: None
    Price Limit: None
    TrailAmount: None
    TrailPercent: None
    ExecType: 0
    ExecType: Market
    CommInfo: <backtrader.comminfo.CommInfoBase object at 0x105e9d198>
    End of Session: 736445.9999999999
    Info: AutoOrderedDict()
    Broker: None
    Alive: False
    -------------------------------------------------- ORDER END
    -------------------------------------------------- TRADE BEGIN 2018-05-25 15:24:01.452040
    ref:1
    data:<utils.customcsv.HistData object at 0x105e4f198>
    tradeid:0
    size:10
    price:1.01333
    value:10.1333
    commission:0.0
    pnl:0.0
    pnlcomm:0.0
    justopened:True
    isopen:True
    isclosed:False
    baropen:3936
    dtopen:736445.9895833334
    barclose:0
    dtclose:0.0
    barlen:0
    historyon:False
    history:[]
    status:1
    -------------------------------------------------- TRADE END
    -------------------------------------------------- ORDER BEGIN 2018-05-25 15:24:01.455764
    Ref: 2
    OrdType: 1
    OrdType: Sell
    Status: 4
    Status: Completed
    Size: -10
    Price: None
    Price Limit: None
    TrailAmount: None
    TrailPercent: None
    ExecType: 3
    ExecType: Stop
    CommInfo: <backtrader.comminfo.CommInfoBase object at 0x105e9d198>
    End of Session: 736445.9999999999
    Info: AutoOrderedDict()
    Broker: None
    Alive: False
    -------------------------------------------------- ORDER END
    -------------------------------------------------- TRADE BEGIN 2018-05-25 15:24:01.455853
    ref:1
    data:<utils.customcsv.HistData object at 0x105e4f198>
    tradeid:0
    size:0
    price:1.01333
    value:0.0
    commission:0.0
    pnl:0.0004999999999988347
    pnlcomm:0.0004999999999988347
    justopened:False
    isopen:False
    isclosed:True
    baropen:3936
    dtopen:736445.9895833334
    barclose:3937
    dtclose:736446.0
    barlen:1
    historyon:False
    history:[]
    status:2
    -------------------------------------------------- TRADE END
    ===============================================================================
    TradeAnalyzer:
      -----------------------------------------------------------------------------
      - total:
        - total: 1
        - open: 0
        - closed: 1
      -----------------------------------------------------------------------------
      - streak:
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - won:
          - current: 1
          - longest: 1
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - lost:
          - current: 0
          - longest: 0
      -----------------------------------------------------------------------------
      - pnl:
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - gross:
          - total: 0.0004999999999988347
          - average: 0.0004999999999988347
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - net:
          - total: 0.0004999999999988347
          - average: 0.0004999999999988347
      -----------------------------------------------------------------------------
      - won:
        - total: 1
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - pnl:
          - total: 0.0004999999999988347
          - average: 0.0004999999999988347
          - max: 0.0004999999999988347
      -----------------------------------------------------------------------------
      - lost:
        - total: 0
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - pnl:
          - total: 0.0
          - average: 0.0
          - max: 0.0
      -----------------------------------------------------------------------------
      - long:
        - total: 1
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - pnl:
          - total: 0.0004999999999988347
          - average: 0.0004999999999988347
          *************************************************************************
          - won:
            - total: 0.0004999999999988347
            - average: 0.0004999999999988347
            - max: 0.0004999999999988347
          *************************************************************************
          - lost:
            - total: 0.0
            - average: 0.0
            - max: 0.0
        - won: 1
        - lost: 0
      -----------------------------------------------------------------------------
      - short:
        - total: 0
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - pnl:
          - total: 0.0
          - average: 0.0
          *************************************************************************
          - won:
            - total: 0.0
            - average: 0.0
            - max: 0.0
          *************************************************************************
          - lost:
            - total: 0.0
            - average: 0.0
            - max: 0.0
        - won: 0
        - lost: 0
      -----------------------------------------------------------------------------
      - len:
        - total: 1
        - average: 1.0
        - max: 1
        - min: 1
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - won:
          - total: 1
          - average: 1.0
          - max: 1
          - min: 1
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - lost:
          - total: 0
          - average: 0.0
          - max: 0
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - long:
          - total: 1
          - average: 1.0
          - max: 1
          - min: 1
          *************************************************************************
          - won:
            - total: 1
            - average: 1.0
            - max: 1
            - min: 1
          *************************************************************************
          - lost:
            - total: 0
            - average: 0.0
            - max: 0
            - min: 9223372036854775807
        +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        - short:
          - total: 0
          - average: 0.0
          - max: 0
          - min: 9223372036854775807
          *************************************************************************
          - won:
            - total: 0
            - average: 0.0
            - max: 0
            - min: 9223372036854775807
          *************************************************************************
          - lost:
            - total: 0
            - average: 0.0
            - max: 0
            - min: 9223372036854775807
    

    EDIT: repost after fixing stop loss order type


  • administrators

    And what's missing? You buy, a trade is opened, you sell, a trade is closed ...



  • I was expecting this to be a lost trade since a stop loss has occurred.

    I edited both the code and the results, the order type for stop loss order was not bt.Order.Stop, fixed and reposted results.


  • administrators

    And at what price has the Sell order been executed? And coming back to the original point, what's missing?



  • the missing trades will come in a separate post. I thought it would make more sense to figure out first why the analyzer claims a winner trade instead of an expected losing one.


  • administrators

    Probably because you have a wrong understanding of what a Stop order is. It is not a stop-loss, it is a Stop, it may sell with a profit. When a trigger price is met, a Market order is executed. Unless you check the execution prices, both buy and sell it is pointless to make assumptions about a Trade being wrong.

    For the meaning of each order type, see: