FRANKY v2
THE SOURCE FILE /code/franky.pine
Read the file, copy it, or download the exact bytes served at this address. No performance figure is claimed on this page.
//@version=6
// FRANKY v2 โ DAAVILE-owned. NOT backtest-validated. No performance is claimed.
// Signal and sizing are the proven breakout base, unchanged: prior closed bar
// high/low plus one tick, exchange-time session, ATR stop, fixed-risk grid,
// one shared exit at the position average. Gamma, stretch and the DLL guard
// remain soft overlays, permissive by default. Trail, gap backstop, adverse
// close and all four prop stops are now live instead of sitting behind a switch.
strategy("FRANKY v2 โ Sauce on Breakout", overlay=true,
initial_capital = 100000,
commission_type = strategy.commission.percent,
commission_value = 0,
pyramiding = 4,
process_orders_on_close = false)
//--- The only eight editable inputs. Meaning and defaults are unchanged
riskPercent = input.float(5.0, "Risk per trade, % of equity", minval=0.01, maxval=100.0)
sessionStart = input.int(20, "Session start hour (exchange time)", minval=0, maxval=23)
sessionEnd = input.int(21, "Session end hour (exchange time)", minval=0, maxval=23)
useGrid = input.bool(true, "Grid entries (pyramid)")
gridLevels = input.int(4, "Grid levels (1 = single entry)", minval=1, maxval=4)
addMode = input.string("Positive", "Add direction", options=["Positive", "Negative"])
stopAtr = input.float(0.5, "Stop loss, in ATRs", minval=0.01)
targetAtr = input.float(1.0, "Take profit, in ATRs (0 = none)", minval=0.0)
//--- Named values live in source, not in an input slot
bufferPoints = 1.0 // trigger sits one tick past the prior bar
atrLen = 3 // short ATR the proven base was measured with
allowBuys = true // long side keeps its own permission
allowSells = true // short side keeps its own permission
enableGammaVeto = false // diagnostic until deliberately switched on
enableStretchFilter = false // same: never edits the entry on its own
enableDllGuard = true // on, but the floor is far below day equity
dllGuardDollars = 75.0 // headroom band under the moving floor
dllFloorDropPct = 0.10 // floor is ten percent below day-start equity
volLookback = 20 // baseline volume window for the gamma proxy
gexWindow = 24 // candles summed into the gamma reading
volRatioCap = 3.0 // caps one odd volume spike
proxyThreshold = 0.35 // reading beyond this casts a side veto
stretchLen = 20 // moving-average basis for stretch
stretchCap = 4.0 // only extreme extension blocks a side
trailActivateAtr = 0.5 // half the default target, so the trail can engage
trailStep = 2.0 // distance back from the extreme, points or percent
basicSlAtr = 8.0 // gap backstop, wider than the working stop on purpose
closeMode = "Percent" // unit used by the adverse-close trigger
closeTrigger = 3.0 // give back three percent from the peak and leave
maxTotalRiskPct = 25.0 // ceiling on total open cash risk
profitToStopPct = 50.0 // day profit that halts fresh entries
ddToStopPct = 20.0 // day drawdown that halts fresh entries
//--- Helpers
inSession(h) =>
sessionStart < sessionEnd ? h >= sessionStart and h < sessionEnd : h >= sessionStart or h < sessionEnd
// Negative averaging can put a leg on the broker-invalid side of market.
// Reject the whole grid before any leg is submitted.
gridValid(isBuy, base, step, dir, refPx, levels) =>
valid = true
for i = 0 to levels - 1
px = isBuy ? base + dir * step * i : base - dir * step * i
valid := valid and (isBuy ? px > refPx : px < refPx)
valid
// Adverse close measures give-back from the extreme in either unit.
closeTriggerHit(extreme, current, mode, trigger) =>
move = math.abs(extreme - current)
given = (mode == "Percent" and extreme > 0.0) ? move / extreme * 100.0 : move
given >= trigger
//--- Base values (the entry signal is untouched)
dayKey = math.floor(time / 86400000)
atrVal = ta.atr(atrLen)
minDist = syminfo.mintick
buf = bufferPoints * syminfo.mintick
stopDist = stopAtr * atrVal
tgtDist = targetAtr * atrVal
buyStop = high[1] + buf
sellStop = low[1] - buf
// Fixed risk is split across legs. Adding legs never adds budget.
riskMoney = strategy.equity * riskPercent / 100.0
qtyRaw = stopDist > 0.0 ? riskMoney / stopDist : 0.0
qty = math.floor(qtyRaw)
activeLvls = useGrid ? gridLevels : 1
perQty = activeLvls > 0 ? qty / activeLvls : 0.0
gridStep = activeLvls > 0 ? stopDist / activeLvls : 0.0
dirSign = addMode == "Negative" ? -1 : 1
buyGridValid = gridValid(true, buyStop, gridStep, dirSign, close, activeLvls)
sellGridValid = gridValid(false, sellStop, gridStep, dirSign, close, activeLvls)
//--- Soft overlays: a veto only, never an entry. Neutral passes both sides
clv = high == low ? 0.0 : ((close - low) - (high - close)) / (high - low)
volSma = ta.sma(volume, volLookback)
volRatio = na(volSma) or volSma == 0.0 ? 1.0 : math.min(volume / volSma, volRatioCap)
netGex = math.sum(clv * volRatio, gexWindow)
longVetoed = enableGammaVeto and netGex < -proxyThreshold
shortVetoed = enableGammaVeto and netGex > proxyThreshold
basis = ta.sma(close, stretchLen)
stretch = atrVal > 0.0 ? (close - basis) / atrVal : 0.0
longStretched = enableStretchFilter and stretch > stretchCap
shortStretched = enableStretchFilter and stretch < -stretchCap
// Floor moves with each day's equity, so ordinary trades stay untouched.
var float dayStartEquity = na
var float guardDay = na
if na(dayStartEquity) or dayKey != guardDay
dayStartEquity := strategy.equity
guardDay := dayKey
headroom = strategy.equity - dayStartEquity * (1.0 - dllFloorDropPct)
dllBlocked = enableDllGuard and headroom < dllGuardDollars
//--- Persistent exit state
var float trailPeak = na
var int trailSide = 0
var float propHaltDay = na
var bool propHaltLatch = false
// One cash ledger feeds open risk, proposed risk and the daily halt.
openRiskMoney = math.abs(strategy.position_size) * nz(stopDist, 0.0)
openRiskPct = strategy.equity > 0.0 ? openRiskMoney / strategy.equity * 100.0 : 0.0
proposedRiskPct = strategy.equity > 0.0 ? (openRiskMoney + riskMoney) / strategy.equity * 100.0 : 100.0
riskBudgetOK = proposedRiskPct <= maxTotalRiskPct
if barstate.isconfirmed
positionSide = strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0
if positionSide == 0 or positionSide != trailSide
trailPeak := na
trailSide := positionSide
// A halt is sticky for the day: no fresh grid until the next day key.
if na(propHaltDay) or dayKey != propHaltDay
propHaltDay := dayKey
propHaltLatch := false
dayPnlPct = dayStartEquity > 0.0 ? (strategy.equity - dayStartEquity) / dayStartEquity * 100.0 : 0.0
if dayPnlPct >= profitToStopPct or dayPnlPct <= -ddToStopPct or openRiskPct > maxTotalRiskPct
propHaltLatch := true
// Cancel only the opposite side so a same-side ladder survives its fills.
if strategy.position_size == 0
strategy.cancel("L1")
strategy.cancel("L2")
strategy.cancel("L3")
strategy.cancel("L4")
strategy.cancel("S1")
strategy.cancel("S2")
strategy.cancel("S3")
strategy.cancel("S4")
else if strategy.position_size > 0
strategy.cancel("S1")
strategy.cancel("S2")
strategy.cancel("S3")
strategy.cancel("S4")
else
strategy.cancel("L1")
strategy.cancel("L2")
strategy.cancel("L3")
strategy.cancel("L4")
if not inSession(hour(time))
strategy.cancel_all()
strategy.close_all()
else
if strategy.position_size > 0
avgPx = strategy.position_avg_price
ownedStop = avgPx - stopDist
// Ratchet the one shared stop up from the highest high.
trailPeak := na(trailPeak) or high > trailPeak ? high : trailPeak
if trailPeak - avgPx >= trailActivateAtr * atrVal
tstop = closeMode == "Percent" ? trailPeak * (1.0 - trailStep / 100.0) : trailPeak - trailStep
ownedStop := math.max(ownedStop, tstop)
// Backstop is wider on purpose: it only binds if the working stop is skipped.
ownedStop := math.max(ownedStop, avgPx - basicSlAtr * atrVal)
llim = targetAtr > 0.0 ? avgPx + tgtDist : na
strategy.exit("LX1", "L1", stop=ownedStop, limit=llim)
if activeLvls >= 2
strategy.exit("LX2", "L2", stop=ownedStop, limit=llim)
if activeLvls >= 3
strategy.exit("LX3", "L3", stop=ownedStop, limit=llim)
if activeLvls >= 4
strategy.exit("LX4", "L4", stop=ownedStop, limit=llim)
if strategy.position_size < 0
avgPx = strategy.position_avg_price
ownedStop = avgPx + stopDist
// Ratchet the one shared stop down from the lowest low.
trailPeak := na(trailPeak) or low < trailPeak ? low : trailPeak
if avgPx - trailPeak >= trailActivateAtr * atrVal
tstop = closeMode == "Percent" ? trailPeak * (1.0 + trailStep / 100.0) : trailPeak + trailStep
ownedStop := math.min(ownedStop, tstop)
ownedStop := math.min(ownedStop, avgPx + basicSlAtr * atrVal)
slim = targetAtr > 0.0 ? avgPx - tgtDist : na
strategy.exit("SX1", "S1", stop=ownedStop, limit=slim)
if activeLvls >= 2
strategy.exit("SX2", "S2", stop=ownedStop, limit=slim)
if activeLvls >= 3
strategy.exit("SX3", "S3", stop=ownedStop, limit=slim)
if activeLvls >= 4
strategy.exit("SX4", "S4", stop=ownedStop, limit=slim)
// Give-back from the extreme flattens the whole net position.
if strategy.position_size > 0 and closeTriggerHit(trailPeak, close, closeMode, closeTrigger)
strategy.close_all()
if strategy.position_size < 0 and closeTriggerHit(trailPeak, close, closeMode, closeTrigger)
strategy.close_all()
if propHaltLatch
strategy.cancel_all()
strategy.close_all()
distOk = not na(stopDist) and stopDist >= minDist and (targetAtr == 0.0 or tgtDist >= minDist)
if strategy.position_size == 0 and distOk and qty > 0 and not propHaltLatch and riskBudgetOK
if allowBuys and buyGridValid and buyStop > close + minDist and not dllBlocked and not longVetoed and not longStretched
strategy.entry("L1", strategy.long, qty=perQty, stop=buyStop)
if activeLvls >= 2
strategy.entry("L2", strategy.long, qty=perQty, stop=buyStop + dirSign * gridStep)
if activeLvls >= 3
strategy.entry("L3", strategy.long, qty=perQty, stop=buyStop + dirSign * gridStep * 2)
if activeLvls >= 4
strategy.entry("L4", strategy.long, qty=perQty, stop=buyStop + dirSign * gridStep * 3)
if allowSells and sellGridValid and sellStop < close - minDist and not dllBlocked and not shortVetoed and not shortStretched
strategy.entry("S1", strategy.short, qty=perQty, stop=sellStop)
if activeLvls >= 2
strategy.entry("S2", strategy.short, qty=perQty, stop=sellStop - dirSign * gridStep)
if activeLvls >= 3
strategy.entry("S3", strategy.short, qty=perQty, stop=sellStop - dirSign * gridStep * 2)
if activeLvls >= 4
strategy.entry("S4", strategy.short, qty=perQty, stop=sellStop - dirSign * gridStep * 3)
plot(buyStop, "Buy stop", color=color.new(color.teal, 0), linewidth=1)
plot(sellStop, "Sell stop", color=color.new(color.red, 0), linewidth=1)
#THE CLAIM
No profit factor, return or win-rate claim is made here.
#SOURCE
Pine v6 ยท 229 lines ยท /code/franky.pine ยท category Grid & reversion.
#NOTES FROM THE REPO
The eight editable inputs and their defaults are unchanged. Grid 4, risk 5%, Positive adds, buys and sells both allowed. Gamma veto and stretch filter stay OFF; the DLL guard stays ON but permissive.
Scripts are provided for research; publication is not a performance claim. No backtest result is implied by this file. Not financial advice.
Source is provided for research, not as a performance claim. Paste into a Pine editor before use. Not financial advice.