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/

    Crossing Problem

    General Discussion
    2
    7
    94
    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.
    • S
      sadegh last edited by

      Hi
      I am using two cross indicators like this

      Capture.PNG

      but my qustion is why it find crossing with delay !

      my code is this for Strategy

      import backtrader as bt
      import numpy as np
      
      
      class BBANDS(bt.Strategy):
          params = (('fast', 9), ('slow', 26))
      
          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.netprices = []
              self.periods = []
              self.period = None
              self.Log = False
              self.dataclose = self.datas[0].close
              self.order = None
              self.buyprice = None
              self.buycomm = None
              self.buytime = None
              self.bbands_mid = bt.indicators.BollingerBands(self.data).lines.mid
              self.bbands_bot = bt.indicators.BollingerBands(self.data).lines.bot
              self.bbands_cross_bot = bt.indicators.CrossOver(self.data, self.bbands_bot, plotname='Cross Bottom Line')
              self.bbands_cross_mid = bt.indicators.CrossOver(self.data, self.bbands_mid, plotname='Cross Mid Line')
      
          def next(self):
      
              if self.Log:
                  self.log('Close, %.2f' % self.dataclose[0])
      
              if not self.position:
                  if self.bbands_cross_bot > 0:
                      self.order = self.buy()
      
              else:
      
                  if self.bbands_cross_mid > 0:
                      self.order = self.sell()
      
      1 Reply Last reply Reply Quote 0
      • S
        sadegh last edited by

        it has one day delay for finding cross and one day delay for buying that !!!!

        1 Reply Last reply Reply Quote 0
        • S
          sadegh last edited by

          is there any solution for that ?

          1 Reply Last reply Reply Quote 0
          • D
            dasch last edited by

            you use one day data, so the candle will be available the next day. the result is as expected. you would need to replay intraday data to watch for the cross to create orders.

            1 Reply Last reply Reply Quote 0
            • S
              sadegh last edited by

              But why buy signal create after indicator cross? is there any solution to do that!

              1 Reply Last reply Reply Quote 0
              • D
                dasch last edited by

                if you want to look at a candle from yesterday, then it is finished at the end of the day. This means you can look at that candle when it is available, which is the next day. Then the cross is next day.

                If you want to look for the cross intraday, you would need data with a smaller timeframe. a candle is available after the period is over (when the candle is fully formed).

                1 Reply Last reply Reply Quote 0
                • D
                  dasch last edited by

                  so you could use intraday data and replay it to 1day timeframe

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