For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

ATR problem



  • Can someone put ATR into my code? I get that you can either build one yourself , utilize a built-in indicator, or use a third-party's but I am not sure how to use the make one myself or even use the built-in.

    I am trying to use the ATR as stop loss(Exiting a long position).

    import datetime
    import backtrader as bt
    
    # Create a subclass of Strategy to define the indicators and logic
    
    class SmaCross(bt.Strategy):
        # list of parameters which are configurable for the strategy
        params = dict(
            pfast=10,  # period for the fast moving average
            pslow=30   # period for the slow moving average
        )
    
        def __init__(self):
            sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
            sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
            self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
            
    
        def next(self):
            if not self.position:  # not in the market
                if self.crossover > 0:  # if fast crosses slow to the upside
                    self.buy()  # enter long
    
            elif self.crossover < 0:  # in the market & cross to the downside
                self.close()  # close long position
    
    
    cerebro = bt.Cerebro()  # create a "Cerebro" engine instance
    
    data = bt.feeds.YahooFinanceCSVData(
        dataname="oracle.csv",
        fromdate=datetime.datetime(2000, 3, 1),
        todate=datetime.datetime(2000, 8, 29),
        reverse=False)
    cerebro.adddata(data)
    
    cerebro.addstrategy(SmaCross)  # Add the trading strategy
    cerebro.run()  # run it all
    cerebro.plot(style="candlestick",barup='green', bardown='red')
    


  • I found a blog on how to build your own indicator, and the example is an ATR indicator.

    40a188de-c30a-4b00-9741-ad7cef6ea082-image.png

    I tried running this but somethings not right

    class ATR (bt.indicator):
            range_total = 0
            for i in range(-13, 1):
                true_range = self.datahigh[i] - self.datalow[i]
                range_total += true_range
            ATR = range_total / 14
    
       def next(self):
            # CLOSING LOGS
            self.log('Close, %.2f, ATR: %.4f' % (self.dataclose[0], AtrStop))
    

    c80fcac0-0ff2-4a98-97d1-af0b26caf53c-image.png



  • Average True Range is a built in indicator found here. Build in indicators are mostly added during the init() of the strategy class. So for example, in your code:

    
        def __init__(self):
            sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
            sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
            self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
            self.my_atr = bt.ind.ATR(period=14)
    

    You can now access the value of your ATR at each bar in next:

    def next(self):
        if self.my_atr[0] > 'whatever':
             'do something'
    

    Spend some time on the basic concepts and quickstart in the documentation. This will help you a lot.


Log in to reply
 

});