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/

    Problem fetching execution price

    General Code/Help
    execution price order
    6
    11
    239
    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.
    • S
      stanski last edited by

      Hello everyone,

      I'm trying to exit a position if price > executed price + x.
      How can I fetch the order.executed.price?

      I tried it with _orders[data][0]but it doesn't work:

      def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  return
              if order.status in [order.Completed]:
                  if order.isbuy():
                      
                      DISTCALC = ((self.ema1[0]-order.executed.price)/order.executed.price*100)
                      self.log('BUY EXECUTED, %.2f' % order.executed.price)
                      self.log('EMA Price, %.2f' % self.ema1[0])
                      self.log('Price distance to EMA, %.2f' % DISTCALC + str('%'))
                      
                  elif order.issell():
                      self.log('SELL EXECUTED, %.2f' % order.executed.price)
                 
          def next(self):
              if not self.position and self.order==None: 
                  if self.distance and self.below800EMA:
                      self.price = self.sma1[0]
                      self.order = self.buy(self.datas[0], exectype=bt.Order.Market, size=1)
                        
                  if self.sma1[0] > self._orders[data][0]+10:
                      
                      self.order = self.close()
      
      

      The error is:
      TypeError: list indices must be integers or slices, not GenericCSVData

      vladisld run-out 2 Replies Last reply Reply Quote 0
      • vladisld
        vladisld @stanski last edited by

        @stanski _orders is designed to be only used internally in Backtrader framework I think.

        The better way to implement what you want is to probably manage an association (dictionary) between data and order (assuming only a single active order could ever be created for each data) in your own strategy class.

        1 Reply Last reply Reply Quote 2
        • run-out
          run-out @stanski last edited by

          @stanski For a non-standard, non-approved and untested answer (well, I use it all the time... )

          When you are in notify order that your new order is complete and your position is filled, and preferrably when no one is looking....

          Attach an attribute to your data line that is the value of the executed price.

          # In notify order and presuming a long only strategy for simplicity.
          if order.status in [order.Completed]:    
              if order.isbuy():    
                  order.data.enter_price = order.executed.price
          

          Then in your next:

          if self.datas[0].close[0] > self.datas[0].enter_price + x:
              <more code>
          

          RunBacktest.com

          S 1 Reply Last reply Reply Quote 1
          • S
            stanski @run-out last edited by

            @run-out I've tried it with the attached attribute, but unfortunately, I get an AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'enter_price

            run-out 1 Reply Last reply Reply Quote 1
            • run-out
              run-out @stanski last edited by

              @stanski That's why I should have tested! Missed a couple lines.

              We have to initialize the variable on the datas in init. And then use if to check if None in next.

              def __init__(self):
                  self.datas[0].enter_price = None
              
              ...
              if self.datas[0].enter_price:
                  print(f"From next: {self.datas[0].enter_price}")
              
              Output
              From next: 3239.5
              From next: 3239.5
              From next: 3239.5
              From next: 3239.5
              From next: 3239.5

              RunBacktest.com

              S 1 Reply Last reply Reply Quote 1
              • hghhgghdf dfdf
                hghhgghdf dfdf last edited by

                just use self.position.price.

                self.positions[data].price if using multiple data sets.

                1 Reply Last reply Reply Quote 0
                • S
                  stanski @run-out last edited by

                  @run-out well, whatever I do now with self.datas[0].enter_price in terms of multiplication or addition, I get a TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
                  I'm considering to discard this idea. This is actually an alternative try-out for a percentage-based take profit, which I couldn't get working in another way.
                  Maybe I should post the initial code in a new post

                  run-out 1 Reply Last reply Reply Quote 1
                  • run-out
                    run-out @stanski last edited by

                    @stanski No worries. It's just a technique I use from time to time when attaching info on datas.

                    RunBacktest.com

                    S 1 Reply Last reply Reply Quote 0
                    • S
                      stanski @run-out last edited by

                      @run-out nonetheless, thank you for sharing the idea! I just made a new post with the initial code with my take profit problem

                      1 Reply Last reply Reply Quote 0
                      • G
                        GinoPacocha last edited by

                        Well, this is not an easy task to perform and I really admire facing challenges from my side so that I can able to learn more. As I have checked this through online assignment help blogs and found that it is not so affordable.

                        1 Reply Last reply Reply Quote 0
                        • alessandro zuin
                          alessandro zuin last edited by

                          in the:

                          def next(self) 
                          ...
                              if self.position:
                                  print(self.position.price)
                          

                          it works for me, also in a parallel environment.

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors