Trying to output a list using class

Posted by captain morgan on Stack Overflow See other posts from Stack Overflow or by captain morgan
Published on 2013-11-01T21:28:23Z Indexed on 2013/11/01 21:53 UTC
Read the original article Hit count: 245

Filed under:
|

Am trying to get the moving average of a price..but i keep getting an attribute error in my Moving_Average class. ('Moving_Average' object has no attribute 'days'). Here is what I have:

class Moving_Average:

    def calculation(self, alist:list,days:int):
        m = self.days
        prices = alist[1::2]
        average = [0]* len(prices)
        signal = ['']* len(prices)
        for m in range(0,len(prices)-days+1):
            average[m+2] = sum(prices[m:m+days])/days
            if prices[m+2] < average[m+2]:
                signal[m+2]='SELL'
            elif prices[m+2] > average[m+2] and prices[m+1] < average[m+1]:
                signal[m+2]='BUY'
            else:
                signal[m+2] =''
        return average,signal




def print_report(symbol:str,strategy:str):
        print('SYMBOL: ', symbol)
        print('STRATEGY: ', strategy)
        print('Date            Closing        Strategy        Signal')


def user():
strategy = '''
Which of the following strategy would you like to use?
* Simple Moving Average [S]
* Directional Indicator[D]

Please enter your choice: '''

if signal_strategy in 'Ss':
    days = input('Please enter the number of days for the average')
    days = int(days)
    strategy = 'Simple Moving Average {}-days'.format(str(days))
    m = Moving_Average()
    ma = m.calculation(gg, days)
    print(ma)

gg is an list that contains date and prices. [2013-10-01,60,2013-10-02,60]

The output is supposed to look like:

 Date       Price      Average      Signal
2013-10-01   60.0                       
2013-10-02   60.0       60.00         BUY

© Stack Overflow or respective owner

Related posts about python

Related posts about class