INVITE-ONLY SCRIPT

悟隐高阶战法

36
//version=5
indicator("悟隐高阶战法", shorttitle='HT', overlay=true, max_labels_count=500, max_bars_back=5000)

// 输入参数
count_bull = input.int(3, title='非连续阳线数量', group='设置')
count_bear = input.int(3, title='非连续阴线数量', group='设置')
show_points = input.bool(true, title='显示高低点')
show_lines = input.bool(true,title = '显示线条')
max_points = input.int(200, title='最大点数')
line_color = input.color(color.purple, "连线颜色")
line_width = input.int(2, "线宽", minval=1, maxval=4)
h_color=input.color(color.red,title = 'H点颜色')
h_trs=input.float(80,title = 'h点透明度')
l_color=input.color(color.green,title = 'L点颜色')
l_trs=input.float(80,title = 'L点透明度')

// 警报设置
enable_alerts = input.bool(true, title='启用警报', group='警报设置')
show_signals = input.bool(true, title='显示BUY/SELL标记', group='信号显示设置')
signal_size = input.string('normal', title='信号标记大小', options=['tiny', 'small', 'normal', 'large', 'huge'], group='信号显示设置')
signal_distance = input.float(0.1, title='信号标记距K线距离(%)', minval=0.05, maxval=0.5, group='信号显示设置')

// 各信号显示开关
show_signal_yyx = input.bool(true, title='显示看跌【阴阳阴】', group='信号开关')
show_signal_ayy = input.bool(true, title='显示看涨【阳阴阳】', group='信号开关')
show_signal_bull_1v2 = input.bool(true, title='显示看涨【一大于二】', group='信号开关')
show_signal_bear_1v2 = input.bool(true, title='显示看跌【一大于二】', group='信号开关')
show_signal_bear_2v2 = input.bool(true, title='显示看跌【二大于二】', group='信号开关')
show_signal_bull_2v2 = input.bool(true, title='显示看涨【二大于二】', group='信号开关')
show_signal_bull_engulf = input.bool(true, title='显示看涨吞没【吞没随阴线】', group='信号开关')
show_signal_bear_engulf = input.bool(true, title='显示看跌【吞没随阳线】', group='信号开关')

// 子条件显示开关
show_signal_bear_2v2_c1 = input.bool(true, title='看跌【二大于二】-条件1', group='子条件开关')
show_signal_bear_2v2_c2 = input.bool(true, title='看跌【二大于二】-条件2', group='子条件开关')
show_signal_bull_2v2_c1 = input.bool(true, title='看涨【二大于二】-条件1', group='子条件开关')
show_signal_bull_2v2_c2 = input.bool(true, title='看涨【二大于二】-条件2', group='子条件开关')

// ------------------ 自定义数据类型 ------------------
type RecordPrice
int bar_index
float open_price
float high_price
float low_price
float close_price

type Extremum
chart.point pt
string kind // "high" 或 "low"

type ABCPoints
float price_a
int index_a
float price_b
int index_b
float price_c
int index_c
string trend_type // "bull" 或 "bear"
bool broken // 是否被突破

// ------------------ 数组初始化 ------------------
var bullRecords = array.new<RecordPrice>()
var bearRecords = array.new<RecordPrice>()
var allPoints = array.new<Extremum>() // 存储所有高低点
var label[] pointLabels = array.new<label>() // 存储绘制的标签,方便清理

var abcBullPoints = ABCPoints.new() // 存储牛市ABC点
var abcBearPoints = ABCPoints.new() // 存储熊市ABC点
// 存储黄线和标签的引用
var line[] yellowLines = array.new<line>()
var label[] yellowLabels = array.new<label>()

// 存储绿线和标签的引用
var line[] greenLines = array.new<line>()
var label[] greenLabels = array.new<label>()

// 突破状态变量
var bool cBroken = false
var int breakBarIndex = na

var bool fBroken = false
var int breakBarIndexBear = na

// 信号检测变量
var string currentSignal = ""
var string alertMessage = ""
var bool buySignal = false
var bool sellSignal = false

// ------------------ 状态变量 ------------------
var string lastPosition = "none"
var float lastHigh = na
var int lastHighBarIndex = na
var float lastLow = na
var int lastLowBarIndex = na

