本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// ═══════════════════════════════════════════════════════════════════════════════════════════
// CRT LIQUIDITY EXECUTION PRO
// Candle Range Theory (CRT) — Liquidity Sweep + Body Recovery Execution Indicator
// Pine Script® Version 6
// ═══════════════════════════════════════════════════════════════════════════════════════════
//
// © Michael_Fx_Trader — All Rights Reserved.
//
// ───────────────────────────────────────────────────────────────────────────────────────────
// AUTHOR VERIFICATION
// ───────────────────────────────────────────────────────────────────────────────────────────
// Script Name : CRT Liquidity Execution PRO
// Author : Michael_Fx_Trader
// Publisher : Michael_Fx_Trader
// Rights Holder : Michael_Fx_Trader (full ownership of copyright and publishing rights)
// TradingView ID : Michael_Fx_Trader
// Language : Pine Script® v6
// Script Type : Indicator (overlay) — NOT a strategy
// Creation Date : 14 November 2026
//
// ───────────────────────────────────────────────────────────────────────────────────────────
// DECLARATION OF ORIGINALITY
// ───────────────────────────────────────────────────────────────────────────────────────────
// I, Michael_Fx_Trader, being the author, publisher and sole rights holder of this script,
// hereby declare and confirm that:
//
// 1. ORIGINAL WORK — This script is an original implementation written specifically for
// my own use and publication. The detection engine, trade-plan logic, drawing engine,
// trade-management tracker and alert system were designed and coded from scratch.
//
// 2. NOT COPIED — No source code has been copied, translated, re-skinned, mashed-up or
// adapted from any other TradingView script, open-source or closed-source, and no
// third-party library or external import is used.
//
// 3. CONCEPT vs. CODE — "Candle Range Theory (CRT)" and "liquidity sweep" are general,
// publicly known price-action concepts that belong to no individual. Only the concepts
// are shared; this specific code, logic structure, visual design and tooling are my
// own original expression of them.
//
// 4. SINGLE-PURPOSE LOGIC — The script uses pure price action only (open, high, low,
// close of the chart's own bars). It contains no hidden dependencies, no request.*()
// calls, no lookahead and no external data.
//
// 5. RIGHTS — All rights to this script are held by Michael_Fx_Trader. Redistribution,
// resale, republishing or re-use of this code without written permission from the
// author is not allowed.
//
// 6. EDUCATIONAL NOTICE — This indicator is a charting and analysis tool. It is not
// financial advice and does not guarantee profit. Trading involves substantial risk.
//
// ───────────────────────────────────────────────────────────────────────────────────────────
// CORE LOGIC SUMMARY
// ───────────────────────────────────────────────────────────────────────────────────────────
// BULLISH CRT (BUY)
// • CRT reference candle : the previous candle [1] is bearish (close < open).
// • Sweep candle : the current candle trades BELOW the CRT Low (sell-side
// liquidity sweep) and CLOSES back inside the CRT candle BODY
// (between the CRT Close and CRT Open).
// • ENTRY : close of the sweep candle.
// • STOP LOSS : low of the sweep candle.
// • TAKE PROFIT : high of the original CRT reference candle, plus the optional
// "TP Extension" (default 20% of the CRT range; 0 = exact high).
//
// BEARISH CRT (SELL) — exact mirror
// • CRT reference candle : the previous candle [1] is bullish (close > open).
// • Sweep candle : trades ABOVE the CRT High and CLOSES back inside the CRT body
// (between the CRT Open and CRT Close).
// • ENTRY : close of the sweep candle.
// • STOP LOSS : high of the sweep candle.
// • TAKE PROFIT : low of the original CRT reference candle, minus the optional
// "TP Extension" (default 20% of the CRT range; 0 = exact low).
//
// Only the IMMEDIATE next candle can confirm a setup. A sweep two or more candles later
// never creates a signal.
//
// ACCURACY FILTERS (optional, ON by default, price-geometry only)
// Master switch "High Accuracy Mode" adds quality checks on top of the pure CRT rule:
// meaningful CRT range (vs ATR) and body, controlled sweep depth (fake-out, not a
// breakout), strong rejection wick on the sweep candle, rejection candle colour, and a
// minimum reward-to-risk. Switch it OFF to get the pure CRT logic with no extra filters.
//
// ───────────────────────────────────────────────────────────────────────────────────────────
// NON-REPAINTING GUARANTEES
// ───────────────────────────────────────────────────────────────────────────────────────────
// • Every signal, drawing and status change is produced only on a CONFIRMED (closed) bar
// (barstate.isconfirmed). Historical signals therefore equal real-time signals.
// • No lookahead, no security()/request.*() calls, no repainting pivot logic.
// • Entry, SL, TP and CRT reference levels are frozen at signal time and never move.
//
// ───────────────────────────────────────────────────────────────────────────────────────────
// PLATFORM LIMITATIONS (stated honestly)
// ───────────────────────────────────────────────────────────────────────────────────────────
// • Pine Script cannot change the TradingView chart canvas background or the crosshair
// colour. The optional "Paint Black Background" input only tints the bar columns with
// bgcolor(). For a fully black canvas set: Chart Settings → Canvas → Background.
// • If TP and SL are both touched inside one candle, the intrabar order cannot be known
// from chart data, so the trade is marked UNRESOLVED instead of guessing.
// ═══════════════════════════════════════════════════════════════════════════════════════════
//@version=6
indicator(
title = "CRT Liquidity Execution PRO",
shorttitle = "CRT PRO",
overlay = true,
max_boxes_count = 500,
max_lines_count = 500,
max_labels_count = 500)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 1. INPUTS
// ═══════════════════════════════════════════════════════════════════════════════════════════
const string G_DET = "CRT Detection"
const string G_VIS = "Trade Visualization"
const string G_APP = "Appearance"
const string G_MGT = "Trade Management"
const string G_ALR = "Alerts"
const string G_ACC = "Accuracy Filters"
// ── CRT Detection ───────────────────────────────────────────────────────────────────────────
bool enableBull = input.bool(true, "Enable Bullish CRT", group = G_DET)
bool enableBear = input.bool(true, "Enable Bearish CRT", group = G_DET)
bool showCrtLines = input.bool(true, "Enable CRT High/Low Lines", group = G_DET)
bool showCrtBox = input.bool(true, "Enable CRT Range Box", group = G_DET)
bool showSweepLbl = input.bool(true, "Enable Liquidity Sweep Labels", group = G_DET)
// ── Trade Visualization ─────────────────────────────────────────────────────────────────────
bool showBuy = input.bool(true, "Show BUY Signals", group = G_VIS)
bool showSell = input.bool(true, "Show SELL Signals", group = G_VIS)
bool showEntry = input.bool(true, "Show Entry Lines", group = G_VIS)
bool showSL = input.bool(true, "Show SL Lines", group = G_VIS)
bool showTP = input.bool(true, "Show TP Lines", group = G_VIS)
bool showBullBoxes = input.bool(true, "Show Bullish Trade Boxes", group = G_VIS)
bool showBearBoxes = input.bool(true, "Show Bearish Trade Boxes", group = G_VIS)
int extBars = input.int(20, "Box Extension Bars", minval = 1, maxval = 500, group = G_VIS,
tooltip = "How many bars the CRT zone and trade boxes extend to the right of the signal candle.")
int maxSetups = input.int(20, "Historical Setups Limit", minval = 1, maxval = 50, group = G_VIS,
tooltip = "Maximum number of setups kept on the chart. Oldest drawings are deleted automatically.")
float tpExtPct = input.float(20.0, "TP Extension (% of CRT range)", minval = 0.0, maxval = 300.0, step = 5.0, group = G_VIS,
tooltip = "Pushes Take Profit beyond the CRT High (BUY) or CRT Low (SELL) by this % of the CRT candle range. 0 = TP exactly at the CRT extreme (original rule).")
// ── Appearance ──────────────────────────────────────────────────────────────────────────────
bool paintBg = input.bool(true, "Paint Black Background (bgcolor)", group = G_APP,
tooltip = "Tints bar columns only. Pine cannot change the chart canvas. Set Chart Settings → Canvas for a full black chart.")
color bgClr = input.color(#000000, "Chart Background Color", group = G_APP)
bool applyCandles = input.bool(true, "Apply Custom Candle Colors", group = G_APP)
color bullCandleClr = input.color(#168BFF, "Bullish Candle Color", group = G_APP)
color bearCandleClr = input.color(#FFFFFF, "Bearish Candle Color", group = G_APP)
color dojiCandleClr = input.color(#9E9E9E, "Doji Candle Color", group = G_APP)
color crtLineClr = input.color(#FFFFFF, "CRT Line Color", group = G_APP)
color crtTextClr = input.color(#FFFFFF, "CRT Text Color", group = G_APP)
color entryClr = input.color(#168BFF, "Entry Line Color", group = G_APP)
color tpClr = input.color(#00E676, "Take Profit Color", group = G_APP)
color slClr = input.color(#FF1744, "Stop Loss Color", group = G_APP)
color crtZoneClr = input.color(#9E9E9E, "CRT Range Zone Color", group = G_APP)
color bodyZoneClr = input.color(#FF8A00, "CRT Body Highlight Color", group = G_APP)
color slZoneClr = input.color(#B71C1C, "Stop-Loss Zone Color", group = G_APP)
int bullBoxTransp = input.int(72, "Bullish Box Transparency", minval = 0, maxval = 100, group = G_APP)
int bearBoxTransp = input.int(72, "Bearish Box Transparency", minval = 0, maxval = 100, group = G_APP)
int slBoxTransp = input.int(60, "Stop-Loss Box Transparency", minval = 0, maxval = 100, group = G_APP)
int crtZoneTransp = input.int(86, "CRT Range Zone Transparency",minval = 0, maxval = 100, group = G_APP)
int lineWidth = input.int(1, "Line Width", minval = 1, maxval = 4, group = G_APP)
string lblSize = input.string(size.small, "Label Size", options = [size.tiny, size.small, size.normal, size.large], group = G_APP)
// ── Trade Management ────────────────────────────────────────────────────────────────────────
bool showStatus = input.bool(true, "Show Trade Status", group = G_MGT)
bool showRR = input.bool(true, "Show Risk/Reward Ratio", group = G_MGT)
// ── Accuracy Filters ────────────────────────────────────────────────────────────────────────
bool useAcc = input.bool(true, "High Accuracy Mode (master switch)", group = G_ACC,
tooltip = "ON = extra quality filters below. OFF = pure CRT rule only. Filters reduce the number of signals; they cannot guarantee profit.")
int atrLen = input.int(14, "ATR Length", minval = 1, group = G_ACC)
float minRangeAtr = input.float(0.6, "Min CRT Range (x ATR)", minval = 0.0, step = 0.1, group = G_ACC,
tooltip = "The CRT candle range must be at least this multiple of ATR. Removes tiny, noisy candles.")
float minBodyPct = input.float(30.0, "Min CRT Body (% of range)", minval = 0.0, maxval = 100.0, step = 5.0, group = G_ACC,
tooltip = "The CRT candle body must be at least this % of its High-Low range. Removes dojis / spinning tops.")
float maxDepthPct = input.float(100.0, "Max Sweep Depth (% of CRT range)", minval = 1.0, maxval = 500.0, step = 5.0, group = G_ACC,
tooltip = "How far beyond the CRT extreme the sweep may go. Very deep sweeps are breakouts, not fake-outs.")
float minWickPct = input.float(30.0, "Min Rejection Wick (% of sweep candle range)", minval = 0.0, maxval = 100.0, step = 5.0, group = G_ACC,
tooltip = "The sweep wick (below the body for BUY, above for SELL) must be at least this % of the sweep candle range.")
bool reqRejColor = input.bool(true, "Require Rejection Candle Color", group = G_ACC,
tooltip = "BUY: sweep candle must close above its open (bullish). SELL: must close below its open (bearish).")
float minRRfilter = input.float(1.0, "Min Reward / Risk", minval = 0.0, step = 0.1, group = G_ACC,
tooltip = "Skip setups where (TP distance / SL distance) is below this value.")
// ── Alerts ──────────────────────────────────────────────────────────────────────────────────
bool useDynamicAlerts = input.bool(true, "Send dynamic alert() messages", group = G_ALR,
tooltip = "Create ONE alert and choose 'Any alert() function call' to receive full details (symbol, timeframe, direction, entry, SL, TP). Alternatively create separate alerts from the individual alert conditions.")
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 2. TRADE OBJECT (one per confirmed setup — holds levels, status and all drawings)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Status codes: 0 = ACTIVE, 1 = TP HIT, 2 = SL HIT, 3 = UNRESOLVED (TP and SL in same bar),
// 4 = EXPIRED (extension limit reached with no TP/SL)
type CrtTrade
int dir // +1 = BUY, -1 = SELL
float entry
float sl
float tp
float risk
float reward
float rr
int crtBar // bar_index of the CRT reference candle
int sigBar // bar_index of the sweep (signal) candle
int endBar // right edge of the drawings
int status
float crtHigh
float crtLow
float crtOpen
float crtClose
float sweepLvl // swept liquidity level (sweep candle extreme)
box crtBox
box bodyBox
box tpBox
box slBox
line lnCrtHigh
line lnCrtLow
line lnEntry
line lnTp
line lnSl
line lnSweep
label lblSignal
label lblCrtHigh
label lblCrtLow
label lblSweep
label lblInfo
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 3. HELPER FUNCTIONS
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Human-readable trade status
f_statusName(int st) =>
st == 0 ? "ACTIVE" : st == 1 ? "TP HIT" : st == 2 ? "SL HIT" : st == 3 ? "UNRESOLVED (TP+SL SAME BAR)" : "EXPIRED (LIMIT)"
// Text shown at the right end of the entry line (status and risk-reward only; no price text)
f_infoText(CrtTrade t) =>
string s = ""
if showStatus
s := f_statusName(t.status)
if showRR
s := (s == "" ? "" : s + "\n") + "RR 1:" + str.tostring(t.rr, "#.##")
s
// Dynamic alert text (symbol, timeframe, direction, entry, SL, TP)
f_alertText(string title, CrtTrade t) =>
title + " | " + syminfo.ticker + " | TF " + timeframe.period + " | " + (t.dir == 1 ? "BUY" : "SELL") +
" | Entry " + str.tostring(t.entry, format.mintick) +
" | SL " + str.tostring(t.sl, format.mintick) +
" | TP " + str.tostring(t.tp, format.mintick)
// Positions every drawing of a trade. Single source of truth for geometry; it is called when
// the trade is created and again when the right edge is shortened (TP / SL reached).
f_layout(CrtTrade t, int endBar) =>
t.endBar := endBar
int midBar = math.round((t.crtBar + endBar) / 2.0)
// Boxes
box.set_lefttop(t.crtBox, t.crtBar, t.crtHigh)
box.set_rightbottom(t.crtBox, endBar, t.crtLow)
box.set_lefttop(t.bodyBox, t.crtBar, math.max(t.crtOpen, t.crtClose))
box.set_rightbottom(t.bodyBox, t.sigBar, math.min(t.crtOpen, t.crtClose))
box.set_lefttop(t.tpBox, t.sigBar, math.max(t.entry, t.tp))
box.set_rightbottom(t.tpBox, endBar, math.min(t.entry, t.tp))
box.set_lefttop(t.slBox, t.sigBar, math.max(t.entry, t.sl))
box.set_rightbottom(t.slBox, endBar, math.min(t.entry, t.sl))
// Lines
line.set_xy1(t.lnCrtHigh, t.crtBar, t.crtHigh)
line.set_xy2(t.lnCrtHigh, endBar, t.crtHigh)
line.set_xy1(t.lnCrtLow, t.crtBar, t.crtLow)
line.set_xy2(t.lnCrtLow, endBar, t.crtLow)
line.set_xy1(t.lnEntry, t.sigBar, t.entry)
line.set_xy2(t.lnEntry, endBar, t.entry)
line.set_xy1(t.lnTp, t.sigBar, t.tp)
line.set_xy2(t.lnTp, endBar, t.tp)
line.set_xy1(t.lnSl, t.sigBar, t.sl)
line.set_xy2(t.lnSl, endBar, t.sl)
line.set_xy1(t.lnSweep, t.sigBar, t.sweepLvl)
line.set_xy2(t.lnSweep, endBar, t.sweepLvl)
// Labels
label.set_xy(t.lblCrtHigh, midBar, t.crtHigh)
label.set_xy(t.lblCrtLow, midBar, t.crtLow)
label.set_xy(t.lblSweep, midBar, t.sweepLvl)
label.set_xy(t.lblInfo, endBar, t.entry)
t
// Creates a complete trade (levels + all drawings). Called once per confirmed setup.
f_newTrade(int dir, float entry, float sl, float tp, float crtH, float crtL, float crtO, float crtC) =>
int bi = bar_index
bool isBull = dir == 1
bool boxOn = isBull ? showBullBoxes : showBearBoxes
bool infoOn = showStatus or showRR
float risk = math.abs(entry - sl)
float reward = math.abs(tp - entry)
CrtTrade t = CrtTrade.new(
dir = dir,
entry = entry,
sl = sl,
tp = tp,
risk = risk,
reward = reward,
rr = risk > 0 ? reward / risk : 0.0,
crtBar = bi - 1,
sigBar = bi,
endBar = bi + extBars,
status = 0,
crtHigh = crtH,
crtLow = crtL,
crtOpen = crtO,
crtClose = crtC,
sweepLvl = sl)
// Colours resolved from the visibility toggles (hidden = na)
color tpFill = boxOn ? color.new(tpClr, isBull ? bullBoxTransp : bearBoxTransp) : na
color slFill = boxOn ? color.new(slZoneClr, slBoxTransp) : na
color crtFill = showCrtBox ? color.new(crtZoneClr, crtZoneTransp) : na
color crtBrd = showCrtBox ? color.new(crtZoneClr, 45) : na
color bodyFill = showCrtBox ? color.new(bodyZoneClr, 62) : na
color cCrtLine = showCrtLines ? crtLineClr : na
color cCrtTxt = showCrtLines ? crtTextClr : na
color cEntry = showEntry ? entryClr : na
color cTp = showTP ? tpClr : na
color cSl = showSL ? slClr : na
color cSweep = showSweepLbl ? crtLineClr : na
color cSweepTx = showSweepLbl ? crtTextClr : na
color cInfoTx = infoOn ? entryClr : na
bool sigOn = isBull ? showBuy : showSell
// ── Boxes (creation order = z-order: CRT range at the back) ──
t.crtBox := box.new(bi - 1, crtH, bi, crtL, border_color = crtBrd, border_width = 1, border_style = line.style_dashed,
bgcolor = crtFill, text = showCrtBox ? "CRT CANDLE" : "", text_size = lblSize, text_color = crtTextClr,
text_halign = text.align_center, text_valign = text.align_top)
t.bodyBox := box.new(bi - 1, crtO, bi, crtC, border_color = na, bgcolor = bodyFill)
t.tpBox := box.new(bi, tp, bi, entry, border_color = na, bgcolor = tpFill,
text = boxOn ? (isBull ? "BUY | BULLISH CRT" : "SELL | BEARISH CRT") : "",
text_size = lblSize, text_color = crtTextClr, text_halign = text.align_center, text_valign = text.align_center)
t.slBox := box.new(bi, entry, bi, sl, border_color = na, bgcolor = slFill,
text = (boxOn and showSweepLbl) ? "CLOSED IN RANGE" : "",
text_size = lblSize, text_color = crtTextClr, text_halign = text.align_center, text_valign = text.align_center)
// ── Lines ──
t.lnCrtHigh := line.new(bi, crtH, bi, crtH, xloc = xloc.bar_index, color = cCrtLine, style = line.style_solid, width = lineWidth)
t.lnCrtLow := line.new(bi, crtL, bi, crtL, xloc = xloc.bar_index, color = cCrtLine, style = line.style_solid, width = lineWidth)
t.lnEntry := line.new(bi, entry, bi, entry, xloc = xloc.bar_index, color = cEntry, style = line.style_solid, width = lineWidth)
t.lnTp := line.new(bi, tp, bi, tp, xloc = xloc.bar_index, color = cTp, style = line.style_solid, width = lineWidth)
t.lnSl := line.new(bi, sl, bi, sl, xloc = xloc.bar_index, color = cSl, style = line.style_solid, width = lineWidth)
t.lnSweep := line.new(bi, sl, bi, sl, xloc = xloc.bar_index, color = cSweep, style = line.style_dashed, width = lineWidth)
// ── Labels ──
t.lblSignal := label.new(bi, isBull ? low : high, sigOn ? (isBull ? "BUY" : "SELL") : "",
style = isBull ? label.style_label_up : label.style_label_down,
color = sigOn ? (isBull ? entryClr : #FFFFFF) : na,
textcolor = isBull ? #FFFFFF : #000000, size = lblSize)
t.lblCrtHigh := label.new(bi - 1, crtH, showCrtLines ? "CRT HIGH" : "",
style = label.style_label_down, color = na, textcolor = cCrtTxt, size = lblSize)
t.lblCrtLow := label.new(bi - 1, crtL, showCrtLines ? "CRT LOW" : "",
style = label.style_label_up, color = na, textcolor = cCrtTxt, size = lblSize)
t.lblSweep := label.new(bi, sl, showSweepLbl ? (isBull ? "CRT LOW SWEEP" : "CRT HIGH SWEEP") : "",
style = isBull ? label.style_label_up : label.style_label_down, color = na, textcolor = cSweepTx, size = lblSize)
t.lblInfo := label.new(bi, entry, f_infoText(t),
style = label.style_label_left, color = na, textcolor = cInfoTx, size = lblSize)
f_layout(t, bi + extBars)
t
// Applies a new status: shortens the drawings when the trade is closed and refreshes visuals
f_applyStatus(CrtTrade t) =>
bool boxOn = t.dir == 1 ? showBullBoxes : showBearBoxes
bool infoOn = showStatus or showRR
if t.status == 1 or t.status == 2 or t.status == 3
f_layout(t, bar_index)
color sc = t.status == 1 ? tpClr : t.status == 2 ? slClr : t.status == 3 ? color.gray : entryClr
label.set_text(t.lblInfo, f_infoText(t))
label.set_textcolor(t.lblInfo, infoOn ? sc : na)
if boxOn
if t.status == 1
box.set_border_color(t.tpBox, tpClr)
box.set_border_width(t.tpBox, 2)
else if t.status == 2
box.set_border_color(t.slBox, slClr)
box.set_border_width(t.slBox, 2)
else if t.status == 3
box.set_border_color(t.tpBox, color.gray)
box.set_border_color(t.slBox, color.gray)
t
// Safely removes every drawing that belongs to a trade
f_deleteTrade(CrtTrade t) =>
box.delete(t.crtBox)
box.delete(t.bodyBox)
box.delete(t.tpBox)
box.delete(t.slBox)
line.delete(t.lnCrtHigh)
line.delete(t.lnCrtLow)
line.delete(t.lnEntry)
line.delete(t.lnTp)
line.delete(t.lnSl)
line.delete(t.lnSweep)
label.delete(t.lblSignal)
label.delete(t.lblCrtHigh)
label.delete(t.lblCrtLow)
label.delete(t.lblSweep)
label.delete(t.lblInfo)
t
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 4. CRT DETECTION (pure OHLC price action, evaluated on the closed sweep candle)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Reference candle = previous candle [1]; sweep candle = current candle [0].
bool crtIsBearish = close[1] < open[1] // bearish CRT reference candle (BUY setup)
bool crtIsBullish = close[1] > open[1] // bullish CRT reference candle (SELL setup)
// BUY: low sweeps below CRT Low AND close recovers inside the bearish body [close[1] .. open[1]]
bool bullSweep = crtIsBearish and low < low[1] and close >= close[1] and close <= open[1]
// SELL: high sweeps above CRT High AND close recovers inside the bullish body [open[1] .. close[1]]
bool bearSweep = crtIsBullish and high > high[1] and close <= close[1] and close >= open[1]
// ── High Accuracy filters (price geometry only; skipped entirely when the master switch is off) ──
float atrVal = ta.atr(atrLen) // evaluated on every bar for consistency
float atrRef = atrVal[1] // ATR as it stood at the CRT reference candle (no sweep-candle influence)
float crtRange = high[1] - low[1]
float crtBody = math.abs(close[1] - open[1])
float sweepRange = high - low
// CRT candle must be meaningful: enough range vs ATR and a real body (not a doji)
bool crtQualityOk = crtRange >= minRangeAtr * atrRef and crtBody >= crtRange * minBodyPct / 100.0
// BUY side
float bullDepth = low[1] - low // how far below CRT Low the sweep went
float bullWick = math.min(open, close) - low // rejection wick under the sweep body
float bullRR = (high[1] - close) / (close - low) // reward (to CRT High) / risk (to sweep Low)
bool accBull = crtQualityOk and bullDepth <= crtRange * maxDepthPct / 100.0 and sweepRange > 0 and
bullWick >= sweepRange * minWickPct / 100.0 and (not reqRejColor or close > open) and bullRR >= minRRfilter
// SELL side
float bearDepth = high - high[1]
float bearWick = high - math.max(open, close)
float bearRR = (close - low[1]) / (high - close)
bool accBear = crtQualityOk and bearDepth <= crtRange * maxDepthPct / 100.0 and sweepRange > 0 and
bearWick >= sweepRange * minWickPct / 100.0 and (not reqRejColor or close < open) and bearRR >= minRRfilter
// Only closed candles can produce signals (non-repainting)
bool bullSetup = barstate.isconfirmed and enableBull and bullSweep and (not useAcc or accBull)
bool bearSetup = barstate.isconfirmed and enableBear and bearSweep and (not useAcc or accBear)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 5. TRADE ENGINE
// ═══════════════════════════════════════════════════════════════════════════════════════════
var array<CrtTrade> trades = array.new<CrtTrade>()
// One-bar event flags (reset every bar → alerts fire once, never repeatedly)
bool evBullDet = false
bool evBearDet = false
bool evBuy = false
bool evSell = false
bool evBuyTP = false
bool evBuySL = false
bool evSellTP = false
bool evSellSL = false
// Values exposed to alertcondition() placeholders
float sigEntry = na
float sigSL = na
float sigTP = na
float hitEntry = na
float hitSL = na
float hitTP = na
if barstate.isconfirmed
// 5a. Update open trades (only bars AFTER the signal candle can close a trade)
for t in trades
if t.status == 0 and bar_index > t.sigBar
bool tpHit = t.dir == 1 ? high >= t.tp : low <= t.tp
bool slHit = t.dir == 1 ? low <= t.sl : high >= t.sl
if tpHit and slHit
// Intrabar order cannot be established → do not assume a winner
t.status := 3
f_applyStatus(t)
else if tpHit
t.status := 1
f_applyStatus(t)
hitEntry := t.entry
hitSL := t.sl
hitTP := t.tp
if t.dir == 1
evBuyTP := true
else
evSellTP := true
if useDynamicAlerts
alert(f_alertText(t.dir == 1 ? "BUY Take Profit Hit" : "SELL Take Profit Hit", t), alert.freq_once_per_bar_close)
else if slHit
t.status := 2
f_applyStatus(t)
hitEntry := t.entry
hitSL := t.sl
hitTP := t.tp
if t.dir == 1
evBuySL := true
else
evSellSL := true
if useDynamicAlerts
alert(f_alertText(t.dir == 1 ? "BUY Stop Loss Hit" : "SELL Stop Loss Hit", t), alert.freq_once_per_bar_close)
else if bar_index >= t.endBar
t.status := 4
f_applyStatus(t)
// 5b. New BUY setup: Entry = sweep close | SL = sweep low | TP = original CRT high
if bullSetup
CrtTrade nt = f_newTrade(1, close, low, high[1] + crtRange * tpExtPct / 100.0, high[1], low[1], open[1], close[1])
array.push(trades, nt)
evBullDet := true
evBuy := true
sigEntry := nt.entry
sigSL := nt.sl
sigTP := nt.tp
if useDynamicAlerts
alert(f_alertText("Bullish CRT Detected / BUY Signal Confirmed", nt), alert.freq_once_per_bar_close)
// 5c. New SELL setup: Entry = sweep close | SL = sweep high | TP = original CRT low
if bearSetup
CrtTrade nt = f_newTrade(-1, close, high, low[1] - crtRange * tpExtPct / 100.0, high[1], low[1], open[1], close[1])
array.push(trades, nt)
evBearDet := true
evSell := true
sigEntry := nt.entry
sigSL := nt.sl
sigTP := nt.tp
if useDynamicAlerts
alert(f_alertText("Bearish CRT Detected / SELL Signal Confirmed", nt), alert.freq_once_per_bar_close)
// 5d. Respect the historical setups limit (delete oldest drawings safely)
while array.size(trades) > maxSetups
f_deleteTrade(array.shift(trades))
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 6. CHART APPEARANCE (candles, background, CRT reference-candle highlight)
// ═══════════════════════════════════════════════════════════════════════════════════════════
color candleClr = close > open ? bullCandleClr : close < open ? bearCandleClr : dojiCandleClr
barcolor(applyCandles ? candleClr : na, title = "Custom Candle Colors")
bgcolor(paintBg ? bgClr : na, title = "Black Background")
// Subtle highlight on the CRT reference candle (offset -1 = the candle before the signal candle;
// it is drawn only after the signal candle has closed, so there is no repaint).
bgcolor((bullSetup or bearSetup) and showCrtBox ? color.new(crtZoneClr, 82) : na, offset = -1, title = "CRT Reference Candle")
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 7. ALERT PLACEHOLDER VALUES (shown in Data Window only, not on the chart)
// ═══════════════════════════════════════════════════════════════════════════════════════════
plot(sigEntry, "Entry", display = display.data_window)
plot(sigSL, "Stop Loss", display = display.data_window)
plot(sigTP, "Take Profit", display = display.data_window)
plot(hitEntry, "Closed Entry", display = display.data_window)
plot(hitSL, "Closed SL", display = display.data_window)
plot(hitTP, "Closed TP", display = display.data_window)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// 8. ALERT CONDITIONS (confirmed events only — each fires once on the closing tick)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Note: "CRT Detected" is the confirmed CRT pattern (reference candle + valid sweep + body
// recovery close), so it fires on the same candle as the matching BUY / SELL signal.
alertcondition(evBullDet, "Bullish CRT Detected",
'Bullish CRT Detected | {{ticker}} | TF {{interval}} | Entry {{plot("Entry")}} | SL {{plot("Stop Loss")}} | TP {{plot("Take Profit")}}')
alertcondition(evBearDet, "Bearish CRT Detected",
'Bearish CRT Detected | {{ticker}} | TF {{interval}} | Entry {{plot("Entry")}} | SL {{plot("Stop Loss")}} | TP {{plot("Take Profit")}}')
alertcondition(evBuy, "BUY Signal Confirmed",
'BUY Signal Confirmed | {{ticker}} | TF {{interval}} | Entry {{plot("Entry")}} | SL {{plot("Stop Loss")}} | TP {{plot("Take Profit")}}')
alertcondition(evSell, "SELL Signal Confirmed",
'SELL Signal Confirmed | {{ticker}} | TF {{interval}} | Entry {{plot("Entry")}} | SL {{plot("Stop Loss")}} | TP {{plot("Take Profit")}}')
alertcondition(evBuyTP, "BUY Take Profit Hit",
'BUY Take Profit Hit | {{ticker}} | TF {{interval}} | Entry {{plot("Closed Entry")}} | SL {{plot("Closed SL")}} | TP {{plot("Closed TP")}}')
alertcondition(evBuySL, "BUY Stop Loss Hit",
'BUY Stop Loss Hit | {{ticker}} | TF {{interval}} | Entry {{plot("Closed Entry")}} | SL {{plot("Closed SL")}} | TP {{plot("Closed TP")}}')
alertcondition(evSellTP, "SELL Take Profit Hit",
'SELL Take Profit Hit | {{ticker}} | TF {{interval}} | Entry {{plot("Closed Entry")}} | SL {{plot("Closed SL")}} | TP {{plot("Closed TP")}}')
alertcondition(evSellSL, "SELL Stop Loss Hit",
'SELL Stop Loss Hit | {{ticker}} | TF {{interval}} | Entry {{plot("Closed Entry")}} | SL {{plot("Closed SL")}} | TP {{plot("Closed TP")}}')
// ═══════════════════════════════════════════════════════════════════════════════════════════
// END OF SCRIPT — CRT Liquidity Execution PRO | © Michael_Fx_Trader | All Rights Reserved
// ═══════════════════════════════════════════════════════════════════════════════════════════