从另一个内部调用一个函数时遇到麻烦
这是我的代码:
supported_timeframes = ['1m', '5m', '1h', '1d']
supported_indicators = ['SMA', ... ]
supported_sources = ['open', 'close']
indicator_values = {}
class Indicator:
def __init__(self):
self.indictor = '' # indicator
self.timeframe = '' # timeframe
self.period = 0 # look-back period
self.source = '' # open / close
def error(self, indicator, timeframe, period, source):
if timeframe not in supported_timeframes:
return 'timeframe not supported --', 'supported
timeframes:', supported_indicators
if indicator not in supported_indicators:
return 'indicator not found --', 'supported indicators:',
supported_indicators
if source not in supported_sources:
return 'source is not -- open -- or -- close'
else:
pass
def SMA(self, indicator, timeframe, period, source):
error()
# TODO get candle data from timeframe
# TODO calc requested indicator from candle data and append to
# indicator_values dict
# return indicator
for indicator in indicator_values:
return indicator
我首先遇到函数错误,以检查是否在参数中输入了错误的值。
然后我想在def SMA(...)
函数中调用该函数,因为我将拥有更多的函数,每个函数都会计算出与SMA相似的指标,因此为了简洁起见,我尝试调用{{1 }},而不是每次都复制并粘贴代码。
但是,这样做时,我在error()
中收到一条错误消息,指出def SMA(...)
是未定义的。
如果能帮助我,请提前多谢!
答案 0 :(得分:2)
除非我丢失了某些内容,否则只需在调用error()的位置添加self即可。
def SMA(self, indicator, timeframe, period, source):
self.error()