Skip to content Skip to sidebar Skip to footer

Calculating An Indicator Value For A Particular Date Using The Pandas_ta Package

The below code calculates the values for an indicator ( for example, say SMA) for all the values of the close price in a dataframe: import pandas_ta as pta df['SMA']= pta.sma(clos

Solution 1:

Try subsetting your dataframe, copying the result into a new one ( be sure to use df_subset.copy() in order not to change the original df ), performing your calculation, and then retrieving the last value and inserting it back into the original dataframe.

length=10
start = df.shape[0] - length
aux = df.loc[start:,:].copy()
aux['SMA']= pta.sma(close=aux['close_price'], length=10)
aux2 = aux['SMA'].tail(1)
df.loc[14, 'SMA'] = aux2.values

That way, your new calculations will always be performed on a fixed size dataframe, which is only as large as it needs to be. I'm sure there are more elegant solutions out there, but this will get the job done.

Post a Comment for "Calculating An Indicator Value For A Particular Date Using The Pandas_ta Package"