var float currentHigh = 0.0
var float currentLow = 0.0
var int currentHighBar = 0
var int currentLowBar = 0

// 使用Map存储每根K线是否创新高/新低
var map<int, bool> newHighMap = map.new<int, bool>()
var map<int, bool> newLowMap = map.new<int, bool>()

// 初始化
if barstate.isfirst
currentHigh := high
currentLow := low
currentHighBar := bar_index
currentLowBar := bar_index

// 更新区间高低并标记
bool isNewHigh = false
bool isNewLow = false

if high > currentHigh
currentHigh := high
currentHighBar := bar_index
isNewHigh := true
if low < currentLow
currentLow := low
currentLowBar := bar_index
isNewLow := true

// 存储当前K线的标记
map.put(newHighMap, bar_index, isNewHigh)
map.put(newLowMap, bar_index, isNewLow)

// ------------------ 方法 ------------------
method putRecord(array<RecordPrice> arr, int bar_idx, float open_pr, float high_pr, float low_pr, float close_pr) =>
newRecord = RecordPrice.new()
newRecord.bar_index := bar_idx
newRecord.open_price := open_pr
newRecord.high_price := high_pr
newRecord.low_price := low_pr
newRecord.close_price := close_pr
array.push(arr, newRecord)

method getRecord(array<RecordPrice> arr, int index) =>
if array.size(arr) > index and index >= 0
array.get(arr, index)
else
na

method removeRecord(array<RecordPrice> arr, int index) =>
if array.size(arr) > index and index >= 0
array.remove(arr, index)

method getLastRecord(array<RecordPrice> arr) =>
int size = array.size(arr)
if size > 0
array.get(arr, size - 1)
else
na

method isIncreasingLastN(array<RecordPrice> arr, int N) =>
if array.size(arr) < N
false
else
inc = true
for i = array.size(arr) - N to array.size(arr) - 2
prev = arr.getRecord(i)
curr = arr.getRecord(i + 1)
if curr.high_price <= prev.high_price
inc := false
break
inc

method isDecreasingLastN(array<RecordPrice> arr, int N) =>
if array.size(arr) < N
false
else
dec = true
for i = array.size(arr) - N to array.size(arr) - 2
prev = arr.getRecord(i)
curr = arr.getRecord(i + 1)
if curr.low_price >= prev.low_price
dec := false
break
dec

// 删除所有黄线和标签的方法
deleteYellowLinesAndLabels() =>
// 删除黄线
if array.size(yellowLines) > 0
for i = 0 to array.size(yellowLines) - 1
line l = array.get(yellowLines, i)
if not na(l)
line.delete(l)
array.clear(yellowLines)

// 删除标签
if array.size(yellowLabels) > 0
for i = 0 to array.size(yellowLabels) - 1
label lab = array.get(yellowLabels, i)
if not na(lab)
label.delete(lab)
array.clear(yellowLabels)

// 删除所有绿线和标签的方法
deleteGreenLinesAndLabels() =>
// 删除绿线
if array.size(greenLines) > 0
for i = 0 to array.size(greenLines) - 1
line l = array.get(greenLines, i)
if not na(l)
line.delete(l)
array.clear(greenLines)

// 删除标签
if array.size(greenLabels) > 0
for i = 0 to array.size(greenLabels) - 1
label lab = array.get(greenLabels, i)
if not na(lab)
label.delete(lab)
array.clear(greenLabels)

// 辅助函数:判断K线类型
isBullish(int offset = 0) =>
close[offset] > open[offset]

isBearish(int offset = 0) =>
close[offset] < open[offset]

// 辅助函数:获取当前趋势
getCurrentTrend() =>
if lastPosition == "low"
"uptrend" // 上涨趋势
else if lastPosition == "high"
"downtrend" // 下跌趋势
else
"none"

// 辅助函数:检查指定偏移的K线是否创过新高
wasNewHigh(int offset) =>
int targetBarIndex = bar_index - offset
bool result = false
if map.contains(newHighMap, targetBarIndex)
result := map.get(newHighMap, targetBarIndex)
result

