Can someone describe why 'and' would work there? I've always thought with > and < that those would need to be & between them. If that makes sense. Also I thought parens are always needed for those comparisons to avoid error.
Misc: I think this might be a good place to mention in passing a caution, that the following type of thing, while it doesn't produce an error, at certain places in pipeline would only process one part of it:
(close[4] > close[3] > close[2])
... would have to be separated out:
(close[4] > close[3] ) & (close[3] > close[2])
... or something to that effect. I discovered it the hard way with the gold king & his knights algo in what now seems like ages ago and just thought it worth mentioning.
I'm thinking you might use a 'for' loop. This factor with a loop that I found seems a bit much and just thinking out loud.
for i in range(self.window_length):
if close[i] > close[i+1]
class MACD(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 60
def ema(self, data, window): # Initial value for EMA is taken as trialing SMA
import numpy as np
c = 2.0 / (window + 1)
ema = np.mean(data[-(2*window)+1:-window+1], axis=0)
for value in data[-window+1:]:
ema = (c * value) + ((1 - c) * ema)
return ema
def compute(self, today, assets, out, close):
close = nanfill(close)
fema = self.ema(close, 12)
sema = self.ema(close, 26)
macd_line = fema - sema
macd = []
macd.insert(0, self.ema(close,12) - self.ema(close,26))
for i in range(1,15, 1):
macd.insert(0, self.ema(close[:-i],12) - self.ema(close[:-i],26))
signal = self.ema(macd,9)
out[:] = macd_line - signal
I did not answer his question.