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



  • You could create a dictionary in strategy.init. In notify_order: order.completed, insert an item with either the symbol or order.ref as the key, and either the order object (to later retrieve size and price), or order.executed.price as the value.


Log in to reply
 

});