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/

    Slicing fails upon inputting signal generation logic

    General Code/Help
    2
    7
    170
    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.
    • R
      Ron Aw last edited by

      Good Day,

      I have the following code for my strategy:

      class Strat(bt.Strategy):
      
          params = (("WindowSize", 5), ("Dummy1", 1), ("Dummy2", 2))
      
          def __init__(self):
      
              self.DataFeed1 = self.datas[0]
              self.DataFeed2 = self.datas[1]
      
          def log(self, Input):
      
              print("Date: {0}, Text: {1}".format(self.DataFeed1.datetime.date(0), Input))
      
          def next(self): #Method that is called on every bar
      
              Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize))
              Slice2 = np.array(self.DataFeed2.close.get(ago = 0, size = self.params.WindowSize)) 
      
              self.log(Slice1)
              self.log(Slice2)
      

      The code above is able to successfully slice the current value and the previous 4 values resulting in a numpy array of size 5:

      Date: 2020-11-23, Text: [0.73049 0.73051 0.72977 0.72978 0.72948]
      Date: 2020-11-23, Text: [0.69375 0.69385 0.69314 0.69327 0.69295]
      

      However when I change the code to this:

      class Strat(bt.Strategy):
      
          params = (("WindowSize", 5), ("Dummy1", 1), ("Dummy2", 2))
      
          def __init__(self):
      
              self.DataFeed1 = self.datas[0]
              self.DataFeed2 = self.datas[1]
      
          def log(self, Input):
      
              print("Date: {0}, Text: {1}".format(self.DataFeed1.datetime.date(0), Input))
      
          def next(self): #Method that is called on every bar
      
              Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize))
              Slice2 = np.array(self.DataFeed2.close.get(ago = 0, size = self.params.WindowSize)) 
      
              self.log(Slice1)
              self.log(Slice2)
      
              Logic(Slice1, Slice2)
      

      The slicing fails and I get this:

      Date: 2013-11-25, Text: []
      Date: 2013-11-25, Text: []
      

      Is this behaviour intended and is anyone else having the same issue?

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Could you clarify what you are talking about?

        RunBacktest.com

        1 Reply Last reply Reply Quote 2
        • R
          Ron Aw last edited by

          The difference between the first strategy class and the second strategy class is the presence of the Logic(Slice1, Slice2)function.

          The Logic(Slice1, Slice2) function contains the signal generation logic for my strategy and it takes in Slice1 and Slice2 as arguments.

          Each of the slices contains close prices in the following format[t-5, t-4, t-3, t-2, t-1, t] where t indicates the current timestamp.

          Without the Logic(Slice1, Slice2) function in the next() method of the first strategy class, both Slice1 and Slice2 are able to be converted into numpy arrays with the previously mentioned format on every call of the next() method. However, including the Logic(Slice1, Slice2) in the next() method of the second strategy class magically results in both Slice1 and Slice2 becoming empty numpy arrays on every call of the next() method.

          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @Ron Aw last edited by

            @Ron-Aw said in Slicing fails upon inputting signal generation logic:

            However, including the Logic(Slice1, Slice2) in the next() method of the second strategy class magically results in both Slice1 and Slice2 becoming empty numpy arrays on every call of the next() method.

            I think the 'magical' problem is contained in, or caused by, Logic(Slikce1, Slice2), which like all good magic tricks, can't be solved when you cannot see what's going on in the trick. OK, I'm just kidding.

            You need to debug your code and we can't help you with this since a custom code is causing your problem and we don't know what your custom code is doing.

            Try creating more log outputs or debug your code in your ide.

            RunBacktest.com

            1 Reply Last reply Reply Quote 0
            • R
              Ron Aw last edited by

              I don't think that it's Logic(Slice1, Slice2) causing the issue because the error I get when running the second strategy class is TypeError: expected non-empty vector for x . It appears that the two lines:

              Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize))
              Slice2 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize))
              

              are returning empty numpy arrays whenever Logic(Slice1, Slice2) is included in the next() method. Hence, the TypeError that Logic(Slice1, Slice2) is raising is correct because Logic(Slice1, Slice2) has no values to work with. The print output below also confirms this.

              Date: 2013-11-25, Text: []
              Date: 2013-11-25, Text: []
              

              In the next() method of the second strategy class, I've tried replacing Logic(Slice1, Slice2) with Calculation = np.polyfit(Slice1, Slice2, 1)[0] and again, I get TypeError: expected non-empty vector for x. This means that the previously mentioned 2 lines are magically producing empty numpy arrays whenever something else is included in the next() method.

              1 Reply Last reply Reply Quote 0
              • run-out
                run-out last edited by

                @Ron-Aw said in Slicing fails upon inputting signal generation logic:

                alculation = np.polyfit(Slice1, Slice2, 1)[0]

                When you first run the program,

                @Ron-Aw said in Slicing fails upon inputting signal generation logic:

                Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize))
                Slice2 = np.array(self.DataFeed2.close.get(ago = 0, size = self.params.WindowSize))

                next() needs to run ("WindowSize", 5), bars before you get any values. Your Logic is trying to make calculations before the 5 bars are loaded, so you are making calculations on empty arrays.
                Simply check the length of the Slice1 and/or Slice2 before calling your Logic function.

                if len(Slice1) > 0 and len(Slice2) > 0:
                    calculation = np.polyfit(Slice1, Slice2, 1)[0]
                    self.log(calculation)
                

                Results in output:

                Date: 2019-11-04: Slice1: []
                Date: 2019-11-04: Slice2: []
                Date: 2019-11-05: Slice1: []
                Date: 2019-11-05: Slice2: []
                Date: 2019-11-06: Slice1: []
                Date: 2019-11-06: Slice2: []
                Date: 2019-11-07: Slice1: []
                Date: 2019-11-07: Slice2: []
                Date: 2019-11-08: Slice1: [63.12 63.03 63.05 63.78 63.95]
                Date: 2019-11-08: Slice2: [194.72 194.32 191.55 190.42 190.84]
                Date: 2019-11-08: Calculation: -3.41
                
                Date: 2019-11-11: Slice1: [63.03 63.05 63.78 63.95 64.46]
                Date: 2019-11-11: Slice2: [194.32 191.55 190.42 190.84 189.61]
                Date: 2019-11-11: Calculation: -2.42
                
                Date: 2019-11-12: Slice1: [63.05 63.78 63.95 64.46 64.4 ]
                Date: 2019-11-12: Slice2: [191.55 190.42 190.84 189.61 194.47]
                Date: 2019-11-12: Calculation: 0.38
                

                RunBacktest.com

                1 Reply Last reply Reply Quote 1
                • R
                  Ron Aw last edited by

                  Great, this makes so much sense now! Thank you so much for your help!

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