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/

    Open and exit at specific time

    General Code/Help
    3
    5
    48
    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.
    • kian hong Tan
      kian hong Tan last edited by

      Hello community, I wanted to test opening and exiting a trade everyday at a specific hour of the day. Below is my code. There is no error msg but there is no trade being opened. Hope you can assist me on this. Many tks.

      class daily_strategy(bt.Strategy):
          params = (('entrytime', 1), ('exittime', 12),('printlog', False))  
                   #open trade at 1:00 UTC, exit at 12:00 UTC 
      
          def log(self, txt, dt=None):
              ''' Logging function fot this strategy'''
              if self.params.printlog: 
                  dt = dt or self.datas[0].datetime.datetime(0).strftime('%Y-%m-%d %H:%M:%S')
                  print('%s, %s' % (dt, txt))   
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              # BUY & SELL Signal Conditions
      
              self.buy_sig = self.datas[0].datetime.datetime(0).strftime('%H') == self.params.entrytime
              self.buy_exit = self.datas[0].datetime.datetime(0).strftime('%H') == self.params.exittime
      
          def next(self):
              self.log('Close, %.2f' % self.dataclose[0])
      
              if self.order:
                  return
      
              if not self.position.size and self.buy_sig :
      
                  self.log('BUY CREATE, %.2f' % self.dataclose[0])
                  self.order = self.buy()
                     
              
              if self.position.size > 0 and self.buy_exit:
      
                  self.log('EXIT BUY CREATE, %.2f' % self.dataclose[0])
                  self.order = self.close()
      
      R 1 Reply Last reply Reply Quote 0
      • R
        rajanprabu @kian hong Tan last edited by

        @kian-hong-tan

        You can keep the date condition is next itself..

        class daily_strategy(bt.Strategy):
            params = (('entrytime', 1), ('exittime', 12),('printlog', False))  
                     #open trade at 1:00 UTC, exit at 12:00 UTC 
        
            def log(self, txt, dt=None):
                ''' Logging function fot this strategy'''
                if self.params.printlog: 
                    dt = dt or self.datas[0].datetime.datetime(0).strftime('%Y-%m-%d %H:%M:%S')
                    print('%s, %s' % (dt, txt))   
        
            def __init__(self):
                # Keep a reference to the "close" line in the data[0] dataseries
                self.dataclose = self.datas[0].close
        
            def next(self):
                self.log('Close, %.2f' % self.dataclose[0])
        
                if self.order:
                    return
        
                if not self.position and self.datas[0].datetime.time() == datetime.time(self.params.entrytime , 0) :
        
                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
                    self.order = self.buy()
                       
                
                if self.position > 0 and self.datas[0].datetime.time() == datetime.time(self.params.exittime , 0) :
        
                    self.log('EXIT BUY CREATE, %.2f' % self.dataclose[0])
                    self.order = self.close()
        
        vladisld kian hong Tan 2 Replies Last reply Reply Quote 2
        • vladisld
          vladisld @rajanprabu last edited by

          Another alternative is to use timers: https://www.backtrader.com/docu/timers/timers/

          kian hong Tan 1 Reply Last reply Reply Quote 2
          • kian hong Tan
            kian hong Tan @rajanprabu last edited by

            @rajanprabu It works. Tks for your guidance!

            1 Reply Last reply Reply Quote 0
            • kian hong Tan
              kian hong Tan @vladisld last edited by

              @vladisld thank you. Will explore further.

              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(); }); }