|
|
复制到pine脚本, 保存,添加即可使用;
采用吞没订单块策略。
//@version=5
indicator("订单块策略(仅供参考)", overlay=true, max_boxes_count=500, max_labels_count=500, max_lines_count=500)
// ———— SETTINGS ————
bull_ext_last = input.int(3, "Bullish OB", minval=1, inline="bull")
bg_bull_css = input.color(color.new(#ff1100, 80), "", inline="bull")
bull_css = input.color(#ff1100, "", inline="bull")
bull_avg_css = input.color(color.new(#9598a1, 37), "", inline="bull")
bear_ext_last = input.int(3, "Bearish OB", minval=1, inline="bear")
bg_bear_css = input.color(color.new(#169400, 80), "", inline="bear")
bear_css = input.color(#169400, "", inline="bear")
bear_avg_css = input.color(color.new(#9598a1, 37), "", inline="bear")
line_style = input.string("⎯⎯⎯", "Average Line Style", options=["⎯⎯⎯", "----", "····"])
line_width = input.int(1, "Average Line Width", minval=1)
mitigation = input.string("Wick", "Mitigation Methods", options=["Wick", "Close"])
// 吞没允许误差(默认0.2)
allow_error = input.float(0.2, "Engulfing Allow Error", step=0.1, minval=0.0)
label_bars_right = input.int(10, "Label Bars to the Right", minval=1)
// ———— 获取周期字符串 ————
get_interval() =>
mins = timeframe.multiplier
tf = timeframe.period
res = ""
if tf == "D"
res := "1D"
else if tf == "4H"
res := "4H"
else if tf == "1H"
res := "1H"
else if tf == "30"
res := "30m"
else if tf == "15"
res := "15m"
else if tf == "5"
res := "5m"
else if tf == "1"
res := "1m"
else
res := str.tostring(mins) + "m"
res
tf_label = get_interval()
// ———— FUNCTIONS ————
get_line_style(style) =>
switch style
"⎯⎯⎯" => line.style_solid
"----" => line.style_dashed
"····" => line.style_dotted
get_coordinates(condition, top, btm, ob_val)=>
var ob_top = array.new_float(0)
var ob_btm = array.new_float(0)
var ob_avg = array.new_float(0)
var ob_left = array.new_int(0)
var ob_mitigated = array.new_bool(0)
float ob = na
if condition
avg = math.avg(top, btm)
array.unshift(ob_top, top)
array.unshift(ob_btm, btm)
array.unshift(ob_avg, avg)
array.unshift(ob_left, time[1])
array.unshift(ob_mitigated, false)
ob := ob_val
[ob_top, ob_btm, ob_avg, ob_left, ob,ob_mitigated]
remove_mitigated(ob_top, ob_btm, ob_left, ob_avg, target, bull,ob_mitigated)=>
bool mitigated = false
len = array.size(ob_top)
if len > 0
int i = len - 1
while i >= 0
top_val = array.get(ob_top, i)
btm_val = array.get(ob_btm, i)
if (bull and target < btm_val) or (not bull and target > top_val)
mitigated := true
array.set(ob_mitigated,i, true)
i := i - 1
mitigated
set_order_blocks(ob_top, ob_btm, ob_left, ob_avg, ext_last, bg_css, border_css, lvl_css,ob_mitigated)=>
var ob_box = array.new_box(0)
var ob_lvl = array.new_line(0)
var ob_lbl = array.new_label(0)
if barstate.isfirst
for i = 0 to ext_last-1
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)))
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))
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))
if barstate.islast
array.clear(ob_box)
array.clear(ob_lvl)
array.clear(ob_lbl)
for i = 0 to ext_last-1
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)))
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))
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))
if array.size(ob_top) > 0
max_i = math.min(ext_last-1, array.size(ob_top)-1)
for i = 0 to max_i
bx = array.get(ob_box, i)
ln = array.get(ob_lvl, i)
lb = array.get(ob_lbl, i)
left_tm = array.get(ob_left, i)
y_center = array.get(ob_avg, i)
box.set_lefttop(bx, left_tm, array.get(ob_top, i))
box.set_rightbottom(bx, left_tm, array.get(ob_btm, i))
line.set_xy1(ln, left_tm, y_center)
line.set_xy2(ln, left_tm+1, y_center)
if(array.get(ob_mitigated, i))
label.set_xy(lb, time + label_bars_right * timeframe.multiplier * 60 * 1000, y_center)
label.set_text(lb, "broken")
// ———— KLINE PATTERN ————
bodyTop = math.max(open, close)
bodyBot = math.min(open, close)
// 看涨吞没(带允许误差),前一根K线为阴线,后一根K线为阳线,且阳线实体吞没阴线实体
bullish_pattern1 =
close[1] < open[1] and close > open and
bodyTop > bodyTop[1] - allow_error and
bodyBot < bodyBot[1] + allow_error
//十字星,前一根K线为十字星,后一根K线为阳线,且吞没前一根K线
bodyK = math.abs(close - open)
rangK = high - low
bullish_pattern2 = bodyK[1] < 0.1*rangK[1] and close > open and bodyTop > bodyTop[1] - allow_error and bodyBot < bodyBot[1] + allow_error
bullish_pattern = bullish_pattern1 or bullish_pattern2
// 看跌吞没(带允许误差),前一根K线为阳线,后一根K线为阴线,且阴线实体吞没阳线实体
bearish_pattern1 =
close[1] > open[1] and close < open and
bodyTop >= bodyTop[1] - allow_error and
bodyBot <= bodyBot[1] + allow_error
bearish_pattern2 = bodyK[1] < 0.1*rangK[1] and close < open and
bodyTop >= bodyTop[1] - allow_error and
bodyBot <= bodyBot[1] + allow_error
bearish_pattern = bearish_pattern1 or bearish_pattern2
target_bull = mitigation == "Close" ? close : low
target_bear = mitigation == "Close" ? close : high
// ———— ORDER BLOCKS ————
[bull_top, bull_btm, bull_avg, bull_left, bull_ob, bull_mitigated] = get_coordinates(bullish_pattern, high[1], low[1], low[1])
[bear_top, bear_btm, bear_avg, bear_left, bear_ob, bear_mitigated] = get_coordinates(bearish_pattern, high[1], low[1], high[1])
mitigated_bull = remove_mitigated(bull_top, bull_btm, bull_left, bull_avg, target_bull, true, bull_mitigated)
mitigated_bear = remove_mitigated(bear_top, bear_btm, bear_left, bear_avg, target_bear, false, bear_mitigated)
// ———— DRAW ————
set_order_blocks(bull_top, bull_btm, bull_left, bull_avg, bull_ext_last, bg_bull_css, bull_css, bull_avg_css,bull_mitigated)
set_order_blocks(bear_top, bear_btm, bear_left, bear_avg, bear_ext_last, bg_bear_css, bear_css, bear_avg_css,bear_mitigated)
// ———— ALERTS ————
alertcondition(bull_ob, "Bullish OB Formed", "Bullish order block detected")
alertcondition(bear_ob, "Bearish OB Formed", "Bearish order block detected")
alertcondition(mitigated_bull, "Bullish OB Mitigated", "Bullish order block mitigated")
alertcondition(mitigated_bear, "Bearish OB Mitigated", "Bearish order block mitigated")
// // ———— DEBUG 专用:支持回放,显示当前K线实体信息 ————
// var label debug_lbl = label.new(bar_index, high, "", style=label.style_label_left, size=size.normal)
// label.set_xy(debug_lbl, bar_index, high)
// 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])))
// label.set_color(debug_lbl, color.new(#000000, 80))
// label.set_textcolor(debug_lbl, color.black)
复制代码 |
|