class EWMAWeekly(CustomFactor):
inputs = [USEquityPricing.close]
window_length = (13 + 2 * 13 - 1) * 5 # Initial 13 weeks for sma, then 25 more weeks to improve the accuracy of the current ema.
def compute(
self,
today,
assets,
out,
data,
):
alpha = 2 / (13 + 1)
weekly_data = data[4::5] # len = 38, index from 0 - 37
ema = average(weekly_data[:13]) # Initial SMA
i = 0
while i < 25:
ema = weekly_data[13 + i] * alpha + ema * (1 - alpha)
i += 1
out[:] = ema
以上CustomFactor
是我目前拥有的。当我通过管道运行它时,输出是average(weekly_data[:13])
25SMA
周前的输出。该代码不会引发任何错误,并且我已经测试了 while 循环,所以我知道它正在运行。我认为问题出ema
在 while 循环内重新分配变量。我可能有片刻的愚蠢,但我似乎找不到问题所在。任何建议表示赞赏。
谢谢