Strategy vault / Mean reversion
Turnaround After Consolidation Strategy
The main idea of this strategy is to detect obvious short-term consolidation in stock price movements, and then judge the likely next direction based on the consolidation pattern formed during the "consolidation"…

Overview
The main idea of this strategy is to detect obvious short-term consolidation in stock price movements, and then judge the likely next direction based on the consolidation pattern formed during the "consolidation" phase, so as to take corresponding long or short positions.
Strategy Logic
The strategy uses the Stochastic oscillator indicator to determine if the stock price has entered consolidation. Oscillation of Stochastic oscillator in overbought or oversold zones indicates price consolidation.
When Stochastic oscillator oscillates, candlestick directional changes are used to detect trend reversal points. A candle change from black to white signals consolidation end and go long. A change from white to black signals consolidation end and go short.
Profit targets and stop loss after entry are set dynamically based on entry price using trailing stops. Fixed profit/loss used for full position, trailing stops used for partial position.
The strategy supports both full and partial position trading. Fixed points used for full position, trailing stops used for partial position.
Trading hours are also configured in the strategy. Trades only allowed during set hours.
Advantage Analysis
Stochastic oscillator accurately identifies short-term price consolidation.
Trading at reversal points after consolidation improves accuracy.
Trailing stops lock in profits as price moves favorably.
Supports full and partial position trading based on risk preference.
Trading hours avoid wrong trades during volatile periods.
Risk Analysis
Stochastic oscillator prone to false signals, missing entries or giving premature entries.
Candlestick reversals may not be accurate for detecting trend changes.
Trailing stops subject to being hit by price whipsaws.
Higher risk with partial position trading, losses could magnify on reversals.
Stop loss and trailing stop parameters need tuning for different instruments.
Major events can affect strategy performance.
Enhancement Opportunities
Optimize Stochastic oscillator parameters for better consolidation detection.
Add filters to confirm candlestick signals, improving accuracy.
Enhance trailing stop algorithms for better tracking.
Add position sizing rules to limit losses in single stocks.
Avoid major event driven volatility by incorporating event schedule.
Improve partial position model to better capture trends.
Conclusion
The turnaround after consolidation strategy identifies short-term consolidation using Stochastic oscillator and trades at trend reversal points after the consolidation phase. It has a decent winning rate and allows locking in segment profits in trends. However, Stochastic is prone to false signals. Accuracy can be improved by optimizing parameters, adding filters etc. In addition, optimizing the trailing stops, controlling position sizing, and avoiding event risks are areas that require focus. Overall, this strategy provides a reference model but needs tuning and risk control for live trading based on individual trading style.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_int_1 | 2022 | Ano |
| v_input_int_2 | true | Mes |
| v_input_int_3 | true | Dia |
| v_input_int_4 | true | hora |
| v_input_int_5 | false | minuto |
| v_input_int_6 | 10 | hora Inicio Operacao Robo |
| v_input_int_7 | 40 | Minuto Encerra Tudo |
| v_input_int_8 | 17 | hora Fechamento |
| v_input_int_9 | 50 | Minuto Encerra Novas Operacoes |
| v_input_int_10 | 50 | Minuto Encerra Tudo |
| v_input_int_11 | 90 | Estocastico Alvo - Para Short |
| v_input_int_12 | 10 | Estocastico Alvo - Para Buy |
| v_input_1 | true | Parcial ? |
| v_input_int_13 | 150 | Pontos para Gain |
| v_input_int_14 | 150 | Pontos para Loss |
| v_input_int_15 | 50 | Pontos para Parcial |
Source Pine Script
/*backtest
start: 2022-10-27 00:00:00
end: 2023-11-02 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy('Cross', overlay=true, initial_capital=1000 )
// Creditos : Cleber.martinelli
////////////////////////////////////////////////////////
// //
// //
// CALENDARIO //
// //
// //
////////////////////////////////////////////////////////
//052)
// trading view solicita que se ja informado data para gerar backtest a partir de tal data
//começa backtest do trading sistem em qual data ?
ano = input.int(2022, minval=1, title="Ano")
mes = input.int(1, minval=1, maxval=12, title="Mes")
dia = input.int(1, minval=1, maxval=31, title="Dia")
hora = input.int(1, minval=1, maxval=23, title="hora")
minuto = input.int(0, minval=0, maxval=59, title="minuto")
horaabertura = input.int(10, minval=1, maxval=23, title="hora Inicio Operacao Robo")
minutoabertura = input.int(40, minval=1, maxval=59, title="Minuto Encerra Tudo")
horaencerra = input.int(17, minval=1, maxval=23, title="hora Fechamento")
minutoencerra = input.int(50, minval=1, maxval=59, title="Minuto Encerra Novas Operacoes")
minutofinaliza = input.int(50, minval=1, maxval=59, title="Minuto Encerra Tudo")
//valida se o dia de hoje é posterior ao dia informado acima
Validadia = year >= ano and month >= mes and dayofmonth >= dia
//cria horario de abertura de negociaçao, considerar default 10 hs, pois os indicadores ja estarão corrigidos
abreloja = year >= ano and month >= mes and dayofmonth >= dia and hour >= horaabertura
//and minute >= minutoabertura)
//cria horario de fechamento de todas as negociaçoes, considerar default 17:00 hs
//nenhuma ordem pode ser aberta depois dessa data e as abertas devem ser fechadas
fechaloja = year >= ano and month >= mes and dayofmonth >= dia and hour >= horaencerra
//and minute >= minutoencerra)
fechaloja2 = year >= ano and month >= mes and dayofmonth >= dia and hour >= horaencerra
//and minute >= minutofinaliza)
//valida horario de negociação, pra liberar as operacoes.
lojaaberta = abreloja == true and fechaloja == false and fechaloja2 == false
////////////////////////////////////////////////////////
// //
// //
// Codigo Operacional //
// //
// //
////////////////////////////////////////////////////////
// Inputs da Estratégia
pmax = input.int(90, minval=0, maxval=100, title="Estocastico Alvo - Para Short")
pmin = input.int(10, minval=0, maxval=100, title="Estocastico Alvo - Para Buy ")
parcial = input(title="Parcial ? ", defval=true)
p_gain = input.int(150, minval=0, maxval=1000, title="Pontos para Gain ")
p_loss = input.int(150, minval=0, maxval=1000, title="Pontos para Loss")
p_parcial = input.int(50, minval=0, maxval=100, title="Pontos para Parcial ")
// puxando os indicadores que usaremos
estoc = ta.stoch(close,high,low,5)
if (estoc >=pmax and close < open)
strategy.entry("Vende", strategy.short ,qty = 2)
if (estoc <=pmax and close > open)
strategy.entry("Compra", strategy.long ,qty = 2 )
pm_ativo = strategy.opentrades.entry_price(0)
if strategy.position_size > 0 and parcial// Posicionado na compra
if strategy.position_size == 2 // Mão cheia
if close < pm_ativo - 100
strategy.exit("Fecha Compra", "Compra", loss=p_loss , qty = 2 )
if close > pm_ativo + 50
strategy.exit("Fecha Compra", "Compra", profit=p_gain , qty = 1 )
if strategy.position_size == 1// Mão cheia
if close < pm_ativo
strategy.exit("Fecha Compra", "Compra", loss=0 , qty = 1 )
if close > pm_ativo + 100
strategy.exit("Fecha Compra", "Compra", profit= p_gain * 1.5 , qty = 1 )
if strategy.position_size < 0 and parcial // Posicionado na Venda
if strategy.position_size == -2 // Mão cheia
if close > pm_ativo - 100
strategy.exit("Fecha Venda", "Vende", loss=p_loss , qty = 2 )
if close < pm_ativo + 50
strategy.exit("Fecha Venda", "Vende", profit=p_gain , qty = 1 )
if strategy.position_size == -1// Mão cheia
if close > pm_ativo
strategy.exit("Fecha Venda", "Vende", loss=0 , qty = 1 )
if close < pm_ativo + 100
strategy.exit("Fecha Venda", "Vende", profit=p_gain*1.5 , qty = 1 )
if strategy.position_size > 0 and parcial == false // Sem Parcial
strategy.exit("Fecha Compra", "Compra", loss=p_loss , profit = p_gain , qty = 2 )
if strategy.position_size < 0 and parcial == false // Sem Parcial
strategy.exit("Fecha Venda", "Vende", loss=p_loss , profit = p_gain , qty = 2 )
Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-03. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Turnaround-After-Consolidation-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Adaptive Trading Strategy Combining Dynamic Trend Reversal and Stochastic IndicatorPine Script · Mean reversion · 1d
- Advanced 15 Minute Chart Trading Signal StrategyPine Script · Mean reversion · 3h
- Automated Market Reversal Trading Strategy Based on Stochastic Indicator and Candlestick PatternsPine Script · Mean reversion · 2m
- Bollinger Bands Stochastic RSI Extreme Strategy BBSR Extreme StrategyPine Script · Mean reversion · 1h
- BuySell Strategy depends on AOStochRSIATRPine Script · Mean reversion · 30m
- Byron Serpent Cloud Quant StrategyPine Script · Mean reversion · 1h