Examples
Strategy: fade a delta divergence
A complete simulated strategy: when price makes a new 20-bar high on a bar whose delta is strongly negative (buyers did not push it), sell with a stop and a target in ticks; the mirror case buys. The Report gives the results in R, with DEV and VAL halves.
strategy "Delta divergence fade" { overlay: true, costs: { commission: 2.5 usd, slippage: 1 tick }, fill_model: through, latency: 60ms }
len = input.int("Lookback (bars)", 20, min: 5)
minRank = input.float("Min |delta| rank", 80, min: 50, max: 100)
stopTicks = input.int("Stop (ticks)", 10, min: 2)
targetTicks = input.int("Target (ticks)", 15, min: 2)
hh = highest(high, len)
ll = lowest(low, len)
dRank = stats.rank(abs(nz(delta)), 50)
newHigh = high >= hh and delta < 0 and dRank >= minRank
newLow = low <= ll and delta > 0 and dRank >= minRank
if position.flat and position.pending == 0 and bar.confirmed and session.rth {
if newHigh {
enter("sell", stop: price(stopTicks), target: price(targetTicks), timeout: 30m, tag: "fade high")
} else if newLow {
enter("buy", stop: price(stopTicks), target: price(targetTicks), timeout: 30m, tag: "fade low")
}
}
on exit(x) {
label.new(x.time, x.price, x.reason + " " + str(x.r, "+0.00") + "R", color: x.usd > 0 ? #22c55e : #ef4444)
}
marker(newHigh, shape: "diamond", location: "above", color: #ef5350, size: "small")
marker(newLow, shape: "diamond", location: "below", color: #26a69a, size: "small")What to notice:
price(n)converts a number of ticks held in a setting into a price distance (n ticksonly works with a literal);- a decision in the body is made at the bar's close and can fill after the 60 ms latency;
- the four settings are sweepable from the Report; look for a plateau, not the best point.