Problem: I have two formulas. With one I need to calculate the first number and with the second each additional.
Formula 1:
avg_gain = delta_up.rolling(window=periods, min_periods=periods).mean()[: periods 1]
this will give me the mean over period(10, 20, etc.) numbers.
Variables:
clean_data = data.dropna()
delta = clean_data.diff()
delta_up = delta.clip(lower=0)
Problem: I want to have this Formula as a Python Code which calculates every further number:
Formula 2: (previous avg_gain * (period -1) delta_up)/period
where the delta_up is the number after the previous avg_gain. so the actual delta(gain)
I have tried it like this, but it didnt worked:
for i, row in enumerate(avg_gain.iloc[periods 1 :]):
avg_gain.iloc[i periods 1] = (avg_gain.iloc[i periods] * (periods - 1)
avg_gain.iloc[i periods 1]) / periods
Output from pytest:
E Series values are different (96.7 %)
E [index]: ...
E [left]: [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 73.1359482102056, ...]
E [right]: [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 73.1359482102056, 76.23725134270262, 74.4009472344933, ...]
left is the actual data and the right are the expectation. so left data needs to be exactly like right.
rigth data is calculated with excel. (with the same formulas, but only in excel)
CodePudding user response:
I think you can simply do this by "simple moving average" code.
CodePudding user response:
The Wilder's Smoothing study is similar to the Exponential Moving Average with the difference that Wilder's Smoothing uses a smoothing factor of 1/length.
You can use this code using pandas while your data is in a dataframe. you can implement it for different periods by the argument "span".
df['D'] = df['A'].ewm(span = 10
alpha=1.0 / period,
adjust=False,
).mean()
hope it works