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/

    Order Executed Price Error

    General Code/Help
    4
    4
    203
    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.
    • T
      TYmax54 last edited by

      class TestStrategy(bt.Strategy):
          
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' %(dt.isoformat(), txt))
              
          def __init__(self):
              self.dataclose = self.datas[0].close
              
              self.order = None
          
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  return
              
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, %.2f' % order.executed.price)
                  elif order.issell():
                      self.log('SELL EXECUTED, %.2f' % order.executed.price)
                  self.bar_executed = len(self)
                  
              elif order.status in [order.Cancelled, order.Margin, order.Rejected]:
                  self.log('Order Cancelled/Margin/Rejected')
                  
              self.order = None
              
          def next(self):
              self.log('Close, %.2f' %self.dataclose[0])
              
              if self.order:
                  return
              
              if not self.position:
              
                  if self.dataclose[0] < self.dataclose[-1]:
      
                      if self.dataclose[-1] < self.dataclose[-2]:
      
                          self.log('BUY CREATE, %.2f' %self.dataclose[0])
                          self.buy()
                          
              else:
                  
                  if len(self) >= (self.bar_executed + 5):
                      self.log('SELL CREATE, %.2f' %self.dataclose[0])
                      
                      self.order = self.sell()
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          
          cerebro.addstrategy(TestStrategy)
          
          data = bt.feeds.YahooFinanceCSVData(
              dataname='tsla.csv',
              # Do not pass values before this date
              fromdate=datetime.datetime(2014, 12, 31),
              # Do not pass values after this date
              todate=datetime.datetime(2021, 12, 31),
              reverse=False)
          
          cerebro.adddata(data)
          
          cerebro.broker.setcash(1000000.0)
          
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          
          cerebro.run()
          
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      I ran the same code as the quick start guide. I have used my own data. I got everything correctly except the buy executed and sell executed are showing value of 0 instead of buy create and sell create value. Everything is working completely fine. I don't know where I am going wrong.

      b76963ab-fe89-4cc1-9dbb-72c1742cdc9f-image.png

      This is the result I am getting.

      Thank you

      run-out D D 3 Replies Last reply Reply Quote 0
      • run-out
        run-out @TYmax54 last edited by

        Looking at your code I couldn't see anything wrong with it. So I copied and ran it using just tsla downloaded data from yahoo and it seems to run fine. Maybe try another data source?

        Starting Portfolio Value: 1000000.00
        2014-01-02, Close, 30.02
        2014-01-02, BUY CREATE, 30.02
        2014-01-03, BUY EXECUTED, 30.00
        2014-01-03, Close, 29.91
        2014-01-06, Close, 29.40
        2014-01-07, Close, 29.87
        2014-01-08, Close, 30.26
        2014-01-09, Close, 29.51
        2014-01-10, Close, 29.14
        2014-01-10, SELL CREATE, 29.14
        2014-01-13, SELL EXECUTED, 29.16
        2014-01-13, Close, 27.87
        2014-01-13, BUY CREATE, 27.87
        2014-01-14, BUY EXECUTED, 28.10
        2014-01-14, Close, 32.25
        

        RunBacktest.com

        1 Reply Last reply Reply Quote 0
        • D
          davidavr @TYmax54 last edited by

          @tymax54 Can you show the first few lines of tsla.csv? I'm wondering if for some reason the default column for the open price is empty or zero.

          1 Reply Last reply Reply Quote 1
          • D
            Dec-2 @TYmax54 last edited by

            @tymax54 i have same thing wrong with u, could u tell me how to solve this problem please? thanks!

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