0bps

Strategy vault / Mean reversion

CRYPTO TREND REVERSAL STRATEGY BASED ON PIVOT SWING HIGH AND LOW POINTS

This strategy identifies trend reversals in crypto assets based on PIVOT swing high/low points and breakout signals. It belongs to the breakout reversal strategy category. The strategy first calculates the recent…

Author
ChaoZhang
Complexity
Intermediate · 74 lines
Parameters
7
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy identifies trend reversals in crypto assets based on PIVOT swing high/low points and breakout signals. It belongs to the breakout reversal strategy category. The strategy first calculates the recent highest and lowest price points as PIVOT levels, then detects if price breaks out these key levels, signaling major trend changes.

How The Strategy Works

  1. Calculate PIVOT High/Low Points

    Uses ta.pivothigh() and ta.pivotlow() to find highest high and lowest low prices over a custom bar lookback period to plot PIVOT points.

  2. Identify Breakout Signals

    If price breaks above PIVOT low point, or breaks below PIVOT high point, the strategy considers it as trend reversal signal.

  3. Set Filter Conditions

    Requires price to breakout PIVOT levels by meaningful distance, and closing price crosses 150 bar closing prices to avoid whipsaws.

  4. Entries and Exits

    Trigger buy signal on long condition, close long position on exit condition. Similarly for short setup rules.

Advantages

  1. PIVOT points are sensitive to major trend shifts
  2. Avoids whipsaws in consolidation trends with filters
  3. Captures reversals early with swing high/low breakouts

Risks

  1. Larger cycles can cause strategy to get whipsawed
  2. PIVOT points and filters need tuning for each asset
  3. Exchange fees impact results, need near-zero fee structure

Enhancement Opportunities

  1. Test different PIVOT lookback periods
  2. Add moving stop loss to control loss per trade
  3. Combine with other indicators for filter

Conclusion

The strategy is robust overall to capture large reversals, but needs customized parameters per asset and risk controls. With further optimization and guardrails, it can perform well across crypto markets.

Parameters

NameDefaultWhat it controls
v_input_timeframe_1Timeframe
v_input_int_110(?LENGTH LEFT / RIGHT)Pivot High
v_input_int_210/
v_input_1redcolorH
v_input_int_310Pivot Low
v_input_int_410/
v_input_2bluecolorL

Source Pine Script

/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nkrastins95

//@version=5
strategy("Swing Hi Lo", overlay=true, margin_long=100, margin_short=100)

//-----------------------------------------------------------------------------------------------------------------------//

tf = input.timeframe(title="Timeframe", defval="")

gr="LENGTH LEFT / RIGHT"
leftLenH = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High",group=gr)
rightLenH = input.int(title="/", defval=10, minval=1, inline="Pivot High",group=gr)
colorH = input(title="", defval=color.red, inline="Pivot High",group=gr)

leftLenL = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low", group=gr)
rightLenL = input.int(title="/", defval=10, minval=1, inline="Pivot Low",group=gr)
colorL = input(title="", defval=color.blue, inline="Pivot Low",group=gr)

//-----------------------------------------------------------------------------------------------------------------------//

pivotHigh(ll, rl) =>
    maxLen = 1000
    float ph = ta.pivothigh(ll, rl)
    int offset = 0
    while offset < maxLen
        if not na(ph[offset])
            break 
        offset := offset + 1
    ph[offset]

pivotLow(ll, rl) =>
    maxLen = 1000
    float pl = ta.pivotlow(ll, rl)
    int offset = 0
    while offset < maxLen
        if not na(pl[offset])
            break 
        offset := offset + 1
    pl[offset]


//-----------------------------------------------------------------------------------------------------------------------//

ph = request.security(syminfo.tickerid, tf, pivotHigh(leftLenH, rightLenH), barmerge.gaps_off, barmerge.lookahead_on)
pl = request.security(syminfo.tickerid, tf, pivotLow(leftLenL, rightLenL), barmerge.gaps_off, barmerge.lookahead_on)

drawLabel(_offset, _pivot, _style, _color) =>
    if not na(_pivot)
        label.new(bar_index[_offset], _pivot, str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=#131722)

//-----------------------------------------------------------------------------------------------------------------------//

VWAP = ta.vwap(ohlc4)

longcondition = ta.crossunder(close,pl) and close > close[150]
exitcondition = close > ph

shortcondition = ta.crossover(close,ph) and close < close[150]
covercondition = close < pl

strategy.entry("long", strategy.long, when = longcondition)
strategy.close("long", when = exitcondition)

strategy.entry("Short", strategy.short, when = shortcondition)
strategy.close("Short", when = covercondition)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-12. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/CRYPTO-TREND-REVERSAL-STRATEGY-BASED-ON-PIVOT-SWING-HIGH-AND-LOW-POINTS.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open