Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Best way to implement Alphalens results into a backtest?

I've been using Alphalens a lot, but I had a question about implementing the Alphalens results into a successful backtest. What would be the best way to go about this (mainly regarding quantiles)?
Say I have good results from my alpha factors in Alphalens, and I want to go long/short on specific quantiles. What would be the best way to go about this?

Thanks

3 responses

If the problem is only about the quantiles the answer is short and easy: the pipeline Factor class has the method 'quantiles' which does the job.

def make_pipeline(context):  
    [...]  
    factor = MyFactor(...)  
    factor = factor.quantiles(bins=5, mask=myuniverse)  
    pipe.add(factor, "factor")  
    [...]

def before_trading_start(context, data):  
   results = pipeline_output('mypipe')  
   longs  = results[ results['factor'] == 5 ]['factor']  
   shorts = results[ results['factor'] == 1 ]['factor']  
   longs[:]  = 1.0 /  longs.sum()  
   shorts[:]  = -1.0 / shorts.sum()  

Thanks Luca,

So there's no disparity between the quantile outputs in Alphalens and through the pipeline? This is mainly what I was unsure about, I didn't know if there was something going on with Alphalens that I maybe couldn't see.

Both Alphalens and Pipiline Factor use pandas.qcut function to compute quantiles, but you can also let Pipeline computes the factor and run pandas.qcut on the results if you like.