// 辅助函数:检查指定偏移的K线是否创过新低
wasNewLow(int offset) =>
int targetBarIndex = bar_index - offset
bool result = false
if map.contains(newLowMap, targetBarIndex)
result := map.get(newLowMap, targetBarIndex)
result

// 辅助函数:检查过去N根K线中是否有创新高
hasNewHigh(int lookback = 3) =>
bool newHigh = false
for i = 0 to lookback - 1
if wasNewHigh(i)
newHigh := true
break
newHigh

// 辅助函数:检查过去N根K线中是否有创新低
hasNewLow(int lookback = 3) =>
bool newLow = false
for i = 0 to lookback - 1
if wasNewLow(i)
newLow := true
break
newLow

// 辅助函数:在过去5根K线中检查阴阳阴组合且有新高(相对于趋势起点)
checkBearishYYXPattern() =>
bool hasPattern = false
bool hasHighInRange = false

// 检查过去5根K线中是否有创新高
for i = 0 to 4
if wasNewHigh(i)
hasHighInRange := true
break

if hasHighInRange
// 检查过去3根K线的阴阳阴组合
if isBearish(0) and isBullish(1) and isBearish(2)
hasPattern := true
hasPattern

// 辅助函数:在过去5根K线中检查阳阴阳组合且有新低(相对于趋势起点)
checkBullishAYYPattern() =>
bool hasPattern = false
bool hasLowInRange = false

// 检查过去5根K线中是否有创新低
for i = 0 to 4
if wasNewLow(i)
hasLowInRange := true
break

if hasLowInRange
// 检查过去3根K线的阳阴阳组合
if isBullish(0) and isBearish(1) and isBullish(2)
hasPattern := true

hasPattern

// 辅助函数:检查是否是看涨吞没形态
isBullishEngulf(int offset = 0) =>
// 当前K线是阳线
isBullish(offset) and
// 吞没前一根K线
high[offset] > high[offset + 1] and low[offset] < low[offset + 1] and
// 确保只吞没前一根,不吞没前二根
not (high[offset] > high[offset + 2] and low[offset] < low[offset + 2])

// 辅助函数:检查是否是看跌吞没形态
isBearishEngulf(int offset = 0) =>
// 当前K线是阴线
isBearish(offset) and
// 吞没前一根K线
high[offset] > high[offset + 1] and low[offset] < low[offset + 1] and
// 确保只吞没前一根,不吞没前二根
not (high[offset] > high[offset + 2] and low[offset] < low[offset + 2])

// ------------------ 阳线/阴线处理 ------------------
if close > open
bullRecords.putRecord(bar_index, open, high, low, close)
if array.size(bullRecords) > count_bull
bullRecords.removeRecord(0)

if close < open
bearRecords.putRecord(bar_index, open, high, low, close)
if array.size(bearRecords) > count_bear
bearRecords.removeRecord(0)


// 做一个统计 ,记录当时的高价
var int count_long=na
var int count_short=na
var float high_price=na
var float low_price=na

// ------------------ 寻找低点 ------------------
if array.size(bullRecords) >= count_bull and bullRecords.isIncreasingLastN(count_bull) and close > open
if (lastPosition == "none" or lastPosition == "high") and low!=currentLow and bullRecords.first().bar_index >= currentLowBar
lastPosition := "low"
lastLow := currentLow
lastLowBarIndex := currentLowBar

if array.size(allPoints) >= max_points
array.shift(allPoints)
array.push(allPoints, Extremum.new(chart.point.from_index(lastLowBarIndex, lastLow), "low"))

currentHigh := high
currentHighBar := bar_index
count_long:=0
high_price:=high

// ------------------ 寻找高点 ------------------
if array.size(bearRecords) >= count_bear and bearRecords.isDecreasingLastN(count_bear) and close < open
if (lastPosition == "none" or lastPosition == "low") and high!=currentHigh and bearRecords.first().bar_index >= currentHighBar
lastPosition := "high"
lastHigh := currentHigh
lastHighBarIndex := currentHighBar

if array.size(allPoints) >= max_points
array.shift(allPoints)
array.push(allPoints, Extremum.new(chart.point.from_index(lastHighBarIndex, lastHigh), "high"))

