DAAVILE Kinematics Grid
THE SOURCE FILE /code/kinematics-grid.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
// DAAVILE KINEMATICS GRID โ the DAAVILE sauce.
// A self-contained strategy: structure levels, an online win-rate learner,
// price kinematics, timed entries and a shared trailing-stop grid.
// 1. allow long / allow short independently
// 2. entry trigger: immediately, after N bars, or after N seconds
// 3. trailing stop (Chandelier-style) instead of a fixed stop loss
// 4. close conditions: trailing hit, goal hit, conviction flip, timeout, session end
// Grid maths come from algorithms/framework/GridEngine.pine (inline fallback form).
// Pine strategy() is a simulator and cannot place MFP orders.
strategy("DAAVILE Kinematics Grid", overlay=true,
initial_capital = 100000,
commission_type = strategy.commission.percent,
commission_value = 0,
pyramiding = 4,
process_orders_on_close = false)
//--- PLATFORM EDITABLE: only the first 8 input.* calls appear in settings.
allowLong = input.bool(true, "Allow buys")
allowShort = input.bool(true, "Allow sells")
entryMode = input.string("Immediately", "Entry trigger", options=["Immediately", "After 1 bar", "After 2 bars", "After 3 bars", "After 10 seconds", "After 30 seconds", "After 60 seconds"])
useGrid = input.bool(true, "Grid entries (pyramid)")
gridLevels = input.int(3, "Grid levels (1 = single entry)", minval=1, maxval=4)
addMode = input.string("Positive", "Add direction", options=["Positive", "Negative"])
riskPercent = input.float(2.0, "Risk per idea, % of equity", minval=0.01, maxval=100.0)
trailATR = input.float(2.0, "Trailing stop, in ATRs", minval=0.5)
//--- CODE-EDITABLE โ the 8-input-cap bypass (same trick as breakout's buffer).
//--- These are NOT input.* calls, so they do not take a settings slot. They
//--- stay named here at the top: change the value in source and recompile.
float minRR = 1.5 // minimum reward/risk before an idea is accepted
bool oneTradeDay = true // one idea per day
int sessionStart = 20 // session start hour (exchange time)
int sessionEnd = 21 // session end hour (exchange time)
//--- frozen research constants
int PIV = 5
int LOOK = 4
float DECAY = 0.9
float BREAK = 0.15
int MAXBAR = 36
int PENDMAX= 12
//--- helpers / shared grid maths
gA(float x) => x > 0 ? 1 : x < 0 ? -1 : 0
gridActive(bool ug, int lv) => ug ? math.max(1, math.min(4, lv)) : 1
perLevelQty(float tq, int n) => n > 0 ? tq / n : 0.0
gridStepSize(float sd, int n)=> n > 0 ? sd / n : 0.0
addSign(string m) => m == "Negative" ? -1 : 1
levelPrice(float b, float step, int sign, int k) => b + sign * step * k
//--- confirmed HTF references (offset >= 1: no lookahead)
float pdh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
float pdl = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
float pdc = request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)
float wH = request.security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
float wL = request.security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)
float atr = ta.atr(14)
float vwap = ta.vwap(hlc3)
float pivot = (pdh + pdl + pdc) / 3.0
float swH = ta.pivothigh(high, PIV, PIV)[1]
float swL = ta.pivotlow(low, PIV, PIV)[1]
//--- structure level book, rebuilt each bar
array<float> bpx = array.new_float()
array<int> bfam = array.new_int()
array<float> bdy = array.new_float()
addLvl(float px, int fam, float dy) =>
if not na(px) and px > 0
array.push(bpx, px)
array.push(bfam, fam)
array.push(bdy, dy)
addLvl(pdh, 0, 1.0)
addLvl(pdl, 0, 1.0)
addLvl(pivot, 0, 0.8)
addLvl((pdh + pdl) / 2.0, 0, 0.7)
addLvl(wH, 1, 1.1)
addLvl(wL, 1, 1.1)
addLvl(vwap, 2, 1.2)
addLvl(swH, 3, 1.3)
addLvl(swL, 3, 1.3)
//--- online 7-family learner
var array<float> win = array.new_float(7, 0.0)
var array<float> los = array.new_float(7, 0.0)
var array<int> pDir = array.new_int(7, 0)
var array<int> pBar = array.new_int(7, 0)
var array<float> pPx = array.new_float(7, na)
cB(int f) => 2.0 * (array.get(win, f) + 3.0) / (array.get(win, f) + array.get(los, f) + 6.0)
if barstate.isconfirmed and not na(atr) and atr > 0
for i = 0 to 6
if array.get(pDir, i) != 0 and bar_index - array.get(pBar, i) >= LOOK
bool held = array.get(pDir, i) == 1 ? close > array.get(pPx, i) : close < array.get(pPx, i)
if held
array.set(win, i, array.get(win, i) * DECAY + 1.0)
else
array.set(los, i, array.get(los, i) * DECAY + 1.0)
array.set(pDir, i, 0)
if array.size(bpx) > 0
for k = 0 to array.size(bpx) - 1
float px = array.get(bpx, k)
if math.abs(close - px) >= atr * BREAK
int f = array.get(bfam, k)
if array.get(pDir, f) == 0
if close[1] <= px and close > px
array.set(pDir, f, 1)
array.set(pBar, f, bar_index)
array.set(pPx, f, px)
else if close[1] >= px and close < px
array.set(pDir, f, -1)
array.set(pBar, f, bar_index)
array.set(pPx, f, px)
//--- kinematics and conviction
float v3 = (close - close[3]) / 3.0
float a3 = v3 - nz(v3[3])
float velN = atr > 0 ? v3 / atr : 0.0
float accN = atr > 0 ? a3 / atr : 0.0
int regime = velN > 0.12 and accN > 0 ? 1 : velN < -0.12 and accN < 0 ? -1 : 0
float bestLong = 0.0
float bestShort = 0.0
float longPx = na
float shortPx = na
if atr > 0 and array.size(bpx) > 0
for k = 0 to array.size(bpx) - 1
float px = array.get(bpx, k)
float sc = array.get(bdy, k) * cB(array.get(bfam, k)) - 0.25 * math.abs(close - px) / atr
if px < close and sc > bestLong
bestLong := sc
longPx := px
if px > close and sc > bestShort
bestShort := sc
shortPx := px
float kin = 0.32 * gA(close - vwap) + 0.24 * gA(velN) + 0.22 * gA(close - pivot) + 0.22 * regime
float convLong = kin + 0.45 * bestLong - 0.30 * bestShort
float convShort = -kin + 0.45 * bestShort - 0.30 * bestLong
bool strong = math.max(convLong, convShort) >= 0.85 and math.abs(convLong - convShort) >= 0.35
//--- state: pending idea -> armed idea -> trailing position
var int pendSide = 0
var int pendBorn = na
var int pendTime = na
var float pendEntry = na
var float pendGoal = na
var int ideaSide = 0
var int ideaBorn = na
var float ideaEntry = na
var float ideaGoal = na
var float ideaRisk = na
var int lastDay = na
var float hiSince = na
var float loSince = na
var float trailPx = na
if barstate.isconfirmed
int dk = math.floor(time / 86400000)
int n = gridActive(useGrid, gridLevels)
int sg = addSign(addMode)
bool inSess = sessionStart < sessionEnd ? hour(time) >= sessionStart and hour(time) < sessionEnd : hour(time) >= sessionStart or hour(time) < sessionEnd
bool dayFree = not oneTradeDay or na(lastDay) or dk != lastDay
// NEW IDEA -> pending (frozen plan, no order yet)
if ideaSide == 0 and pendSide == 0 and strong and dayFree and inSess
int side = convLong >= convShort ? 1 : -1
bool allowed = side == 1 ? allowLong : allowShort
float entry = side == 1 ? longPx : shortPx
if allowed and not na(entry) and not na(atr) and atr > 0
float recent = side == 1 ? ta.lowest(low, 6)[1] : ta.highest(high, 6)[1]
float stopDist = math.max(atr * 1.5, side == 1 ? entry - recent : recent - entry)
float goal = na
if array.size(bpx) > 0
for k = 0 to array.size(bpx) - 1
float px = array.get(bpx, k)
if side * (px - entry) >= minRR * stopDist and (na(goal) or side * (px - goal) < 0)
goal := px
if na(goal)
goal := entry + side * minRR * stopDist
pendSide := side
pendBorn := bar_index
pendTime := time_close
pendEntry := entry
pendGoal := goal
lastDay := dk
// pending invalidation before the trigger fires
if pendSide != 0 and (
bar_index - pendBorn > PENDMAX
or not inSess
or (pendSide == 1 and convLong < 0)
or (pendSide == -1 and convShort < 0))
pendSide := 0
// entry trigger -> arm the grid. First leg is at market; adds are limits.
int delayBars = entryMode == "After 1 bar" ? 1 : entryMode == "After 2 bars" ? 2 : entryMode == "After 3 bars" ? 3 : 0
int delaySecs = entryMode == "After 10 seconds" ? 10 : entryMode == "After 30 seconds" ? 30 : entryMode == "After 60 seconds" ? 60 : 0
bool fired = delayBars > 0 ? bar_index - pendBorn >= delayBars : delaySecs > 0 ? (not na(pendTime) and time - pendTime >= delaySecs * 1000) : true
if pendSide != 0 and fired and ideaSide == 0 and strategy.position_size == 0 and not na(atr) and atr > 0
float riskDist = trailATR * atr
float riskMoney = strategy.equity * riskPercent / 100.0
float qty = riskDist > 0 ? math.floor(riskMoney / riskDist) : 0.0
float perQty = perLevelQty(qty, n)
float gStep = gridStepSize(riskDist, n)
if perQty > 0
int sign = pendSide * sg
if pendSide == 1
strategy.entry("L1", strategy.long, qty = perQty)
if n >= 2
strategy.entry("L2", strategy.long, qty = perQty, limit = levelPrice(pendEntry, gStep, sign, 1))
if n >= 3
strategy.entry("L3", strategy.long, qty = perQty, limit = levelPrice(pendEntry, gStep, sign, 2))
if n >= 4
strategy.entry("L4", strategy.long, qty = perQty, limit = levelPrice(pendEntry, gStep, sign, 3))
else
strategy.entry("S1", strategy.short, qty = perQty)
if n >= 2
strategy.entry("S2", strategy.short, qty = perQty, limit = levelPrice(pendEntry, gStep, -sign, 1))
if n >= 3
strategy.entry("S3", strategy.short, qty = perQty, limit = levelPrice(pendEntry, gStep, -sign, 2))
if n >= 4
strategy.entry("S4", strategy.short, qty = perQty, limit = levelPrice(pendEntry, gStep, -sign, 3))
ideaSide := pendSide
ideaBorn := bar_index
ideaEntry := pendEntry
ideaGoal := pendGoal
ideaRisk := riskDist
hiSince := high
loSince := low
pendSide := 0
// cancel opposite-side legs
if strategy.position_size == 0
// Keep just-armed grid legs resting: only sweep when no idea is live.
if ideaSide == 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")
ideaRisk := na
hiSince := na
loSince := na
trailPx := na
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")
// TRAILING STOP โ no fixed stop loss
if strategy.position_size > 0 and not na(ideaRisk)
hiSince := na(hiSince) ? high : math.max(hiSince, high)
trailPx := hiSince - ideaRisk
strategy.exit("LX1", "L1", stop = trailPx, limit = ideaGoal)
if n >= 2
strategy.exit("LX2", "L2", stop = trailPx, limit = ideaGoal)
if n >= 3
strategy.exit("LX3", "L3", stop = trailPx, limit = ideaGoal)
if n >= 4
strategy.exit("LX4", "L4", stop = trailPx, limit = ideaGoal)
if strategy.position_size < 0 and not na(ideaRisk)
loSince := na(loSince) ? low : math.min(loSince, low)
trailPx := loSince + ideaRisk
strategy.exit("SX1", "S1", stop = trailPx, limit = ideaGoal)
if n >= 2
strategy.exit("SX2", "S2", stop = trailPx, limit = ideaGoal)
if n >= 3
strategy.exit("SX3", "S3", stop = trailPx, limit = ideaGoal)
if n >= 4
strategy.exit("SX4", "S4", stop = trailPx, limit = ideaGoal)
// my close conditions: conviction flip, timeout, session end
bool closeIdea = ideaSide != 0 and (
bar_index - ideaBorn > MAXBAR
or not inSess
or (ideaSide == 1 and convLong < 0)
or (ideaSide == -1 and convShort < 0))
if closeIdea
strategy.close_all()
ideaSide := 0
ideaRisk := na
hiSince := na
loSince := na
trailPx := na
plot(strategy.position_size, "Net position", display = display.data_window)
plot(convLong, "Long conviction", display = display.data_window)
plot(convShort, "Short conviction", display = display.data_window)
plot(trailPx, "Trailing stop", display = display.data_window)
#THE CLAIM
The algo is a separate, purpose-built strategy that keeps the useful mechanics (levels, online learner, kinematics) and uses the shared grid engine.
#SOURCE
Pine v6 ยท 303 lines ยท /code/kinematics-grid.pine ยท category Structure.
#NOTES FROM THE REPO
A grep for `input.(string|bool|int|float)` returns exactly **8** matches. This matches the MFP platform's 8-input cap. Declarations after these are `const` and are **not user-editable on the platform**.
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.