voice32 / stock_market_indicators

A small Python library with most common stock market indicators
224 stars 108 forks source link

NameError: name 'ema' is not defined #8

Open akitxu opened 2 years ago

akitxu commented 2 years ago

Dear all. When executing the function that calculates the MACD, it gives me the following error show on the titlle.

`import pandas as pd import yfinance as yf

def macd(data, period_long=26, period_short=12, period_signal=9, column='Close'): remove_cols = [] if not 'ema' + str(period_long) in data.columns: data = ema(data, period_long) remove_cols.append('ema' + str(period_long))

if not 'ema' + str(period_short) in data.columns:
    data = ema(data, period_short)
    remove_cols.append('ema' + str(period_short))

data['macd_val'] = data['ema' + str(period_short)] - data['ema' + str(period_long)]
data['macd_signal_line'] = data['macd_val'].ewm(ignore_na=False, min_periods=0, com=period_signal, adjust=True).mean()

data = data.drop(remove_cols, axis=1)

return data

data = yf.download('AAPL', start= '2016-1-4', end = '2021-12-30')

data = macd(data, period_long=26, period_short=12, period_signal=9, column='Close') data

NameError Traceback (most recent call last) /tmp/ipykernel_12947/116451859.py in 21 data = yf.download('AAPL', start= '2016-1-4', end = '2021-12-30') 22 ---> 23 data = macd(data, period_long=26, period_short=12, period_signal=9, column='Close') 24 data

/tmp/ipykernel_12947/116451859.py in macd(data, period_long, period_short, period_signal, column) 5 remove_cols = [] 6 if not 'ema' + str(period_long) in data.columns: ----> 7 data = ema(data, period_long) 8 remove_cols.append('ema' + str(period_long)) 9

NameError: name 'ema' is not defined`

I don't understand this error. I will appreciate clarification

I don't understand this error.