Algonney Breakout Hunter

Algonney · strategy · 116 行 · 点赞 18 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
strategy("Algonney Breakout Hunter", shorttitle = "ABH", overlay = true, initial_capital = 10000, commission_type = strategy.commission.percent, commission_value = 0.1, pyramiding = 0, margin_long = 100, margin_short = 100)
 
// ───── Inputs ─────
gS = "Signals"
entryLen = input.int(60, "Breakout window (candles)", minval = 2, group = gS)
exitLen  = input.int(30, "Exit window (candles)", minval = 2, group = gS)
trendLen = input.int(400, "Trend window (candles)", minval = 10, group = gS)
bodyMin  = input.float(60, "Min body % of candle", minval = 0, maxval = 100, group = gS)
volLen   = input.int(40, "Volume average (candles)", minval = 1, group = gS)
volMult  = input.float(2.0, "Volume x average", minval = 0, step = 0.1, group = gS)
 
gR = "Risk"
stopLen  = input.int(5, "Stop = low/high of last N candles", minval = 1, group = gR)
riskPct  = input.float(1.0, "Risk per trade %", minval = 0.1, step = 0.1, group = gR)
useLong  = input.bool(true, "Take longs", group = gR)
useShort = input.bool(true, "Take shorts", group = gR)
startT   = input.time(timestamp("01 Jan 2022 00:00 +0000"), "Start date", group = gR)
 
gV = "Visuals"
showChan = input.bool(true, "Show breakout channel", group = gV)
showDash = input.bool(true, "Show dashboard", group = gV)
 
// ───── Colors ─────
cBull = #00E5A8
cBear = #FF4D6D
cGold = #F5B942
cBg   = #0B0F19
 
// ───── Logic ─────
hh       = ta.highest(high, entryLen)[1]
ll       = ta.lowest(low, entryLen)[1]
exitLow  = ta.lowest(low, exitLen)[1]
exitHigh = ta.highest(high, exitLen)[1]
mid      = (ta.highest(high, trendLen) + ta.lowest(low, trendLen)) / 2
 
rng     = high - low
bodyPct = rng > 0 ? math.abs(close - open) / rng * 100 : 0.0
volAvg  = ta.sma(volume, volLen)[1]
volOk   = volume >= volAvg * volMult
inDate  = time >= startT
 
longSig  = inDate and useLong  and close > hh and bodyPct >= bodyMin and volOk and close > mid
shortSig = inDate and useShort and close < ll and bodyPct >= bodyMin and volOk and close < mid
 
longStopLvl  = ta.lowest(low, stopLen)
shortStopLvl = ta.highest(high, stopLen)
 
// Size: risk X% of equity, max 1x leverage
riskCash = strategy.equity * riskPct / 100
maxQty   = strategy.equity / close
qtyL = close - longStopLvl > 0 ? math.min(riskCash / (close - longStopLvl), maxQty) : 0.0
qtyS = shortStopLvl - close > 0 ? math.min(riskCash / (shortStopLvl - close), maxQty) : 0.0
 
var float lStop = na
var float sStop = na
 
inLong  = strategy.position_size > 0
inShort = strategy.position_size < 0
 
if longSig and not inLong and qtyL > 0
    lStop := longStopLvl
    strategy.entry("Long", strategy.long, qty = qtyL, comment = "LONG")
 
if shortSig and not inShort and qtyS > 0
    sStop := shortStopLvl
    strategy.entry("Short", strategy.short, qty = qtyS, comment = "SHORT")
 
// Stop loss
strategy.exit("L-Stop", from_entry = "Long",  stop = lStop, comment = "Stop")
strategy.exit("S-Stop", from_entry = "Short", stop = sStop, comment = "Stop")
 
// Exit condition (no take profit)
if inLong and close < exitLow and not shortSig
    strategy.close("Long", comment = "Exit")
if inShort and close > exitHigh and not longSig
    strategy.close("Short", comment = "Exit")
 
// ───── Visuals ─────
pH = plot(showChan ? hh : na, "Breakout high", color.new(cBull, 45), 1)
pL = plot(showChan ? ll : na, "Breakout low",  color.new(cBear, 45), 1)
fill(pH, pL, color = color.new(color.gray, 94))
plot(mid, "Trend middle", close > mid ? color.new(cBull, 15) : color.new(cBear, 15), 2)
plot(inLong ? lStop : inShort ? sStop : na, "Stop", cGold, 2, plot.style_linebr)
plot(inLong ? exitLow : inShort ? exitHigh : na, "Exit line", color.new(color.white, 35), 1, plot.style_linebr)
 
plotshape(longSig and not inLong,  "Long signal",  shape.labelup,   location.belowbar, cBull, text = "LONG",  textcolor = color.black, size = size.small)
plotshape(shortSig and not inShort, "Short signal", shape.labeldown, location.abovebar, cBear, text = "SHORT", textcolor = color.white, size = size.small)
barcolor(longSig ? cBull : shortSig ? cBear : na)
 
// ───── Dashboard ─────
var table dash = table.new(position.top_right, 2, 9, bgcolor = color.new(cBg, 5), border_color = color.new(color.gray, 75), border_width = 1)
 
f_row(r, k, v, c) =>
    table.cell(dash, 0, r, k, text_color = color.gray, text_size = size.small, text_halign = text.align_left)
    table.cell(dash, 1, r, v, text_color = c, text_size = size.small, text_halign = text.align_right)
 
if barstate.isfirst
    table.merge_cells(dash, 0, 0, 1, 0)
 
if showDash and barstate.islast
    netPct  = strategy.netprofit / strategy.initial_capital * 100
    wr      = strategy.closedtrades > 0 ? 100.0 * strategy.wintrades / strategy.closedtrades : 0.0
    pf      = strategy.grossloss > 0 ? strategy.grossprofit / strategy.grossloss : 0.0
    posTxt  = inLong ? "LONG" : inShort ? "SHORT" : "FLAT"
    posCol  = inLong ? cBull : inShort ? cBear : color.gray
    stopTxt = inLong ? str.tostring(lStop, format.mintick) : inShort ? str.tostring(sStop, format.mintick) : "-"
    table.cell(dash, 0, 0, "ALGONNEY BREAKOUT HUNTER", text_color = cGold, text_size = size.normal)
    f_row(1, "Trend",         close > mid ? "BULL" : "BEAR", close > mid ? cBull : cBear)
    f_row(2, "Position",      posTxt, posCol)
    f_row(3, "Stop",          stopTxt, cGold)
    f_row(4, "Net profit",    str.tostring(netPct, "#.##") + " %", netPct >= 0 ? cBull : cBear)
    f_row(5, "Profit factor", str.tostring(pf, "#.##"), pf >= 1 ? cBull : cBear)
    f_row(6, "Win rate",      str.tostring(wr, "#.#") + " %", color.white)
    f_row(7, "Max drawdown",  str.tostring(strategy.max_drawdown_percent, "#.##") + " %", cBear)
    f_row(8, "Trades",        str.tostring(strategy.closedtrades), color.white)

← 返回列表