【tradingview的订单块指标】自己写的

| 发表于 2026-5-11 22:28:17 | 显示全部楼层 |复制链接
复制到pine脚本, 保存,添加即可使用;
采用吞没订单块策略。
  1. //@version=5
  2. indicator("订单块策略(仅供参考)", overlay=true, max_boxes_count=500, max_labels_count=500, max_lines_count=500)
  3. // ———— SETTINGS ————
  4. bull_ext_last = input.int(3, "Bullish OB", minval=1, inline="bull")
  5. bg_bull_css = input.color(color.new(#ff1100, 80), "", inline="bull")
  6. bull_css = input.color(#ff1100, "", inline="bull")
  7. bull_avg_css = input.color(color.new(#9598a1, 37), "", inline="bull")
  8. bear_ext_last = input.int(3, "Bearish OB", minval=1, inline="bear")
  9. bg_bear_css = input.color(color.new(#169400, 80), "", inline="bear")
  10. bear_css = input.color(#169400, "", inline="bear")
  11. bear_avg_css = input.color(color.new(#9598a1, 37), "", inline="bear")
  12. line_style = input.string("⎯⎯⎯", "Average Line Style", options=["⎯⎯⎯", "----", "····"])
  13. line_width = input.int(1, "Average Line Width", minval=1)
  14. mitigation = input.string("Wick", "Mitigation Methods", options=["Wick", "Close"])
  15. // 吞没允许误差(默认0.2)
  16. allow_error = input.float(0.2, "Engulfing Allow Error", step=0.1, minval=0.0)
  17. label_bars_right = input.int(10, "Label Bars to the Right", minval=1)
  18. // ———— 获取周期字符串 ————
  19. get_interval() =>
  20.     mins = timeframe.multiplier
  21.     tf = timeframe.period
  22.     res = ""
  23.     if tf == "D"
  24.         res := "1D"
  25.     else if tf == "4H"
  26.         res := "4H"
  27.     else if tf == "1H"
  28.         res := "1H"
  29.     else if tf == "30"
  30.         res := "30m"
  31.     else if tf == "15"
  32.         res := "15m"
  33.     else if tf == "5"
  34.         res := "5m"
  35.     else if tf == "1"
  36.         res := "1m"
  37.     else
  38.         res := str.tostring(mins) + "m"
  39.     res
  40. tf_label = get_interval()
  41. // ———— FUNCTIONS ————
  42. get_line_style(style) =>
  43.     switch style
  44.         "⎯⎯⎯" => line.style_solid
  45.         "----" => line.style_dashed
  46.         "····" => line.style_dotted
  47. get_coordinates(condition, top, btm, ob_val)=>
  48.     var ob_top  = array.new_float(0)
  49.     var ob_btm  = array.new_float(0)
  50.     var ob_avg  = array.new_float(0)
  51.     var ob_left = array.new_int(0)
  52.     var ob_mitigated = array.new_bool(0)
  53.     float ob = na
  54.     if condition
  55.         avg = math.avg(top, btm)
  56.         array.unshift(ob_top, top)
  57.         array.unshift(ob_btm, btm)
  58.         array.unshift(ob_avg, avg)
  59.         array.unshift(ob_left, time[1])
  60.         array.unshift(ob_mitigated, false)
  61.         ob := ob_val
  62.     [ob_top, ob_btm, ob_avg, ob_left, ob,ob_mitigated]
  63. remove_mitigated(ob_top, ob_btm, ob_left, ob_avg, target, bull,ob_mitigated)=>
  64.     bool mitigated = false
  65.     len = array.size(ob_top)
  66.     if len > 0
  67.         int i = len - 1
  68.         while i >= 0
  69.             top_val = array.get(ob_top, i)
  70.             btm_val = array.get(ob_btm, i)
  71.             if (bull and target < btm_val) or (not bull and target > top_val)
  72.                 mitigated := true
  73.                 array.set(ob_mitigated,i, true)
  74.             i := i - 1
  75.     mitigated
  76. set_order_blocks(ob_top, ob_btm, ob_left, ob_avg, ext_last, bg_css, border_css, lvl_css,ob_mitigated)=>
  77.     var ob_box = array.new_box(0)
  78.     var ob_lvl = array.new_line(0)
  79.     var ob_lbl = array.new_label(0)
  80.     if barstate.isfirst
  81.         for i = 0 to ext_last-1
  82.             array.unshift(ob_box, box.new(na,na,na,na, xloc=xloc.bar_time, extend=extend.right, bgcolor=bg_css, border_color=color.new(border_css,70)))
  83.             array.unshift(ob_lvl, line.new(na,na,na,na, xloc=xloc.bar_time, extend=extend.right, color=lvl_css, style=get_line_style(line_style), width=line_width))
  84.             array.unshift(ob_lbl, label.new(na, na, "", xloc=xloc.bar_time, color=color.new(color.white, 50), textcolor=color.new(color.black, 50), style=label.style_label_center, size=size.normal))
  85.     if barstate.islast
  86.         array.clear(ob_box)
  87.         array.clear(ob_lvl)
  88.         array.clear(ob_lbl)
  89.         for i = 0 to ext_last-1
  90.             array.unshift(ob_box, box.new(na,na,na,na, xloc=xloc.bar_time, extend=extend.right, bgcolor=bg_css, border_color=color.new(border_css,70)))
  91.             array.unshift(ob_lvl, line.new(na,na,na,na, xloc=xloc.bar_time, extend=extend.right, color=lvl_css, style=get_line_style(line_style), width=line_width))
  92.             array.unshift(ob_lbl, label.new(na, na, "", xloc=xloc.bar_time, color=color.new(color.white, 50), textcolor=color.new(color.black, 50), style=label.style_label_center, size=size.normal))
  93.         if array.size(ob_top) > 0
  94.             max_i = math.min(ext_last-1, array.size(ob_top)-1)
  95.             for i = 0 to max_i
  96.                 bx = array.get(ob_box, i)
  97.                 ln = array.get(ob_lvl, i)
  98.                 lb = array.get(ob_lbl, i)
  99.                 left_tm = array.get(ob_left, i)
  100.                 y_center = array.get(ob_avg, i)
  101.                 box.set_lefttop(bx, left_tm, array.get(ob_top, i))
  102.                 box.set_rightbottom(bx, left_tm, array.get(ob_btm, i))
  103.                 line.set_xy1(ln, left_tm, y_center)
  104.                 line.set_xy2(ln, left_tm+1, y_center)
  105.                 if(array.get(ob_mitigated, i))
  106.                     label.set_xy(lb, time + label_bars_right * timeframe.multiplier * 60 * 1000, y_center)
  107.                     label.set_text(lb, "broken")
  108. // ———— KLINE PATTERN ————
  109. bodyTop = math.max(open, close)
  110. bodyBot = math.min(open, close)
  111. // 看涨吞没(带允许误差),前一根K线为阴线,后一根K线为阳线,且阳线实体吞没阴线实体
  112. bullish_pattern1 =
  113.   close[1] < open[1] and close > open and
  114.   bodyTop > bodyTop[1] - allow_error and
  115.   bodyBot < bodyBot[1] + allow_error
  116. //十字星,前一根K线为十字星,后一根K线为阳线,且吞没前一根K线
  117. bodyK = math.abs(close - open)
  118. rangK = high - low
  119. bullish_pattern2 = bodyK[1] < 0.1*rangK[1] and close > open and bodyTop > bodyTop[1] - allow_error and bodyBot < bodyBot[1] + allow_error
  120. bullish_pattern = bullish_pattern1 or bullish_pattern2
  121. // 看跌吞没(带允许误差),前一根K线为阳线,后一根K线为阴线,且阴线实体吞没阳线实体
  122. bearish_pattern1 =
  123.   close[1] > open[1] and close < open and
  124.   bodyTop >= bodyTop[1] - allow_error and
  125.   bodyBot <= bodyBot[1] + allow_error
  126. bearish_pattern2 = bodyK[1] < 0.1*rangK[1] and close < open and
  127.   bodyTop >= bodyTop[1] - allow_error and
  128.   bodyBot <= bodyBot[1] + allow_error
  129. bearish_pattern = bearish_pattern1 or bearish_pattern2
  130. target_bull = mitigation == "Close" ? close : low
  131. target_bear = mitigation == "Close" ? close : high
  132. // ———— ORDER BLOCKS ————
  133. [bull_top, bull_btm, bull_avg, bull_left, bull_ob, bull_mitigated] = get_coordinates(bullish_pattern, high[1], low[1], low[1])
  134. [bear_top, bear_btm, bear_avg, bear_left, bear_ob, bear_mitigated] = get_coordinates(bearish_pattern, high[1], low[1], high[1])
  135. mitigated_bull = remove_mitigated(bull_top, bull_btm, bull_left, bull_avg, target_bull, true, bull_mitigated)
  136. mitigated_bear = remove_mitigated(bear_top, bear_btm, bear_left, bear_avg, target_bear, false, bear_mitigated)
  137. // ———— DRAW ————
  138. set_order_blocks(bull_top, bull_btm, bull_left, bull_avg, bull_ext_last, bg_bull_css, bull_css, bull_avg_css,bull_mitigated)
  139. set_order_blocks(bear_top, bear_btm, bear_left, bear_avg, bear_ext_last, bg_bear_css, bear_css, bear_avg_css,bear_mitigated)
  140. // ———— ALERTS ————
  141. alertcondition(bull_ob, "Bullish OB Formed", "Bullish order block detected")
  142. alertcondition(bear_ob, "Bearish OB Formed", "Bearish order block detected")
  143. alertcondition(mitigated_bull, "Bullish OB Mitigated", "Bullish order block mitigated")
  144. alertcondition(mitigated_bear, "Bearish OB Mitigated", "Bearish order block mitigated")
  145. // // ———— DEBUG 专用:支持回放,显示当前K线实体信息 ————
  146. // var label debug_lbl = label.new(bar_index, high, "", style=label.style_label_left, size=size.normal)
  147. // label.set_xy(debug_lbl, bar_index, high)
  148. // label.set_text(debug_lbl,  "bodyTop    = " + str.tostring(bodyTop) + "\n" + "bodyBot    = " + str.tostring(bodyBot) + "\n" + "bodyTop[1] = " + str.tostring(bodyTop[1]) + "\n" +    "bodyBot[1] = " + str.tostring(bodyBot[1]) + "\n" +    "abs_top_diff = " + str.tostring(math.abs(bodyTop - bodyTop[1])) + "\n" +    "abs_bot_diff = " + str.tostring(math.abs(bodyBot - bodyBot[1])))
  149. // label.set_color(debug_lbl, color.new(#000000, 80))
  150. // label.set_textcolor(debug_lbl, color.black)
复制代码
举报

评论 使用道具

精彩评论6

zc123
DD
| 发表于 2026-5-11 23:23:00 | 显示全部楼层
举报

点赞 评论 使用道具

157142600
D
| 发表于 2026-5-13 13:43:14 | 显示全部楼层
订单块识别与Pine Script一致性:

吞没形态判断完全按照Pine脚本的逻辑,支持普通吞没和十字星吞没。

允许误差(EngulfError)实现为前一根K线波动的百分比。

订单块边界定义为吞没形态前一根K线的高/低(与Pine脚本中 high[1], low[1] 一致)。

均线取块区域的中点。

突破检测(Mitigation)未在绘制中体现,但已保存在结构体中。
举报

点赞 评论 使用道具

jackychan
D
| 发表于 2026-5-13 22:58:58 | 显示全部楼层
怎么用的
举报

点赞 评论 使用道具

zzzz0759
D
| 发表于 2026-6-7 22:00:32 | 显示全部楼层
谢谢分享,楼主能不能贴张图,这样直观很多
举报

点赞 评论 使用道具

zyl51314
D
| 发表于 2026-6-9 13:11:54 | 显示全部楼层
看起来很不错 我去试试看
举报

点赞 评论 使用道具

2623885357
DD
| 发表于 2026-6-12 03:35:44 | 显示全部楼层
MT4编译有错误,楼主改改呗
举报

点赞 评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册