4

I am very new to this, and looking for some help. I have a .csv file which I have pulled into a dataframe. It contains 200 days of tickers, open, high, low & close prices.

I am trying to use pandas_ta (sma) to calculate the 10, 50 & 100 day SMA. I tried did 3 commands:

df.ta.sma(length=10, append=True)
df.ta.sma(length=50, append=True)
df.ta.sma(length=100, append=True)

But I do not think this is the way. I see hundreds of variations on this, and not sure what to do.

If I try this:

df.ta.sma(df["Close"].values), 

it gives me some Keyword error on "Close". (KeyError: 'Close')

Was hoping someone can give me an example on the syntax and such. I also have to calculate RSI & MACD for this task.

Many thanks!

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Landon Statis
  • 683
  • 2
  • 10
  • 25

2 Answers2

3

Be sure to use:

import pandas_ta  as ta

And I've used it like your examples with ema, with the exception my OHLC is all lower case. Check your actual syntax: it is close or Close?

df.ta.ema(df['close'], length=14, offset=None, append=True)
Jonathan Leon
  • 5,440
  • 2
  • 6
  • 14
1

When you pass the dataframe to pandas_ta it will find the close column so you do not need to specify it. "Close". (KeyError: 'Close') is caused by the column label. You will have to rename the column to 'close' from 'Close'.

import pandas_ta as ta
df[SMA 10] = df.ta.sma(10)
df[SMA 50] = df.ta.sma(50)
df[SMA 100] = df.ta.sma(100)

Alternatively, you could do it this way as well.

sma10 = df.ta.sma(10)
sma50 = df.ta.sma(50)
sma100 = df.ta.sma(100)
df = pd.concat([df, sma10, sma50, sma100], axis=1)