Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
CVXPY in pipeline factor?

CVXPY was recently released on the Quantopian platform. Can it be used within a pipeline custom factor?

4 responses

It will work. There's nothing particularly special about the code in custom factors; if something works elsewhere, it works in a factor.

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 Dan. I'll give it a go, at some point. I was under the impression that the pipeline computations need to be zippy, and that an optimizer might not be efficient enough. I guess that brings up the question, will the new Quantopian optimization API work within pipeline custom factors, and based on your response above, the answer would also be 'yes'.

Got this running, but there is a CVXPY error:

SolverError: Solver 'ECOS' failed. Try another solver.
There was a runtime error on line 44.

At this point, the custom factor idea is half-baked; I'm just trying to see how to use CVXPY within a custom factor.

import cvxpy as cvx  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline import CustomFactor  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.data import morningstar as mstar  
import numpy as np  
from quantopian.pipeline.filters import Q1500US

class OptRev(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 20  
    def compute(self, today, assets, out, close):  
        x_tilde = np.mean(close,axis=0)/close[-1,:]  
        m = len(x_tilde)  
        b_t = 1.0*np.ones(m)/m  
        x = cvx.Variable(m)  
        objective = cvx.Minimize(cvx.sum_entries(cvx.square(x-b_t)))  
        constraints = [cvx.sum_entries(x) == 1, cvx.sum_entries(x_tilde*x) >= 1]  
        prob = cvx.Problem(objective, constraints)  
        prob.solve()  
        out[:] = np.squeeze(np.asarray(x.value))  
def initialize(context):  
    attach_pipeline(make_pipeline(context), 'my_pipe')  
def make_pipeline(context):  
    profitable = mstar.valuation_ratios.ev_to_ebitda.latest > 0  
    market_cap = mstar.valuation.market_cap.latest  
    top_market_cap = market_cap.top(100, mask = (Q1500US() & profitable))  
    optrev = OptRev()

    return Pipeline(columns={'optrev': optrev},screen = top_market_cap)

def before_trading_start(context,data):

    output = pipeline_output('my_pipe')  
    context.stocks = output.index.tolist()  

Got it running. Apparently, CVXPY was choking on NaNs, which are now addressed w/ np.nan_to_num (not necessarily the best approach, but easy to apply just to get the thing running).

One thing to sort out is how to apply a custom factor to a limited set of stocks. I gather that the factor may be running on all stocks in the Quantopian universe, but I think one would want to apply the optimization to a subset.