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/

    how to call a custom function inside strategy class to output a list

    Indicators/Strategies/Analyzers
    3
    4
    188
    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.
    • W
      wymak last edited by

      I want to call a function inside strategy object to output a list member in the object, but it fail with
      TypeError: retrieveLog() missing 1 required positional argument: 'self'
      Please enlighten me on how to do it properly?

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      
      # Create a Stratey
      class simpleStrategy(bt.Strategy):
      
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              logLine = '%s, %s' % (dt.isoformat(), txt)
              
              # LOG TO LIST INSTEAD OF PRINTING IT OUT
              self.logStr.append(logLine)
      
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              # To keep track of log string
              self.logStr = []
      
          def next(self):
              # log to list
              self.log('test')
              
          # PRINT THE LOG LIST
          def retrieveLog(self):
              print(self.logStr)
              
       
      
       
      
      cerebro = bt.Cerebro()
      
      # Add a strategy
      cs = simpleStrategy
      cerebro.addstrategy(cs)
      
      # Add the Data Feed to Cerebro1
      cerebro.adddata(bt.feeds.PandasData(dataname=theDf))
      
      
      # Print out the starting conditions
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      # Run over everything
      cerebro.run()
      
      # Print out the final result
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      
      #  -------------- ERROR ---------------------------------
      # TypeError: retrieveLog() missing 1 required positional argument: 'self'
      cs.retrieveLog()
      
      C 1 Reply Last reply Reply Quote 0
      • C
        CDubya @wymak last edited by

        @wymak

        likely try cs.retrievelog(self)

        because your function call has "self" in the ()

        def retrieveLog(self):
        print(self.logStr)

        you could probably leave that self out in your definition

        but then again. Python is a bit screwy compared to other languages I've used, and I could be completely off base

        1 Reply Last reply Reply Quote 0
        • W
          wymak last edited by

          I am afraid the remedy is not working

          (1) had tried to add self in the parameter list, as cs.retrievelog(self)
          the result being:
          NameError: name 'self' is not defined

          (2) For definition of method in class (as in def retrieveLog(self):), I think 'self' cannot be left out

          Any suggestion?

          G 1 Reply Last reply Reply Quote 0
          • G
            goloap @wymak last edited by

            @wymak The problem is with the declaration: cs = simpleStrategy. You don't actually create a simpleStrategy object you simply assign the object type to the variable, so when you can't call cs.retrieveLog() at the end.

            The object is actually created by cerebro.run() and I don't know if there is a way to get to it from outside. A potential solution is for you to pass the log function as an argument to your strategy and then use that to log. The function can then be declared from somewhere you can get to the buffer.

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