Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with custom factor

Hello,
I'm new to quant trading, but I had an idea that I really wanted to test. I have a list of stocks and I want to build an algorithm that buys the ones with the most statistically low value of a custom factor and short the ones with the highest.
I've attached my algorithm so far and I'm having some trouble with pipeline.
I've built a 10 day moving average of this factor and a 30 day moving average. What I want is to calculate the percent difference and attach it to the pipeline and use my custom filter to screen stocks. I get an error saying that there is nothing to graph. I assume that the pieline output is different from what I think it is.
I also noticed that there are some errors with my execution code.
I would greatly appreciate any help

Thank you so much,
Dan

from quantopian.pipeline import Pipeline, CustomFilter, CustomFactor  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline.factors import Latest  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.data import morningstar  
import numpy as np

class SidInList(CustomFilter):  
    inputs = []  
    window_length = 1  
    params = ('sid_list',)  
    def compute(self, today, assets, out, sid_list):  
        out[:] = np.in1d(assets, sid_list)  
class CustomFactor_10 (CustomFactor):  
    inputs = [morningstar.balance_sheet.net_assets, morningstar.valuation.market_cap, USEquityPricing.close]  
    window_length=10  
    def compute (self, today, assets, out, net, market, close):  
        out[:] = close[-1]/ (net[-1]/market[-1])  
class CustomFactor_30 (CustomFactor):  
    inputs = [morningstar.balance_sheet.net_assets, morningstar.valuation.market_cap, USEquityPricing.close]  
    window_length=30  
    def compute (self, today, assets, out, net, market, close):  
        out[:] = close[-1]/ (net[-1]/market[-1])  
CF_10= CustomFactor_10()  
CF_30= CustomFactor_30()  
percent_diff = (CF_10-CF_30)/CF_30

def my_pipeline(context):  
    set_symbol_lookup_date('2016-1-1')  
    my_sid_filter = SidInList(  
        sid_list = (  
            symbol('AFT').sid,  
            symbol('ARDC').sid,  
            symbol('ACP').sid,  
            symbol('BHL').sid,  
            symbol('FRA').sid,  
            symbol('BGT').sid,  
            symbol('BSL').sid,  
            symbol('BGX').sid,  
            symbol('BGB').sid,  
            symbol('EFT').sid,  
            symbol('EFF').sid,  
            symbol('EFR').sid,  
            symbol('EVF').sid,  
            symbol('FCT').sid,  
            symbol('VVR').sid,  
            symbol('VTA').sid,  
            symbol('JQC').sid,  
            symbol('JRO').sid,  
            symbol('JFR').sid,  
            symbol('NSL').sid,  
            symbol('JSD').sid,  
            symbol('OXLC').sid,  
            symbol('TSLF').sid,  
            symbol('PPR').sid,  
            symbol('PHD').sid,  
            symbol('TLI').sid,  
                )  
            )  
    close = Latest(  
            inputs=[USEquityPricing.close,percent_diff],  
            mask = my_sid_filter,  
            )           

    pipe = Pipeline(  
            columns = {'close' : close,  
                       'percent_diff' : percent_diff,  
                      },  
            screen = my_sid_filter,  
            )  
    return pipe

def initialize(context):  
    attach_pipeline(my_pipeline, 'my_pipeline')  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open())  
def trade (context,data):  
    results = pipeline_output('my_pipeline')  
    securities_in_results = results.index  
    for sec in securities_in_results:  
        if data.can_trade(sec) & (percent_diff < 0):  
            order_target_percent(sec, 1.0 / len(securities_in_results))  
        if data.can_trade(sec) & (percent_diff > 0):  
            order_target_percent(sec,-1.0 / len (securities_in_results))  
2 responses

Hi Dan,

Sorry about the misleading error. What's happening is that you're calling attach_pipeline on a function (my_pipeline); that's the function that gets complained about having no .to_graph (Pipelines themselves have that method). To avoid that error, call the function to use attach_pipeline on the Pipeline object that is returned:

attach_pipeline(my_pipeline(context), 'my_pipeline')  

A second issue with your code is that you're passing percent_diff into Latest. Latest converts a BoundColumn into a Factor; BoundColumns are columns of data like USEquityPricing.close. However, percent_diff is already a Factor, since it's constructed by doing arithmetic on custom Factors. So it's not necessary to pass percent_diff into Latest; it can be used by itself as a Pipeline column already. So remove percent_diff from the inputs argument to Latest.

With those two changes your algorithm should be up and running. Let me know if there's any more trouble.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Nathan,
I appreciate the help