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/

    Summing And Splicing Data

    General Code/Help
    2
    3
    31
    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.
    • D
      dmeharchand last edited by

      Hi Community,

      I'm new to programming but have a finance background.
      I'm trying to sum a backward looking range (ex take the highest value from the pervious 55 days). Not sure what I am doing wrong, any help would be greatly appreciated.

      Specifically:

              if self.dataclose[0] > max(self.data.get(0, -self.params.period)):
                  # BUY, BUY, BUY!!! (with all possible default parameters)
                  self.log('BUY CREATE, %.2f' % self.dataclose[0])
                  self.buy()
      
      import pandas as pd
      import math
      import backtrader as bt
      
      class MyFirstStrategy(bt.Strategy):
          params = dict(period55 = 55)
          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
      
          def next(self):
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
      
              if self.dataclose[0] > max(self.data.get(0, -self.params.period)):
                  # BUY, BUY, BUY!!! (with all possible default parameters)
                  self.log('BUY CREATE, %.2f' % self.dataclose[0])
                  self.buy()
      
      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Best to create your highest indicator in the init.
        See example here and reference here.

        def __init__():
            self.highest_value = bt.ind.Highest(self.data.high, period=self.p.period)
        def next():
            if self.dataclose[0] > self.highest_value[0]:
                etc...
        
        D 1 Reply Last reply Reply Quote 0
        • D
          dmeharchand @run-out last edited by

          @run-out thank you so much. Really appreciate you helping me out.

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }