When you measure the distance from the peak in standard deviation volatility unit, will you find the return deviation from the daily mean return up to the next high/low? Is it correct or am I wrong, or there is a way to explain better?
Yes, that's correct. We calculate the distance between the swing highs and swing lows in standard deviation volatility units. But if you refer to the code again, we calculate the rolling standard deviation using the rebased close price, which is the daily price instead of the returns that you have mentioned. This is done to identify the regime by establishing floor and ceiling.
Code reference:
Calculate volatility from main dataframe and populate regime dataframe
regime['stdev'] = round(data['rebased_close'].rolling(
window=63, min_periods=63).std(0), 2)
Here, we use rebased close. We use the rebased value of Bank of America's close price and not the returns. Then, we define and declare all the variables that we need for establishing the floor and ceiling to identify the regime.
You can find the ceiling by dividing the swing high with the calculated rolling standard deviation like shown below:
Test for the Highest
ceiling_test = round((regime['srebased_high'][i]-top)/regime['stdev'][i], 1)
Similarly, we find the floor by dividing the swing low with the calculated rolling standard deviation. Refer to the code below:
# Test for the lowest
floor_test = round((regime['srebased_low'][i]-bottom)/regime['stdev'][i], 1)
We assign floor and ceiling if the other conditions pass the test.
This is just one line of code under the fifth section of the notebook where we perform looping.
I hope you got the point.