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 can I get the price at which buy executed?

    General Code/Help
    3
    8
    3129
    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.
    • N
      nullpy last edited by

      Hello. I'm trying to make a exit strategy that clears a position if the current price is lower than the price at which buy executed for the last time, for example, like the below.

      if self.price_executed < self.dataclose[0]:
      self.order = self.sell()

      I wonder how I can get the price of the last buy execution .

      Thank you for reading this!

      1 Reply Last reply Reply Quote 0
      • Maxim Korobov
        Maxim Korobov last edited by

        There is

        def notify_order(self, order)
        

        event in strategy.

        Read the quickstart: https://www.backtrader.com/docu/quickstart/quickstart.html.

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

          The notified order contains two fields:

          • order.created

          and

          • order.executed

          The second contains further fields like price, to indicate the matching price. As pointed out by @Maxim-Korobov , the QuickStart contains code using it:

          self.log('BUY EXECUTED, %.2f' % order.executed.price)
          
          N 1 Reply Last reply Reply Quote 0
          • N
            nullpy @backtrader last edited by

            @backtrader

            Thank you for your answer! I'm following the QuickStart and now I understand that the command shows executed prices, but I actually don't understand how to use it in the function to compare the two prices. Here's the error I get and what I tried.

                Traceback (most recent call last):
            File "D:\PYTHON_SOURCE\temp.py", line 159, in <module>
              cerebro.run()
            File "D:\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 794, in run
              runstrat = self.runstrategies(iterstrat)
            File "D:\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 919, in runstrategies
              self._runonce(runstrats)
            File "D:\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1294, in _runonce
              strat._oncepost(dt0)
            File "D:\Anaconda3\lib\site-packages\backtrader\strategy.py", line 267, in _oncepost
              self.next()
            File "D:\PYTHON_SOURCE\temp.py", line 108, in next
              if order.executed.price < self.dataclose[0]:
            NameError: name 'order' is not defined
            
            
            
            def next(self): 
                self.log('Close, %.2f' % self.dataclose[0])
            
                if self.order:
                    return
            
                if not self.position:
                    if self.stoch[0] > 80:
                        self.log('BUY CREATE, %.2f' % self.dataclose[0])
                        self.order = self.buy()
                else:
                    if order.executed.price < self.dataclose[0]:    #ERROR OCCURS HERE
                        self.log('SELL CREATE, %.2f' % self.dataclose[0])
                        self.order = self.sell()
            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              You need to keep a reference to the order notified in notify_order, to later use it in next.

              N 1 Reply Last reply Reply Quote 0
              • N
                nullpy @backtrader last edited by

                @backtrader
                I'm sorry. I'm new to Object Oriented Programming and Python. Do you mean that I need to add order to next as a method parameter? I wonder why def notify_order(self, order): can get order.executed.price when it occurs error in def next(self, order):
                I would appreciate it if you could give me an example code, if it doesn't take too much of your time.

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

                  Since your nickname is also nullpy, the assumption is that also Python is not your strongest point and not only OOP.

                  A very sincere recommendation is to polish your Python skills before using backtrader or any other algorithmic trading platform. A winning algorithm is not going to be so simple and will have several interactions between not only methods of the same class/instance but with many other.

                  In any case: def next(self, order) doesn't exist. The signature of the method is in every code sample def next(self). No order is sent to next.

                  N 1 Reply Last reply Reply Quote 0
                  • N
                    nullpy @backtrader last edited by

                    @backtrader
                    Yes, I think I should study more on the language. Thank you for your recommendation. I will try to find ou how the program works!

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