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
The CustomFactor
above is what I currently have. When I run this through my pipeline, the output is average(weekly_data[:13])
, which is the SMA
from 25wks ago. The code doesn't raise any errors, and I've tested the while loop, so I know it is running. I believe the problem is with the reassignment of the ema
variable inside the while loop. I may be having a moment of stupidity, but I can't seem to find the problem. Any advice is appreciated.
Thanks
It looks like I made a silly mistake. I used ints to calculate the alpha instead of floats. The corrected code is below.