HD BarBreakout
THE SOURCE FILE /code/hd-barbreakout.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
// HD Bar Breakout — v8
// Built FROM version 7. Original task completed: the BUFFER input is DELETED
// and its value is set directly in code (bufferPoints = 1.0). Every other
// value remains a real input with the requested default.
// Platform caps editable inputs at 8; declarations after that keep defaults.
strategy("HD Bar Breakout", overlay=true,
initial_capital = 100000,
commission_type = strategy.commission.percent,
commission_value = 0,
pyramiding = 4,
process_orders_on_close = false)
//--- 8 editable inputs
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
bufferPoints = 1.0
//--- helpers
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)
activeLvls = useGrid ? gridLevels : 1
perQty = activeLvls > 0 ? qty / activeLvls : 0.0
gridStep = activeLvls > 0 ? stopDist / activeLvls : 0.0
dirSign = addMode == "Negative" ? -1 : 1
var float avgPx = na
//--- confirmed bars only
if barstate.isconfirmed
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.close_all()
else
if strategy.position_size > 0
avgPx := strategy.position_avg_price
lstop = avgPx - stopDist
llim = targetAtr > 0 ? avgPx + tgtDist : na
strategy.exit("LX1", "L1", stop = lstop, limit = llim)
if activeLvls >= 2
strategy.exit("LX2", "L2", stop = lstop, limit = llim)
if activeLvls >= 3
strategy.exit("LX3", "L3", stop = lstop, limit = llim)
if activeLvls >= 4
strategy.exit("LX4", "L4", stop = lstop, limit = llim)
if strategy.position_size < 0
avgPx := strategy.position_avg_price
sstop = avgPx + stopDist
slim = targetAtr > 0 ? avgPx - tgtDist : na
strategy.exit("SX1", "S1", stop = sstop, limit = slim)
if activeLvls >= 2
strategy.exit("SX2", "S2", stop = sstop, limit = slim)
if activeLvls >= 3
strategy.exit("SX3", "S3", stop = sstop, limit = slim)
if activeLvls >= 4
strategy.exit("SX4", "S4", stop = sstop, limit = slim)
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
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)
lastTradeDay := dayKey
if sellStop < close - minDist
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)
lastTradeDay := dayKey
//--- visuals
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
The platform has no Strategy Tester and no backtest endpoint.
#SOURCE
Pine v6 · 133 lines · /code/hd-barbreakout.pine · category Breakout.
#NOTES FROM THE REPO
word "backtest" appears in none of the 107 documentation pages, and the 38
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.