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

Find if an open position is a losing position



  • Hello,
    Is there a way to find if an open position is losing, when inside the Strategy next () method?
    by losing I mean its PnL<0

    Thank you.


  • administrators

    After much thinking, there is no direct method. It is possible, but if doesn't feel natural and will for sure be modeled in a better way.

    The howto:

    • Get the position for the data of your choice
    • Get the commission info object which applies to that data
    • Calculate the current profit and loss

    In code

    def next(self):
        pos = self.getposition(self.data)  # for the default data (aka self.data0 and aka self.datas[0])
        comminfo = self.broker.getcommissioninfo(self.data)
        pnl = comminfo.profitandloss(pos.size, pos.price, self.data.close[0])
    


  • Eventually, I saved the open price in a file and read it in next() method, easy...


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.