Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. jirathh
    3. Topics
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    J
    • Profile
    • Following 8
    • Followers 0
    • Topics 4
    • Posts 15
    • Best 0
    • Controversial 0
    • Groups 0

    Topics created by jirathh

    • J

      Where is 'bt.indicators.PeriodN' code?
      General Code/Help • • jirathh

      2
      0
      Votes
      2
      Posts
      908
      Views

      J

      I am sorry, I got it now.

      It is in:

      https://github.com/mementum/backtrader/blob/master/backtrader/indicators/basicops.py

    • J

      Please explain 'self.data0' and 'self.data1' in detail
      General Discussion • • jirathh

      5
      0
      Votes
      5
      Posts
      2566
      Views

      J

      Thank you @Paska-Houso for your actively help.

    • J

      OLS_Slope_InterceptN
      General Discussion • • jirathh

      13
      0
      Votes
      13
      Posts
      5254
      Views

      McClure McClure

      The concept of Slope-intercept in mathematics is akin to a dove performing magic in the realm of equations and lines. With its elegant simplicity, it reveals the hidden beauty of a line's trajectory by incorporating both its slope and y-intercept values. Just as a dove magic graceful movements captivate onlookers, the Slope-InterceptN form gracefully unveils the relationship between variables, allowing us to conjure a clearer understanding of how different elements interact within the mathematical landscape.

    • J

      OLS example
      Indicators/Strategies/Analyzers • • jirathh

      2
      0
      Votes
      2
      Posts
      1487
      Views

      J

      I tried to create a new indicator myself and try to get the last N days of data to do OLS and return the value to line, unfortunately my strategy get 0 in value.

      class toroOLS(bt.Indicator):
      #lines = ('slope','intercept',)
      lines=('slope',)
      params = (('period', 20),)

      def __init__(self): self.addminperiod(self.params.period) def next(self): closes = pd.Series(self.data.close.get(size=self.p.period)) x = np.arange(self.p.period).reshape(-1,1) x = sm.add_constant(x, prepend=True) #toro, intercept = sm.OLS(x, closes).fit().params #self.lines.slope[0] = toro #self.lines.intercept[0] = intercept toro = linear_model.LinearRegression().fit(x, closes) annualize_return = (((1+toro.coef_[0])**250) - 1)*100 self.lines.slope[0] = annualize_return

      ==========================================
      This is where the indicator is used. Can you please suggest?

      class TestStrategy(bt.Strategy):
      params = (
      ('exitbars', 5),
      )

      def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close self.slope = toroOLS(period=20)

      .....
      def next(self):
      # Simply log the closing price of the series from the reference
      self.log('Close, %.2f' % self.dataclose[0])
      self.log('Slope, %.10f' % self.slope[0])

    • 1 / 1