Grid Engine Demo
THE SOURCE FILE /code/grid-engine-demo.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
// Demo/proof of the shared grid engine — NOT a new strategy, NOT live-compile-validated.
// Faithful mechanical refactor of HD_BarBreakout v8: same 8 inputs, same buffer,
// same session, same one-trade-per-day, same stop/target, same cancel logic.
// Every grid number now comes from GridEngine; this script only places orders.
//
// OPTION A (this file): import the published library. Replace USERNAME with your
// TradingView handle after publishing GridEngine.pine as a library.
// OPTION B (no import): comment the import below, paste the ge_* fallback block
// from the end of GridEngine.pine, and swap grid.NAME(...) -> ge_NAME(...).
import USERNAME/GridEngine/1 as grid
strategy("HD Bar Breakout (GridEngine demo)", overlay=true,
initial_capital = 100000,
commission_type = strategy.commission.percent,
commission_value = 0,
pyramiding = 4,
process_orders_on_close = false)
//--- 8 editable inputs (platform cap: same eight as v8)
riskPercent = input.float(0.5, "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(false, "Grid entries (pyramid)")
gridLevels = input.int(1, "Grid levels (1 = single entry)", minval=1, maxval=4)
addMode = input.string("Positive", "Add direction", options=["Positive", "Negative"])
stopAtr = input.float(2.0, "Stop loss, in ATRs", minval=0.01)
targetAtr = input.float(10.0, "Take profit, in ATRs (0 = none)", minval=0.0)
//--- declared inputs beyond the platform's 8-input settings limit
oneTradeDay = input.bool(true, "One trade per day")
atrLen = input.int(1, "ATR period", minval=1)
//--- buffer input DELETED; value set directly (as in v8)
bufferPoints = 1.0
//--- helpers (unchanged from v8)
inSession(h) =>
sessionStart < sessionEnd ? h >= sessionStart and h < sessionEnd : h >= sessionStart or h < sessionEnd
dayKey = math.floor(time / 86400000)
var float lastTradeDay = na
atrVal = ta.atr(atrLen)
minDist = syminfo.mintick
buf = bufferPoints * syminfo.mintick
stopDist = stopAtr * atrVal
tgtDist = targetAtr * atrVal
signalHigh = high[1]
signalLow = low[1]
buyStop = signalHigh + buf
sellStop = signalLow - buf
riskMoney = strategy.equity * riskPercent / 100.0
qtyRaw = stopDist > 0 ? riskMoney / stopDist : 0.0
qty = math.floor(qtyRaw)
//--- the grid numbers: every line here replaces an v8 calculation line
activeLvls = grid.gridActive(useGrid, gridLevels)
perQty = grid.perLevelQty(qty, activeLvls)
gridStep = grid.gridStepSize(stopDist, activeLvls)
dirSign = grid.addSign(addMode)
array<float> longLvls = grid.longLevels(buyStop, gridStep, dirSign, activeLvls)
array<float> shortLvls = grid.shortLevels(sellStop, gridStep, dirSign, activeLvls)
//--- confirmed bars only (control flow unchanged from v8)
if barstate.isconfirmed
//--- cancel logic: same as v8
if strategy.position_size == 0
for i = 0 to 3
strategy.cancel("L" + tostring(i + 1))
strategy.cancel("S" + tostring(i + 1))
else if strategy.position_size > 0
for i = 0 to 3
strategy.cancel("S" + tostring(i + 1))
else
for i = 0 to 3
strategy.cancel("L" + tostring(i + 1))
if not inSession(hour(time))
strategy.close_all()
else
//--- ONE exit anchored at position_avg_price, shared by every live leg
if strategy.position_size != 0
float avgPx = strategy.position_avg_price
bool isLongPos = strategy.position_size > 0
array<float> ex = grid.exitLevels(avgPx, stopDist, tgtDist, isLongPos)
float exStop = array.get(ex, 0)
float exLimit = array.get(ex, 1)
string side = isLongPos ? "L" : "S"
for i = 0 to 3
if i < activeLvls
strategy.exit(side + "X" + tostring(i + 1), side + tostring(i + 1), stop = exStop, limit = exLimit)
dayIsFree = not oneTradeDay or na(lastTradeDay) or dayKey != lastTradeDay
distOk = not na(stopDist) and stopDist >= minDist and (targetAtr == 0 or tgtDist >= minDist)
if strategy.position_size == 0 and dayIsFree and distOk and qty > 0
if buyStop > close + minDist
for i = 0 to 3
if i < activeLvls
strategy.entry("L" + tostring(i + 1), strategy.long, qty = perQty, stop = array.get(longLvls, i))
lastTradeDay := dayKey
if sellStop < close - minDist
for i = 0 to 3
if i < activeLvls
strategy.entry("S" + tostring(i + 1), strategy.short, qty = perQty, stop = array.get(shortLvls, i))
lastTradeDay := dayKey
//--- visuals (unchanged from v8)
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
Strategies call the math and still place their own orders.
#SOURCE
Pine v6 · 114 lines · /code/grid-engine-demo.pine · category Framework.
#NOTES FROM THE REPO
The operator already proved a grid-entry mechanism inside HD Bar Breakout v8.
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.