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/

    stop order issue with 1 or 2 days valid no working

    General Code/Help
    2
    2
    620
    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.
    • A
      asuralm last edited by

      I met a problem when I code the stoploss. my code looks like:

      def next(self)
      ...
      ...
      
              if self.params.modelno == 3:
                 if not self.position:
                     if self.volume[0] > self.volume[-1] and self.volume[-1] > self.volume[-2]:
                         self.log('SELL CREATED, Close: %.2f, Two consecutive day volume UP, ' % self.dataclose[0])
                         self.order = self.sell()
      
                 else:
                     valid = self.data.datetime.date(0) + datetime.timedelta(days=1)
                     #self.log('close: %.2f' %  self.dataclose[0])
      
                     price1 = self.dataclose[0]  * 1.005
                     if (self.datas[0].high[0] > price1):
                         print("**************************** this should be a stop price")
                     if price1 > 0.0 :
                        self.order = self.buy(size=abs(self.position.size), price=price1 , exectype=bt.Order.Stop, valid=valid)
                        self.log('stop price: %.2f, Valid: %s, close= %.2f, high=%.2f, close-1=%.2f' % (price1, valid, self.dataclose[0], self.datas[0].high[0], self.dataclose[-1]))
      

      The logic is defined as follow the above 0.5% of the latest close price. And the stop order is valid for just one day long, but the execution result is that the stop order is never executed even the price high has touched the stop level.
      0_1493026222184_upload-43228f91-621e-4909-8229-332881cd619e

      The execution statistics is like:
      0_1493026253719_upload-70900d23-0035-4f6c-aa11-e397f9bf4ca8

      When I set the valid timedelta to 0 or 1,
      valid = self.data.datetime.date(0) + datetime.timedelta(days=0) there is no stop hit.
      But if I change the timedelta to days=2, valid = self.data.datetime.date(0) + datetime.timedelta(days=0)it seems working alright.

      What's the problem please?

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

        It doesn't seem to be a problem. Your timeframe is daily and the resolution plays an important role:

        • timedelta(0) - You are basically expiring the order on creation. No surprise.
        • timedelta(1) - You ask for the order to expire on the next day. The next day is actually the first chance for the order to be evaluated. The broker tries to evaluate and sees you asked for the order to be expired.
        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(); }); }