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



  • 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()
    


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



  • is there any solution for that ?



  • 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.



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



  • 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).



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


Log in to reply
 

});