Thank You very much Eugene and SuperTicker for your answers
Eugene, the SVEHLZZperc.Series is not exactly what i am looking for. Whilst i appreciate the beauty of
TASC 2013-07 | The Step Candle Pattern (Vervoort), this is probably a bit too advanced for what i am trying to accomplish. I will try with the NRTR_WATR indicator. It seems to be doing the job.
SuperTicker, thank you very much for the elegant simplicity of your solution. My coding is a bit rusty and this is the kind of inspiration i need to get back into the coding grove. I agree with You: ATR is probably not the best measure of volatility, but it is simple enough for simple things. And You are right, I am trying to do something a little more advanced:
The Floor/Ceiling method using ATRs
https://www.quora.com/What-is-the-most-precise-way-to-draw-support-and-resistance-lines-for-forex-trading/answer/Laurent-BernutRegime definition is one of the most vexing topic for traders. The Floor/Ceiling method is a simple robust way to define regime. A bull ends when ceiling collapses. A bear ends when floor rises. This eliminates restrictive conditions such as higher highs for bull and lower lows for bears.
Logic
1) Swing calculation: conditions are symmetrical for swing highs and lows.
a) Swing high: retracement in n. ATRs from peak. peak = highest high since last swing low.
b) if trough is found then highest peak since last swing low highest peak = swing high
Sometimes market runs up without clocking a low, so peaks are reset higher until a trough is found on the other side,
2) Ceiling detection:
a) if (swing high >= top && swing high bar > floorBar) then swing high = top
else if (top - swing high) / ATR[20] > detection threshold then
top = ceiling; topBar = ceilingBar
The idea is to reset swing high to its highest level and then measure all subsequent swing highs against the top until there is enough distance to conclude that the market has hit a ceiling. detection threshold is express is retracement ATRs. For example, if retracement = 2 ATRs, then detection threshold could be = 2 * retracements = 2 * 2 = 4 ATRs
This turns everything into a multiple of retracements and makes optimisation a lot easier
Once a ceiling is found, market either goes sideways or bearish. Basic hypothesis is market goes sideways until there is evidence a trend is under way.
3) Range definition
Lower bound: is the lower range = trough since ceilingbar; lowerrangebar = troughbar;
Upper bound: there are two methods here. first one is easier to calculate
a) Upper bound is the highest point after a low range. upper bound = peak since troughBar; upper bound bar = peak bar since troughbar
b) Record the highest swing high after the top : if(swing high< top && swing high >= swing high[n-1] Then upper bound = swing high upper bound bar = swing high bar
Both methods are valid. The first one assumes that markets first rebound off a low and then bounces back from a peak. Ranges dynamically follow the evolution of the counter-trend. Second method assumes all swing highs between the ceiling and now are fair game. this gives a wider band. In practice, there is not much difference, except in shorter time frames.
4) Trend resumption:
a) There are many ways to identify trend resumption but let's go with one that traders are most familiar with: breakout/breakdown.
If Close > upper bound then bullish = true;
If Close < lower bound then bearish = true;
b) After a ceiling is detected, the lowest bound of the range serves as an anchor for floor detection. This illustrates the principle that bull markets do not just die, and then "voila" bear markets start. Bear markets begin as bulls die and vice versa. The practical application is that a floor can be found within a sideways range if distance to the lowest bound is big enough
Why ATRs instead of percentage retracement?
I have carbonara spaghetti coded this floor ceiling using peakbar and peak series in percent retracement mode (that code will give instantaneous eye cancer and brain tumors to any seasoned programmer). The problem is percent retracement ignores volatility. For some sleepy securities, regime is vastly lagging. For choppy stuff, regime flip-flops like a career congressman. Now that the volatility genie is out of the box, it might be useful to incorporate it in the strategy so that it does not resurface in the portfolio later on.
Difference with 1-2-3 method
This floor ceiling method resembles the 1-2-3 pattern but with a few differences. First, everything is done in volatility units. It is more respectful of the volatility signatures of different instruments. Secondly, swings do not have to be consecutive. The top is compared with every subsequent swing high until it either finds one low enough to warrant a ceiling or makes a fresh top. There is no ambiguity possible. It incorporates sideways markets in the form of ranges. (For full disclosure, I am a big fan of 1-2-3 that i discovered through WLD)
Strategy(ies)
This Floor/Ceiling method gives traders objective starting/ending points to deploy their strategies. For example, ceiling is detected: switch on mean reversion and turn off trend following and vice versa when trend resumes. There are of course endless variations on this theme, scaling-in/out, relative/absolute, multi-time frames etc, but for now, let's take a simple strategy that is still an enduring rendition:
A. Initial entry: Look for an entry on ceiling or floor detection
1) Ceiling detection: a) swing high detected && (top - swing high)/ATR[20] > detection threshold b) no active position
Sell short at bar +1
riskstoplevel = ceiling
2) Floor detection: b) swing low detected && (swing Low - bottom)/ATR[20] > detection threshold b) no active position
Buy Long at bar +1
riskstop level = floor
B. Reversal: close active positions and reverse side
1) Floor detection b) swing low detected && (swing Low - bottom)/ATR[20] > detection threshold b) active Short position
Buy to Cover at bar +1
Buy Long at bar +1
riskstoplevel = floor
2) Ceiling detection: a) swing high detected && (top - swing high)/ATR[20] > detection threshold b) active Long position
Sell Long at bar +1
Sell Short at bar +1
riskstoplevel = ceiling
This strategy assumes only two broad regimes: bull and bear. It goes long when a floor is detected assuming sideways to bull and short when a ceiling is found, sideways to bear. Since range is by definition contained below the ceiling and above the floor, setting the stop loss at either floor or ceiling enables traders to navigate through the sideways gyrations relatively unharmed.
It does not make explicit use of the ranges. A variation would be to cover a short and go long if Close > upper band (and vice versa on the short side) with stop loss somewhere along the range: aggressive = upper bound, prudent = mid-range, conservative = lower bound. Please feel to elaborate more sophisticated strategies. My objective is simply to provide traders with a solid skeleton to put muscle on.
Conclusion: simplicity, robustness and versatility
The Floor/Ceiling method is a simple answer to the complicated question of regime definition: bull, bear, sideways. Complexity is a form of laziness: no Ministry of Magic Fibonacci, no Christmas garland moving averages. As such, this method works across asset classes, time frames, relative to an index or in absolute, even multi-time frames (see the Quora post above). It is a powerful asset allocation tool as well. Trend following strategies work best in trending regimes and vice versa for mean reversion strategies in sideways markets. I would be immensely grateful if someone helped code the Floor/Ceiling method. As You start to play with this idea, You will probably realize that it can be noisy at times. As an expression of humble gratitude, i would offer a powerful gift to whomever would comes up with a simple elegant code.
Once again, thank You very much Eugene, SuperTicker and Swissi