Cross sectional momentum - Calculate stock returns

Hi, 



can someone help me with this syntax:


Calculate stock returns with hold_returns * lookback_signal after selecting the rows without NaNs from both the dataframes


stocks_returns = hold_returns[180::41] * lookback_signal[::41]

It's not very clear to me the mechanics of this calculation and the double colon within square brackets significance.

Thanks

hold_returns probably refers to returns on holding the stock and lookback signal refers to the signals generated from the strategy.

So without looking at the entire code i m guessing stock_returns gives you the strategy returns of 41 days among the 180 days under consideration.

For the colon part try google search-py manipulating pandas data in a list…it should be in the indexing part.

Hi Luca,



The colons within the square brackets are used to slice the dataframe.



While working with dataframes, you can have three indices separated by a colon, like [start : stop : step]. The slicing would begin at the index start (included) and end at stop (excluded). Step is the number jumps you would require between two values.



Thus, hold_returns[180 : : 41] will start the calculation at the row index 180 and will continue till the end of the dataframe as the stop index is not mentioned. The jump between the two indices would be 41, that is after 180, the index would be 221, 262 and so on.



Hope this helps.

Many thanks. Now it's more clear.