Advertisement
Guest User

TradeAssist

a guest
Jun 11th, 2025
290
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.12 KB | Cryptocurrency | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Your Name Here - Emerald Trading Assistant
  3.  
  4. //@version=5
  5. indicator("Emerald Trading Assistant", shorttitle="ETA", overlay=true,
  6. max_labels_count=500, max_lines_count=500, max_bars_back=5000)
  7.  
  8. // ════════════════════════════════════════════════════════════════════════════════
  9. // ────── INPUT GROUPS ──────
  10. // ════════════════════════════════════════════════════════════════════════════════
  11.  
  12. string g_fibo = "Fibonacci & Volume Profile"
  13. string g_fvg = "FVG / Imbalance Settings"
  14. string g_paa = "PAA Pivot Settings"
  15. string g_volume = "Volume Analysis"
  16. string g_rejection = "Rejection Candles"
  17. string g_liquidity = "Liquidity Grabs"
  18. string g_trend = "Trend Analysis"
  19. string g_vwap = "VWAP Settings"
  20. string g_display = "Display Options"
  21. string g_risk = "Risk Management"
  22. string g_alerts = "Alert Settings"
  23.  
  24. // --- Fibo & VP Settings ---
  25. string fibo_timeframe = input.timeframe("D", title="Fibo Timeframe", group=g_fibo, options=["120", "240", "480", "720", "D", "W", "M"], tooltip="Higher timeframe for calculating Fibonacci levels.")
  26. float fibo_thickness = input.float(0.05, "Fibo Level Thickness (ATR %)", minval=0.01, maxval=1.0, step=0.01, group=g_fibo, tooltip="Controls the thickness of the Fibo levels as a percentage of the 30-period ATR. Smaller value = thinner.")
  27. string vp_vol_type = input.string("All", title="VP Volume Type", group=g_fibo, options=["Bear", "Bull", "All"], tooltip="Choose Volume Type to Show in Boxes")
  28. int vp_levels = input.int(12, "VP Volume Levels", group=g_fibo)
  29. bool show_fibo_levels = input.bool(true, "Show Fibo Levels?", inline="fibo_vis", group=g_fibo)
  30. bool show_fibo_target = input.bool(true, "Show Fibo Target?", inline="fibo_vis", group=g_fibo)
  31. bool show_vp = input.bool(false, "Show Volume Profile?", inline="vp_vis", group=g_fibo)
  32. bool show_poc = input.bool(true, "Show POC?", inline="vp_vis", group=g_fibo)
  33. float vp_width = input.float(20, "VP Width (%)", minval=1, maxval=100, group=g_fibo)
  34. int vp_offset = input.int(5, "VP Horizontal Offset", minval=0, group=g_fibo, tooltip="Positions the profile X bars to the RIGHT of the last candle.")
  35.  
  36. // --- FVG Settings ---
  37. bool fvg_show = input.bool(true, "Show FVGs?", group=g_fvg, inline="fvg1")
  38. int fvg_max_count = input.int(10, "Max FVGs to Show", minval=1, maxval=50, group=g_fvg, inline="fvg1")
  39. int fvg_max_len = input.int(80, "Max FVG Length (Bars)", minval=0, group=g_fvg, tooltip="Sets the maximum bars an FVG will extend. 0 = Indefinite.")
  40. float fvg_min_atr_size = input.float(25.0, "Min FVG Size (% of ATR)", minval=0.0, step=5.0, group=g_fvg, tooltip="Filters out small FVGs. Size must be at least this % of the 14-period ATR to be shown.")
  41. string fvg_htf = input.timeframe("60", "FVG Higher Timeframe (HTF)", group=g_fvg, tooltip="MUST be a higher timeframe than the chart's.")
  42. color fvg_bull_color = input.color(color.new(color.blue, 85), "Bullish", group=g_fvg, inline="fvgcolor")
  43. color fvg_bear_color = input.color(color.new(color.red, 85), "Bearish", group=g_fvg, inline="fvgcolor")
  44. bool fvg_show_partial_fill = input.bool(true, "Show Partial Fills?", group=g_fvg)
  45. float fvg_lv_atr_mult = input.float(2.0, "Large FVG ATR Multiplier", minval=0.1, group=g_fvg)
  46.  
  47. // --- PAA Pivot Settings ---
  48. int paa_lookback = input.int(15, "PAA Pivot Lookback", minval=5, maxval=50, group=g_paa)
  49. bool show_paa_pivots = input.bool(true, "Show PAA Pivots", group=g_paa)
  50. bool paa_extend_lines = input.bool(false, "Extend PAA Lines", group=g_paa)
  51. int paa_valid_bars = input.int(30, "Max Bars to Keep PAA Levels", minval=10, maxval=100, group=g_paa)
  52.  
  53. // --- Volume Analysis Settings ---
  54. int vol_len = input.int(20, "Average Volume Length", minval=5, maxval=100, group=g_volume)
  55. float hiVolMult = input.float(1.5, "High Volume Multiplier", minval=1.0, maxval=5.0, step=0.1, group=g_volume)
  56. float ultraVolMult = input.float(2.5, "Ultra Volume Multiplier", minval=1.5, maxval=10.0, step=0.1, group=g_volume)
  57. int cvdSmoothLen = input.int(3, "CVD Smoothing Length", minval=1, maxval=20, group=g_volume)
  58.  
  59. // --- Rejection Candle Settings ---
  60. float rejWickMin = input.float(0.60, "Min Rejection Wick (% of bar)", minval=0.1, maxval=0.9, step=0.05, group=g_rejection)
  61. float rejBodyMax = input.float(0.30, "Max Body Size (% of bar)", minval=0.05, maxval=0.5, step=0.05, group=g_rejection)
  62. bool enable_rejection = input.bool(true, "Enable Rejection Analysis", group=g_rejection)
  63.  
  64. // --- Liquidity Grab Settings ---
  65. int liqLookback = input.int(5, "Liquidity Lookback Period", minval=3, maxval=20, group=g_liquidity)
  66. int liqReclaimBars = input.int(2, "Bars to Reclaim Level", minval=1, maxval=10, group=g_liquidity)
  67. bool enable_liquidity = input.bool(true, "Enable Liquidity Analysis", group=g_liquidity)
  68.  
  69. // --- Trend Analysis Settings ---
  70. bool trend_filter = input.bool(true, "Enable Trend Filter", group=g_trend)
  71. bool show_trend_bgcolor = input.bool(false, "Show Trend Background Color", group=g_trend)
  72. int ema_len = input.int(50, "EMA Length", minval=10, maxval=200, group=g_trend)
  73.  
  74. // --- VWAP Settings ---
  75. int length_vwap = input.int(20, "Rolling VWAP Length", minval=5, maxval=200, group=g_vwap)
  76. bool show_session_vwap = input.bool(true, "Show Session VWAP", group=g_vwap)
  77. bool show_rolling_vwap = input.bool(true, "Show Rolling VWAP", group=g_vwap)
  78.  
  79. // --- Display Settings ---
  80. bool show_a_grade_labels = input.bool(true, "Show A-Grade Labels", group=g_display)
  81. bool show_b_grade_labels = input.bool(true, "Show B-Grade Labels", group=g_display)
  82. bool show_c_grade_labels = input.bool(false, "Show C-Grade Labels", group=g_display)
  83. int label_offset = input.int(0, "Label Offset", minval=-10, maxval=10, group=g_display)
  84. bool show_dashboard = input.bool(true, "Show Info Dashboard", group=g_display)
  85.  
  86. // --- Risk Management Settings ---
  87. bool show_risk_labels = input.bool(true, "Show Risk/Reward Labels", group=g_risk)
  88. float sl_atr_mult = input.float(1.0, "Stop Loss ATR Multiplier", minval=0.1, step=0.1, group=g_risk, tooltip="Sets the Stop Loss distance. E.g., a value of 1.0 places the SL 1 ATR away from the signal candle's high/low.")
  89. float rr_ratio_1 = input.float(1.0, "Target 1 R:R", minval=0.5, step=0.5, group=g_risk)
  90. float rr_ratio_2 = input.float(2.0, "Target 2 R:R", minval=0.5, step=0.5, group=g_risk)
  91. float rr_ratio_3 = input.float(3.0, "Target 3 R:R", minval=0.5, step=0.5, group=g_risk)
  92.  
  93. // --- Alert Settings ---
  94. bool alert_a_setups = input.bool(true, "Alert A-Grade Setups", group=g_alerts)
  95. bool alert_b_setups = input.bool(true, "Alert B-Grade Setups", group=g_alerts)
  96. bool alert_c_setups = input.bool(false, "Alert C-Grade Setups", group=g_alerts)
  97. bool pre_alert = input.bool(true, "Early Setup Alerts", group=g_alerts)
  98.  
  99.  
  100. // ════════════════════════════════════════════════════════════════════════════════
  101. // ────── ADVANCED FVG SYSTEM (PORTED) ──────
  102. // ════════════════════════════════════════════════════════════════════════════════
  103.  
  104. type fvgObject
  105. bool isBullish
  106. bool isLV
  107. float top
  108. float bottom
  109. int startTime
  110. string tfType
  111. float currentTop
  112. float currentBottom
  113.  
  114. var string TF_TYPE_LTF = "LTF"
  115. var string TF_TYPE_HTF = "HTF"
  116.  
  117. f_detectFvg(currentAtr) =>
  118. fvgObject fvg = na
  119. bool isFvgFound = false
  120. float minSize = currentAtr * (fvg_min_atr_size / 100.0)
  121. if low > high[2] and barstate.isconfirmed and (low - high[2]) >= minSize
  122. isFvgFound := true
  123. fvg := fvgObject.new()
  124. fvg.isBullish := true
  125. fvg.top := low
  126. fvg.bottom := high[2]
  127. fvg.startTime := time[2]
  128. fvg.tfType := TF_TYPE_LTF
  129. fvg.currentTop := fvg.top
  130. fvg.currentBottom := fvg.bottom
  131. fvg.isLV := (fvg.top - fvg.bottom) / currentAtr >= fvg_lv_atr_mult
  132. else if high < low[2] and barstate.isconfirmed and (low[2] - high) >= minSize
  133. isFvgFound := true
  134. fvg := fvgObject.new()
  135. fvg.isBullish := false
  136. fvg.top := low[2]
  137. fvg.bottom := high
  138. fvg.startTime := time[2]
  139. fvg.tfType := TF_TYPE_LTF
  140. fvg.currentTop := fvg.top
  141. fvg.currentBottom := fvg.bottom
  142. fvg.isLV := (fvg.top - fvg.bottom) / currentAtr >= fvg_lv_atr_mult
  143. [isFvgFound, fvg]
  144.  
  145. f_detectMultiTFFvg(htfHigh1, htfLow1, htfTime1, htfHigh3, htfLow3, htfTime3, tfAtr) =>
  146. fvgObject fvg = na
  147. bool isFvgFound = false
  148. float minSize = tfAtr * (fvg_min_atr_size / 100.0)
  149. if not na(htfTime1) and not na(htfTime3)
  150. if not na(htfLow1) and not na(htfHigh3) and htfLow1 > htfHigh3 and (htfLow1 - htfHigh3) >= minSize
  151. isFvgFound := true
  152. fvg := fvgObject.new()
  153. fvg.isBullish := true
  154. fvg.top := htfLow1
  155. fvg.bottom := htfHigh3
  156. fvg.startTime := int(htfTime3)
  157. fvg.tfType := TF_TYPE_HTF
  158. fvg.currentTop := fvg.top
  159. fvg.currentBottom := fvg.bottom
  160. fvg.isLV := (fvg.top - fvg.bottom) / tfAtr >= fvg_lv_atr_mult
  161. else if not na(htfHigh1) and not na(htfLow3) and htfHigh1 < htfLow3 and (htfLow3 - htfHigh1) >= minSize
  162. isFvgFound := true
  163. fvg := fvgObject.new()
  164. fvg.isBullish := false
  165. fvg.top := htfLow3
  166. fvg.bottom := htfHigh1
  167. fvg.startTime := int(htfTime3)
  168. fvg.tfType := TF_TYPE_HTF
  169. fvg.currentTop := fvg.top
  170. fvg.currentBottom := fvg.bottom
  171. fvg.isLV := (fvg.top - fvg.bottom) / tfAtr >= fvg_lv_atr_mult
  172. [isFvgFound, fvg]
  173.  
  174. f_checkMitigation(isBullish, fvgTop, fvgBottom) => (isBullish and low <= fvgBottom) or (not isBullish and high >= fvgTop)
  175.  
  176. f_checkPartialMitigation(isBullish, cTop, cBot) =>
  177. float newLevel = na
  178. if isBullish and not na(cTop) and low < cTop
  179. newLevel := low
  180. else if not isBullish and not na(cBot) and high > cBot
  181. newLevel := high
  182. newLevel
  183.  
  184. // ════════════════════════════════════════════════════════════════════════════════
  185. // ────── GLOBAL VARIABLES & CORE CALCULATIONS ──────
  186. // ════════════════════════════════════════════════════════════════════════════════
  187.  
  188. var fvgs = array.new<fvgObject>()
  189. var fvgBoxes = array.new<box>()
  190. var fvgMidLines = array.new<line>()
  191. var Fibo = array.new_float(6, 0.0)
  192. var vpBoxes = array.new<box>()
  193. var fiboBoxes = array.new<box>()
  194. var box pocBox = na
  195. var line paa_high_line = line.new(na, na, na, na, color=color.new(color.red, 70), style=line.style_dashed, width=1)
  196. var label paa_high_label = label.new(na, na, "", color=color.new(color.red, 90), textcolor=color.white, style=label.style_label_down, size=size.tiny)
  197. var line paa_low_line = line.new(na, na, na, na, color=color.new(color.green, 70), style=line.style_dashed, width=1)
  198. var label paa_low_label = label.new(na, na, "", color=color.new(color.green, 90), textcolor=color.white, style=label.style_label_up, size=size.tiny)
  199.  
  200. var int newWeekBarIndex = na
  201. var int newWeekTIME = na
  202. string weeklyTF = timeframe.isdwm ? "M" : "W"
  203. bool weekChange = timeframe.change(weeklyTF)
  204. if weekChange
  205. newWeekBarIndex := bar_index
  206. newWeekTIME := time
  207.  
  208. float ltfAtr = ta.atr(14)
  209. float fiboAtr = ta.atr(30)
  210. bool isHtfValid = timeframe.in_seconds(fvg_htf) > timeframe.in_seconds(timeframe.period)
  211.  
  212. float htfAtr = request.security(syminfo.tickerid, fvg_htf, ltfAtr)
  213. [htfHigh1, htfLow1, htfTime1, htfHigh3, htfLow3, htfTime3] = request.security(syminfo.tickerid, fvg_htf, [high[1], low[1], time[1], high[3], low[3], time[3]], lookahead=barmerge.lookahead_on)
  214. float FH = request.security(syminfo.tickerid, fibo_timeframe, high[1])
  215. float FL = request.security(syminfo.tickerid, fibo_timeframe, low[1])
  216. float FC = request.security(syminfo.tickerid, fibo_timeframe, close[1])
  217. float ema = ta.ema(close, ema_len)
  218. bool uptrend = trend_filter ? close > ema : true
  219. bool downtrend = trend_filter ? close < ema : true
  220. float session_vwap = ta.vwap(hlc3)
  221. float rolling_vwap = ta.sma(hlc3 * volume, length_vwap) / ta.sma(volume, length_vwap)
  222. float avg_volume = ta.sma(volume, vol_len)
  223. bool high_volume = volume > avg_volume * hiVolMult
  224. bool ultra_volume = volume > avg_volume * ultraVolMult
  225. float volume_percentile = ta.percentrank(volume, vol_len * 2)
  226. float pivot_high_price = ta.pivothigh(high, paa_lookback, paa_lookback)
  227. float pivot_low_price = ta.pivotlow(low, paa_lookback, paa_lookback)
  228. bool is_high_volume_pivot = high_volume[paa_lookback]
  229. var float paa_high = na
  230. var float paa_low = na
  231. var int paa_high_bar = na
  232. var int paa_low_bar = na
  233. var float paa_high_vol = na
  234. var float paa_low_vol = na
  235. if not na(pivot_high_price) and is_high_volume_pivot
  236. paa_high := pivot_high_price
  237. paa_high_bar := bar_index - paa_lookback
  238. paa_high_vol := volume[paa_lookback]
  239. if not na(pivot_low_price) and is_high_volume_pivot
  240. paa_low := pivot_low_price
  241. paa_low_bar := bar_index - paa_lookback
  242. paa_low_vol := volume[paa_lookback]
  243. var float cvd = 0.0
  244. float price_range = high - low
  245. float delta = price_range > 0 ? volume * (close - open) / price_range : 0
  246. cvd += delta
  247. float cvd_smooth = ta.ema(cvd, cvdSmoothLen)
  248. float bar_range = high - low
  249. float upper_wick = high - math.max(open, close)
  250. float lower_wick = math.min(open, close) - low
  251. float body_size = math.abs(close - open)
  252. bool is_bullish_rejection = enable_rejection and bar_range > 0 and (lower_wick/bar_range >= rejWickMin) and (body_size/bar_range <= rejBodyMax) and (close > open) and high_volume
  253. bool is_bearish_rejection = enable_rejection and bar_range > 0 and (upper_wick/bar_range >= rejWickMin) and (body_size/bar_range <= rejBodyMax) and (close < open) and high_volume
  254. float lookback_low = ta.lowest(low, liqLookback)[1]
  255. float lookback_high = ta.highest(high, liqLookback)[1]
  256. bool bullish_liquidity = enable_liquidity and low < lookback_low and close > lookback_low and (ta.barssince(close > lookback_low) <= liqReclaimBars) and (is_bullish_rejection or ultra_volume) and close > open
  257. bool bearish_liquidity = enable_liquidity and high > lookback_high and close < lookback_high and (ta.barssince(close < lookback_high) <= liqReclaimBars) and (is_bearish_rejection or ultra_volume) and close < open
  258. bool at_paa_low = not na(paa_low) and low <= paa_low * 1.002 and high >= paa_low * 0.998 and bar_index - paa_low_bar <= paa_valid_bars
  259. bool at_paa_high = not na(paa_high) and high >= paa_high * 0.998 and low <= paa_high * 1.002 and bar_index - paa_high_bar <= paa_valid_bars
  260. bool above_session_vwap = close > session_vwap
  261. bool above_rolling_vwap = close > rolling_vwap
  262. bool vwap_bullish_confluence = above_session_vwap and above_rolling_vwap
  263. bool vwap_bearish_confluence = not above_session_vwap and not above_rolling_vwap
  264.  
  265. [ema5m_close, ema5m_ema] = request.security(syminfo.tickerid, "5", [close, ta.ema(close, ema_len)])
  266. [ema15m_close, ema15m_ema] = request.security(syminfo.tickerid, "15", [close, ta.ema(close, ema_len)])
  267. [ema1h_close, ema1h_ema] = request.security(syminfo.tickerid, "60", [close, ta.ema(close, ema_len)])
  268. [ema4h_close, ema4h_ema] = request.security(syminfo.tickerid, "240", [close, ta.ema(close, ema_len)])
  269. bool is_5m_bull = ema5m_close > ema5m_ema
  270. bool is_15m_bull = ema15m_close > ema15m_ema
  271. bool is_1h_bull = ema1h_close > ema1h_ema
  272. bool is_4h_bull = ema4h_close > ema4h_ema
  273.  
  274.  
  275. // ════════════════════════════════════════════════════════════════════════════════
  276. // ────── FVG LOGIC (RUNS ON EVERY BAR) ──────
  277. // ════════════════════════════════════════════════════════════════════════════════
  278.  
  279. if fvg_show
  280. [isLtfFvg, ltfFvg] = f_detectFvg(ltfAtr)
  281. if isLtfFvg
  282. array.push(fvgs, ltfFvg)
  283. array.push(fvgBoxes, box.new(na, na, na, na, xloc=xloc.bar_time))
  284. array.push(fvgMidLines, line.new(na, na, na, na, xloc=xloc.bar_time, style=line.style_solid))
  285. if isHtfValid and not na(htfTime1) and (na(htfTime1[1]) or htfTime1 != htfTime1[1])
  286. [isHtfFvg, htfFvg] = f_detectMultiTFFvg(htfHigh1, htfLow1, htfTime1, htfHigh3, htfLow3, htfTime3, htfAtr)
  287. if isHtfFvg
  288. array.push(fvgs, htfFvg)
  289. array.push(fvgBoxes, box.new(na, na, na, na, xloc=xloc.bar_time))
  290. array.push(fvgMidLines, line.new(na, na, na, na, xloc=xloc.bar_time, style=line.style_solid))
  291.  
  292. if array.size(fvgs) > 0
  293. for i = array.size(fvgs) - 1 to 0
  294. fvgObject fvg = array.get(fvgs, i)
  295. box b = array.get(fvgBoxes, i)
  296. line l = array.get(fvgMidLines, i)
  297. bool isExpired = fvg_max_len > 0 and time > (fvg.startTime + fvg_max_len * timeframe.in_seconds(timeframe.period) * 1000)
  298. if f_checkMitigation(fvg.isBullish, fvg.top, fvg.bottom) or isExpired
  299. box.delete(b)
  300. line.delete(l)
  301. array.remove(fvgBoxes, i)
  302. array.remove(fvgMidLines, i)
  303. array.remove(fvgs, i)
  304. continue
  305.  
  306. if fvg_show_partial_fill
  307. float newLevel = f_checkPartialMitigation(fvg.isBullish, fvg.currentTop, fvg.currentBottom)
  308. if not na(newLevel)
  309. if fvg.isBullish
  310. fvg.currentTop := newLevel
  311. else
  312. fvg.currentBottom := newLevel
  313.  
  314. color boxColor = fvg.isBullish ? fvg_bull_color : fvg_bear_color
  315. color borderColor = fvg.isBullish ? color.new(color.blue, 85) : color.new(color.red, 85)
  316. color midlineColor = fvg.isBullish ? color.new(color.blue, 70) : color.new(color.red, 70)
  317. color finalBorderColor = fvg.isLV ? color.new(color.yellow, 20) : borderColor
  318. color finalMidlineColor = fvg.isLV ? color.new(color.yellow, 20) : midlineColor
  319. int rightTime = fvg_max_len > 0 ? fvg.startTime + fvg_max_len * timeframe.in_seconds(timeframe.period) * 1000 : time + 500 * timeframe.in_seconds(timeframe.period) * 1000
  320.  
  321. box.set_left(b, fvg.startTime)
  322. box.set_top(b, fvg.currentTop)
  323. box.set_bottom(b, fvg.currentBottom)
  324. box.set_right(b, rightTime)
  325. box.set_bgcolor(b, boxColor)
  326. box.set_border_color(b, finalBorderColor)
  327. box.set_border_width(b, fvg.isLV ? 2 : 1)
  328. if fvg_max_len == 0
  329. box.set_extend(b, extend.right)
  330.  
  331. float midY = (fvg.currentTop + fvg.currentBottom) / 2
  332. line.set_xy1(l, fvg.startTime, midY)
  333. line.set_xy2(l, rightTime, midY)
  334. line.set_color(l, finalMidlineColor)
  335. if fvg_max_len == 0
  336. line.set_extend(l, extend.right)
  337.  
  338. while array.size(fvgs) > fvg_max_count
  339. box.delete(array.shift(fvgBoxes))
  340. line.delete(array.shift(fvgMidLines))
  341. array.shift(fvgs)
  342.  
  343. // ════════════════════════════════════════════════════════════════════════════════
  344. // ────── SETUP GRADING SYSTEM ──────
  345. // ════════════════════════════════════════════════════════════════════════════════
  346.  
  347. bool long_trend_ok = uptrend
  348. bool long_location_ok = at_paa_low
  349. bool long_rejection_ok = is_bullish_rejection
  350. bool long_liquidity_ok = bullish_liquidity
  351. bool short_trend_ok = downtrend
  352. bool short_location_ok = at_paa_high
  353. bool short_rejection_ok = is_bearish_rejection
  354. bool short_liquidity_ok = bearish_liquidity
  355. bool long_strong_confirmation = long_rejection_ok or long_liquidity_ok
  356. bool short_strong_confirmation = short_rejection_ok or short_liquidity_ok
  357. bool a_grade_long = long_trend_ok and long_location_ok and long_strong_confirmation
  358. bool a_grade_short = short_trend_ok and short_location_ok and short_strong_confirmation
  359. bool b_grade_long_1 = long_trend_ok and long_location_ok and (high_volume or vwap_bullish_confluence)
  360. bool b_grade_long_2 = long_trend_ok and long_strong_confirmation and not long_location_ok
  361. bool b_grade_long = (b_grade_long_1 or b_grade_long_2) and not a_grade_long
  362. bool b_grade_short_1 = short_trend_ok and short_location_ok and (high_volume or vwap_bearish_confluence)
  363. bool b_grade_short_2 = short_trend_ok and short_strong_confirmation and not short_location_ok
  364. bool b_grade_short = (b_grade_short_1 or b_grade_short_2) and not a_grade_short
  365. bool c_grade_long = long_trend_ok and high_volume and vwap_bullish_confluence and not (a_grade_long or b_grade_long)
  366. bool c_grade_short = short_trend_ok and high_volume and vwap_bearish_confluence and not (a_grade_short or b_grade_short)
  367.  
  368. // ════════════════════════════════════════════════════════════════════════════════
  369. // ────── VISUALIZATION (PLOTS, GRADES, LABELS) ──────
  370. // ════════════════════════════════════════════════════════════════════════════════
  371.  
  372. plot(show_session_vwap ? session_vwap : na, "Session VWAP", color=color.new(#2962FF, 0), linewidth=2)
  373. plot(show_rolling_vwap ? rolling_vwap : na, "Rolling VWAP", color=color.new(#00BFA5, 40), linewidth=1)
  374. plotshape(show_paa_pivots and not na(pivot_high_price), "PAA High", shape.triangledown, location.abovebar, color.new(color.red, 40), size=size.small, offset=-paa_lookback)
  375. plotshape(show_paa_pivots and not na(pivot_low_price), "PAA Low", shape.triangleup, location.belowbar, color.new(color.green, 40), size=size.small, offset=-paa_lookback)
  376. plot(trend_filter ? ema : na, "Trend EMA", color=uptrend ? color.new(color.green, 80) : color.new(color.red, 80), linewidth=2)
  377. bgcolor(trend_filter and show_trend_bgcolor ? (uptrend ? color.new(color.green, 95) : color.new(color.red, 95)) : na)
  378.  
  379. f_draw_paa_level(bar_pos, price, vol, is_valid, line_obj, label_obj) =>
  380. if is_valid and show_paa_pivots
  381. line.set_xy1(line_obj, bar_pos, price)
  382. line.set_xy2(line_obj, bar_index, price)
  383. label.set_xy(label_obj, bar_pos, price)
  384. label.set_text(label_obj, "Vol: " + str.tostring(vol, "#.##"))
  385. else
  386. line.set_xy1(line_obj, na, na)
  387. line.set_xy2(line_obj, na, na)
  388. label.set_xy(label_obj, na, na)
  389.  
  390. if paa_extend_lines
  391. f_draw_paa_level(paa_high_bar, paa_high, paa_high_vol, bar_index - paa_high_bar < paa_valid_bars, paa_high_line, paa_high_label)
  392. f_draw_paa_level(paa_low_bar, paa_low, paa_low_vol, bar_index - paa_low_bar < paa_valid_bars, paa_low_line, paa_low_label)
  393.  
  394. plotshape(show_a_grade_labels and a_grade_long, "A-Long", shape.labelup, location.belowbar, color.new(#00C853, 0), text="A+", textcolor=color.white, size=size.normal, offset=label_offset)
  395. plotshape(show_a_grade_labels and a_grade_short, "A-Short", shape.labeldown, location.abovebar, color.new(#D50000, 0), text="A+", textcolor=color.white, size=size.normal, offset=label_offset)
  396. plotshape(show_b_grade_labels and b_grade_long, "B-Long", shape.labelup, location.belowbar, color.new(#64DD17, 0), text="B", textcolor=color.black, size=size.small, offset=label_offset)
  397. plotshape(show_b_grade_labels and b_grade_short, "B-Short", shape.labeldown, location.abovebar, color.new(#FF6D00, 0), text="B", textcolor=color.white, size=size.small, offset=label_offset)
  398. plotshape(show_c_grade_labels and c_grade_long, "C-Long", shape.triangleup, location.belowbar, color.new(color.green, 70), size=size.tiny, offset=label_offset)
  399. plotshape(show_c_grade_labels and c_grade_short, "C-Short", shape.triangledown, location.abovebar, color.new(color.red, 70), size=size.tiny, offset=label_offset)
  400.  
  401. if show_risk_labels
  402. if a_grade_long or b_grade_long
  403. float stop_loss = low - (ltfAtr * sl_atr_mult)
  404. float entry_price = close
  405. float risk = entry_price - stop_loss
  406. if risk > 0
  407. float tp1 = entry_price + risk * rr_ratio_1
  408. label.new(bar_index, low - ta.tr, "SL: "+str.tostring(stop_loss,"#.##")+"\nTP1: "+str.tostring(tp1,"#.##"), style=label.style_label_up, color=color.new(#263238, 20), textcolor=color.white)
  409. if a_grade_short or b_grade_short
  410. float stop_loss = high + (ltfAtr * sl_atr_mult)
  411. float entry_price = close
  412. float risk = stop_loss - entry_price
  413. if risk > 0
  414. float tp1 = entry_price - risk * rr_ratio_1
  415. label.new(bar_index, high + ta.tr, "SL: "+str.tostring(stop_loss,"#.##")+"\nTP1: "+str.tostring(tp1,"#.##"), style=label.style_label_down, color=color.new(#263238, 20), textcolor=color.white)
  416.  
  417. // ════════════════════════════════════════════════════════════════════════════════
  418. // ────── LAST BAR LOGIC (FIBO, VP, DASHBOARD) ──────
  419. // ════════════════════════════════════════════════════════════════════════════════
  420.  
  421. if barstate.islast
  422. float band = fiboAtr * fibo_thickness
  423. float PP = (FH + FL + FC) / 3
  424. array.set(Fibo, 0, PP + 0.382 * (FH - FL))
  425. array.set(Fibo, 1, PP - 0.382 * (FH - FL))
  426. array.set(Fibo, 2, PP + 0.618 * (FH - FL))
  427. array.set(Fibo, 3, PP - 0.618 * (FH - FL))
  428. array.set(Fibo, 4, PP + (FH - FL))
  429. array.set(Fibo, 5, PP - (FH - FL))
  430. float FT = array.get(Fibo, 4)
  431. float FB = array.get(Fibo, 5)
  432.  
  433. int BAR1 = na(newWeekBarIndex) ? bar_index : math.min(bar_index - newWeekBarIndex, 4998)
  434. if BAR1 > 0
  435. float[] VPlevels = array.new_float(vp_levels + 1)
  436. float step = (FT - FB) / vp_levels
  437. for i = 0 to vp_levels by 1
  438. array.set(VPlevels, i, FB + step * i)
  439. float[] Volumes = array.new_float(vp_levels, 0.0)
  440. float[] BUllV = array.new_float(vp_levels, 0.0)
  441. float[] BEARV = array.new_float(vp_levels, 0.0)
  442. float totalGreenVol = 0.0
  443. float totalRedVol = 0.0
  444. float totalVol = 0.0
  445. for i = 0 to BAR1
  446. float close_i = close[i]
  447. float open_i = open[i]
  448. float volume_i = nz(volume[i])
  449. for x = 0 to vp_levels - 1
  450. if close_i > array.get(VPlevels, x) and close_i < array.get(VPlevels, x + 1)
  451. array.set(Volumes, x, array.get(Volumes, x) + volume_i)
  452. if close_i > open_i
  453. array.set(BUllV, x, array.get(BUllV, x) + volume_i)
  454. else
  455. array.set(BEARV, x, array.get(BEARV, x) + volume_i)
  456. break
  457. if close_i > open_i
  458. totalGreenVol += volume_i
  459. else
  460. totalRedVol += volume_i
  461. totalVol += volume_i
  462. float GR = totalVol > 0 ? totalGreenVol / totalVol : 0
  463. float RR = totalVol > 0 ? totalRedVol / totalVol : 0
  464.  
  465. if array.size(fiboBoxes) > 0
  466. for i=0 to array.size(fiboBoxes)-1
  467. box.delete(array.get(fiboBoxes, i))
  468. array.clear(fiboBoxes)
  469. box.delete(pocBox)
  470.  
  471. if show_fibo_levels
  472. for i = 0 to 5
  473. bool C = i == 0 or i == 2 or i == 4
  474. color fiboColor = C ? color.new(color.red,85) : color.new(color.blue,85)
  475. color fiboBorderColor = C ? color.new(color.red,60) : color.new(color.blue,60)
  476. array.push(fiboBoxes, box.new(newWeekTIME, array.get(Fibo, i) + band, time, array.get(Fibo, i) - band, bgcolor=fiboColor, border_color=fiboBorderColor, xloc=xloc.bar_time, extend=extend.right))
  477.  
  478. if show_vp
  479. if array.size(vpBoxes) > 0
  480. for i = 0 to array.size(vpBoxes) - 1
  481. box.delete(array.get(vpBoxes, i))
  482. array.clear(vpBoxes)
  483.  
  484. int profileStartTime = time + (vp_offset * timeframe.in_seconds(timeframe.period) * 1000)
  485. float max_volume = array.max(Volumes) > 0 ? array.max(Volumes) : 1
  486. float maxProfileWidthInBars = BAR1 * (vp_width / 100.0)
  487. for i = 0 to vp_levels - 1
  488. string str = vp_vol_type == "All" ? str.tostring(array.get(Volumes, i), format.volume) : vp_vol_type == "Bear" ? str.tostring(array.get(BEARV, i), format.volume) : str.tostring(array.get(BUllV, i), format.volume)
  489. float current_volume = array.get(Volumes, i)
  490. float barWidthInBars = (current_volume / max_volume) * maxProfileWidthInBars
  491. int barWidthMs = math.round(barWidthInBars * timeframe.in_seconds(timeframe.period) * 1000)
  492. color vpColor = color.from_gradient(current_volume, 0, max_volume, color.new(color.red,75), color.new(color.green,75))
  493. array.push(vpBoxes, box.new(profileStartTime, array.get(VPlevels, i + 1), profileStartTime + barWidthMs, array.get(VPlevels, i), xloc=xloc.bar_time, border_color=na, bgcolor=vpColor, text=str, text_color=color.white, text_size=size.tiny, text_halign=text.align_left))
  494.  
  495. if show_poc
  496. int MaxVID = array.indexof(Volumes, array.max(Volumes))
  497. pocBox := box.new(newWeekTIME, VPlevels.get(MaxVID) + band, time, VPlevels.get(MaxVID + 1) + band, #e0f2e93a, border_style=line.style_solid, bgcolor=color.new(#586161, 75), text="POC", text_color=chart.fg_color, text_halign=text.align_center, text_valign=text.align_center, xloc=xloc.bar_time, extend=extend.right)
  498.  
  499. var line targetLine = na
  500. var label targetLabel = na
  501. line.delete(targetLine)
  502. label.delete(targetLabel)
  503.  
  504. if (close < FT) and (close > FB) and show_fibo_target
  505. float targetY = na
  506. color targetColor = na
  507. if GR > RR
  508. if close > (FB + FT) / 2
  509. targetY := array.get(Fibo, 2)
  510. else
  511. targetY := array.get(Fibo, 0)
  512. targetColor := color.new(color.green, 20)
  513. else
  514. if close > (FB + FT) / 2
  515. targetY := array.get(Fibo, 1)
  516. else
  517. targetY := array.get(Fibo, 3)
  518. targetColor := color.new(color.red, 20)
  519. if not na(targetY)
  520. targetLine := line.new(bar_index, hl2, bar_index + 10, targetY, color=targetColor, style=line.style_arrow_right, width=1)
  521. targetLabel := label.new(bar_index + 10, targetY, "Possible Target", style=label.style_label_lower_left, color=targetColor, textcolor=color.white, size=size.small)
  522.  
  523. if show_dashboard
  524. var table info_table = table.new(position.top_right, 2, 10, bgcolor = color.new(#1e222d, 20), border_color = color.new(color.gray, 80))
  525. table.clear(info_table, 0, 0, 1, 9)
  526.  
  527. color bullColor = color.new(#00C853, 0)
  528. color bearColor = color.new(#D50000, 0)
  529. color labelColor = color.new(color.white, 50)
  530. color headerColor = color.new(color.white, 30)
  531.  
  532. table.cell(info_table, 0, 0, "Emerald Dash", 2, 1, bgcolor=color.new(color.gray, 90), text_color=color.white, text_halign=text.align_center, text_size=size.small)
  533.  
  534. table.cell(info_table, 0, 1, "Market Trend", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  535. table.cell(info_table, 1, 1, uptrend ? "BULLISH" : "BEARISH", text_color=(uptrend ? bullColor : bearColor), text_halign=text.align_right, text_size=size.small)
  536.  
  537. table.cell(info_table, 0, 2, "VWAP Position", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  538. table.cell(info_table, 1, 2, above_session_vwap ? "ABOVE" : "BELOW", text_color=(above_session_vwap ? bullColor : bearColor), text_halign=text.align_right, text_size=size.small)
  539.  
  540. table.cell(info_table, 0, 3, "Dist to VWAP", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  541. float dist_to_vwap = (close - session_vwap) / session_vwap * 100
  542. color dist_color = dist_to_vwap > 0.5 or dist_to_vwap < -0.5 ? color.new(color.orange, 20) : color.new(color.white, 40)
  543. table.cell(info_table, 1, 3, str.tostring(dist_to_vwap, "#.##") + "%", text_color=dist_color, text_halign=text.align_right, text_size=size.small)
  544.  
  545. table.cell(info_table, 0, 4, "Volume Rank", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  546. color volRankColor = volume_percentile > 80 ? bullColor : color.new(color.white, 40)
  547. table.cell(info_table, 1, 4, str.tostring(volume_percentile, "#") + "%", text_color=volRankColor, text_halign=text.align_right, text_size=size.small)
  548.  
  549. table.cell(info_table, 0, 5, "EMA Lineup", 2, 1, text_color=headerColor, text_halign=text.align_left, text_size=size.small)
  550.  
  551. table.cell(info_table, 0, 6, "4 H", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  552. table.cell(info_table, 1, 6, is_4h_bull ? "BULLISH" : "BEARISH", text_color=(is_4h_bull ? bullColor : bearColor), text_halign=text.align_right, text_size=size.small)
  553.  
  554. table.cell(info_table, 0, 7, "1 H", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  555. table.cell(info_table, 1, 7, is_1h_bull ? "BULLISH" : "BEARISH", text_color=(is_1h_bull ? bullColor : bearColor), text_halign=text.align_right, text_size=size.small)
  556.  
  557. table.cell(info_table, 0, 8, "15 M", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  558. table.cell(info_table, 1, 8, is_15m_bull ? "BULLISH" : "BEARISH", text_color=(is_15m_bull ? bullColor : bearColor), text_halign=text.align_right, text_size=size.small)
  559.  
  560. table.cell(info_table, 0, 9, "5 M", text_color=labelColor, text_halign=text.align_left, text_size=size.small)
  561. table.cell(info_table, 1, 9, is_5m_bull ? "BULLISH" : "BEARISH", text_color=(is_5m_bull ? bullColor : bearColor), text_halign=text.align_right, text_size=size.small)
  562.  
  563.  
  564. // ════════════════════════════════════════════════════════════════════════════════
  565. // ────── ALERT SYSTEM ──────
  566. // ════════════════════════════════════════════════════════════════════════════════
  567.  
  568. alertcondition(pre_alert and is_bullish_rejection and at_paa_low, "Potential Long Setup", "⚠️ POTENTIAL LONG: Rejection at PAA Support - Price: {{close}}")
  569. alertcondition(pre_alert and is_bearish_rejection and at_paa_high, "Potential Short Setup", "⚠️ POTENTIAL SHORT: Rejection at PAA Resistance - Price: {{close}}")
  570. alertcondition(alert_a_setups and a_grade_long, "A-Grade Long", "🟢 A-GRADE LONG: Trend+PAA+Confirmation - Price: {{close}}")
  571. alertcondition(alert_a_setups and a_grade_short, "A-Grade Short", "🔴 A-GRADE SHORT: Trend+PAA+Confirmation - Price: {{close}}")
  572. alertcondition(alert_b_setups and b_grade_long, "B-Grade Long", "🟡 B-GRADE LONG: Strong confluence - Price: {{close}}")
  573. alertcondition(alert_b_setups and b_grade_short, "B-Grade Short", "🟡 B-GRADE SHORT: Strong confluence - Price: {{close}}")
  574. alertcondition(alert_c_setups and c_grade_long, "C-Grade Long", "⚪ C-GRADE LONG: Basic setup - Price: {{close}}")
  575. alertcondition(alert_c_setups and c_grade_short, "C-Grade Short", "⚪ C-GRADE SHORT: Basic setup - Price: {{close}}")
  576. alertcondition(bullish_liquidity, "Bullish Liquidity Grab", "💧 BULLISH LIQUIDITY: Reclaimed level - Price: {{close}}")
  577. alertcondition(bearish_liquidity, "Bearish Liquidity Grab", "💧 BEARISH LIQUIDITY: Reclaimed level - Price: {{close}}")
Tags: pine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement