Backtrader Community

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

    wymak

    @wymak

    0
    Reputation
    1
    Profile views
    2
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    wymak Unfollow Follow

    Latest posts made by wymak

    • RE: how to call a custom function inside strategy class to output a list

      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?

      posted in Indicators/Strategies/Analyzers
      W
      wymak
    • how to call a custom function inside strategy class to output a list

      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()
      
      posted in Indicators/Strategies/Analyzers
      W
      wymak