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 Validity

    General Discussion
    3
    3
    1178
    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.
    • K
      KT last edited by

      Hi!! Is it possible to set the validity of an order until the next bar instead of using dates? If yes, how do I do it?

      Thanks :)

      1 Reply Last reply Reply Quote 0
      • F
        fivo last edited by

        You can still used dates to set the validity of every order to exactly one bar. Let's say your candles are 60 minutes long.

        dt = self.datas[0].datetime.datetime()
        self.sell(exectype=bt.Order.Limit,
                  size=self.position.size,
                  price=self.ema[0],
                  valid=dt + datetime.timedelta(minutes=60))
        

        If you want to make it dependent on your data, pass timeframe_in_m to your strategy.

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

          @fivo said in Order Validity:

          If you want to make it dependent on your data, pass timeframe_in_m to your strategy.

          Data feeds have a parameter named timeframe which carries the original value when the data feed was created. Using it (rather than an extra parameter) together with the chosen compression, you can get it ...

          auto_timer = dict(
              bt.TimeFrame.Seconds=lambda x: datetime.timedelta(seconds=x),
              bt.TimeFrame.Minutes=lambda x: datetime.timedelta(minutes=x),
              bt.TimeFrame.Days=lambda x: datetime.timedelta(days=x),
          )
          
          nextbar = self.data.num2date() + auto_timer[self.data.p.timeframe](self.data.p.compression)
          

          Notice self.data.num2date() which

          • If no numeric value is given uses the current value from the datetime line (i.e.: self.lines.datetime[0])
          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors