Hi, I'm new to Quantopian, can someone kindly teach me how to create a factor that returns 1 when roa > 0 and returns -1 when roa < 0 in the pipeline?
Thanks so much!
Hi, I'm new to Quantopian, can someone kindly teach me how to create a factor that returns 1 when roa > 0 and returns -1 when roa < 0 in the pipeline?
Thanks so much!
Oscar, I've been playing with this for a while and have an easy way to do this kind of thing with a Custom Factor. It may not be the most efficient way, but works great. The code below should do this for you.
class ROA_binary(CustomFactor):
inputs = [morningstar.Fundamentals.roa]
window_length = 1
def compute(self, today, assets, out, ROA):
binary_factor=ROA[-1]-ROA[-1] #sets binary_factor as a variable
binary_factor[ROA[-1]<0]=-1 #outputs -1 when latest ROA is < 0
binary_factor[ROA[-1]>0]= 1 #outputs 1 when ROA is > 0
out[:] = binary_factor
make sure to have the following imported:
from quantopian.pipeline.factors import CustomFactor
and
import scipy.stats
when you have the customfactor, just add "ROA_binary()" to your pipeline
Let me know if it doesn't work for you.
Cheers,
Neil