currentLow := low
currentLowBar := bar_index
count_short:=0
low_price:=low


// ------------------ 绘制 ------------------
if barstate.islast and array.size(allPoints) > 1
// 删除旧的标签
for l in pointLabels
label.delete(l)
array.clear(pointLabels)

// 历史折线
if array.size(allPoints) > 2 and show_lines
tmpPts = array.new<chart.point>()
for i = 0 to array.size(allPoints)-2
ex = array.get(allPoints, i)
array.push(tmpPts, ex.pt)
polyline.new(tmpPts, xloc=xloc.bar_index, line_color=line_color, line_width=line_width)

// 最后一段线
if array.size(allPoints) >= 2 and show_lines
ex1 = array.get(allPoints, array.size(allPoints)-2)
ex2 = array.get(allPoints, array.size(allPoints)-1)
line.new(x1=ex1.pt.index, y1=ex1.pt.price, x2=ex2.pt.index, y2=ex2.pt.price,
xloc=xloc.bar_index, color=line_color, width=line_width)

// 高低点标签
if show_points
for i = 0 to array.size(allPoints)-1
ex = array.get(allPoints, i)
label lbl = na
if ex.kind == "low"
lbl := label.new(ex.pt.index, ex.pt.price, text=str.tostring(ex.pt.price),
style=label.style_label_up, color=color.new(color.green,80), textcolor=color.white)
else
lbl := label.new(ex.pt.index, ex.pt.price, text=str.tostring(ex.pt.price),
style=label.style_label_down, color=color.new(color.red,80), textcolor=color.white)
array.push(pointLabels,lbl)

// ------------------ 虚线预测段 ------------------
var line lastDashedLine = na
var label lastDashedLabel = na

if barstate.islast and array.size(allPoints) > 0
lastEx = array.get(allPoints, array.size(allPoints)-1)

if lastEx.kind == "low" and show_lines
if not na(lastDashedLine)
line.delete(lastDashedLine)
lastDashedLine := line.new(lastEx.pt.index, lastEx.pt.price, currentHighBar, currentHigh,
xloc=xloc.bar_index, color=line_color, width=line_width, style=line.style_dashed)
if show_points
if not na(lastDashedLabel)
label.delete(lastDashedLabel)
lastDashedLabel := label.new(lastEx.pt.index, lastEx.pt.price, text='H',
style=label.style_label_down, color=color.new(h_color,h_trs), textcolor=color.white)

if lastEx.kind == "high" and show_lines
if not na(lastDashedLine)
line.delete(lastDashedLine)
lastDashedLine := line.new(lastEx.pt.index, lastEx.pt.price, currentLowBar, currentLow,
xloc=xloc.bar_index, color=line_color, width=line_width, style=line.style_dashed)
if show_points
if not na(lastDashedLabel)
label.delete(lastDashedLabel)
lastDashedLabel := label.new(currentLowBar, currentLow, text='L',
style=label.style_label_up, color=color.new(l_color,l_trs), textcolor=color.white)

// ------------------ 信号检测逻辑 ------------------
// 重置信号
buySignal := false
sellSignal := false
currentSignal := ""
alertMessage := ""

if enable_alerts and bar_index >= 2
// 获取当前趋势
trend = getCurrentTrend()

// 信号1:看跌【阴阳阴】- 上涨趋势中,过去5根K线中有新高且出现阴线-阳线-阴线组合
if show_signal_yyx and trend == "uptrend" and checkBearishYYXPattern()
sellSignal := true
currentSignal := "SELL"
alertMessage := "看跌【阴阳阴】"

// 信号2:看涨【阳阴阳】- 下跌趋势中,过去5根K线中有新低且出现阳线-阴线-阳线组合
if show_signal_ayy and trend == "downtrend" and checkBullishAYYPattern()
buySignal := true
currentSignal := "BUY"
alertMessage := "看涨【阳阴阳】"

// 信号3:看涨【一大于二】- 下跌趋势中创新低,出现阳线,左边2根K至少有一根阴线
if show_signal_bull_1v2 and trend == "downtrend" and isBullish(0) and (isBearish(1) or isBearish(2)) and hasNewLow(3)
// 条件1:阳线实体最高价(收盘价)大于左边2根K实体最高价且实体最低价(开盘价)小于左边2根K实体最低价
condition1 = close > math.max(close[1], open[1]) and close > math.max(close[2], open[2]) and
open < math.min(close[1], open[1]) and open < math.min(close[2], open[2])

