本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Qullamaggie breakout strength momentum entry exit indicator 3.0", shorttitle = "QBreak", overlay = true)
// Version 3.0. Qullamaggie breakout and 10/20-period moving-average exit concepts.
// Authored from the supplied behavior specification and public documentation.
// This file models one long position through chart OHLC bars. It creates no orders.
// The supplied specification controls the source tracker and order timing separately.
int pivotWidth = input.int(3, "Pivot width (left and right)", minval = 1, display = display.none)
string averageTimeframe = input.timeframe("D", "Moving average timeframe", display = display.none)
string averageKind = input.string("SMA", "Moving average type", options = ["SMA", "EMA"], display = display.none)
int firstAverageLength = input.int(10, "First moving average length", minval = 1, display = display.none)
int secondAverageLength = input.int(20, "Second moving average length", minval = 1, display = display.none)
int trendAverageLength = input.int(50, "Trend moving average length", minval = 1, display = display.none)
string exitChoice = input.string("Second", "Exit moving average", options = ["Second", "First"], display = display.none)
int stopLookback = input.int(3, "Protective stop low lookback", minval = 1, display = display.none)
bool useTrendFilter = input.bool(true, "Use moving average trend filter", display = display.none)
bool limitDistance = input.bool(false, "Limit breakout distance", display = display.none)
float atrPercent = input.float(100.0, "Maximum distance as daily ATR percent", minval = 0.0, display = display.none)
int firstAllowedTime = input.time(timestamp("01 Jan 2019 06:00 +0000"), "Start time (exclusive)", display = display.none)
int lastAllowedTime = input.time(timestamp("01 Jan 2100 00:00 +0000"), "End time (exclusive)", display = display.none)
color entryColor = input.color(#0052FF, "Entry candle color", display = display.none)
color exitColor = input.color(#FF00EC, "Exit candle color", display = display.none)
bool showAverages = input.bool(false, "Show moving averages", display = display.none)
f_average(int length) =>
float arithmeticAverage = ta.sma(close, length)
float exponential = ta.ema(close, length)
averageKind == "EMA" ? exponential : arithmeticAverage
[firstAverage, secondAverage, trendAverage] = request.security(syminfo.tickerid, averageTimeframe, [f_average(firstAverageLength), f_average(secondAverageLength), f_average(trendAverageLength)], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
float dailyAtr = request.security(syminfo.tickerid, "D", ta.atr(14), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
float exitAverage = exitChoice == "First" ? firstAverage : secondAverage
// Every pending price has next-bar eligibility. The protective sell may be attached
// to a pending buy; it becomes relevant as soon as that buy establishes the position.
type OrderBook
bool longActive = false
bool buyQueued = false
float pendingBuy = na
bool buyReady = false
float protectiveSell = na
bool sellReady = false
bool closeReady = false
bool entered = false
bool exited = false
float lastEntryPrice = na
float lastExitPrice = na
method fillBuy(OrderBook book, float price) =>
book.longActive := true
book.entered := true
book.lastEntryPrice := price
book.buyQueued := false
book.pendingBuy := na
book.buyReady := false
method fillSell(OrderBook book, float price) =>
book.longActive := false
book.exited := true
book.lastExitPrice := price
book.protectiveSell := na
book.sellReady := false
book.closeReady := false
method checkProtection(OrderBook book, float price) =>
if book.longActive and not na(book.protectiveSell)
if book.sellReady or price <= book.protectiveSell
book.fillSell(price)
method openBar(OrderBook book, float price) =>
// Market close requests take effect at the next open. Price orders crossed by
// the overnight gap also use this open. A buy can activate attached protection.
if book.longActive and book.closeReady
book.fillSell(price)
book.closeReady := false
book.checkProtection(price)
if not book.longActive and book.buyQueued
if book.buyReady or (not na(book.pendingBuy) and price >= book.pendingBuy)
book.fillBuy(price)
book.checkProtection(price)
method walkLeg(OrderBook book, float fromPrice, float toPrice) =>
if toPrice > fromPrice
if not book.longActive and book.buyQueued and not na(book.pendingBuy)
if fromPrice < book.pendingBuy and toPrice >= book.pendingBuy
float fillPrice = book.pendingBuy
book.fillBuy(fillPrice)
book.checkProtection(fillPrice)
else if toPrice < fromPrice
if book.longActive and not na(book.protectiveSell)
if fromPrice > book.protectiveSell and toPrice <= book.protectiveSell
book.fillSell(book.protectiveSell)
method walkBar(OrderBook book, float barOpen, float barHigh, float barLow, float barClose) =>
book.entered := false
book.exited := false
// The written specification resolves equal distances with the low first.
bool highFirst = math.abs(barOpen - barHigh) < math.abs(barOpen - barLow)
array<float> route = array.from(barOpen, highFirst ? barHigh : barLow, highFirst ? barLow : barHigh, barClose)
book.openBar(array.get(route, 0))
for segment = 1 to array.size(route) - 1
book.walkLeg(array.get(route, segment - 1), array.get(route, segment))
var OrderBook book = OrderBook.new()
var float breakoutLevel = na
float newPivot = ta.pivothigh(high, pivotWidth, pivotWidth)
bool pivotIsUsable = not na(newPivot) and newPivot != 0.0 and not na(newPivot - newPivot)
if pivotIsUsable
breakoutLevel := newPivot
// The source tracker deliberately begins unknown. A delayed raw signal sets its
// direction; a current low touching the previous calculated stop may set it flat.
var int sourceTracker = na
var float calculatedStop = na
float previousCalculatedStop = calculatedStop[1]
float rollingLow = ta.lowest(low, stopLookback)
bool insideDates = time > firstAllowedTime and time < lastAllowedTime
bool trendAllowed = not useTrendFilter or (not na(breakoutLevel) and not na(trendAverage) and breakoutLevel > trendAverage)
bool distanceAllowed = not limitDistance or (not na(previousCalculatedStop) and not na(dailyAtr) and breakoutLevel - previousCalculatedStop < dailyAtr * atrPercent / 100.0)
bool entryAllowed = insideDates and trendAllowed and distanceAllowed
bool rawHighCross = ta.crossover(high, breakoutLevel)
bool rawCloseCross = ta.crossunder(close, exitAverage)
bool rawEntry = rawHighCross and entryAllowed
bool guardedClose = low[1] > exitAverage and rawCloseCross
if barstate.isconfirmed
// Only orders staged at an earlier close participate in this bar's route.
book.walkBar(open, high, low, close)
if rawEntry[1]
sourceTracker := 1
else if rawCloseCross[1]
sourceTracker := 0
else if not na(previousCalculatedStop) and low <= previousCalculatedStop
sourceTracker := 0
else
sourceTracker := sourceTracker[1]
calculatedStop := sourceTracker == 1 ? previousCalculatedStop : rollingLow
// Eligibility replaces the single pending buy at this close. A failed filter
// leaves a previously pending buy untouched, including after the date bound.
if not book.longActive and entryAllowed
book.buyQueued := true
book.pendingBuy := breakoutLevel
book.buyReady := na(breakoutLevel) or close >= breakoutLevel
// Protection is refreshed each close using the preceding calculated stop.
// Staging alongside a pending buy permits a later same-bar entry and exit.
if book.longActive or book.buyQueued
if not na(previousCalculatedStop)
book.protectiveSell := previousCalculatedStop
book.sellReady := book.longActive and close <= previousCalculatedStop
else
book.protectiveSell := na
book.sellReady := false
book.closeReady := book.longActive and guardedClose
// Chart-native colors remain visible on every bar without an actual modeled fill.
// A bar containing both fills shows the exit color. Open realtime bars stay native.
color eventColor = barstate.isconfirmed ? (book.exited ? exitColor : book.entered ? entryColor : na) : na
barcolor(eventColor, title = "Entry / exit candles", editable = false)
plot(showAverages ? firstAverage : na, title = "First average", color = #3388FF, display = display.pane)
plot(showAverages ? secondAverage : na, title = "Second average", color = #F59E0B, display = display.pane)
plot(showAverages ? trendAverage : na, title = "Trend average", color = #8B5CF6, display = display.pane)