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/

    Data executed twice on end of the day

    General Code/Help
    order twice
    2
    2
    397
    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.
    • Han Yang
      Han Yang last edited by Han Yang

      I have two data samples, one is 5 minutes, one is day, and when [next] comes to close of the day, which is 13:30,
      the logic flow would run [next] twice somehow on the same bar, 2019-xx-xx 13:30:00

      I solved this issue by using simple flag to avoid double order execution.
      None of the solution works for me, add a session start, end on data feeds, specify the data to listen
      Did I implement something wrong
      https://community.backtrader.com/topic/1304/cancel-repeated-order-when-multiple-datafeed/7

      https://imgur.com/WmODcVA
      https://imgur.com/McpytIk

      Here's my code

      def runstrat(args=None):
             dtstart = dt.datetime.strptime('09:00', '%H:%M')
             dtend = dt.datetime.strptime('13:30', '%H:%M')
      
            data0 = bt.feeds.MySQLData(fromdate=dt.datetime(2018, 3, 1), todate=dt.datetime(2018, 12, 31),
                    server='local', username='trader', password='trader', stockID='TX00', KLine='5', 
                    timeframe=bt.TimeFrame.Minutes,  sessionstart=dtstart,  sessionend=dtend,compression=5)
      
           data1 = bt.feeds.MySQLData(fromdate=dt.datetime(2018, 3, 1), todate=dt.datetime(2018, 12, 31), server='local', 
                  username='trader', password='trader', stockID='TX00', KLine='0', timeframe=bt.TimeFrame.Days,
                  sessionstart=dtstart,  sessionend=dtend,compression=1 )
      
      
      def next(self):
          if self.order:
              return  # pending order execution
          mins = self.error.get(size=50)
          if not self.getposition(self.datas[0]):
              if self.mcross == 1 condition and self.data == self.datas[0]:
                  isplacedbet = True
                  trade_type = 0
                  pdist = self.atr[0] * self.p.atrdist
                  self.pstop = self.data.close[0] - pdist
                  self.order = self.sell(data=self.datas[0])
      
              if self.mcross == -1 and condition and self.data == self.datas[0]: 
                  isplacedbet = True
                  trade_type = 1
                  pdist = self.atr[0] * self.p.atrdist
                  self.pstop = self.data.close[0] - pdist
                  self.order = self.buy(data=self.datas[0])
          else:
              pclose = self.data.close[0]
              pstop = self.pstop
              if (condition) and trade_type == 0 and self.data == self.datas[0]:
                  isplacedbet = False
                  self.close(data=self.datas[0])
      			
              elif (condition) and trade_type == 1 and self.data == self.datas[0]: 
                  isplacedbet = False
                  self.close(data=self.datas[0])
              else:
                  pdist = self.atr[0] * self.p.atrdist
                  self.pstop = max(pstop, pclose - pdist)
      
      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @Han Yang last edited by

        @han-yang said in Data executed twice on end of the day:

        Here's my code

        No. That's a redacted version of something which therefore doesn't help. If you want to hide your valuable, profitable and secret intellectual property, create a small snippet which demonstrates your problem.

        @han-yang said in Data executed twice on end of the day:

        the logic flow would run [next] twice somehow on the same bar, 2019-xx-xx 13:30:00

        There is NO SOMEHOW TWICE. If one of the data feeds doesn't move forward because it cannot move forward (it would overtake the other data feed) it will deliver the same value, which is the only value available.

        You want to read the Documentation on Concepts => Platform Concepts and the section about the len of lines

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