// 条件2:阳线最高价分别大于左边2根K最高价且最低价分别小于左边2根K最低价
condition2 = high > high[1] and high > high[2] and low < low[1] and low < low[2]

if condition1 or condition2
buySignal := true
currentSignal := "BUY"
alertMessage := "看涨【一大于二】"

// 信号4:看跌【一大于二】- 上涨趋势中,出现阴线,左边2根K至少有一根阳线
if show_signal_bear_1v2 and trend == "uptrend" and isBearish(0) and (isBullish(1) or isBullish(2)) and hasNewHigh(3)
// 条件1:阴线实体最高价(开盘价)大于左边2根K实体最高价且实体最低价(收盘价)小于左边2根K实体最低价
condition1 = open > math.max(close[1], open[1]) and open > math.max(close[2], open[2]) and
close < math.min(close[1], open[1]) and close < math.min(close[2], open[2])

// 条件2:阴线最高价分别大于左边2根K最高价且最低价分别小于左边2根K最低价
condition2 = high > high[1] and high > high[2] and low < low[1] and low < low[2]

if condition1 or condition2
sellSignal := true
currentSignal := "SELL"
alertMessage := "看跌【一大于二】"

// 信号5:看跌【二大于二】- 上涨趋势中
if show_signal_bear_2v2 and trend == "uptrend" and bar_index >= 3
// 条件1:连续2根阴线收盘价低于左边2根K线,且4根K线前3根必须有一根创新高(相对于趋势起点)
condition1 = show_signal_bear_2v2_c1 and isBearish(0) and isBearish(1) and
close < close[2] and close < close[3] and close[1] < close[2] and close[1] < close[3] and
(wasNewHigh(1) or wasNewHigh(2) or wasNewHigh(3))

// 条件2:连续2根阳线后再连续2根阴线,2根阴线最低价低于前面2根阳线最低价,最高价大于前面2根阳线最高价,中间2根K必须有一根创新高(相对于趋势起点)
condition2 = show_signal_bear_2v2_c2 and isBearish(0) and isBearish(1) and isBullish(2) and isBullish(3) and
math.min(low, low[1]) < math.min(low[2], low[3]) and
math.max(high, high[1]) > math.max(high[2], high[3]) and
(wasNewHigh(1) or wasNewHigh(2))


if condition1 or condition2
sellSignal := true
currentSignal := "SELL"
alertMessage := "看跌【二大于二】" + (condition1 ? "-条件1" : "") + (condition2 ? "-条件2" : "")

// 信号6:看涨【二大于二】- 下跌趋势中
if show_signal_bull_2v2 and trend == "downtrend" and bar_index >= 3
// 条件1:连续2根阳线收盘价高于左边2根K线,且4根K线前3根必须有一根创新低(相对于趋势起点)
condition1 = show_signal_bull_2v2_c1 and isBullish(0) and isBullish(1) and
close > close[2] and close > close[3] and close[1] > close[2] and close[1] > close[3] and
(wasNewLow(1) or wasNewLow(2) or wasNewLow(3))

// 条件2:连续2根阴线后再连续2根阳线,2根阳线最低价低于前面2根阴线最低价,最高价大于前面2根阴线最高价,中间2根K必须有一根创新低(相对于趋势起点)
condition2 = show_signal_bull_2v2_c2 and isBullish(0) and isBullish(1) and isBearish(2) and isBearish(3) and
math.min(low, low[1]) < math.min(low[2], low[3]) and
math.max(high, high[1]) > math.max(high[2], high[3]) and
(wasNewLow(1) or wasNewLow(2))


if condition1 or condition2
buySignal := true
currentSignal := "BUY"
alertMessage := "看涨【二大于二】" + (condition1 ? "-条件1" : "") + (condition2 ? "-条件2" : "")

