Hi,
I'm really having a hard time mastering this whole Custom Factor business. I seem to have all the basics down pretty solidly, but there are certain things where I just seem to go in circles. So I'm hoping one of you all has found some exercises to help with mastering the data structures for more complex calculations.
As for my current task, I'm simply trying to call the pipeline one single time, and in that pipeline I would like to call a Custom Factor that will reference a dictionary based on the sector code, and then calculate the ratio spread with the current (assets close)/(sector etf close).
Is there an easier way to do this?
Here is what I was trying but getting nowhere with.
Thanks in advance!
class Spread_Ratio(CustomFactor):
"""
Ratio Spread Based on Input Stock Symbol
"""
inputs=[USEquityPricing.close, ac.morningstar_sector_code]
window_length = 1
def compute(self, today, assets, out, close, sectors):
prices = close
#etf = []
#for x in sectors[-1]:
# etf.append(sector_ETFs[x].sid)
# get index of benchmark
benchmark_index = np.where((assets == sector_ETFs[sectors[-1]].sid) == True)[0][0]
# get returns of benchmark
benchmark_prices = prices[-1, benchmark_index]
out[:] = close[-1, :]/benchmark_prices
sector_ETFs = {
101: symbols('XLB'),
102: symbols('XLY'),
103: symbols('XLF'),
104: symbols('IYR'),
205: symbols('XLP'),
206: symbols('XLV'),
207: symbols('XLU'),
308: symbols('IYZ'),
309: symbols('XLE'),
310: symbols('XLI'),
311: symbols('XLK')
}