Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    How to add an indicator showing whether previous trade made money or lost money

    Indicators/Strategies/Analyzers
    4
    8
    1306
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      chaT123 last edited by

      Using the moving average cross over system. But I want to add another criteria that I only buy when the previous trade made money.
      I.e I buy if MA(20) > MA(40) AND last trade made money.
      How do I implement that indicator showing whether previous trade made money or not?

      B 1 Reply Last reply Reply Quote 0
      • D
        dasch last edited by dasch

        You could store the last cost in your strategy. You would watch for finished trades and then inspect the attributes of the closed trade:

        commission (float): current accumulated commission
        pnl (float): current profit and loss of the trade (gross pnl)
        pnlcomm (float): current profit and loss of the trade minus commission (net pnl)

        The notification can be accessed in notify_trade

        You could also create an indicator the same way.

        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators @chaT123 last edited by

          @chat123 said in How to add an indicator showing whether previous trade made money or lost money:

          How do I implement that indicator showing whether previous trade made money or not?

          The concept Indicator is meant to operate on the data feeds (or information already derived thereof) and not on the money flows.

          What you are looking for, if you really want to package it, is an Analyzer which has, as the Strategy a notify_trade method and you can keep count of the positive/negative trades without mixing it up with your logic.

          Or else follow the approach mentioned by @dasch

          1 Reply Last reply Reply Quote 0
          • D
            dasch last edited by

            @backtrader i did not check if indicators get notified of trades. Learned something today :)

            Here is some code i use as an analyzer, i stripped some code, only left trade check in:

            from __future__ import (absolute_import, division, print_function,
                                   unicode_literals)
            
            import os
            
            import backtrader as bt
            
            class InMoovTradeAnalyzer(bt.Analyzer):
               params = dict(
                 path='',              # path being observed
                 range_positive=40,    # range for positive positions in pips
                 range_negative=20,    # range for negative positions in pips
                 stoploss=15,          # stoploss distance in pips
                 pips_notif=5,         # notify every n pips 
               )
            
               def __init__(self):
                   self._path = os.path.abspath(self.p.path)    # path to store files
                   self._live = False                           # notify inmoov only if live trading
                   self._lastState = None          # Store last order state (positive/negative)
                   self._lastPipDiff = None        # Last pip diff seen (pips, always positive)
            
               def notify_trade(self, trade):
                   #if trade.justopened:
                   #    self.createAction("text", "Attention, a new trade was just opened.")
                   #    self.createAction("gesture", "trade")
                   #    self.createAction("notif", "negative_1")
                       
                   if trade.isclosed:
                       # Reset informations after trade is closed
                       self._lastState = None
                       self._lastPipDiff
                       #if trade.pnl > 0:
                       #    self.createAction("text", "Trade was successfully closed. Profit is %.2f Euro." % trade.pnl)
                       #    self.createAction("gesture", "profit")
                       #else:
                       #    self.createAction("text", "Trade was closed. Negative trade with %.2f Euro." % trade.pnl)
                       #    self.createAction("gesture", "loss")
            
            
            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              Seems the right way to go, imho.

              1 Reply Last reply Reply Quote 0
              • C
                chaT123 last edited by

                Thank you all! Will I be able to use analyzer's value to determinate the next trade? I.e I buy if MA(20) > MA(40) AND last trade made money. How to code this with a analyzer showing if last trade made money?

                1 Reply Last reply Reply Quote 0
                • C
                  chaT123 last edited by

                  i.e to code the logic "buy if MA(20) > MA(40) AND last trade made money." in "def next(self):", can I use analyzer? Or best way is dasch's way above?

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader @chaT123 last edited by ab_trader

                    @chat123 I believe the best way is the clear way. If you know how to implement @dasch 's approach, than use it.

                    1 Reply Last reply Reply Quote 1
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                    $(document).ready(function () { app.coldLoad(); }); }