Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Multiple instances of a custom factor & managing the output in Pipeline

Hello all!

I have a custom factor which I'm using to generate Alpha signals in a pipeline.

The custom factor has several integer parameters (x and y), which are currently set to to two fixed integers.

I know from working with this custom factor in excel that there are a range of integers that will produce Alpha.

In order to increase the quantity of Alpha signals, I'd like to pass the custom factor an array of values to use as the "x" and "y" parameters. ("y" is the window_length)

A horrible way to do this (but probably the only one I'm currently capable of) would be to create a custom factor for each of the x*y permutations (would be over 300 custom factors!).

I've read that "for loops" are a bad idea in the pipe-line. Would this be an exception?

Or would it be possible to create a custom factor that had a multi-dimensional output? (not entirely sure what that means, or how I would reference it back in pipeline).

I've had a good look around on the old posts here and can't really find a similar example.

Any tips and advice appreciated!

3 responses

hi @cal

Thanks for the code and advice for the above problem.

I've put in a notebook and been tinkering with it.

One thing I'm not clear on is how to use the output from the custom factors within pipeline?

Before this current version of the algo, which is using a large number of instances of the same custom factor, I was using a single instance.

The output was easy to handle in pipeline where is used it in the logic to generate signals.

One of the logic steps was to compare the daily delta for the custom factor.

With so many different instances, i'm wondering how I would perform this delta check?

I understand a for loop within the pipeline is a no-no.

Thanks again for your help.

Made an IDE version of the above notebook, which I attach.

from quantopian.pipeline import Pipeline  
from quantopian.algorithm import attach_pipeline, pipeline_output  
#from quantopian.research import run_pipeline  
from quantopian.pipeline.factors import CustomFactor  
from quantopian.pipeline.data import USEquityPricing  
from quantopian.pipeline.filters import QTradableStocksUS  
import numpy as np 



def call_MyFactor(orda, **kwargs):  
    class MyFactor(CustomFactor):  
        def compute(self, today, asset_ids, out, high,low):  
             out[:] = ((orda*np.nanmin(high[:self.window_length],axis=0))-(orda*np.nanmin(low[:self.window_length],axis=0)))  
    return MyFactor(**kwargs)  


def make_factors():  
    factors = {}  
    for o in range(1,7):  
        for c in range(1,11):  
            factors['factor {}'.format(c*o)] = call_MyFactor(orda=o,window_length=c,inputs = [USEquityPricing.high,USEquityPricing.low])  
    return factors

def initialize(context):  
    my_pipe = Pipeline()  
    attach_pipeline(my_pipe, 'devel')

def make_pipeline():

    return Pipeline(columns=make_factors(),screen=QTradableStocksUS())



def before_trading_start(context, data):  
    context.pipeline_output= pipeline_output('devel')  
    print context.pipeline_output.head(5)  
    i = 1  
    for col in context.pipeline_output:  
        pipeline_output[col] = pipeline_output[col] * i  
        i += 1

    pipeline_output  

When I try and print the output I'm getting an empty dataframe.

My goal is to use the factors in some trade logic within "before_trading_start"

Any hints and tips appreciated.

Hi @Cal

Just updating this notebook here with my latest changes.

I'm now producing the c*o instances of the custom factors happily. This is just a silly example of the custom factor, and has no real alpha.

My problem is I do not know how to handle the custom factors in my trade logic.

If somebody could post an example to get me going that would be much appreciated?

An example may be, compare the latest price to the factor (for each stock). If the latest price is less than the factor then produce a buy signal.

Once I'm able to convert the factors into lists of daily buy and sell signals I can plug into the standard long/short trading algo.

Thanks in advance.