ajiim

🎁

Nov 22nd, 2026
1,157
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.66 KB | None | 2 0
  1. //@version=6
  2. indicator("7H Profile · Fractal Model Pro", shorttitle = "7H Fractal Pro", overlay = true, max_lines_count = 500, max_boxes_count = 500, max_labels_count = 500, max_bars_back = 1000)
  3.  
  4. const string G_GEN = "GENERAL"
  5. const string G_TXT = "TEXT & MARKERS"
  6. const string G_BIAS = "DAILY BIAS"
  7. const string G_REL = "RELEVANT SWING"
  8. const string G_FAIL = "FAILURE & SEPARATION"
  9. const string G_CONT = "CONTINUATION & SWEEP CANDLE"
  10. const string G_PROF = "7H PROFILE"
  11. const string G_PDRW = "PROFILE DRAWINGS"
  12. const string G_OPEN = "OPEN LINE"
  13. const string G_ENT = "ENTRY"
  14. const string G_CISD = "CISD & LEG"
  15. const string G_SIG = "ENTRY SIGNALS"
  16. const string G_RISK = "RISK"
  17. const string G_RBOX = "RISK BOXES"
  18. const string G_PNL = "7H CANDLES PANEL"
  19. const string G_DSH = "DASHBOARD"
  20.  
  21. i_tz = input.string("America/New_York", "Timezone", group = G_GEN)
  22. i_dayStart = input.int(18, "Trading Day Start (hour)", minval = 0, maxval = 23, group = G_GEN)
  23. i_profHrs = input.int(7, "Profile Candle (hours)", minval = 1, maxval = 12, group = G_GEN)
  24. i_hist = input.int(3, "History (trading days)", minval = 1, maxval = 15, group = G_GEN)
  25. i_clean = input.bool(true, "Clean Mode", group = G_GEN)
  26. i_maxLine = input.int(96, "Max Live Line Length (bars)", minval = 10, maxval = 900, group = G_GEN)
  27.  
  28. c_txt = input.color(#787b86, "Text", inline = "tx1", group = G_TXT)
  29. i_txtSize = input.string("Tiny", "Size", options = ["Tiny", "Small", "Normal"], inline = "tx1", group = G_TXT)
  30. i_dotSize = input.string("Tiny", "Dot Size", options = ["Micro", "Tiny", "Small", "Normal"], group = G_TXT)
  31. c_grey = input.color(#b0b4ba, "Retired Levels", group = G_TXT)
  32.  
  33. i_swingLen = input.int(1, "Swing Strength (daily candles)", minval = 1, maxval = 5, group = G_BIAS)
  34. i_atrLen = input.int(14, "Daily ATR Length", minval = 1, maxval = 50, group = G_BIAS)
  35. i_biasConf = input.string("Reaction close", "Bias Trigger", options = ["Reaction close", "Close beyond sweep candle"], group = G_BIAS)
  36. i_wickFlt = input.bool(true, "Skip After Opposing Wick", inline = "wk", group = G_BIAS)
  37. i_wickMax = input.float(40, "Max %", minval = 1, maxval = 100, inline = "wk", group = G_BIAS)
  38.  
  39. i_showBias = input.bool(true, "Relevant Swing", inline = "rs1", group = G_REL)
  40. c_rel = input.color(#5f6368, "", inline = "rs1", group = G_REL)
  41. i_relSt = input.string("Solid", "Style", options = ["Solid", "Dotted", "Dashed"], inline = "rs2", group = G_REL)
  42. i_relW = input.int(1, "Width", minval = 1, maxval = 4, inline = "rs2", group = G_REL)
  43. i_showRelDot = input.bool(true, "Dot", inline = "rs3", group = G_REL)
  44. c_relDot = input.color(#3d7bff, "", inline = "rs3", group = G_REL)
  45. i_showRelLbl = input.bool(true, "Caption", group = G_REL)
  46.  
  47. i_failFlt = input.bool(true, "Separation Filter", group = G_FAIL)
  48. i_sepMult = input.float(0.5, "Min Separation (× ATR)", minval = 0, step = 0.1, group = G_FAIL)
  49. i_failPct = input.float(50, "Max Retracement %", minval = 5, maxval = 100, group = G_FAIL)
  50. i_showFail = input.bool(true, "Failure Swings", inline = "fs1", group = G_FAIL)
  51. c_fail = input.color(#b0b4ba, "", inline = "fs1", group = G_FAIL)
  52. i_showSep = input.bool(true, "Separation Bar", inline = "fs2", group = G_FAIL)
  53. c_sep = input.color(color.new(#3d7bff, 55), "", inline = "fs2", group = G_FAIL)
  54.  
  55. i_showCont = input.bool(true, "Continuation Marks", inline = "ct1", group = G_CONT)
  56. c_contLn = input.color(color.new(#5f6368, 20), "", inline = "ct1", group = G_CONT)
  57. i_contInv = input.bool(true, "50% Invalidation", inline = "ct2", group = G_CONT)
  58. c_invBox = input.color(color.new(#9aa0a6, 88), "", inline = "ct2", group = G_CONT)
  59. i_showConf = input.bool(true, "Sweep Candle Level", inline = "ct3", group = G_CONT)
  60. c_conf = input.color(#5f6368, "", inline = "ct3", group = G_CONT)
  61.  
  62. i_align = input.bool(true, "Align With Daily Bias", group = G_PROF)
  63. i_asia = input.bool(true, "Asia", inline = "ps", group = G_PROF)
  64. i_london = input.bool(true, "London", inline = "ps", group = G_PROF)
  65. i_ny = input.bool(true, "New York", inline = "ps", group = G_PROF)
  66. i_invMode = input.string("Extreme → expansion close", "Invalidation Level", options = ["Extreme → expansion close", "Expansion candle 50%"], group = G_PROF)
  67. i_invTrig = input.string("Wick", "Invalidation Trigger", options = ["Wick", "Close"], group = G_PROF)
  68. i_extViol = input.bool(true, "Invalidate On Extreme Break", group = G_PROF)
  69.  
  70. i_showProf = input.bool(true, "Show Profile Drawings", group = G_PDRW)
  71. c_profLn = input.color(#5f6368, "Swept Level", inline = "pd1", group = G_PDRW)
  72. i_profW = input.int(1, "Width", minval = 1, maxval = 4, inline = "pd1", group = G_PDRW)
  73. c_profDot = input.color(#3d7bff, "Extreme Dot", group = G_PDRW)
  74. i_caption = input.bool(true, "Caption", group = G_PDRW)
  75. c_zone = input.color(color.new(#9aa0a6, 88), "Zone Fill", group = G_PDRW)
  76. c_inv = input.color(#5f6368, "50% Line", group = G_PDRW)
  77. c_zoneExt = input.color(#5f6368, "Extreme Line", group = G_PDRW)
  78.  
  79. i_showOpen = input.bool(true, "Open Line", inline = "ol1", group = G_OPEN)
  80. c_open = input.color(#b0b4ba, "", inline = "ol1", group = G_OPEN)
  81. i_openHr = input.int(8, "Time", minval = 0, maxval = 23, inline = "ol2", group = G_OPEN)
  82. i_openMin = input.int(0, ":", minval = 0, maxval = 59, inline = "ol2", group = G_OPEN)
  83. i_openSt = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"], inline = "ol3", group = G_OPEN)
  84. i_openW = input.int(1, "Width", minval = 1, maxval = 4, inline = "ol3", group = G_OPEN)
  85. i_openLen = input.int(24, "Max Length (bars)", minval = 3, maxval = 300, group = G_OPEN)
  86. i_openLbl = input.bool(true, "Label", group = G_OPEN)
  87.  
  88. i_showEnt = input.bool(true, "Enable Entries", group = G_ENT)
  89. i_method = input.string("Auto", "Entry Method", options = ["Auto", "A: 7H + 15m", "B: 1H + 5m", "B: 30m + 3m"], group = G_ENT)
  90. i_pivLen = input.int(3, "Swing Strength (method A)", minval = 1, maxval = 10, group = G_ENT)
  91. i_cisdMax = input.int(20, "Max Bars Sweep → CISD", minval = 2, maxval = 200, group = G_ENT)
  92. i_needProf = input.bool(true, "Require Aligned Profile", group = G_ENT)
  93. i_endH = input.int(12, "Entry Window Ends (hour)", minval = 1, maxval = 23, group = G_ENT)
  94. i_maxEnt = input.int(1, "Entries Per Day", minval = 1, maxval = 5, group = G_ENT)
  95. i_cool = input.int(4, "Min Bars Between Entries", minval = 0, maxval = 100, group = G_ENT)
  96. i_keepSet = input.int(3, "Setups Kept", minval = 1, maxval = 20, group = G_ENT)
  97.  
  98. i_ctxLines = input.bool(true, "CISD Line", inline = "cl1", group = G_CISD)
  99. c_cisd = input.color(#3d7bff, "", inline = "cl1", group = G_CISD)
  100. i_cisdW = input.int(2, "Width", minval = 1, maxval = 4, inline = "cl1", group = G_CISD)
  101. i_keepCisd = input.int(6, "CISD Lines Kept", minval = 1, maxval = 50, group = G_CISD)
  102. c_ext = input.color(#5f6368, "Sweep Extreme Line", group = G_CISD)
  103. i_showLeg = input.bool(true, "Leg Line", inline = "cl2", group = G_CISD)
  104. c_leg = input.color(#e5484d, "", inline = "cl2", group = G_CISD)
  105. i_showMid = input.bool(true, "CISD 50% Line", inline = "cl3", group = G_CISD)
  106. c_mid = input.color(#5f6368, "", inline = "cl3", group = G_CISD)
  107. i_lineW = input.int(1, "Leg / Extreme / 50% Width", minval = 1, maxval = 4, group = G_CISD)
  108.  
  109. i_showArr = input.bool(true, "Arrow", inline = "sg1", group = G_SIG)
  110. c_entBull = input.color(#3d7bff, "Long", inline = "sg1", group = G_SIG)
  111. c_entBear = input.color(#e5484d, "Short", inline = "sg1", group = G_SIG)
  112. i_paintEnt = input.bool(true, "Paint Entry Candle", group = G_SIG)
  113.  
  114. i_slMode = input.string("Sweep extreme", "Stop", options = ["Sweep extreme", "Profile extreme"], group = G_RISK)
  115. i_slBuf = input.int(0, "Stop Buffer (ticks)", minval = 0, group = G_RISK)
  116. i_tpMode = input.string("Fixed R:R", "Target", options = ["Fixed R:R", "Daily relevant swing"], group = G_RISK)
  117. i_rr = input.float(2.0, "R:R", minval = 0.5, maxval = 20, step = 0.25, group = G_RISK)
  118. i_timeExit = input.bool(true, "Time Exit", inline = "tx", group = G_RISK)
  119. i_exitHr = input.int(15, "Hour", minval = 0, maxval = 23, inline = "tx", group = G_RISK)
  120.  
  121. c_tp = input.color(color.new(#3d7bff, 85), "Target", inline = "rb1", group = G_RBOX)
  122. c_sl = input.color(color.new(#9aa0a6, 85), "Stop", inline = "rb1", group = G_RBOX)
  123. i_boxLen = input.int(20, "Box Length (bars)", minval = 3, maxval = 300, group = G_RBOX)
  124. i_showPath = input.bool(true, "Price Path", inline = "rb2", group = G_RBOX)
  125. c_path = input.color(#8a8f98, "", inline = "rb2", group = G_RBOX)
  126.  
  127. i_showPnl = input.bool(true, "Show Panel", group = G_PNL)
  128. i_pnlCount = input.int(4, "Candles", minval = 2, maxval = 8, group = G_PNL)
  129. i_pnlOff = input.int(6, "Offset (bars)", minval = 1, maxval = 100, group = G_PNL)
  130. i_pnlW = input.int(6, "Width", minval = 2, maxval = 20, inline = "pn1", group = G_PNL)
  131. i_pnlGap = input.int(3, "Gap", minval = 1, maxval = 20, inline = "pn1", group = G_PNL)
  132. i_pnlFit = input.int(60, "Fit Lookback (bars)", minval = 10, maxval = 300, group = G_PNL)
  133. i_pnlPct = input.int(45, "Height %", minval = 15, maxval = 85, group = G_PNL)
  134. i_pnlCap = input.bool(true, "Caption", group = G_PNL)
  135. i_pnlTrue = input.bool(true, "True Price Scale", group = G_PNL)
  136. c_bullC = input.color(#ffffff, "Bull", inline = "pn2", group = G_PNL)
  137. c_bearC = input.color(#3b3b3b, "Bear", inline = "pn2", group = G_PNL)
  138. c_wick = input.color(#787b86, "Outline", group = G_PNL)
  139. c_pnlHl = input.color(#e5484d, "Profile Frame", group = G_PNL)
  140.  
  141. i_showDash = input.bool(true, "Show Dashboard", group = G_DSH)
  142. i_dashPos = input.string("Top Right", "Position", options = ["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group = G_DSH)
  143. i_dashSize = input.string("Small", "Text Size", options = ["Tiny", "Small", "Normal"], group = G_DSH)
  144. i_theme = input.string("Light", "Theme", options = ["Light", "Dark", "Custom"], group = G_DSH)
  145. c_dTx = input.color(#2a2a2a, "Custom Text", group = G_DSH)
  146.  
  147. C_NONE = color.new(#ffffff, 100)
  148.  
  149. if not timeframe.isintraday or timeframe.in_seconds() > 3600
  150. runtime.error("7H Fractal Pro works on intraday charts of 1 hour or lower (15m / 5m / 3m recommended).")
  151.  
  152. int barMs = timeframe.in_seconds() * 1000
  153.  
  154. bool vFail = i_showFail and not i_clean
  155. bool vSep = i_showSep and not i_clean
  156. bool vCtx = i_ctxLines and not i_clean
  157. bool vOpLbl = i_openLbl and not i_clean
  158.  
  159. type Candle
  160. float o
  161. float h
  162. float l
  163. float c
  164. int tO
  165. int tE
  166. int tH
  167. int tL
  168. int bO
  169. int blk = -1
  170.  
  171. type Swing
  172. float p
  173. int t
  174. int tBrk = na
  175. float leg
  176. line ln
  177. line cf
  178. label lb
  179. label dt
  180.  
  181. type Zone
  182. box b
  183. line m
  184. line e
  185. int tC = na
  186.  
  187. type Setup
  188. int dir
  189. float entry
  190. float sl
  191. float tp
  192. int bE
  193. float mfe
  194. float mae
  195. bool live = true
  196. box bxTP
  197. box bxSL
  198. box pgTP
  199. box pgSL
  200. line lnPath
  201. line lnCisd
  202. line lnExt
  203. line lnLeg
  204. line lnMid
  205. label lbArr
  206.  
  207. type Group
  208. int t0
  209. int b0
  210. array<line> lns
  211. array<label> lbs
  212. array<box> bxs
  213.  
  214. var array<Candle> dH = array.new<Candle>()
  215. var array<Candle> pH = array.new<Candle>()
  216. var array<Group> days = array.new<Group>()
  217. var array<line> cisdLines = array.new<line>()
  218. var array<Setup> setups = array.new<Setup>()
  219. var Swing sL = Swing.new()
  220. var Swing sH = Swing.new()
  221. var int rEdge = 0
  222.  
  223. float atrV = ta.atr(50)
  224. float dotOff = nz(atrV, high - low) * 0.14
  225. int trailT = time[math.min(bar_index, i_maxLine)]
  226. float fitHi = nz(ta.highest(high, i_pnlFit), high)
  227. float fitLo = nz(ta.lowest(low, i_pnlFit), low)
  228.  
  229. fSize(string s) =>
  230. s == "Tiny" ? size.tiny : s == "Small" ? size.small : size.normal
  231.  
  232. fLs(string s) =>
  233. s == "Dotted" ? line.style_dotted : s == "Dashed" ? line.style_dashed : line.style_solid
  234.  
  235. fPos(string s) =>
  236. switch s
  237. "Top Left" => position.top_left
  238. "Top Center" => position.top_center
  239. "Top Right" => position.top_right
  240. "Middle Left" => position.middle_left
  241. "Middle Center" => position.middle_center
  242. "Middle Right" => position.middle_right
  243. "Bottom Left" => position.bottom_left
  244. "Bottom Center" => position.bottom_center
  245. => position.bottom_right
  246.  
  247. fDarker(color c) =>
  248. color.new(c, math.max(color.t(c) - 25, 0))
  249.  
  250. fHour(int b) =>
  251. int h = (i_dayStart + b * i_profHrs) % 24
  252. str.format("{0,number,00}:00", h)
  253.  
  254. fProfName(int t) =>
  255. t == 1 ? "asia reversal" : t == 2 ? "london reversal" : t == 3 ? "ny reversal" : "none"
  256.  
  257. fNew(int b) =>
  258. Candle.new(o = open, h = high, l = low, c = close, tO = time, tE = time, tH = time, tL = time, bO = bar_index, blk = b)
  259.  
  260. method grow(Candle this) =>
  261. if high > this.h
  262. this.h := high
  263. this.tH := time
  264. if low < this.l
  265. this.l := low
  266. this.tL := time
  267. this.c := close
  268. this.tE := time
  269. this
  270.  
  271. method half(Candle this) =>
  272. (this.h + this.l) / 2
  273.  
  274. fCapL(array<line> a, int n) =>
  275. while a.size() > n
  276. a.shift().delete()
  277. a.size()
  278.  
  279. fDot(int x, float y, color col, bool isTime = true) =>
  280. string g = i_dotSize == "Micro" ? "•" : "●"
  281. string sz = i_dotSize == "Micro" or i_dotSize == "Tiny" ? size.tiny : i_dotSize == "Small" ? size.small : size.normal
  282. label.new(x, y, g, xloc = isTime ? xloc.bar_time : xloc.bar_index, style = label.style_none, color = C_NONE, textcolor = col, size = sz)
  283.  
  284. fGL(line l) =>
  285. if not na(l) and days.size() > 0
  286. Group g = days.last()
  287. g.lns.push(l)
  288. l
  289.  
  290. fGLb(label lb) =>
  291. if not na(lb) and days.size() > 0
  292. Group g = days.last()
  293. g.lbs.push(lb)
  294. lb
  295.  
  296. fGB(box b) =>
  297. if not na(b) and days.size() > 0
  298. Group g = days.last()
  299. g.bxs.push(b)
  300. b
  301.  
  302. fDelGroup(Group g) =>
  303. while g.lns.size() > 0
  304. g.lns.pop().delete()
  305. while g.lbs.size() > 0
  306. g.lbs.pop().delete()
  307. while g.bxs.size() > 0
  308. g.bxs.pop().delete()
  309.  
  310. fTrail(line ln, int tOrigin) =>
  311. if not na(ln)
  312. ln.set_x1(math.max(tOrigin, trailT))
  313. ln.set_x2(time)
  314.  
  315. fLive(Swing sw, bool isLow) =>
  316. if not na(sw.p) and na(sw.tBrk)
  317. if isLow ? low < sw.p : high > sw.p
  318. sw.tBrk := time
  319. if not na(sw.ln)
  320. int xL = math.max(sw.t, trailT)
  321. sw.ln.set_x1(xL)
  322. sw.ln.set_x2(time)
  323. if not na(sw.lb)
  324. sw.lb.set_x(xL)
  325. if not na(sw.cf)
  326. sw.cf.set_x1(math.max(sw.cf.get_x1(), trailT))
  327. sw.cf.set_x2(time)
  328.  
  329. fDAtr(array<Candle> a, int n) =>
  330. int sz = a.size()
  331. int m = math.min(n, sz - 1)
  332. float s = 0.0
  333. float res = na
  334. if m >= 1
  335. for i = 0 to m - 1
  336. Candle c = a.get(sz - 1 - i)
  337. Candle p = a.get(sz - 2 - i)
  338. s += math.max(c.h - c.l, math.max(math.abs(c.h - p.c), math.abs(c.l - p.c)))
  339. res := s / m
  340. res
  341.  
  342. fLegX(bool isLow, int a, int b, int tMin) =>
  343. float r = na
  344. for j = a to b
  345. Candle cj = dH.get(j)
  346. if cj.tE >= tMin
  347. r := na(r) ? (isLow ? cj.h : cj.l) : (isLow ? math.max(r, cj.h) : math.min(r, cj.l))
  348. r
  349.  
  350. fRetire(Swing sw, bool greyLine, bool keepLabel, int tEnd = na) =>
  351. int x2 = math.max(sw.t, nz(tEnd, time))
  352. int x1 = math.max(sw.t, x2 - i_maxLine * barMs)
  353. if not na(sw.ln)
  354. sw.ln.set_x1(x1)
  355. sw.ln.set_x2(x2)
  356. if greyLine
  357. sw.ln.set_color(c_grey)
  358. fGL(sw.ln)
  359. if not na(sw.dt)
  360. if i_clean or sw.t < x1
  361. sw.dt.delete()
  362. else
  363. sw.dt.set_textcolor(c_grey)
  364. fGLb(sw.dt)
  365. if not na(sw.lb)
  366. if keepLabel
  367. sw.lb.set_x(x1)
  368. fGLb(sw.lb)
  369. else
  370. sw.lb.delete()
  371. sw.ln := na
  372. sw.dt := na
  373. sw.lb := na
  374. sw.tBrk := na
  375.  
  376. fSetRel(Swing sw, bool isLow, float p, int t, bool withLabel) =>
  377. sw.p := p
  378. sw.t := t
  379. sw.tBrk := na
  380. if i_showBias
  381. int x1 = math.max(t, trailT)
  382. sw.ln := line.new(x1, p, time, p, xloc = xloc.bar_time, color = c_rel, style = fLs(i_relSt), width = i_relW)
  383. if i_showRelDot
  384. sw.dt := fDot(t, isLow ? p - dotOff : p + dotOff, c_relDot)
  385. if withLabel and i_showRelLbl
  386. sw.lb := label.new(x1, p, "relevant swing", xloc = xloc.bar_time, style = isLow ? label.style_label_upper_left : label.style_label_lower_left, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize))
  387. sw.p
  388.  
  389. fPivot(Swing sw, bool isLow, Candle pc, int k, float atrU) =>
  390. float p = isLow ? pc.l : pc.h
  391. int t = isLow ? pc.tL : pc.tH
  392. int n = dH.size()
  393. if na(sw.p) or (isLow ? p < sw.p : p > sw.p)
  394. fRetire(sw, true, false, nz(sw.tBrk, t))
  395. fSetRel(sw, isLow, p, t, true)
  396. sw.leg := fLegX(isLow, k, n - 1, 0)
  397. else if p != sw.p
  398. float leg = fLegX(isLow, 0, k, sw.t)
  399. float rng = na(leg) ? na : (isLow ? leg - sw.p : sw.p - leg)
  400. float sep = isLow ? p - sw.p : sw.p - p
  401. float retr = (na(rng) or rng <= 0) ? 100.0 : (isLow ? (leg - p) : (p - leg)) / rng * 100
  402. bool ok = not i_failFlt or (sep >= i_sepMult * atrU and retr <= i_failPct)
  403. if ok
  404. float oldP = sw.p
  405. fRetire(sw, true, false, t)
  406. fSetRel(sw, isLow, p, t, true)
  407. sw.leg := fLegX(isLow, k, n - 1, 0)
  408. if i_showBias and vSep
  409. fGL(line.new(t, isLow ? p - dotOff * 2 : p + dotOff * 2, t, oldP, xloc = xloc.bar_time, color = c_sep, width = 4))
  410. else if i_showBias and vFail
  411. fGLb(fDot(t, isLow ? p - dotOff : p + dotOff, c_fail))
  412. fGL(line.new(math.max(t, time - i_maxLine * barMs), p, time, p, xloc = xloc.bar_time, color = c_fail, style = line.style_dotted, width = 1))
  413. true
  414.  
  415. fDrawProf(bool bull, float swept, int sweptT, float ext, int extT, string cap) =>
  416. label res = na
  417. if i_showProf
  418. fGL(line.new(sweptT, swept, time + barMs * 4, swept, xloc = xloc.bar_time, color = c_profLn, width = i_profW))
  419. fGLb(fDot(extT, bull ? ext - dotOff : ext + dotOff, c_profDot))
  420. if i_caption
  421. res := fGLb(label.new(extT, bull ? ext - dotOff * 2.2 : ext + dotOff * 2.2, cap, xloc = xloc.bar_time, style = bull ? label.style_label_up : label.style_label_down, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize)))
  422. res
  423.  
  424. fZone(int tL, float inv, float edge, float ext) =>
  425. Zone z = Zone.new()
  426. if i_showProf
  427. z.b := fGB(box.new(tL, math.max(inv, edge), time, math.min(inv, edge), xloc = xloc.bar_time, border_color = C_NONE, bgcolor = c_zone))
  428. z.m := fGL(line.new(tL, inv, time, inv, xloc = xloc.bar_time, color = c_inv, width = i_profW))
  429. z.e := fGL(line.new(tL, ext, time, ext, xloc = xloc.bar_time, color = c_zoneExt, style = line.style_dotted, width = 1))
  430. z.tC := time
  431. z
  432.  
  433. fSetup(int d, float cLvl, int cBar, float ext, int extBar, float sl, float tp) =>
  434. float ent = close
  435. int x = bar_index + 1
  436. Setup st = Setup.new(dir = d, entry = ent, sl = sl, tp = tp, bE = x, mfe = ent, mae = ent)
  437. st.bxSL := box.new(x, math.max(ent, sl), x + i_boxLen, math.min(ent, sl), border_color = C_NONE, bgcolor = c_sl)
  438. st.bxTP := box.new(x, math.max(ent, tp), x + i_boxLen, math.min(ent, tp), border_color = C_NONE, bgcolor = c_tp)
  439. st.pgSL := box.new(x, ent, x, ent, border_color = C_NONE, bgcolor = fDarker(c_sl))
  440. st.pgTP := box.new(x, ent, x, ent, border_color = C_NONE, bgcolor = fDarker(c_tp))
  441. st.lnCisd := line.new(cBar, cLvl, bar_index + 2, cLvl, color = c_cisd, width = i_cisdW)
  442. st.lnExt := line.new(extBar, ext, x + 1, ext, color = c_ext, width = i_lineW)
  443. if i_showLeg and extBar < bar_index
  444. st.lnLeg := line.new(extBar, ext, bar_index, d == 1 ? low : high, color = c_leg, width = i_lineW)
  445. if i_showMid
  446. float m = (high + low) / 2
  447. st.lnMid := line.new(bar_index, m, bar_index + 3, m, color = c_mid, width = i_lineW)
  448. if i_showPath
  449. st.lnPath := line.new(x, ent, x, ent, color = c_path, style = line.style_dashed, width = 1)
  450. if i_showArr
  451. st.lbArr := label.new(bar_index, d == 1 ? low : high, d == 1 ? "▲ BUY" : "▼ SELL", style = d == 1 ? label.style_label_up : label.style_label_down, color = C_NONE, textcolor = d == 1 ? c_entBull : c_entBear, size = size.tiny)
  452. st
  453.  
  454. fDelSetup(Setup s) =>
  455. s.bxTP.delete()
  456. s.bxSL.delete()
  457. s.pgTP.delete()
  458. s.pgSL.delete()
  459. s.lnPath.delete()
  460. s.lnCisd.delete()
  461. s.lnExt.delete()
  462. s.lnLeg.delete()
  463. s.lnMid.delete()
  464. s.lbArr.delete()
  465.  
  466. int hNY = hour(time, i_tz)
  467. int nyMins = hNY * 60 + minute(time, i_tz)
  468. int blkLen = i_profHrs * 60
  469. int relMins = (nyMins - i_dayStart * 60 + 1440) % 1440
  470. int blk = math.floor(relMins / blkLen)
  471. bool gap = bar_index > 0 and (time - time[1]) >= blkLen * 60000
  472. bool newBlk = bar_index == 0 or blk != blk[1] or gap
  473. bool newDay = newBlk and (bar_index == 0 or blk <= blk[1])
  474.  
  475. if newDay
  476. days.push(Group.new(t0 = time, b0 = bar_index, lns = array.new<line>(), lbs = array.new<label>(), bxs = array.new<box>()))
  477. while days.size() > i_hist
  478. fDelGroup(days.shift())
  479. if days.size() > 0
  480. int minB = days.first().b0
  481. while setups.size() > 0 and not setups.first().live and setups.first().bE < minB
  482. fDelSetup(setups.shift())
  483.  
  484. int biasEvt = 0
  485. int profEvt = 0
  486. bool invEvt = false
  487. bool biasInv = false
  488. int entryNow = 0
  489. int exitEvt = 0
  490.  
  491. var Candle dCur = na
  492. var int bias = 0
  493. var int todayBias = 0
  494. var bool skipDay = false
  495. var float pendBull = na
  496. var float pendBullInv = na
  497. var float pendBear = na
  498. var float pendBearInv = na
  499. var int revDir = 0
  500. var float revLvl = na
  501. var float contLvl = na
  502. var line contLn = na
  503. var bool contInvalid = false
  504.  
  505. bool dayClosed = false
  506. if newDay
  507. if not na(dCur)
  508. dH.push(dCur)
  509. if dH.size() > 150
  510. dH.shift()
  511. dayClosed := true
  512. dCur := fNew(-1)
  513. else if not na(dCur)
  514. dCur.grow()
  515.  
  516. if dayClosed
  517. Candle dc = dH.last()
  518. int sz = dH.size()
  519. float atrU = nz(fDAtr(dH, i_atrLen), dc.h - dc.l)
  520. sL.cf := na
  521. sH.cf := na
  522. if revDir != 0
  523. if i_showBias and i_showCont
  524. if revDir == 1 and dc.c > revLvl
  525. fGLb(label.new(dc.tH, dc.h, "continuation", xloc = xloc.bar_time, style = label.style_label_left, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize)))
  526. if revDir == -1 and dc.c < revLvl
  527. fGLb(label.new(dc.tL, dc.l, "continuation", xloc = xloc.bar_time, style = label.style_label_left, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize)))
  528. revDir := 0
  529. if not na(pendBull)
  530. if dc.c > pendBull
  531. bias := 1
  532. biasEvt := 1
  533. pendBull := na
  534. else if dc.c < pendBullInv
  535. pendBull := na
  536. if not na(pendBear)
  537. if dc.c < pendBear
  538. bias := -1
  539. biasEvt := -1
  540. pendBear := na
  541. else if dc.c > pendBearInv
  542. pendBear := na
  543. if not na(sL.p)
  544. sL.leg := math.max(nz(sL.leg, dc.h), dc.h)
  545. if not na(sH.p)
  546. sH.leg := math.min(nz(sH.leg, dc.l), dc.l)
  547. if sz >= 2 * i_swingLen + 1
  548. int k = sz - 1 - i_swingLen
  549. Candle pc = dH.get(k)
  550. bool isPH = true
  551. bool isPL = true
  552. for i = 1 to i_swingLen
  553. Candle lc = dH.get(k - i)
  554. Candle rc = dH.get(k + i)
  555. if lc.h >= pc.h or rc.h > pc.h
  556. isPH := false
  557. if lc.l <= pc.l or rc.l < pc.l
  558. isPL := false
  559. if isPL
  560. fPivot(sL, true, pc, k, atrU)
  561. if isPH
  562. fPivot(sH, false, pc, k, atrU)
  563. bool bullR = not na(sL.p) and dc.l < sL.p and dc.c > sL.p
  564. bool bearR = not na(sH.p) and dc.h > sH.p and dc.c < sH.p
  565. if bullR and bearR
  566. bullR := dc.c >= dc.o
  567. bearR := not bullR
  568. if not bullR and not na(sL.p) and dc.c < sL.p
  569. fRetire(sL, false, false, nz(sL.tBrk, dc.tL))
  570. sL.p := na
  571. sL.leg := na
  572. bias := -1
  573. biasEvt := -1
  574. if not bearR and not na(sH.p) and dc.c > sH.p
  575. fRetire(sH, false, false, nz(sH.tBrk, dc.tH))
  576. sH.p := na
  577. sH.leg := na
  578. bias := 1
  579. biasEvt := 1
  580. Candle prv = na
  581. if sz >= 2
  582. prv := dH.get(sz - 2)
  583. if bullR
  584. fRetire(sL, false, true, nz(sL.tBrk, dc.tL))
  585. fSetRel(sL, true, dc.l, dc.tL, false)
  586. sL.leg := dc.h
  587. if i_showBias and i_showConf
  588. sL.cf := fGL(line.new(dc.tH, dc.h, time, dc.h, xloc = xloc.bar_time, color = c_conf, width = i_relW))
  589. if i_biasConf == "Reaction close"
  590. bias := 1
  591. biasEvt := 1
  592. else
  593. pendBull := dc.h
  594. pendBullInv := dc.l
  595. if bearR
  596. fRetire(sH, false, true, nz(sH.tBrk, dc.tH))
  597. fSetRel(sH, false, dc.h, dc.tH, false)
  598. sH.leg := dc.l
  599. if i_showBias and i_showConf
  600. sH.cf := fGL(line.new(dc.tL, dc.l, time, dc.l, xloc = xloc.bar_time, color = c_conf, width = i_relW))
  601. if i_biasConf == "Reaction close"
  602. bias := -1
  603. biasEvt := -1
  604. else
  605. pendBear := dc.l
  606. pendBearInv := dc.h
  607. if not bullR and not bearR and not na(prv)
  608. if bias == 1 and dc.l < prv.l and dc.c > prv.l
  609. if i_showBias and i_showCont
  610. fGL(line.new(prv.tL, prv.l, dc.tL, prv.l, xloc = xloc.bar_time, color = c_contLn, width = i_relW))
  611. fGLb(label.new(dc.tL, dc.l - dotOff, "reversal", xloc = xloc.bar_time, style = label.style_label_up, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize)))
  612. revDir := 1
  613. revLvl := dc.h
  614. else if bias == -1 and dc.h > prv.h and dc.c < prv.h
  615. if i_showBias and i_showCont
  616. fGL(line.new(prv.tH, prv.h, dc.tH, prv.h, xloc = xloc.bar_time, color = c_contLn, width = i_relW))
  617. fGLb(label.new(dc.tH, dc.h + dotOff, "reversal", xloc = xloc.bar_time, style = label.style_label_down, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize)))
  618. revDir := -1
  619. revLvl := dc.l
  620.  
  621. fLive(sL, true)
  622. fLive(sH, false)
  623.  
  624. var int contT = na
  625. if newDay
  626. todayBias := bias
  627. skipDay := false
  628. contInvalid := false
  629. contLvl := na
  630. contLn := na
  631. contT := na
  632. if dayClosed and bias != 0
  633. Candle d1 = dH.last()
  634. float rg = d1.h - d1.l
  635. if i_wickFlt and rg > 0
  636. float opw = bias == 1 ? d1.h - math.max(d1.o, d1.c) : math.min(d1.o, d1.c) - d1.l
  637. if opw / rg * 100 > i_wickMax
  638. todayBias := 0
  639. skipDay := true
  640. if i_contInv and todayBias != 0
  641. float opp = todayBias == 1 ? sL.p : sH.p
  642. if not na(opp) and (todayBias == 1 ? opp < d1.c : opp > d1.c)
  643. contLvl := (d1.c + opp) / 2
  644. if i_showBias and i_showCont
  645. contT := time
  646. contLn := fGL(line.new(time, contLvl, time, contLvl, xloc = xloc.bar_time, color = c_contLn, style = line.style_dotted, width = 1))
  647.  
  648. if not na(contLvl) and not contInvalid and todayBias != 0
  649. if not na(contLn) and not na(contT)
  650. fTrail(contLn, contT)
  651. if barstate.isconfirmed and (todayBias == 1 ? close < contLvl : close > contLvl)
  652. contInvalid := true
  653. biasInv := true
  654. if i_showBias and i_showCont
  655. fGB(box.new(time, math.max(contLvl, close), time + barMs * 8, math.min(contLvl, close), xloc = xloc.bar_time, border_color = C_NONE, bgcolor = c_invBox, text = "invalid", text_size = fSize(i_txtSize), text_color = c_txt, text_halign = text.align_right, text_valign = text.align_center))
  656. rEdge := math.max(rEdge, bar_index + 8)
  657. if not na(contLn)
  658. contLn.set_color(color.new(c_grey, 40))
  659. todayBias := 0
  660.  
  661. var Candle pCur = na
  662. var int pState = 0
  663. var int pType = 0
  664. var int pDir = 0
  665. var Candle pRev = na
  666. var float pExt = na
  667. var float pSwept = na
  668. var float pInv = na
  669. var float dayLo = na
  670. var float dayHi = na
  671. var int dayLoT = na
  672. var int dayHiT = na
  673. var int entCount = 0
  674. var Zone zn = na
  675. var bool zLive = false
  676. var label prCap = na
  677.  
  678. bool profClosed = false
  679. if not na(pCur) and newBlk
  680. pH.push(pCur)
  681. if pH.size() > 30
  682. pH.shift()
  683. profClosed := true
  684.  
  685. if profClosed
  686. Candle C = pH.last()
  687. Candle P = na
  688. if pH.size() >= 2
  689. P := pH.get(pH.size() - 2)
  690. if C.blk == 1 and pType == 1 and (pState == 1 or pState == 2) and not na(pRev)
  691. bool expand = pDir == 1 ? C.c > pRev.h : C.c < pRev.l
  692. pInv := i_invMode == "Expansion candle 50%" ? C.half() : (pExt + C.c) / 2
  693. if expand
  694. pState := 2
  695. if i_showProf
  696. zn := fZone(C.tO, pInv, pDir == 1 ? C.h : C.l, pDir == 1 ? C.l : C.h)
  697. zLive := true
  698. else if not na(P) and (C.blk == 0 or C.blk == 1) and (pState == 0 or pState == 3) and (C.blk == 0 ? i_asia : i_london)
  699. bool fb = C.l < P.l and C.c > P.l and (not i_align or todayBias == 1)
  700. bool fs = C.h > P.h and C.c < P.h and (not i_align or todayBias == -1)
  701. if fb and fs
  702. fb := C.c >= C.o
  703. fs := not fb
  704. if fb or fs
  705. pState := 1
  706. pType := C.blk + 1
  707. pDir := fb ? 1 : -1
  708. pRev := C
  709. pExt := fb ? C.l : C.h
  710. pSwept := fb ? P.l : P.h
  711. pInv := na
  712. profEvt := pDir
  713. zLive := false
  714. prCap := fDrawProf(fb, pSwept, fb ? P.tL : P.tH, pExt, fb ? C.tL : C.tH, fProfName(pType))
  715. if C.blk == 1
  716. pInv := i_invMode == "Expansion candle 50%" ? C.half() : (pExt + C.c) / 2
  717. if i_showProf
  718. zn := fZone(C.tO, pInv, fb ? C.h : C.l, pExt)
  719. zLive := true
  720. if C.blk == 0 or C.blk == 1
  721. if na(dayLo) or C.l < dayLo
  722. dayLo := C.l
  723. dayLoT := C.tL
  724. if na(dayHi) or C.h > dayHi
  725. dayHi := C.h
  726. dayHiT := C.tH
  727.  
  728. if na(pCur) or newBlk
  729. pCur := fNew(blk)
  730. else
  731. pCur.grow()
  732.  
  733. var int dayStartBar = 0
  734. if newDay
  735. dayStartBar := bar_index
  736. pState := 0
  737. pType := 0
  738. pDir := 0
  739. pRev := na
  740. pExt := na
  741. pSwept := na
  742. pInv := na
  743. dayLo := na
  744. dayHi := na
  745. dayLoT := na
  746. dayHiT := na
  747. zLive := false
  748. zn := na
  749. prCap := na
  750. entCount := 0
  751.  
  752. if blk == 2 and pState == 0 and i_ny and not na(dayLo) and not na(dayHi) and not na(pCur) and barstate.isconfirmed
  753. bool bullNY = (not i_align or todayBias == 1) and pCur.l < dayLo and close > dayLo and close > open
  754. bool bearNY = (not i_align or todayBias == -1) and pCur.h > dayHi and close < dayHi and close < open
  755. if bullNY and bearNY
  756. bullNY := todayBias != -1
  757. bearNY := not bullNY
  758. if bullNY or bearNY
  759. pState := 1
  760. pType := 3
  761. pDir := bullNY ? 1 : -1
  762. pRev := pCur
  763. pExt := bullNY ? pCur.l : pCur.h
  764. pSwept := bullNY ? dayLo : dayHi
  765. pInv := pExt
  766. profEvt := pDir
  767. zLive := false
  768. prCap := fDrawProf(bullNY, pSwept, bullNY ? dayLoT : dayHiT, pExt, bullNY ? pCur.tL : pCur.tH, fProfName(3))
  769.  
  770. if profEvt != 0
  771. rEdge := math.max(rEdge, bar_index + 4)
  772.  
  773. if pState == 1 and not na(pRev) and barstate.isconfirmed
  774. if pType == 2 and (pDir == 1 ? close > pRev.h : close < pRev.l)
  775. pState := 2
  776. else if pType == 3 and (pDir == 1 ? close > pRev.o : close < pRev.o)
  777. pState := 2
  778.  
  779. bool invNow = false
  780. if (pState == 1 or pState == 2) and not na(pExt)
  781. if i_extViol and (pDir == 1 ? low < pExt : high > pExt)
  782. invNow := true
  783. if not invNow and blk == 2 and (pType == 1 or pType == 2) and not na(pInv)
  784. bool brk = i_invTrig == "Wick" ? (pDir == 1 ? low < pInv : high > pInv) : (barstate.isconfirmed and (pDir == 1 ? close < pInv : close > pInv))
  785. if brk
  786. invNow := true
  787. if invNow
  788. pState := 3
  789. invEvt := true
  790. zLive := false
  791. if not na(prCap)
  792. prCap.set_textcolor(c_grey)
  793. if i_showProf
  794. float lv = na(pInv) ? close : pInv
  795. fGLb(label.new(time, lv, "⟶ this is invalid", xloc = xloc.bar_time, style = label.style_label_left, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize)))
  796. rEdge := math.max(rEdge, bar_index + 16)
  797.  
  798. if zLive and not na(zn) and not na(zn.b)
  799. if blk > 2 or (not na(zn.tC) and time - zn.tC >= i_maxLine * barMs)
  800. zLive := false
  801. else
  802. zn.b.set_right(time)
  803. zn.m.set_x2(time)
  804. zn.e.set_x2(time)
  805.  
  806. int openMins = i_openHr * 60 + i_openMin
  807. bool isOpen = i_showOpen and nyMins >= openMins and (nz(nyMins[1], -1) < openMins or nyMins < nz(nyMins[1], 0))
  808. if isOpen
  809. int winMins = i_endH * 60 - nyMins
  810. int opBars = winMins > 0 ? math.max(1, math.min(i_openLen, int(math.floor(winMins * 60000 / barMs)))) : i_openLen
  811. int opEndT = time + barMs * opBars
  812. int hr12 = i_openHr % 12 == 0 ? 12 : i_openHr % 12
  813. string ampm = i_openHr >= 12 ? "pm" : "am"
  814. fGL(line.new(time, open, opEndT, open, xloc = xloc.bar_time, color = c_open, style = fLs(i_openSt), width = i_openW))
  815. if vOpLbl
  816. fGLb(label.new(opEndT, open, str.format("{0,number,0}:{1,number,00} {2}", hr12, i_openMin, ampm), xloc = xloc.bar_time, style = label.style_label_left, color = C_NONE, textcolor = c_txt, size = fSize(i_txtSize)))
  817. rEdge := math.max(rEdge, bar_index + opBars + (vOpLbl ? 8 : 0))
  818.  
  819. int tfSec = timeframe.in_seconds()
  820. string rawMth = i_method == "Auto" ? (tfSec >= 600 ? "A" : tfSec >= 240 ? "B1" : "B2") : i_method == "A: 7H + 15m" ? "A" : i_method == "B: 1H + 5m" ? "B1" : "B2"
  821. string mth = (rawMth == "B1" and tfSec >= 3600) or (rawMth == "B2" and tfSec >= 1800) ? "A" : rawMth
  822. bool useA = mth == "A"
  823. string htfTF = mth == "B1" ? "60" : mth == "B2" ? "30" : timeframe.period
  824.  
  825. float pvLow = ta.pivotlow(low, i_pivLen, i_pivLen)
  826. float pvHigh = ta.pivothigh(high, i_pivLen, i_pivLen)
  827. bool htfNew = not useA and timeframe.change(htfTF)
  828.  
  829. var float hCurH = na
  830. var float hCurL = na
  831. var int hCurHB = na
  832. var int hCurLB = na
  833. var float hPrvH = na
  834. var float hPrvL = na
  835. var int hPrvHB = na
  836. var int hPrvLB = na
  837. if htfNew
  838. hPrvH := hCurH
  839. hPrvL := hCurL
  840. hPrvHB := hCurHB
  841. hPrvLB := hCurLB
  842. hCurH := high
  843. hCurL := low
  844. hCurHB := bar_index
  845. hCurLB := bar_index
  846. else
  847. if high > hCurH
  848. hCurH := high
  849. hCurHB := bar_index
  850. if low < hCurL
  851. hCurL := low
  852. hCurLB := bar_index
  853.  
  854. var float refL = na
  855. var int refLB = na
  856. var float refH = na
  857. var int refHB = na
  858. if useA
  859. if not na(pvLow)
  860. refL := pvLow
  861. refLB := bar_index - i_pivLen
  862. if not na(pvHigh)
  863. refH := pvHigh
  864. refHB := bar_index - i_pivLen
  865. else
  866. refL := hPrvL
  867. refLB := hPrvLB
  868. refH := hPrvH
  869. refHB := hPrvHB
  870.  
  871. bool bearC = close < open
  872. bool bullC = close > open
  873. var float dnO = na
  874. var int dnB = na
  875. var float upO = na
  876. var int upB = na
  877. if bearC and not bearC[1]
  878. dnO := open
  879. dnB := bar_index
  880. if bullC and not bullC[1]
  881. upO := open
  882. upB := bar_index
  883.  
  884. var float swL = na
  885. var int swLB = na
  886. var int usedLB = -1
  887. var float cLvlB = na
  888. var int cBarB = na
  889. if newDay
  890. swL := na
  891. cLvlB := na
  892. if na(swL)
  893. if not na(refL) and nz(refLB, -2) != usedLB and low < refL
  894. swL := low
  895. swLB := bar_index
  896. usedLB := refLB
  897. cLvlB := dnO
  898. cBarB := dnB
  899. else
  900. if low <= swL
  901. swL := low
  902. swLB := bar_index
  903. cLvlB := dnO
  904. cBarB := dnB
  905. if bar_index - swLB > i_cisdMax
  906. swL := na
  907.  
  908. var float swH = na
  909. var int swHB = na
  910. var int usedHB = -1
  911. var float cLvlS = na
  912. var int cBarS = na
  913. if newDay
  914. swH := na
  915. cLvlS := na
  916. if na(swH)
  917. if not na(refH) and nz(refHB, -2) != usedHB and high > refH
  918. swH := high
  919. swHB := bar_index
  920. usedHB := refHB
  921. cLvlS := upO
  922. cBarS := upB
  923. else
  924. if high >= swH
  925. swH := high
  926. swHB := bar_index
  927. cLvlS := upO
  928. cBarS := upB
  929. if bar_index - swHB > i_cisdMax
  930. swH := na
  931.  
  932. bool cisdBull = barstate.isconfirmed and not na(swL) and not na(cLvlB) and close > cLvlB and bullC
  933. bool cisdBear = barstate.isconfirmed and not na(swH) and not na(cLvlS) and close < cLvlS and bearC
  934.  
  935. var Setup act = na
  936. var int wins = 0
  937. var int losses = 0
  938. var int timeOuts = 0
  939. var float netR = 0.0
  940. int exitRel = (i_exitHr * 60 - i_dayStart * 60 + 1440) % 1440
  941. bool timeOut = newDay or (i_timeExit and relMins >= exitRel and nz(relMins[1], 0) < exitRel)
  942.  
  943. if not na(act) and act.live and bar_index >= act.bE
  944. bool gapSL = act.dir == 1 ? open <= act.sl : open >= act.sl
  945. bool gapTP = act.dir == 1 ? open >= act.tp : open <= act.tp
  946. bool closeAtOpen = gapSL or gapTP or timeOut
  947. bool hitSL = not closeAtOpen and (act.dir == 1 ? low <= act.sl : high >= act.sl)
  948. bool hitTP = not closeAtOpen and (act.dir == 1 ? high >= act.tp : low <= act.tp)
  949. bool closed = closeAtOpen or hitSL or hitTP
  950. float barHi = closeAtOpen ? open : high
  951. float barLo = closeAtOpen ? open : low
  952. act.mfe := act.dir == 1 ? math.max(act.mfe, barHi) : math.min(act.mfe, barLo)
  953. act.mae := act.dir == 1 ? math.min(act.mae, barLo) : math.max(act.mae, barHi)
  954. float slEdge = act.dir == 1 ? math.max(act.mae, act.sl) : math.min(act.mae, act.sl)
  955. act.pgSL.set_lefttop(act.bE, math.max(act.entry, slEdge))
  956. act.pgSL.set_rightbottom(bar_index + 1, math.min(act.entry, slEdge))
  957. if i_showPath and not na(act.lnPath)
  958. act.lnPath.set_xy2(bar_index, close)
  959. if closed
  960. float xp = closeAtOpen ? open : hitSL ? act.sl : act.tp
  961. float risk = math.max(math.abs(act.entry - act.sl), syminfo.mintick)
  962. float resR = (xp - act.entry) * act.dir / risk
  963. act.live := false
  964. exitEvt := (gapSL or hitSL) ? -1 : (gapTP or hitTP) ? 1 : 2
  965. if exitEvt == 1
  966. wins += 1
  967. else if exitEvt == -1
  968. losses += 1
  969. else
  970. timeOuts += 1
  971. netR += resR
  972. act.bxSL.set_right(bar_index)
  973. act.bxTP.set_right(bar_index)
  974. act.pgTP.set_right(bar_index)
  975. act.pgSL.set_right(bar_index)
  976. if i_showPath and not na(act.lnPath)
  977. act.lnPath.set_xy2(bar_index, xp)
  978.  
  979. var int lastEntBar = -1000
  980. bool inWin = blk == 1 or (blk == 2 and hNY < i_endH)
  981. bool profLiv = pState == 1 or pState == 2
  982. bool ctxB = inWin and (not i_align or todayBias == 1) and (not i_needProf or (profLiv and pDir == 1))
  983. bool ctxS = inWin and (not i_align or todayBias == -1) and (not i_needProf or (profLiv and pDir == -1))
  984. bool actLive = not na(act) and act.live
  985. bool canNew = i_showEnt and barstate.isconfirmed and not actLive and entCount < i_maxEnt and bar_index - lastEntBar >= i_cool
  986. bool goB = canNew and cisdBull and ctxB
  987. bool goS = canNew and cisdBear and ctxS and not goB
  988.  
  989. if goB or goS
  990. int d = goB ? 1 : -1
  991. float ext = goB ? swL : swH
  992. int exB = goB ? swLB : swHB
  993. float cLv = goB ? cLvlB : cLvlS
  994. int cBr = goB ? cBarB : cBarS
  995. float slP = ext + (goB ? -1 : 1) * i_slBuf * syminfo.mintick
  996. if i_slMode == "Profile extreme" and profLiv and pDir == d and not na(pExt)
  997. slP := pExt + (goB ? -1 : 1) * i_slBuf * syminfo.mintick
  998. if na(slP) or (d == 1 ? slP >= close : slP <= close)
  999. slP := d == 1 ? low : high
  1000. float risk = math.abs(close - slP)
  1001. if risk >= syminfo.mintick
  1002. float tpP = d == 1 ? close + risk * i_rr : close - risk * i_rr
  1003. if i_tpMode == "Daily relevant swing"
  1004. float tgt = d == 1 ? sH.p : sL.p
  1005. if not na(tgt) and (d == 1 ? tgt >= close + risk : tgt <= close - risk)
  1006. tpP := tgt
  1007. Setup st = fSetup(d, cLv, cBr, ext, exB, slP, tpP)
  1008. act := st
  1009. setups.push(st)
  1010. while setups.size() > i_keepSet
  1011. fDelSetup(setups.shift())
  1012. entCount += 1
  1013. lastEntBar := bar_index
  1014. entryNow := d
  1015. else if vCtx and barstate.isconfirmed
  1016. if cisdBull and ctxB and not na(cBarB)
  1017. cisdLines.push(fGL(line.new(cBarB, cLvlB, bar_index + 3, cLvlB, color = c_cisd, width = i_cisdW)))
  1018. rEdge := math.max(rEdge, bar_index + 3)
  1019. if cisdBear and ctxS and not na(cBarS)
  1020. cisdLines.push(fGL(line.new(cBarS, cLvlS, bar_index + 3, cLvlS, color = c_cisd, width = i_cisdW)))
  1021. rEdge := math.max(rEdge, bar_index + 3)
  1022. fCapL(cisdLines, i_keepCisd)
  1023.  
  1024. if cisdBull
  1025. swL := na
  1026. cLvlB := na
  1027. if cisdBear
  1028. swH := na
  1029. cLvlS := na
  1030.  
  1031. barcolor(i_paintEnt and entryNow != 0 and time >= last_bar_time - (i_hist + 2) * 86400000 ? (entryNow == 1 ? c_entBull : c_entBear) : na, title = "Entry candle")
  1032. alertcondition(exitEvt == 2, "Time exit", "7H Fractal: setup closed at session exit")
  1033.  
  1034. var array<box> pnB = array.new<box>()
  1035. var array<line> pnL = array.new<line>()
  1036. var array<label> pnT = array.new<label>()
  1037.  
  1038. if barstate.islast
  1039. while pnB.size() > 0
  1040. pnB.pop().delete()
  1041. while pnL.size() > 0
  1042. pnL.pop().delete()
  1043. while pnT.size() > 0
  1044. pnT.pop().delete()
  1045. if i_showPnl and not na(pCur)
  1046. int cnt = pH.size()
  1047. int nC = i_pnlCount
  1048. int rightClr = math.max(bar_index, rEdge)
  1049. for st in setups
  1050. if not na(st.bxTP)
  1051. rightClr := math.max(rightClr, st.bxTP.get_right())
  1052. if not na(st.bxSL)
  1053. rightClr := math.max(rightClr, st.bxSL.get_right())
  1054. int maxBase = bar_index + 495 - nC * (i_pnlW + i_pnlGap)
  1055. int baseX = math.min(rightClr + i_pnlOff, math.max(bar_index + 1, maxBase))
  1056. float lo = na
  1057. float hi = na
  1058. for k = 0 to nC - 1
  1059. int idx = cnt - (nC - 1) + k
  1060. if idx >= 0
  1061. Candle cd = idx < cnt ? pH.get(idx) : pCur
  1062. lo := na(lo) ? cd.l : math.min(lo, cd.l)
  1063. hi := na(hi) ? cd.h : math.max(hi, cd.h)
  1064. if not na(lo) and not na(hi)
  1065. float visRng = math.max(fitHi - fitLo, syminfo.mintick * 10)
  1066. float tgtH = visRng * (i_pnlPct / 100.0)
  1067. float pRng = math.max(hi - lo, syminfo.mintick)
  1068. float scl = i_pnlTrue ? 1.0 : tgtH / pRng
  1069. float pMid = (lo + hi) * 0.5
  1070. float loClamp = fitLo + tgtH * 0.55
  1071. float hiClamp = fitHi - tgtH * 0.50
  1072. float midY = i_pnlTrue ? pMid : (loClamp <= hiClamp ? math.max(loClamp, math.min(hiClamp, close)) : (fitLo + fitHi) * 0.5)
  1073. float panelBot = i_pnlTrue ? lo - visRng * 0.04 : midY - tgtH * 0.5
  1074. float prevXm = na
  1075. for k = 0 to nC - 1
  1076. int idx = cnt - (nC - 1) + k
  1077. if idx >= 0
  1078. Candle cd = idx < cnt ? pH.get(idx) : pCur
  1079. int xl = math.min(baseX + k * (i_pnlW + i_pnlGap), bar_index + 498)
  1080. int xr = math.min(xl + i_pnlW, bar_index + 499)
  1081. int xm = int(math.round((xl + xr) * 0.5))
  1082. bool bull = cd.c >= cd.o
  1083. float yL = midY + (cd.l - pMid) * scl
  1084. float yH = midY + (cd.h - pMid) * scl
  1085. float bot = midY + (math.min(cd.o, cd.c) - pMid) * scl
  1086. float top = midY + (math.max(cd.o, cd.c) - pMid) * scl
  1087. if top - bot < visRng * 0.004
  1088. top := bot + visRng * 0.004
  1089. bool hl = pState > 0 and not na(pRev) and cd.tO == pRev.tO
  1090. color hlCol = pState == 3 ? c_grey : c_pnlHl
  1091. pnL.push(line.new(xm, yL, xm, yH, color = c_wick, width = 1))
  1092. pnB.push(box.new(xl, top, xr, bot, border_color = hl ? hlCol : c_wick, border_width = hl ? 2 : 1, bgcolor = bull ? c_bullC : c_bearC))
  1093. pnT.push(label.new(xm, panelBot - tgtH * 0.08, fHour(cd.blk), style = label.style_label_up, color = C_NONE, textcolor = c_txt, size = size.tiny))
  1094. if hl
  1095. pnT.push(fDot(xm, pDir == 1 ? yL - tgtH * 0.02 : yH + tgtH * 0.02, pState == 3 ? c_grey : c_profDot, false))
  1096. if not na(pSwept) and not na(prevXm)
  1097. float ySw = midY + (pSwept - pMid) * scl
  1098. pnL.push(line.new(int(math.round(prevXm)), ySw, xm, ySw, color = c_profLn, width = 1))
  1099. prevXm := xm
  1100. if i_pnlTrue and pState > 0 and not na(pExt) and not na(pSwept)
  1101. color lkC = pState == 3 ? c_grey : c_profLn
  1102. pnL.push(line.new(bar_index, pSwept, baseX, pSwept, color = lkC, style = line.style_dotted, width = 1))
  1103. pnL.push(line.new(bar_index, pExt, baseX, pExt, color = lkC, style = line.style_dotted, width = 1))
  1104. if i_pnlCap
  1105. string cap = pType > 0 ? fProfName(pType) + " profile ( the " + fHour(pType - 1) + " candle )" + (pState == 3 ? " · invalid" : "") : "no profile"
  1106. int cx = math.min(baseX + int(math.round((nC * (i_pnlW + i_pnlGap) - i_pnlGap) * 0.5)), bar_index + 498)
  1107. pnT.push(label.new(cx, panelBot - tgtH * 0.22, cap, style = label.style_label_up, color = C_NONE, textcolor = pState == 3 ? c_grey : c_txt, size = fSize(i_txtSize)))
  1108.  
  1109. color tBg = C_NONE
  1110. color tTx = i_theme == "Dark" ? #d1d4dc : i_theme == "Light" ? #2a2a2a : c_dTx
  1111. color tNe = #8a8f98
  1112.  
  1113. var table tbl = table.new(fPos(i_dashPos), 2, 8, frame_color = C_NONE, frame_width = 0, border_color = C_NONE, border_width = 0)
  1114.  
  1115. fRow(int r, string k, string v, color vc) =>
  1116. tbl.cell(0, r, k, text_color = tNe, bgcolor = tBg, text_size = fSize(i_dashSize), text_halign = text.align_left)
  1117. tbl.cell(1, r, v, text_color = vc, bgcolor = tBg, text_size = fSize(i_dashSize), text_halign = text.align_right)
  1118.  
  1119. if barstate.islast and i_showDash
  1120. tbl.cell(0, 0, "7H Fractal Model", text_color = tTx, bgcolor = C_NONE, text_size = fSize(i_dashSize), text_halign = text.align_center)
  1121. tbl.merge_cells(0, 0, 1, 0)
  1122. string bTx = todayBias == 1 ? "Bullish ▲" : todayBias == -1 ? "Bearish ▼" : skipDay ? "Skip (" + (bias == 1 ? "bull" : "bear") + " wick)" : contInvalid ? "Invalidated (50%)" : "Neutral"
  1123. color bCl = todayBias == 1 ? c_entBull : todayBias == -1 ? c_entBear : tNe
  1124. fRow(1, "Daily bias", bTx, bCl)
  1125. string pTx = pState == 0 ? "—" : fProfName(pType) + (pState == 1 ? " · formed" : pState == 2 ? " · expanded" : " · invalid")
  1126. color pCl = pState == 0 or pState == 3 ? tNe : (pDir == 1 ? c_entBull : c_entBear)
  1127. fRow(2, "7H profile", pTx, pCl)
  1128. bool hasToday = not na(act) and act.bE >= dayStartBar
  1129. bool okBias = todayBias != 0
  1130. bool okProf = profLiv and (not i_align or pDir == todayBias)
  1131. bool okCisd = hasToday and (not i_align or todayBias == 0 or act.dir == todayBias)
  1132. bool swPend = (todayBias == 1 and not na(swL)) or (todayBias == -1 and not na(swH)) or (todayBias == 0 and (not na(swL) or not na(swH)))
  1133. int score = (okBias ? 1 : 0) + (okProf ? 1 : 0) + (okCisd ? 1 : 0)
  1134. fRow(3, "Bias · Profile · CISD", (okBias ? "●" : "○") + " " + (okProf ? "●" : "○") + " " + (okCisd ? "●" : swPend ? "◐" : "○"), score == 3 ? (todayBias == 1 ? c_entBull : c_entBear) : tNe)
  1135. bool has = hasToday
  1136. fRow(4, "Setup (today)", not has ? "—" : (act.dir == 1 ? "Long" : "Short") + (act.live ? " · live" : " · closed"), not has ? tNe : (act.dir == 1 ? c_entBull : c_entBear))
  1137. fRow(5, "Entry / Stop", has ? str.tostring(act.entry, format.mintick) + " / " + str.tostring(act.sl, format.mintick) : "—", tTx)
  1138. float rrV = has ? math.abs(act.tp - act.entry) / math.max(math.abs(act.entry - act.sl), syminfo.mintick) : na
  1139. fRow(6, "Target", has ? str.tostring(act.tp, format.mintick) + " (" + str.tostring(rrV, "#.0") + "R)" : "—", tTx)
  1140. int wl = wins + losses
  1141. int tot = wl + timeOuts
  1142. string wTx = wl > 0 ? " · " + str.tostring(wins * 100.0 / wl, "#") + "%" : ""
  1143. string rTx = tot > 0 ? " · " + (netR >= 0 ? "+" : "") + str.tostring(netR, "#.0") + "R" : ""
  1144. fRow(7, "W / L / T", str.tostring(wins) + " / " + str.tostring(losses) + " / " + str.tostring(timeOuts) + wTx + rTx, tTx)
  1145.  
  1146. alertcondition(biasEvt == 1, "Bias · Bullish", "7H Fractal: daily bias turned bullish")
  1147. alertcondition(biasEvt == -1, "Bias · Bearish", "7H Fractal: daily bias turned bearish")
  1148. alertcondition(biasInv, "Bias invalidated", "7H Fractal: daily bias invalidated (continuation 50%)")
  1149. alertcondition(profEvt == 1, "Profile · Long", "7H Fractal: bullish 7H reversal profile formed")
  1150. alertcondition(profEvt == -1, "Profile · Short", "7H Fractal: bearish 7H reversal profile formed")
  1151. alertcondition(invEvt, "Profile invalid", "7H Fractal: profile invalidated")
  1152. alertcondition(entryNow == 1, "Entry · Long", "7H Fractal: long CISD entry")
  1153. alertcondition(entryNow == -1, "Entry · Short", "7H Fractal: short CISD entry")
  1154. alertcondition(exitEvt == 1, "Target hit", "7H Fractal: target reached")
  1155. alertcondition(exitEvt == -1, "Stop hit", "7H Fractal: stop loss hit")
Advertisement
Add Comment
Please, Sign In to add comment