Python Forum

Full Version: TypeError that i can't solve it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
class MACD_Strategy:
    def __init__(self, stock_price, MACD, MACD_Signal):
        self.stock_price = stock_price
        self.MACD = MACD
        self.MACD_Signal = MACD_Signal
    
    def trade(self):
        if MACD[-1] < MACD_Signal[-1] and MACD[-2] > MACD_Signal[-2]:
            print(stock_price[0])

        elif MACD[-1] > MACD_Signal[-1] and MACD[-2] < MACD_Signal[-2]:
            print(stock_price[0]*-1)
    
I want to return stock price that fulfill the criteria set out in def trade function.

then i move to the next

Testing = MACD_Strategy(China_Mobile['China Mobile Ltd - Last Price'], China_Mobile['MACD'], China_Mobile['MACD_Signal'])
Testing.trade()
Chnina_Mobile is a dataframe consisting date, last price, MACD and MACD signal.

then i got the error

Error:
TypeError Traceback (most recent call last) <ipython-input-140-4ca2b450b15b> in <module> ----> 1 Testing.trade() <ipython-input-138-debb79fae608> in trade(self) 6 7 def trade(self): ----> 8 if MACD[-1] < MACD_Signal[-1] and MACD[-2] > MACD_Signal[-2]: 9 print(stock_price[0]) 10 TypeError: 'type' object is not subscriptable
PLease help
Apperently the parameters you give are not as expected. Try to play with the parameters. Inspect the contents of the structure "China_Mobile".