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