本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("HTF 5+1 | Sattam", shorttitle = "HTF 5+1", overlay = true, max_lines_count = 500, max_boxes_count = 500, max_labels_count = 500)
// =============================================================================
// Inputs
// =============================================================================
string G_S = "Structure · الهيكل"
string G_E = "Entries · الدخول"
string G_D = "Drawing · الرسم"
string G_P = "Panel · اللوحة"
int iSwing = input.int(2, "Swing confirmation bars · شموع تأكيد القمة والقاع", minval = 1, maxval = 10, group = G_S, tooltip = "Closed bars without a new extreme before a swing is accepted. · عدد الشموع المغلقة بلا قمة أو قاع جديد قبل اعتماد النقطة.")
bool iMedium = input.bool(true, "Medium-probability sweeps · السحب متوسط الاحتمال", group = G_S, tooltip = "Sweep of a subordinate swing that failed to break structure. · سحب نقطة فرعية فشلت في كسر الهيكل.")
bool iInternal = input.bool(false, "Internal setups (ibos) · الإعدادات الداخلية", group = G_S, tooltip = "Counter-trend setups inside the pullback while the main limit waits. Lower probability. · صفقات عكس الاتجاه داخل التراجع أثناء انتظار الحد الأساسي، احتمالها أقل.")
int iInnSwing = input.int(1, "Internal swing bars · شموع الهيكل الداخلي", minval = 1, maxval = 5, group = G_S)
float iFib = input.float(0.71, "Entry level · مستوى الدخول", minval = 0.5, maxval = 0.9, step = 0.01, group = G_E)
bool iObLimit = input.bool(true, "Second limit on closest Order Block · حد ثانٍ على أقرب أوردر بلوك", group = G_E)
float iObRR = input.float(2.0, "Order Block limit target (R) · هدف حد البلوك", minval = 1.0, step = 0.5, group = G_E)
float iMatch = input.float(0.10, "Block/level match tolerance (ATR) · سماحية تطابق البلوك مع المستوى", minval = 0.0, step = 0.05, group = G_E)
int iLqPiv = input.int(2, "Liquidity swing bars (+1) · شموع نقاط السيولة", minval = 1, maxval = 5, group = G_E)
float iEqTol = input.float(0.15, "Equal highs/lows tolerance (ATR) · سماحية القمم والقيعان المتساوية", minval = 0.01, step = 0.01, group = G_E)
bool iShowBos = input.bool(true, "BOS and sweeps · كسر الهيكل والسحب", group = G_D)
bool iShowVol = input.bool(true, "Volume gaps · فجوات الحجم", group = G_D)
bool iShowOb = input.bool(true, "Order Blocks · الأوردر بلوك", group = G_D)
float iObDist = input.float(8.0, "Extend blocks within (ATR) · امتداد البلوك ضمن مسافة", minval = 1.0, step = 1.0, group = G_D, tooltip = "Blocks farther than this from price stop extending so they do not stretch the chart scale. · البلوك الأبعد من هذه المسافة عن السعر يتوقف عن الامتداد حتى لا يضغط مقياس الشارت.")
bool iVolAll = input.bool(false, "Volume on every break · الحجم على كل كسر", group = G_D, tooltip = "Off: volume lines only on breaks that follow a sweep. · عند الإطفاء تُرسم خطوط الحجم فقط على الكسور التي تلي سحب السيولة.")
bool iShowFib = input.bool(true, "Fibonacci levels · مستويات فيبوناتشي", group = G_D)
bool iShowLq = input.bool(true, "Extra liquidity (+1) · السيولة الإضافية", group = G_D)
bool iPanel = input.bool(true, "Show panel · إظهار اللوحة", group = G_P)
string iLang = input.string("العربية", "Panel language · لغة اللوحة", options = ["العربية", "English"], group = G_P)
string iPos = input.string("Top right", "Position · الموضع", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = G_P)
// =============================================================================
// Colors
// =============================================================================
color C_FG = chart.fg_color
color C_TAN = #e0a96d
color C_GREY = #787b86
color C_RED = #f23645
color C_GRN = #22ab94
color C_BLU = #2962ff
color C_NONE = color.new(#000000, 100)
// =============================================================================
// Bar history: filled on every bar so conditionally called functions read true values
// =============================================================================
var array<float> A_O = array.new<float>()
var array<float> A_H = array.new<float>()
var array<float> A_L = array.new<float>()
var array<float> A_C = array.new<float>()
A_O.push(open)
A_H.push(high)
A_L.push(low)
A_C.push(close)
float atr14 = ta.atr(14)
float pHi = ta.pivothigh(high, iSwing, iSwing)
float pLo = ta.pivotlow(low, iSwing, iSwing)
float ipHi = ta.pivothigh(high, iInnSwing, iInnSwing)
float ipLo = ta.pivotlow(low, iInnSwing, iInnSwing)
float lqHi = ta.pivothigh(high, iLqPiv, iLqPiv)
float lqLo = ta.pivotlow(low, iLqPiv, iLqPiv)
// =============================================================================
// Types
// =============================================================================
type Side
float p
int b
bool wait = false
float runP
int runB
bool pulled = false
type Sweep
bool active = false
int kind = 0
float lvl
int lvlB
int bar
type Ob
int s
float top
float bot
int left
bool mit = false
box bx
bool inner = false
int gaps = 1
bool stopG = false
type Ord
int s
int kind
float entry
float sl
float tp
int st = 0
int placed
int fillB
int gid
float plan
line l0
line l71
line l1
label t0
label t71
label t1
type Setup
int s
int kind
float anchor
int anchorB
float swing
int swingB
int st = 0
bool missed = false
int gid
Ob ob
float mnrP
int mnrB
Ord fib
Ord obo
int plus = 0
string plusTx = ""
line lq
label lqT
type Eng
bool internal
int n
int dir = 0
Side up
Side dn
Sweep sw
int pS = 0
int pBar
float pLvl
int pLvlB
bool pWick = false
int cnt = 0
bool wOn = false
float wLvl
int wLvlB
int wBar
float wSub
int wSubB
float subP
int subB
line lastLn
label lastLb
Setup aL
Setup aS
type Stats
int fW = 0
int fL = 0
int oW = 0
int oL = 0
int iW = 0
int iL = 0
float fR = 0.0
float oR = 0.0
float iR = 0.0
int gid = 0
bool cBos = false
bool cVol = false
bool cOb = false
int cSw = 0
int cDir = 0
Setup last
Ob lastOb
var array<Ob> obs = array.new<Ob>()
var array<Ord> live = array.new<Ord>()
var array<float> PHp = array.new<float>()
var array<int> PHb = array.new<int>()
var array<float> PLp = array.new<float>()
var array<int> PLb = array.new<int>()
var Stats g = Stats.new()
var Eng ext = Eng.new(internal = false, n = iSwing, up = Side.new(), dn = Side.new(), sw = Sweep.new())
var Eng inn = Eng.new(internal = true, n = iInnSwing, up = Side.new(), dn = Side.new(), sw = Sweep.new())
// =============================================================================
// Helpers
// =============================================================================
f_t(string ar, string en) => iLang == "English" ? en : ar
f_x(int s, int i) => s == 1 ? A_H.get(i) : A_L.get(i)
f_extreme(int s, int fromB, int toB) =>
float best = na
int bestB = na
for i = fromB to toB
float v = f_x(s, i)
if na(best) or s * (v - best) > 0
best := v
bestB := i
[best, bestB]
// The first volume candle wicks past the candle before it, in the direction of the push.
f_wickPush(int s, int m) =>
bool past = s == 1 ? A_L.get(m) < A_L.get(m - 1) : A_H.get(m) > A_H.get(m - 1)
past and s * (A_C.get(m) - A_O.get(m)) > 0
// Normal push: the first and third candles do not overlap. Wick push: the orders rest
// on the wick, so volume is measured from that candle's body to the next candle.
f_isGap(int s, int m) =>
bool res = s == 1 ? A_H.get(m - 1) < A_L.get(m + 1) : A_L.get(m - 1) > A_H.get(m + 1)
if f_wickPush(s, m)
float edge = s == 1 ? math.min(A_O.get(m), A_C.get(m)) : math.max(A_O.get(m), A_C.get(m))
res := s == 1 ? edge < A_L.get(m + 1) : edge > A_H.get(m + 1)
res
// Volume = a three-candle gap inside the leg that broke structure. Returns the
// first middle candle of the push nearest to the break, or na when none exists.
f_gap(int s, int fromB, int toB) =>
int found = na
if toB >= fromB + 1
for m = toB to fromB + 1
if f_isGap(s, m)
found := m
break
if not na(found)
while found - 1 >= fromB + 1
if f_isGap(s, found - 1)
found -= 1
else
break
found
// Consecutive gaps from the start of the push. One gap followed by overlap is a gap stop.
f_gapRun(int s, int m) =>
int cntG = 1
int k = m + 1
while k <= bar_index - 1
if f_isGap(s, k)
cntG += 1
k += 1
else
break
[cntG, cntG == 1 and m + 1 <= bar_index - 1]
f_txt(int x, float y, string tx, string st, color c) =>
label.new(x, y, text = tx, style = st, color = C_NONE, textcolor = c, size = size.tiny)
f_bR(box b, int x) =>
if not na(b)
box.set_right(b, x)
f_lX(line l, int x) =>
if not na(l)
line.set_x2(l, x)
f_tX(label l, int x) =>
if not na(l)
label.set_x(l, x)
method side(Eng e, int s) => s == 1 ? e.up : e.dn
method slot(Eng e, int s) => s == 1 ? e.aL : e.aS
method setSlot(Eng e, int s, Setup a) =>
if s == 1
e.aL := a
else
e.aS := a
f_visible(Eng e, int s) =>
bool res = not e.internal
if e.internal
Setup x = ext.slot(-s)
if not na(x)
res := x.st == 1
res
f_kill(Ord o) =>
if not na(o)
line.delete(o.l0)
line.delete(o.l71)
line.delete(o.l1)
label.delete(o.t0)
label.delete(o.t71)
label.delete(o.t1)
o.st := 9
f_clearOrders(Setup a) =>
f_kill(a.fib)
f_kill(a.obo)
a.fib := na
a.obo := na
line.delete(a.lq)
label.delete(a.lqT)
a.lq := na
a.lqT := na
a.plus := 0
a.plusTx := ""
// Order Block: last candle before the volume push. When the first volume candle
// wicks past that candle, the orders rest on the wick itself.
f_newOb(int s, int m, bool show, bool showVol) =>
int first = m - 1
int third = m + 1
bool isWick = f_wickPush(s, m)
float obTop = A_H.get(first)
float obBot = A_L.get(first)
int leftB = first
if isWick
float bodyTop = math.max(A_O.get(m), A_C.get(m))
float bodyBot = math.min(A_O.get(m), A_C.get(m))
obTop := s == 1 ? bodyBot : A_H.get(m)
obBot := s == 1 ? A_L.get(m) : bodyTop
leftB := m
int mitB = na
if third <= bar_index
for i = third to bar_index
bool touched = s == 1 ? A_L.get(i) <= obTop : A_H.get(i) >= obBot
if touched
mitB := i
break
Ob o = Ob.new(s, obTop, obBot, leftB, not na(mitB))
[nG, stopG] = f_gapRun(s, m)
o.gaps := nG
o.stopG := stopG
if show and iShowOb
color fillC = s == 1 ? color.new(C_TAN, 78) : color.new(C_GREY, 72)
int rightB = na(mitB) ? bar_index + 5 : mitB
string boxTx = na(mitB) ? "Order Block" : ""
o.bx := box.new(leftB, obTop, rightB, obBot, border_color = C_NONE, bgcolor = fillC, text = boxTx, text_size = size.tiny, text_color = C_FG)
if show and showVol and iShowVol
float near = s == 1 ? obTop : obBot
float v1 = isWick ? near : (s == 1 ? A_H.get(first) : A_L.get(first))
float v2 = s == 1 ? A_L.get(third) : A_H.get(third)
line.new(first, v1, m + 8, v1, color = C_RED, width = 1)
line.new(third, v2, m + 8, v2, color = C_RED, width = 1)
string volTx = nG >= 2 ? "Volume x" + str.tostring(nG) : stopG ? "Volume, gap stop" : "Volume"
f_txt(m + 4, (v1 + v2) / 2, volTx, label.style_none, C_FG)
o
// Nearest unmitigated block beyond the entry level, inside the protected range.
f_pickOb(int s, float fib, float sl) =>
Ob best = na
float bestD = na
for o in obs
if o.s == s and not o.mit and not o.inner
float nearE = s == 1 ? o.top : o.bot
float farE = s == 1 ? o.bot : o.top
if s * (fib - farE) > 0 and s * (nearE - sl) > 0
float d = math.max(s * (fib - nearE), 0.0)
if na(bestD) or d < bestD
best := o
bestD := d
best
f_lqScan(Setup a) =>
if not na(a.fib) and not na(atr14)
line.delete(a.lq)
label.delete(a.lqT)
a.lq := na
a.lqT := na
a.plus := 0
a.plusTx := ""
float entry = a.fib.entry
array<float> ps = a.s == 1 ? PLp : PHp
array<int> bs = a.s == 1 ? PLb : PHb
array<float> xp = array.new<float>()
array<int> xb = array.new<int>()
for [i, v] in ps
if bs.get(i) > a.anchorB and a.s * (v - entry) > 0
xp.push(v)
xb.push(bs.get(i))
int n = xp.size()
int tlA = na
int tlB = na
int eqA = na
int eqB = na
float tolEq = atr14 * iEqTol
float tolTl = atr14 * iEqTol * 2
if n >= 2
for i = 0 to n - 2
int j = i
while j + 1 < n
if a.s * (xp.get(j + 1) - xp.get(j)) > 0
j += 1
else
break
if j - i >= 2
bool fits = true
float slope = (xp.get(j) - xp.get(i)) / (xb.get(j) - xb.get(i))
for k = i + 1 to j - 1
float yL = xp.get(i) + slope * (xb.get(k) - xb.get(i))
if math.abs(xp.get(k) - yL) > tolTl
fits := false
if fits and (na(tlA) or j - i > tlB - tlA)
tlA := i
tlB := j
int q = i
while q + 1 < n
if math.abs(xp.get(q + 1) - xp.get(i)) <= tolEq
q += 1
else
break
if q > i and (na(eqA) or q - i > eqB - eqA)
eqA := i
eqB := q
bool mnrOk = not na(a.mnrP) and a.s * (a.mnrP - entry) > 0
if not na(tlA)
a.plus := 4
a.plusTx := "trendline LQ"
if iShowLq
a.lq := line.new(xb.get(tlA), xp.get(tlA), xb.get(tlB), xp.get(tlB), color = C_FG, width = 1)
a.lqT := f_txt(int((xb.get(tlA) + xb.get(tlB)) / 2), (xp.get(tlA) + xp.get(tlB)) / 2, a.plusTx, a.s == 1 ? label.style_label_up : label.style_label_down, C_FG)
else if not na(eqA)
int cnt = eqB - eqA + 1
a.plus := cnt >= 3 ? 3 : 2
string word = a.s == 1 ? (cnt >= 3 ? " equal lows" : " equal bottoms") : (cnt >= 3 ? " equal highs" : " equal tops")
a.plusTx := str.tostring(cnt) + word
if iShowLq
a.lq := line.new(xb.get(eqA), xp.get(eqA), xb.get(eqB), xp.get(eqA), color = C_FG, style = line.style_dotted, width = 1)
a.lqT := f_txt(int((xb.get(eqA) + xb.get(eqB)) / 2), xp.get(eqA), a.plusTx + " $", a.s == 1 ? label.style_label_up : label.style_label_down, C_FG)
else if mnrOk
a.plus := 1
a.plusTx := "MNR = LQ+"
if iShowLq
a.lq := line.new(a.mnrB, a.mnrP, a.mnrB + 6, a.mnrP, color = C_GRN, width = 1)
a.lqT := label.new(a.mnrB, a.mnrP, text = "MNR = LQ+", style = a.s == 1 ? label.style_label_up : label.style_label_down, color = color.new(C_GRN, 60), textcolor = C_FG, size = size.tiny)
f_price(float p) => str.tostring(p, format.mintick)
// Fibonacci drawn from the protected swing to the retracement point; limit at the
// entry level, stop at 1, target at 0.
method place(Eng e, Setup a, Side S) =>
int t = bar_index
a.swing := S.p
a.swingB := S.b
a.st := 1
float entry = a.swing + (a.anchor - a.swing) * iFib
int k = e.internal ? 2 : 0
Ord f = Ord.new(s = a.s, kind = k, entry = entry, sl = a.anchor, tp = a.swing, placed = t, gid = a.gid, plan = math.abs(entry - a.anchor))
color fC = e.internal ? C_BLU : C_FG
string grade = a.kind == 2 ? " M" : ""
if iShowFib
f.l0 := line.new(a.swingB, a.swing, t + 10, a.swing, color = color.new(fC, 25), width = 1)
f.l71 := line.new(math.min(a.anchorB, a.swingB), entry, t + 10, entry, color = fC, width = 1)
f.l1 := line.new(a.anchorB, a.anchor, t + 10, a.anchor, color = color.new(fC, 25), width = 1)
f.t0 := f_txt(t + 10, a.swing, "0 (" + f_price(a.swing) + ")", label.style_label_left, C_FG)
f.t71 := f_txt(t + 10, entry, str.tostring(iFib) + " (" + f_price(entry) + ")" + grade, label.style_label_left, C_FG)
f.t1 := f_txt(t + 10, a.anchor, "1 (" + f_price(a.anchor) + ")", label.style_label_left, C_FG)
a.fib := f
live.push(f)
if not e.internal and iObLimit
Ob o = f_pickOb(a.s, entry, a.anchor)
if not na(o)
float nearE = a.s == 1 ? o.top : o.bot
float midE = (o.top + o.bot) / 2
float tol = na(atr14) ? 0.0 : atr14 * iMatch
float oEntry = nearE
if math.abs(midE - entry) <= tol or a.s * (midE - entry) > 0
oEntry := entry
else if math.abs(nearE - entry) <= tol or a.s * (nearE - entry) > 0
oEntry := midE
float riskP = math.abs(oEntry - a.anchor)
if a.s * (oEntry - a.anchor) > 0
float oTp = oEntry + a.s * iObRR * riskP
Ord b = Ord.new(s = a.s, kind = 1, entry = oEntry, sl = a.anchor, tp = oTp, placed = t, gid = a.gid, plan = riskP)
a.obo := b
live.push(b)
if not e.internal
f_lqScan(a)
alert((a.s == 1 ? "HTF 5+1 long limit " : "HTF 5+1 short limit ") + syminfo.ticker + " @ " + f_price(entry) + " SL " + f_price(a.anchor) + " TP " + f_price(a.swing), alert.freq_once_per_bar_close)
// A structure break: continuation or reversal. Applies the 5+1 list.
method bos(Eng e, int s, int lvlB, float lvl, int brk, bool hasVol, int gapM, bool rev, int revKind) =>
int t = bar_index
Side S = e.side(s)
Side O = e.side(-s)
[x, xb] = f_extreme(-s, lvlB, brk)
bool vis = f_visible(e, s)
if rev and hasVol and revKind == 3 and not na(e.lastLb)
label.set_text(e.lastLb, "sweep")
line.set_style(e.lastLn, line.style_arrow_right)
string base = e.internal ? "ibos" : "bos"
string tx = hasVol ? base : "no volume " + base
if vis and iShowBos
color cL = e.internal ? C_BLU : C_FG
e.lastLn := line.new(lvlB, lvl, brk, lvl, color = cL, width = 1)
e.lastLb := f_txt(int((lvlB + brk) / 2), lvl, tx, s == 1 ? label.style_label_down : label.style_label_up, cL)
bool swept = rev ? hasVol : e.sw.active
int kind = rev ? revKind : e.sw.kind
if kind == 2 and not iMedium
swept := false
if vis and iShowBos and not rev and e.sw.active and swept
string swTx = kind == 2 ? "sweep M" : "sweep"
line.new(e.sw.lvlB, e.sw.lvl, e.sw.bar, e.sw.lvl, color = C_FG, width = 1, style = line.style_arrow_right)
f_txt(int((e.sw.lvlB + e.sw.bar) / 2), e.sw.lvl, swTx, s == -1 ? label.style_label_down : label.style_label_up, C_FG)
Ob ob = na
if hasVol
ob := f_newOb(s, gapM, vis, swept or iVolAll)
ob.inner := e.internal
if not ob.mit
obs.push(ob)
if obs.size() > 60
obs.shift()
O.p := x
O.b := xb
O.wait := false
S.p := na
S.wait := true
S.pulled := false
[rP, rB] = f_extreme(s, xb, t)
S.runP := rP
S.runB := rB
Setup prev = e.slot(s)
bool valid = swept and hasVol and not na(ob)
if valid
valid := not ob.mit
if e.internal and valid
valid := f_visible(e, s)
if valid
if not na(prev)
f_clearOrders(prev)
g.gid += 1
Setup a = Setup.new(s = s, kind = kind, anchor = x, anchorB = xb, gid = g.gid, ob = ob)
e.setSlot(s, a)
if not e.internal
g.last := a
if vis and iShowBos
label.new(xb, x, text = "", style = label.style_circle, color = color.new(C_RED, 65), textcolor = color.new(C_RED, 20), size = size.normal)
f_txt(xb, x, s == 1 ? "PSL" : "PSH", s == 1 ? label.style_label_up : label.style_label_down, C_FG)
else if not na(prev)
if not na(prev.ob)
if not prev.ob.mit and not rev
prev.mnrP := x
prev.mnrB := xb
f_clearOrders(prev)
prev.st := 0
prev.missed := false
if not e.internal
g.cBos := true
g.cDir := s
g.cVol := hasVol
g.cOb := not na(ob) and not ob.mit
g.cSw := swept ? kind : 0
g.lastOb := ob
e.dir := s
e.cnt := 0
e.sw := Sweep.new()
e.wOn := false
e.subP := na
method resolve(Eng e) =>
int s = e.pS
e.pS := 0
[ox, oxb] = f_extreme(-s, e.pLvlB, e.pBar)
int m = f_gap(s, oxb, e.pBar)
bool hasVol = not na(m)
if e.dir == s or e.dir == 0
e.bos(s, e.pLvlB, e.pLvl, e.pBar, hasVol, m, false, 0)
else if e.pWick
if hasVol
e.bos(s, e.pLvlB, e.pLvl, e.pBar, hasVol, m, true, 1)
e.wOn := false
else if hasVol or e.cnt >= 1
e.bos(s, e.pLvlB, e.pLvl, e.pBar, hasVol, m, true, 3)
else
e.cnt += 1
if not e.sw.active or e.sw.kind != 3
e.sw := Sweep.new(active = true, kind = 3, lvl = e.pLvl, lvlB = e.pLvlB, bar = e.pBar)
Side S = e.side(s)
[rP, rB] = f_extreme(s, e.pLvlB, bar_index)
S.p := na
S.wait := true
S.pulled := false
S.runP := rP
S.runB := rB
e.pWick := false
method confirm(Eng e, int s) =>
Side S = e.side(s)
int t = bar_index
if S.wait
float v = f_x(s, t)
bool newExt = s * (v - S.runP) > 0
if newExt
S.runP := v
S.runB := t
S.pulled := false
if not newExt and t > S.runB and s * (close - f_x(-s, t - 1)) < 0
S.pulled := true
if not newExt and S.pulled and t - S.runB >= e.n
S.p := S.runP
S.b := S.runB
S.wait := false
Setup a = e.slot(s)
if not na(a)
if a.st == 0
float en = S.p + (a.anchor - S.p) * iFib
bool roomy = s * (close - en) > 0
if roomy
e.place(a, S)
if not roomy
a.missed := true
method scan(Eng e, float pH, float pL) =>
int t = bar_index
int d = e.dir
if d != 0
Side M = e.side(d)
Side O = e.side(-d)
float cX = f_x(-d, t)
if not na(O.p) and not O.wait and (-d) * (cX - O.p) > 0 and (not e.sw.active or e.sw.kind != 3)
e.sw := Sweep.new(active = true, kind = 3, lvl = O.p, lvlB = O.b, bar = t)
float piv = d == 1 ? pL : pH
if not na(piv) and not na(M.p) and not M.wait and t - e.n > M.b
e.subP := piv
e.subB := t - e.n
if not na(e.subP) and (-d) * (cX - e.subP) > 0
if not e.sw.active
e.sw := Sweep.new(active = true, kind = 2, lvl = e.subP, lvlB = e.subB, bar = t)
e.subP := na
if not na(M.p) and not M.wait and not e.wOn and d * (f_x(d, t) - M.p) > 0 and d * (close - M.p) <= 0
e.wOn := true
e.wLvl := M.p
e.wLvlB := M.b
e.wBar := t
[wx, wxb] = f_extreme(-d, M.b, t)
e.wSub := wx
e.wSubB := wxb
method pend(Eng e, int s, float lvl, int lvlB, bool isWick) =>
e.pS := s
e.pBar := bar_index
e.pLvl := lvl
e.pLvlB := lvlB
e.pWick := isWick
method detect(Eng e) =>
int d = e.dir
if d == 0
if not na(e.up.p) and close > e.up.p
e.pend(1, e.up.p, e.up.b, false)
else if not na(e.dn.p) and close < e.dn.p
e.pend(-1, e.dn.p, e.dn.b, false)
else
Side M = e.side(d)
Side O = e.side(-d)
if not na(M.p) and not M.wait and d * (close - M.p) > 0
e.pend(d, M.p, M.b, false)
else if not na(O.p) and not O.wait and (-d) * (close - O.p) > 0
e.pend(-d, O.p, O.b, false)
else if e.wOn and (-d) * (close - e.wSub) > 0
e.pend(-d, e.wSub, e.wSubB, true)
method guard(Eng e, int s) =>
Setup a = e.slot(s)
if not na(a)
if a.st == 0 and s * (f_x(-s, bar_index) - a.anchor) < 0
f_clearOrders(a)
e.setSlot(s, na)
method settle(Eng e, int s) =>
Setup a = e.slot(s)
if not na(a) and not na(a.fib)
if a.fib.st >= 1
e.setSlot(s, na)
method step(Eng e, float pH, float pL) =>
if e.pS != 0
e.resolve()
if e.dir == 0
if not na(pH)
e.up.p := pH
e.up.b := bar_index - e.n
if not na(pL)
e.dn.p := pL
e.dn.b := bar_index - e.n
e.guard(1)
e.guard(-1)
e.confirm(1)
e.confirm(-1)
e.scan(pH, pL)
if e.pS == 0
e.detect()
// =============================================================================
// Trades
// =============================================================================
f_finish(Ord o, bool won) =>
int t = bar_index
float exitP = won ? o.tp : o.sl
float rr0 = o.s * (exitP - o.entry) / o.plan
float rr = won ? rr0 : math.min(rr0, 0.0)
o.st := 2
f_lX(o.l0, t)
f_lX(o.l71, t)
f_lX(o.l1, t)
f_tX(o.t0, t)
f_tX(o.t71, t)
f_tX(o.t1, t)
if o.kind == 0
g.fW += won ? 1 : 0
g.fL += won ? 0 : 1
g.fR += rr
else if o.kind == 1
g.oW += won ? 1 : 0
g.oL += won ? 0 : 1
g.oR += rr
else
g.iW += won ? 1 : 0
g.iL += won ? 0 : 1
g.iR += rr
alert("HTF 5+1 " + syminfo.ticker + (won ? " target hit " : " stop hit ") + str.tostring(rr, "#.##") + "R", alert.freq_once_per_bar_close)
if o.kind == 0 and won
for other in live
if other.gid == o.gid and other.kind == 1 and other.st == 0
f_kill(other)
f_orders() =>
int t = bar_index
int n = live.size()
if n > 0
for i = n - 1 to 0
Ord o = live.get(i)
if o.st == 0 and t > o.placed
bool hit = o.s == 1 ? low <= o.entry : high >= o.entry
if hit
o.st := 1
o.fillB := t
if o.s * (open - o.entry) < 0
o.entry := open
alert("HTF 5+1 " + syminfo.ticker + " limit filled @ " + f_price(o.entry), alert.freq_once_per_bar_close)
if o.st == 1
bool slHit = o.s == 1 ? low <= o.sl : high >= o.sl
bool tpHit = t > o.fillB and (o.s == 1 ? high >= o.tp : low <= o.tp)
if slHit or tpHit
f_finish(o, not slHit)
if o.st == 0 or o.st == 1
f_lX(o.l0, t + 10)
f_lX(o.l71, t + 10)
f_lX(o.l1, t + 10)
f_tX(o.t0, t + 10)
f_tX(o.t71, t + 10)
f_tX(o.t1, t + 10)
if o.st >= 2
live.remove(i)
f_mitigate() =>
int n = obs.size()
if n > 0
for i = n - 1 to 0
Ob o = obs.get(i)
bool touched = o.s == 1 ? low <= o.top : high >= o.bot
if touched
o.mit := true
f_bR(o.bx, bar_index)
if not na(o.bx)
box.set_text(o.bx, "")
obs.remove(i)
if not touched and not na(atr14)
if math.abs(close - (o.top + o.bot) / 2) <= iObDist * atr14
f_bR(o.bx, bar_index + 5)
f_pivots() =>
bool changed = false
if not na(lqHi)
PHp.push(lqHi)
PHb.push(bar_index - iLqPiv)
changed := true
if PHp.size() > 40
PHp.shift()
PHb.shift()
if not na(lqLo)
PLp.push(lqLo)
PLb.push(bar_index - iLqPiv)
changed := true
if PLp.size() > 40
PLp.shift()
PLb.shift()
changed
if barstate.isconfirmed
ext.step(pHi, pLo)
if iInternal
inn.step(ipHi, ipLo)
bool newPiv = f_pivots()
if newPiv
Setup aL = ext.slot(1)
Setup aS = ext.slot(-1)
if not na(aL)
if aL.st == 1
f_lqScan(aL)
if not na(aS)
if aS.st == 1
f_lqScan(aS)
f_mitigate()
f_orders()
ext.settle(1)
ext.settle(-1)
if iInternal
inn.settle(1)
inn.settle(-1)
// =============================================================================
// Panel: the 5+1 checklist
// =============================================================================
f_tick(bool ok) => ok ? "✓" : "✗"
f_rate(int w, int l) =>
int n = w + l
n > 0 ? str.tostring(math.round(100.0 * w / n)) + "%" : "-"
f_rowSet(table tb, int r, string a, string b, color c) =>
table.cell(tb, 0, r, a, text_color = C_FG, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 1, r, b, text_color = c, text_size = size.small, text_halign = text.align_right)
var table panel = table.new(iPos == "Top right" ? position.top_right : iPos == "Bottom right" ? position.bottom_right : iPos == "Top left" ? position.top_left : position.bottom_left, 2, 16, frame_color = color.new(C_GREY, 60), frame_width = 1, border_color = color.new(C_GREY, 85), border_width = 1, bgcolor = color.new(chart.bg_color, 8))
if barstate.islast and iPanel
Setup cur = g.last
int sd = ext.dir
bool have = not na(cur)
bool isOpen = false
bool inTrade = false
if have
Setup sl = ext.slot(cur.s)
if not na(sl)
isOpen := sl.gid == cur.gid
if not na(cur.fib)
inTrade := cur.fib.st == 1
bool useCur = isOpen or inTrade
int swK = g.cSw
bool bosOk = g.cBos
bool volOk = g.cVol
bool obOk = g.cOb
Ob gOb = g.lastOb
if useCur
swK := cur.kind
bosOk := true
volOk := true
obOk := true
gOb := cur.ob
string volT = f_tick(volOk)
if volOk and not na(gOb)
if gOb.gaps >= 2
volT := "✓ x" + str.tostring(gOb.gaps)
else if gOb.stopG
volT := f_t("✓ توقف الفجوة", "✓ Gap stop")
color dirC = sd == 1 ? C_GRN : sd == -1 ? C_RED : C_FG
string dirT = sd == 1 ? f_t("صاعد", "Bullish") : sd == -1 ? f_t("هابط", "Bearish") : "-"
string swT = swK == 3 ? f_t("✓ عالي الاحتمال", "✓ High") : swK == 2 ? f_t("✓ متوسط", "✓ Medium") : swK == 1 ? f_t("✓ سحب بالفتيل", "✓ Wick") : "✗"
string fibT = "✗"
string plusT = "-"
string sideT = "-"
string enT = "-"
string slT = "-"
string tpT = "-"
if isOpen or inTrade
sideT := cur.s == 1 ? f_t("شراء", "Long") : f_t("بيع", "Short")
if cur.st == 0
fibT := cur.s == 1 ? f_t("بانتظار القمة", "Waiting for swing") : f_t("بانتظار القاع", "Waiting for swing")
if cur.missed
fibT := f_t("فات مستوى الدخول", "Entry passed")
else if not na(cur.fib)
fibT := inTrade ? f_t("✓ الصفقة مفتوحة", "✓ In trade") : f_t("✓ الحد جاهز", "✓ Limit set")
enT := f_price(cur.fib.entry)
slT := f_price(cur.fib.sl)
tpT := f_price(cur.fib.tp)
plusT := cur.plus > 0 ? "✓ " + cur.plusTx : f_t("غير متوفرة", "None")
f_rowSet(panel, 0, "HTF 5+1", timeframe.period == "240" ? "4H" : f_t("المنهج على 4H", "Method is 4H"), timeframe.period == "240" ? C_FG : C_RED)
f_rowSet(panel, 1, f_t("الاتجاه", "Trend"), dirT, dirC)
f_rowSet(panel, 2, f_t("1. كسر الهيكل", "1. BOS"), f_tick(bosOk), bosOk ? C_GRN : C_RED)
f_rowSet(panel, 3, f_t("2. سحب السيولة", "2. Sweep"), swT, swK > 0 ? C_GRN : C_RED)
f_rowSet(panel, 4, f_t("3. الحجم", "3. Volume"), volT, volOk ? C_GRN : C_RED)
f_rowSet(panel, 5, f_t("4. الأوردر بلوك", "4. Order Block"), f_tick(obOk), obOk ? C_GRN : C_RED)
f_rowSet(panel, 6, f_t("5. فيبوناتشي", "5. Fibonacci"), fibT, str.startswith(fibT, "✗") ? C_RED : C_GRN)
f_rowSet(panel, 7, f_t("+1 سيولة إضافية", "+1 Extra LQ"), plusT, C_FG)
f_rowSet(panel, 8, f_t("الصفقة", "Setup"), sideT, C_FG)
f_rowSet(panel, 9, f_t("الدخول", "Entry"), enT, C_FG)
f_rowSet(panel, 10, f_t("الوقف", "Stop"), slT, C_RED)
f_rowSet(panel, 11, f_t("الهدف", "Target"), tpT, C_GRN)
f_rowSet(panel, 12, f_t("حد 0.71", "0.71 limit"), str.tostring(g.fW) + "/" + str.tostring(g.fW + g.fL) + " " + f_rate(g.fW, g.fL), C_FG)
f_rowSet(panel, 13, f_t(" صافي R", " Net R"), str.tostring(g.fR, "#.##"), g.fR >= 0 ? C_GRN : C_RED)
f_rowSet(panel, 14, f_t("حد البلوك", "Block limit"), str.tostring(g.oW) + "/" + str.tostring(g.oW + g.oL) + " " + f_rate(g.oW, g.oL), C_FG)
f_rowSet(panel, 15, f_t(" صافي R", " Net R"), str.tostring(g.oR, "#.##"), g.oR >= 0 ? C_GRN : C_RED)