-1

I am having some trouble calling one function from inside another

Here is the code I have:

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

I have the function error first to check if wrong values have been entered into the parameters.

Then I want to call that function in def SMA(...) function because I am going to have many more functions that each calculate an indicator similar to the SMA one, so to keep things concise I am trying to call error() in each of them instead of copy and pasting the code within every time.

However when doing this, I get an error message within def SMA(...) that says error() is undefined.

Thank you so much in advance if you can help me out!

GSatterwhite
  • 301
  • 1
  • 3
  • 12

1 Answers1

2

Unless I'm missing something, you just need to add self where you are calling error()

def SMA(self, indicator, timeframe, period, source):
        self.error()