Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calculating multiple related Custom Factors efficiently

I am trying to calculate the following custom factors.
Enterprise_Value, EBITDA and EV_TO_EBITDA ratio.
The following works.
Many of these calculations are redundant when done in separate factors.

Is there a more efficient of building these 3 factors, possibly reusing the previous custom factors OR calculating multiple factors within the same custom factors.

# Create custom factor to calculate a market cap based on yesterday's close  
# We'll use this to get the top 2000 stocks by market cap  
class EnterpriseValue(CustomFactor):  
    # Pre-declare inputs and window_length  
    inputs = [USEquityPricing.close, valuation.shares_outstanding, balance_sheet.long_term_debt,  
              balance_sheet.long_term_debt_and_capital_lease_obligation,  
              balance_sheet.cash_cash_equivalents_and_marketable_securities]  
    window_length = 1  
    # Compute market cap value  
    def compute(self, today, assets, out, close, shares,  
                long_term_debt, long_term_debt_and_capital_lease_obligation,  
                cash_cash_equivalents_and_marketable_securities):  
        out[:] = close[-1] * shares[-1] + long_term_debt_and_capital_lease_obligation[-1] \  
                                        - cash_cash_equivalents_and_marketable_securities[-1]

# Create custom factor to calculate a market cap based on yesterday's close  
# We'll use this to get the top 2000 stocks by market cap  
class EBITDA(CustomFactor):  
    # Pre-declare inputs and window_length  
    inputs = [income_statement.ebitda]  
    window_length = 200  
    # Compute market cap value  
    def compute(self, today, assets, out, ebitda):  
        out[:] = ebitda[-1]+ebitda[-64]+ebitda[-127]+ebitda[-190]  
#        out[:] = ebitda[-60]

# Create custom factor to calculate a market cap based on yesterday's close  
# We'll use this to get the top 2000 stocks by market cap  
class EV_TO_EBITDA(CustomFactor):  
    # Pre-declare inputs and window_length  
    inputs = [USEquityPricing.close, valuation.shares_outstanding, balance_sheet.long_term_debt,  
              balance_sheet.long_term_debt_and_capital_lease_obligation,  
              balance_sheet.cash_cash_equivalents_and_marketable_securities,  
              income_statement.ebitda]  
    window_length = 200  
    # Compute market cap value  
    def compute(self, today, assets, out, close, shares,  
                long_term_debt, long_term_debt_and_capital_lease_obligation,  
                cash_cash_equivalents_and_marketable_securities,ebitda):  
        ev = close[-1] * shares[-1] + long_term_debt_and_capital_lease_obligation[-1] \  
                                        - cash_cash_equivalents_and_marketable_securities[-1]  
        ebitda = ebitda[-1]+ebitda[-64]+ebitda[-127]+ebitda[-190]  
        out[:] = ev / ebitda  
#        out[:] = ebitda[-60]
2 responses

Hi Saravanan,
Once you have calculated your EV and EBITDA factors, you can combine these to create your EV_to_EBITDA factor without having to set them up as a new custom factor. Your before_trading_start would look something like,

    pipe = Pipeline()  
    attach_pipeline(pipe, 'example')  


    # declare your ev custom factor  
    ev = EnterpriseValue()  
    pipe.add(ev, 'ent_value')  


    # declare your ebitda custom factor  
    ebitda = EBITDA()  
    pipe.add(ebitda, 'ebitda')  


    # divide ev/ebitda results in a new factor  
    ev_to_ebitda = ev/ebitda  
    pipe.add(ev_to_ebitda, 'ev_to_ebitda')  
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.

Very helpful, thank you!

It is worth adding to the API docs