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 |
- 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?
- Why there is the negative sign. Isn’t our aim is to pick the highest rank for going long?