Kiann
Kiann

Reputation: 571

Excel xlwings data input for Python Technical Indicators

I am trying to replicate a simple Technical-Analysis indicator using xlwings. However, the list/data seems not to be able to read Excel values. Below is the code

import pandas as pd
import datetime as dt
import numpy as np

@xw.func
def EMA(df, n):  
    EMA = pd.Series(pd.ewma(df['Close'], span = n, min_periods = n - 1), name = 'EMA_' + str(n))  
    df = df.join(EMA)  
    return df

When I enter a list of excel data : EMA = ({1,2,3,4,5}, 5}, I get the following error message TypeError: list indices must be integers, not str EMA = pd.Series(pd.ewma(df['Close'], span = n, min_periods = n - 1), name = 'EMA_' + str(n))

(Expert) help much appreciated! Thanks.

Upvotes: 1

Views: 224

Answers (1)

acidtobi
acidtobi

Reputation: 1365

EMA() expects a DataFrame df and a scalar n, and it returns the EMA in a separate column in the source DataFrame. You are passing a simple list of values, this is not supposed to work.

Construct a DataFrame and assign the values to the Close column:

v = range(100) # use your list of values instead
df = pd.DataFrame(v, columns=['Close'])

Call EMA() with this DataFrame:

EMA(df, 5)

Upvotes: 1

Related Questions