Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to define a custom factor using fundamental data of Morningstar dataset

Hi,
I am totally new to the Quantopian. I took a look at the tutorials and lectures about pipelines and factors. But I am still confusing about how to define a custom factor using fundamental data. From what I understand so far:

define a new class with my factor's name

class my_factor(Custom Factor):
# Then I need my inputs and window-length
input =[]
window-length =

# Then I need to compute  
def compute(self, today, assets, out, close, shares):  
    out[:] = 

Probably, coding should be like this. But I am not sure where should I show it is fundamental data?
And what factors can I define? If anyone can show me some example with instructions that will help a lot.

Thank you for help.

2 responses

Welcome! Hope this explanation helps.

Custom factors have dataset columns as their inputs. Examples of a dataset are USEquityPricing and Fundamentals. Examples of a dataset column are USEquityPricing.close and Fundamentals.cash_and_cash_equivalents. Really just attributes of a dataset. See the docs for a bit more explanation https://www.quantopian.com/help#importing-datasets .

So, to create a custom factor one needs to import the `CustomFactor' class as well as any of the datasets one wishes to use. Something like this.

# Imports needed to create and run pipeline  
from quantopian.pipeline import Pipeline, CustomFactor  
from quantopian.research import run_pipeline  

# Import any pipeline data we want to use. These are most common.  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.data import Fundamentals  # Morningstar  
from quantopian.pipeline.data import factset   # Factset

Next, create a subclass of CustomFactor as a new class. (see the docs https://www.quantopian.com/help#custom-factors)

class MyFactor(CustomFactor):  
    # assign any default input(s). not required but maybe convenient.  
    inputs = [Fundamentals.cash_and_cash_equivalents, USEquityPricing.close]  
    # assign a default window_length. again not required  
    window_length = 2  
    # factors can have multiple outs. if so, then give them names  
    outputs= ["cash_n_days_ago", "close_n_days_ago"]


    def compute(self, today, asset_ids, out, cash, close):  
        # do any logic here  
        # the inputs appear in the same order as assigned above  
        # inputs are numpy arrays.  
        # columns are the assets. rows are the days with the earliest first [0] and most recent last [-1]  
       out.cash_n_days_ago[:] = cash[0]  
       out.close_n_days_ago[:] = close[0]

The above custom factor will return the 'cash_and_cash_equivalents' and the 'close' price from 2 days ago. It needs to be instantiated when defining the pipeline like this

my_factor = MyFactor()  
cash_n_days_ago = my_factor.cash_n_days_ago  
close_n_days_ago = my_factor.close_n_days_ago

This shows how a custom factor can have multiple outputs. Typically one would only have a single output. This is just to show how.

Often one doesn't need to write a custom factor especially for the fundamental data. Typically one just wants the latest value without doing any manipulation. A factor can simply be built directly from the dataset column by using the latest method.

current_cash = Fundamentals.cash_and_cash_equivalents.latest

Finally, there are currently a couple of sources for fundamental data - Morningstar and Factset. Both can be used together if desired. Take a look at the documentation for available fields

https://www.quantopian.com/docs/data-reference/overview
https://www.quantopian.com/docs/data-reference/factset_fundamentals
https://www.quantopian.com/docs/data-reference/morningstar_fundamentals

Attached is a notebook which shows a custom factor in action. It's a modification of the one used in this post https://www.quantopian.com/posts/change-in-fundamental-data-in-research

Hope that helps.

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.

Thank you for your detailed explanation! Really helps me a lot!