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

Profit



  • Hi
    How can I Take the Profit Of A trade in backtrader How can I get change stock price after sell order I tried Like This

    net_price = ((order.executed.price - self.buyprice) / self.buyprice) * 100
    

    in the code:

        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                return
    
            if order.status in [order.Completed]:
                if order.isbuy():
                    self.log(
                        'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                        (order.executed.price,
                         order.executed.value,
                         order.executed.comm))
                    self.period = order.executed.dt
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm
                else:
                    self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                             (order.executed.price,
                              order.executed.value,
                              order.executed.comm))
                    net_price = ((order.executed.price - self.buyprice) / self.buyprice) * 100
                    self.netprices.append(net_price)
                    self.periods.append(order.executed.dt - self.period)
                    print(order.executed.pnl)
                self.bar_executed = len(self)
    
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
    
            self.order = None
    

    I am new in Backtrader :)



  • @sadegh said in Profit:

    Hi
    How can I Take the Profit of A trade in backtrader How can I get change stock price after sell order I tried Like This I mean I want to get change of price after sell it by previous Buy order

    net_price = ((order.executed.price - self.buyprice) / self.buyprice) * 100
    

    in the code:

        def notify_order(self, order):
            if order.status in [order.Submitted, order.Accepted]:
                # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                return
    
            if order.status in [order.Completed]:
                if order.isbuy():
                    self.log(
                        'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                        (order.executed.price,
                         order.executed.value,
                         order.executed.comm))
                    self.period = order.executed.dt
                    self.buyprice = order.executed.price
                    self.buycomm = order.executed.comm
                else:
                    self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                             (order.executed.price,
                              order.executed.value,
                              order.executed.comm))
                    net_price = ((order.executed.price - self.buyprice) / self.buyprice) * 100
                    self.netprices.append(net_price)
                    self.periods.append(order.executed.dt - self.period)
                    print(order.executed.pnl)
                self.bar_executed = len(self)
    
            elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                self.log('Order Canceled/Margin/Rejected')
    
            self.order = None
    

    I am new in Backtrader :)



  • @sadegh said in Profit:

    How can I Take the Profit Of A trade in backtrader

    Trade information is returned in notify_trade() method.

    Some time ago I wrote trades analyser. You can use it as example of trade price processing.


Log in to reply
 

});