Volatility Ranking

Course Name: Trading Alphas: Mining, Optimisation, and System Design, Section No: 23, Unit No: 7, Unit type: Notebook

Volatility Ranking

sig = stock_data.pct_change().Close.rolling(50).std()16
sig2 = (-(sig.rank(axis=1) == 12)
(sig > 0.2)*1)

Output of sig2:

AAPL BAC COST GOOG HSBC JNJ JPM KR META MRNA PFE WMT
Date
2009-12-31 0 0 0 0 0 0 0 0 0 0 0 0
2010-01-04 0 0 0 0 0 0 0 0 0 0 0 0
2010-01-05 0 0 0 0 0 0 0 0 0 0 0 0
2010-01-06 0 0 0 0 0 0 0 0 0 0 0 0
2010-01-07 0 0 0 0 0 0 0 0 0 0 0 0
2022-11-01 1 1 1 1 1 0 1 1 0 1 1 1
2022-11-02 1 1 1 1 1 0 1 1 0 1 1 1
2022-11-03 1 1 1 1 1 0 1 1 0 1 1 1
2022-11-04 1 1 1 1 1 0 1 1 0 1 1 1
2022-11-07 1 1 1 1 1 0 1 1 0 1 1 0
  1. Why output would be 1? There is a negative sign at the beginning of the expression. So, if sig.rank(axis=1) == 12 is True, then value would be -1. -1x1 (if 2nd condition is also True) should be -1?
  2. Why there is the negative sign. Isn’t our aim is to pick the highest rank for going long?

Hi H.E.R Cheung, we will get back to you shortly on this query, we can working on it internally.

Hi H.E.R Cheung,

Thank you for pointing this out. Yes, the “-” sign would not be there, the code has been modified. Another warranted change regarding the highest rank has been modified as well.

You can check the updated codes. Feel free to ask any further question that you may have.

Thanks