Thinkscript 在他们的网站上有 Lindev 代码:
script LinDevTS {
input data = close;
input length = 12;
def avgData = Average(data, length);
plot LinDevTS = ( fold i = 0 to length with LD do LD + AbsValue(avgData - GetValue(data, i)) ) / length;
}
declare lower;
input length = 12;
#plot LinDev1 = LinDev(close, length);
plot LinDev2 = LinDevTS(close, length);
此处的原始代码:https ://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Statistical/LinDev
我正在尝试将其转换为 python,但出现关键错误。我的代码如下:
length = 12
df["avgData"] = df["close"].rolling(window=length).mean()
df["LinDevTS"] = np.repeat(nan, len(df))
for i in range(0, len(df)):
for j in range(0, length):
buffer = np.isnan(df['j'])
df['j'] = df['j'].fillna(0)
df['j'] = df['j'].astype(int)
df["GetValue"] = np.repeat(0.00, length)
for k in range(0, length):
if pd.isnull(df.close[k]) == False and k - df["j"][k] < length:
df["GetValue"][k] = df["close"][k - df["j"][k]]
df.loc[buffer, 'j'] = np.nan
df.loc[df['GetValue'] == 0.00,'GetValue'] = np.nan
df.loc[buffer, 'GetValue'] = np.nan
df["LinDevTS"][i] = df["LinDevTS"][i-1] + (abs(df["avgData"][i] - df["GetValue"][i])) / length
有任何想法吗?谢谢,