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.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 dataseries self.dataclose = self.datas.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()
likely try cs.retrievelog(self)
because your function call has "self" in the ()
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
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
@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
logfunction 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.