// 信号7:看跌【吞没随阴线】- 上涨趋势中阳线吞没前一根K线,阳线后出现阴线,且有一根K创新高
if show_signal_bull_engulf and trend == "uptrend" and bar_index >= 2 and
isBullishEngulf(1) and isBearish(0) and hasNewHigh(3)
sellSignal := true
currentSignal := "SELL"
alertMessage := "看跌【吞没随阴线】"

// 信号8:看涨【吞没随阳线】- 下跌趋势中阴线吞没前一根K线,阴线后出现阳线,且有一根K创新低
if show_signal_bear_engulf and trend == "downtrend" and bar_index >= 2 and
isBearishEngulf(1) and isBullish(0) and hasNewLow(3)
buySignal := true
currentSignal := "BUY"
alertMessage := "看涨【吞没随阳线】"

// ------------------ BUY/SELL标记显示 ------------------
if show_signals
// 根据signal_size设置标记大小
labelSize = switch signal_size
"tiny" => size.tiny
"small" => size.small
"normal" => size.normal
"large" => size.large
"huge" => size.huge
=> size.normal

// 显示BUY信号
if buySignal
label.new(bar_index, low - (high - low) * signal_distance, text="BUY",
style=label.style_label_up, color=color.new(color.green, 0),
textcolor=color.white, size=labelSize, tooltip=alertMessage)

// 显示SELL信号
if sellSignal
label.new(bar_index, high + (high - low) * signal_distance, text="SELL",
style=label.style_label_down, color=color.new(color.red, 0),
textcolor=color.white, size=labelSize, tooltip=alertMessage)

// ------------------ TradingView警报条件 ------------------
// 检查是否有启用的买入/卖出信号(包括子条件)
enabledBuySignals = show_signal_ayy or
(show_signal_bull_1v2 ) or
(show_signal_bull_2v2 and (show_signal_bull_2v2_c1 or show_signal_bull_2v2_c2)) or
show_signal_bear_engulf

enabledSellSignals = show_signal_yyx or
(show_signal_bear_1v2) or
(show_signal_bear_2v2 and (show_signal_bear_2v2_c1 or show_signal_bear_2v2_c2)) or
show_signal_bull_engulf

// 买入信号警报
alertcondition(enable_alerts and enabledBuySignals and buySignal, title="买入信号", message="{{alertMessage}}")

// 卖出信号警报
alertcondition(enable_alerts and enabledSellSignals and sellSignal, title="卖出信号", message="{{alertMessage}}")

// 所有信号的通用警报
alertcondition(enable_alerts and (enabledBuySignals and buySignal or enabledSellSignals and sellSignal), title="所有交易信号",
message="信号类型: {{currentSignal}}, 详情: {{alertMessage}}")

// 具体的信号类型警报
alertcondition(enable_alerts and show_signal_ayy and buySignal and str.contains(alertMessage, "阳阴阳"), title="看涨【阳阴阳】", message="看涨【阳阴阳】")
alertcondition(enable_alerts and show_signal_yyx and sellSignal and str.contains(alertMessage, "阴阳阴"), title="看跌【阴阳阴】", message="看跌【阴阳阴】")
alertcondition(enable_alerts and show_signal_bull_1v2 and buySignal and str.contains(alertMessage, "一大于二"), title="看涨【一大于二】", message="看涨【一大于二】")
alertcondition(enable_alerts and show_signal_bear_1v2 and sellSignal and str.contains(alertMessage, "一大于二"), title="看跌【一大于二】", message="看跌【一大于二】")
alertcondition(enable_alerts and show_signal_bull_2v2 and buySignal and str.contains(alertMessage, "二大于二"), title="看涨【二大于二】", message="看涨【二大于二】")
alertcondition(enable_alerts and show_signal_bear_2v2 and sellSignal and str.contains(alertMessage, "二大于二"), title="看跌【二大于二】", message="看跌【二大于二】")
alertcondition(enable_alerts and show_signal_bull_engulf and sellSignal and str.contains(alertMessage, "吞没随阴线"), title="看涨吞没【吞没随阴线】", message="看涨吞没【吞没随阴线】")
alertcondition(enable_alerts and show_signal_bear_engulf and buySignal and str.contains(alertMessage, "吞没随阳线"), title="看跌【吞没随阳线】", message="看跌【吞没随阳线】")

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.