Backtrader Community

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

    ATR_TrailingStop Indicator

    Indicators/Strategies/Analyzers
    2
    3
    502
    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.
    • Jayden Clark
      Jayden Clark last edited by

      Hi All!

      I've been learning Backtrader over the last 4 days or so and I'm loving it!

      No matter how hard I searched, I haven't been able to find a pre-written ATR_TrailingStop indicator so I created one. I included 'Long' in the indicator name as this was written to begin calculating values from below the close price, therefore simulating a long position.

      import backtrader as bt
      from datetime import datetime
      import backtrader.indicators as btind
      import matplotlib.pyplot as plt
      %matplotlib inline
      
      #https://help.stockopedia.com/product-guide/charts/overlays/atr-trailing-stops/
      
      class ATR_TS_Long(bt.Indicator):
          
          alias = ('ATR_TRAILINGSTOP',)
          
          lines = ('atr_ts',)
          
          params = (
              ('atr_period', 14),
              ('atr_mult', 3),
          )
          
          plotinfo = dict(subplot=False,)  
          plotlines = dict(
              atr_ts = dict(ls = '-'), 
          )
             
          def __init__(self):
              
              self.atr = btind.ATR(self.data, period = self.params.atr_period)
              self.bigatr = self.atr * self.p.atr_mult 
              
              self.buysell = 1
              self.l.atr_ts = self.data.close - self.bigatr #first calculated value is below close price therefore simulating a long position.
              
              btind.CrossUp(self.data.close, self.l.atr_ts)
              btind.CrossDown(self.data.close, self.l.atr_ts)
              
      
              
          def next(self):
              
              if self.buysell > 0: 
                  self.lines.atr_ts[0] = self.data.close[0] - self.bigatr[0]
                  self.l.atr_ts[0] = max(self.l.atr_ts[-1], self.l.atr_ts[0])
                  if self.lines.atr_ts > self.data.close: 
                      self.lines.atr_ts[0] = self.data.close[0] + self.bigatr[0]
                      self.buysell = -1
      
              elif self.buysell < 0:
                  self.lines.atr_ts[0] = self.data.close[0] + self.bigatr[0]
                  self.l.atr_ts[0] = min(self.l.atr_ts[-1], self.l.atr_ts[0])
                  if self.lines.atr_ts < self.data.close:
                      self.l.atr_ts[0] = self.data.close[0] - self.bigatr[0]
                      self.buysell = 1
      

      Here is the link to the .ipynb file on Github.
      https://github.com/jantarktic/ATR_Trailing_Stop_Indicator/blob/master/btind ATR_TS_Long.ipynb

      Any feedback at all is greatly appreciated.
      Cheers!

      1 Reply Last reply Reply Quote 0
      • Bo Vargas
        Bo Vargas last edited by

        @Jayden-Clark said in ATR_TrailingStop Indicator:

        https://github.com/jantarktic/ATR_Trailing_Stop_Indicator/blob/master/btind ATR_TS_Long.ipynb

        Not able to view your Notebook on Github.

        1 Reply Last reply Reply Quote 0
        • Jayden Clark
          Jayden Clark last edited by

          @Bo-Vargas here is the link to the repository:

          https://github.com/jantarktic/ATR_Trailing_Stop_Indicator

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