-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
assumption that all callables define __name__ attribute: #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@kernc Is there a way to get around this? |
The assumption is here: backtesting.py/backtesting/_util.py Lines 23 to 24 in 0436e56
talib.abstract.Function objects, as passed to Strategy.I() , are callable, but they don't have .__name__ . We have to accommodate for that fact so that the interpreter isn't crashed, rather that some other, suitable name is used instead.
|
getattr(value, '__name__', alternative) or some such. But what is the alternative? |
@kernc Thanks for the valuable Tip I found a way To solve it. Rather found the alternative you asked for. if callable(value):
try:
name = value.__name__.replace('<lambda>', 'λ')
except AttributeError:
name = getattr(value, '__name__', repr(value)) |
After this, I Tested To make sure Everything worked correctly class SmaCross(Strategy):
def init(self):
Ind = abstract.Function('sma')
inputs = {
'open': self.data.Open,
'high': self.data.High,
'low': self.data.Low,
'close': self.data.Close,
'volume': self.data.Volume
}
self.ma1 = self.I(Ind, inputs, 10)
self.ma2 = self.I(Ind, inputs, 20)
def next(self):
if crossover(self.ma1, self.ma2):
self.buy()
elif crossover(self.ma2, self.ma1):
self.sell()
bt = Backtest(GOOG, SmaCross,
cash=10000, commission=.002)
print(bt.run()) Results
The Results are the same as the normal way of doing it as mentioned in your |
>>> repr(talib.abstract.Function('sma'))
"{'name': 'SMA', 'group': 'Overlap Studies', 'display_name': 'Simple Moving Average', 'function_flags': ['Output scale same as input'], 'input_names': OrderedDict([('price', 'close')]), 'parameters': OrderedDict([('timeperiod', 30)]), 'output_flags': OrderedDict([('real', ['Line'])]), 'output_names': ['real']}" |
@kernc ok I after more digging I found that rather than defining the std >>> SMA = abstract.Function('sma')
>>> SMA.__dict__
{'_Function__name': b'SMA', '_Function__namestr': 'SMA', '_Function__info': {'name': 'SMA', 'group': 'Overlap Studies',............................}
>>> SMA._Function__namestr
SMA |
So this won't work. It's also too specific to talib, and we need a more generic fix, such as something derived on |
@kernc I understand your concern. But I could not find anything else that would work as a replacement for this in talib. |
@kernc perfect solution |
Callable class instances might not. The solution is generic. Fixes kernc/backtesting.py#93
@kernc I am working an issue where I need to use the abstract module of talib
I need this feature as I am dynamically getting Indicators using just function name
Running this ends up in this error
The issue seems to be way the function is constructed later in the backtesting.py
Any tips on how to get around it?
The text was updated successfully, but these errors were encountered: