Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Understanding parameters

In the following code, for the class SigmaSpikes and the function compute(), what is the meaning of the parameters in the class and function?

In other words, what does it mean to have CustomFactor in the brackets of the class and what does it mean to have the other parameters in compute()? What does today do for example? What does out do? etc

class SigmaSpikes(CustomFactor):  
    returns = (DailyReturns(inputs=[USEquityPricing.close]))  
    inputs = [returns]  
    window_length = 22  

    def compute(self, today, asset_ids, out, returns):  
            std = np.nanstd(np.diff(returns, axis = 0), axis = 0)  
            out[:] = returns[-1]/std  
2 responses

Good questions.

The 'CustomFactor' inside the parenthesis in the first class statement means "define a new sub class, called SigmaSpikes, and inherit all the methods and properties from the CustomFactor class". So, one is making a new factor based on the basic CustomFactor. If interested, there is more on python inheritance here.

One of the functions defined in the basic CustomFactor class is compute. Our SigmaSpikes class inherits it. It comes pre-defined with a number of parameters. One can use these parameters in the compute function to accomplish whatever one wants the SigmaSpikes factor to do.

The first parameter is today. A custom factor runs every day of a backtest or simulation. By referencing the 'today' parameter one can tell which day the factor is currently computing. This could, for example, be used to test and output a different value on Mondays vs Fridays. This isn't used very often. It's just there in case one does need it.

The second parameter is asset_ids. This is a 1D numpy array with the SIDs of the assets being calculated by the factor. Unless a mask is supplied to the factor this will be all the available assets. If a mask is supplied it will only be those that pass the mask filter. The order of the assets is the same order as the columns in the input data. This isn't used very often either.

The out parameter is what one needs to assign the output of the compute function. Rather than returning a value, the compute function simply sets out.

The parameters after out are inputted numpy 2D arrays of data. They appear in the same order as they are listed in inputs. The arrays have a row for each trading day. The number of rows is determined by the window_length. There is a column for each asset.

It should be noted that these names can be whatever one wishes. They are assigned by position so they will map to whatever name one wishes. In the original example the last parameter is named 'returns' but one could call it anything, for example, 'daily_returns'. Usually name the parameters to something which helps document the function.

There is more info on the parameters in the docs

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.

That's very helpful. Thank you, Dan.