214 lines
9.0 KiB
Plaintext
214 lines
9.0 KiB
Plaintext
//@version=6
|
|
indicator(title="裸K趋势反转识别", shorttitle="Naked K Reversal", overlay=true, max_labels_count=500)
|
|
|
|
// ========================= 输入参数 =========================
|
|
// 基础设置
|
|
showLabels = input.bool(true, "显示信号标记")
|
|
showBackground = input.bool(true, "显示背景着色")
|
|
showAlerts = input.bool(true, "启用告警")
|
|
|
|
// 裸K形态参数
|
|
pinbarThreshold = input.float(0.6, "PinBar阈值(0.6-0.8)", minval=0.3, maxval=0.9, step=0.05)
|
|
insideBarThreshold = input.float(0.8, "内包线阈值(0.7-0.9)", minval=0.5, maxval=0.95, step=0.05)
|
|
engulfingThreshold = input.float(0.5, "吞没线阈值(0.4-0.7)", minval=0.3, maxval=0.8, step=0.05)
|
|
|
|
// 趋势确认参数
|
|
trendConfirmationBars = input.int(3, "趋势确认根数", minval=1, maxval=10)
|
|
reversalConfirmationBars = input.int(2, "反转确认根数", minval=1, maxval=5)
|
|
|
|
// 成交量确认
|
|
useVolumeConfirmation = input.bool(true, "使用成交量确认")
|
|
volumeMultiplier = input.float(1.2, "成交量倍数", minval=1.0, maxval=3.0, step=0.1)
|
|
|
|
// 灵敏度
|
|
sensitivity = input.float(1.0, "灵敏度", minval=0.5, maxval=2.0, step=0.1)
|
|
|
|
// 颜色设置
|
|
colBullish = color.new(color.green, 80)
|
|
colBearish = color.new(color.red, 80)
|
|
colNeutral = color.new(color.gray, 85)
|
|
|
|
// ========================= 裸K形态识别 =========================
|
|
// 1. PinBar (锤子线/上吊线)
|
|
barRange = high - low
|
|
bodyRange = math.abs(close - open)
|
|
upperShadow = high - math.max(open, close)
|
|
lowerShadow = math.min(open, close) - low
|
|
|
|
isPinBar = barRange > 0 and bodyRange / barRange < pinbarThreshold
|
|
isBullishPinBar = isPinBar and lowerShadow > upperShadow * 2
|
|
isBearishPinBar = isPinBar and upperShadow > lowerShadow * 2
|
|
|
|
// 2. 内包线 (Inside Bar)
|
|
isInsideBar = high <= high[1] and low >= low[1]
|
|
insideBarStrength = (high[1] - low[1]) > 0 ? (high - low) / (high[1] - low[1]) : 1
|
|
isStrongInsideBar = isInsideBar and insideBarStrength < insideBarThreshold
|
|
|
|
// 3. 吞没线 (Engulfing)
|
|
isBullishEngulfing = close > open and close[1] < open[1] and
|
|
close > open[1] and open < close[1] and
|
|
bodyRange > bodyRange[1] * engulfingThreshold
|
|
|
|
isBearishEngulfing = close < open and close[1] > open[1] and
|
|
close < open[1] and open > close[1] and
|
|
bodyRange > bodyRange[1] * engulfingThreshold
|
|
|
|
// 4. 孕线 (Harami)
|
|
isBullishHarami = close > open and close[1] < open[1] and
|
|
high < high[1] and low > low[1] and
|
|
bodyRange < bodyRange[1] * 0.7
|
|
|
|
isBearishHarami = close < open and close[1] > open[1] and
|
|
high < high[1] and low > low[1] and
|
|
bodyRange < bodyRange[1] * 0.7
|
|
|
|
// 5. 乌云盖顶 (Dark Cloud Cover)
|
|
isDarkCloud = close < open and close[1] > open[1] and
|
|
open > close[1] and close < (open[1] + close[1]) / 2
|
|
|
|
// 6. 刺透形态 (Piercing Pattern)
|
|
isPiercing = close > open and close[1] < open[1] and
|
|
open < low[1] and close > (open[1] + close[1]) / 2
|
|
|
|
// ========================= 趋势判断 =========================
|
|
// 简单移动平均线趋势判断
|
|
emaFast = ta.ema(close, 10)
|
|
emaSlow = ta.ema(close, 20)
|
|
|
|
isUptrend = emaFast > emaSlow and close > emaSlow
|
|
isDowntrend = emaFast < emaSlow and close < emaSlow
|
|
|
|
// 趋势强度
|
|
var int uptrendCount = 0
|
|
var int downtrendCount = 0
|
|
|
|
uptrendCount := isUptrend ? uptrendCount + 1 : 0
|
|
downtrendCount := isDowntrend ? downtrendCount + 1 : 0
|
|
|
|
uptrendConfirmed = uptrendCount >= trendConfirmationBars
|
|
downtrendConfirmed = downtrendCount >= trendConfirmationBars
|
|
|
|
// ========================= 成交量确认 =========================
|
|
volumeMa = ta.sma(volume, 20)
|
|
volumeConfirmed = not useVolumeConfirmation or volume > volumeMa * volumeMultiplier
|
|
|
|
// ========================= 反转信号生成 =========================
|
|
// 看涨反转信号
|
|
bullishReversalSignal = (isBullishPinBar or isBullishEngulfing or isBullishHarami or isPiercing) and
|
|
downtrendConfirmed and volumeConfirmed
|
|
|
|
// 看跌反转信号
|
|
bearishReversalSignal = (isBearishPinBar or isBearishEngulfing or isBearishHarami or isDarkCloud) and
|
|
uptrendConfirmed and volumeConfirmed
|
|
|
|
// 反转确认计数
|
|
var int bullishReversalCount = 0
|
|
var int bearishReversalCount = 0
|
|
|
|
bullishReversalCount := bullishReversalSignal ? bullishReversalCount + 1 : 0
|
|
bearishReversalCount := bearishReversalSignal ? bearishReversalCount + 1 : 0
|
|
|
|
bullishReversalConfirmed = bullishReversalCount >= reversalConfirmationBars
|
|
bearishReversalConfirmed = bearishReversalCount >= reversalConfirmationBars
|
|
|
|
// ========================= 状态机 =========================
|
|
// 0: 无趋势, 1: 上涨趋势, -1: 下跌趋势, 2: 反转中
|
|
var int marketState = 0
|
|
prevState = nz(marketState[1], 0)
|
|
|
|
// 状态转换逻辑
|
|
int newState = prevState
|
|
|
|
if prevState == 0 // 无趋势状态
|
|
if uptrendConfirmed
|
|
newState := 1
|
|
else if downtrendConfirmed
|
|
newState := -1
|
|
|
|
else if prevState == 1 // 上涨趋势
|
|
if bearishReversalConfirmed
|
|
newState := -1
|
|
else if not uptrendConfirmed
|
|
newState := 0
|
|
|
|
else if prevState == -1 // 下跌趋势
|
|
if bullishReversalConfirmed
|
|
newState := 1
|
|
else if not downtrendConfirmed
|
|
newState := 0
|
|
|
|
marketState := barstate.isconfirmed ? newState : nz(marketState[1], prevState)
|
|
|
|
// ========================= 可视化 =========================
|
|
// 背景着色
|
|
bgColor = marketState == 1 ? colBullish : marketState == -1 ? colBearish : colNeutral
|
|
bgcolor(showBackground ? bgColor : na, title="市场状态背景")
|
|
|
|
// 裸K形态标记
|
|
plotshape(showLabels and isBullishPinBar, title="看涨PinBar", style=shape.triangleup,
|
|
location=location.belowbar, color=color.green, size=size.small)
|
|
plotshape(showLabels and isBearishPinBar, title="看跌PinBar", style=shape.triangledown,
|
|
location=location.abovebar, color=color.red, size=size.small)
|
|
|
|
plotshape(showLabels and isBullishEngulfing, title="看涨吞没", style=shape.circle,
|
|
location=location.belowbar, color=color.lime, size=size.small)
|
|
plotshape(showLabels and isBearishEngulfing, title="看跌吞没", style=shape.circle,
|
|
location=location.abovebar, color=color.maroon, size=size.small)
|
|
|
|
plotshape(showLabels and isStrongInsideBar, title="内包线", style=shape.diamond,
|
|
location=location.top, color=color.orange, size=size.small)
|
|
|
|
// 反转信号标记
|
|
plotshape(showLabels and bullishReversalConfirmed, title="看涨反转确认",
|
|
style=shape.labelup, location=location.belowbar, color=color.green,
|
|
text="↑", textcolor=color.white, size=size.normal)
|
|
|
|
plotshape(showLabels and bearishReversalConfirmed, title="看跌反转确认",
|
|
style=shape.labeldown, location=location.abovebar, color=color.red,
|
|
text="↓", textcolor=color.white, size=size.normal)
|
|
|
|
// 趋势线
|
|
plot(emaFast, color=color.new(color.blue, 0), linewidth=1, title="EMA快线")
|
|
plot(emaSlow, color=color.new(color.orange, 0), linewidth=2, title="EMA慢线")
|
|
|
|
// ========================= 告警系统 =========================
|
|
alertcondition(showAlerts and bullishReversalConfirmed, title="看涨反转信号",
|
|
message="裸K看涨反转信号确认")
|
|
alertcondition(showAlerts and bearishReversalConfirmed, title="看跌反转信号",
|
|
message="裸K看跌反转信号确认")
|
|
|
|
// ========================= 信息显示 =========================
|
|
// 在图表上显示当前状态
|
|
var table infoTable = table.new(position.top_right, 2, 6, bgcolor=color.new(color.gray, 90),
|
|
border_width=1)
|
|
|
|
if barstate.islast
|
|
table.cell(infoTable, 0, 0, "裸K趋势反转识别", text_color=color.white,
|
|
bgcolor=color.new(color.blue, 70))
|
|
table.cell(infoTable, 1, 0, "状态", text_color=color.white,
|
|
bgcolor=color.new(color.blue, 70))
|
|
|
|
table.cell(infoTable, 0, 1, "市场状态")
|
|
table.cell(infoTable, 1, 1, marketState == 1 ? "上涨趋势" : marketState == -1 ? "下跌趋势" : "震荡",
|
|
text_color=marketState == 1 ? color.green : marketState == -1 ? color.red : color.gray)
|
|
|
|
table.cell(infoTable, 0, 2, "趋势强度")
|
|
table.cell(infoTable, 1, 2, str.tostring(math.max(uptrendCount, downtrendCount)))
|
|
|
|
table.cell(infoTable, 0, 3, "看涨信号")
|
|
table.cell(infoTable, 1, 3, bullishReversalConfirmed ? "确认" : "等待",
|
|
text_color=bullishReversalConfirmed ? color.green : color.gray)
|
|
|
|
table.cell(infoTable, 0, 4, "看跌信号")
|
|
table.cell(infoTable, 1, 4, bearishReversalConfirmed ? "确认" : "等待",
|
|
text_color=bearishReversalConfirmed ? color.red : color.gray)
|
|
|
|
table.cell(infoTable, 0, 5, "成交量确认")
|
|
table.cell(infoTable, 1, 5, volumeConfirmed ? "是" : "否",
|
|
text_color=volumeConfirmed ? color.green : color.orange)
|
|
|
|
// ========================= 使用说明 =========================
|
|
// 本脚本专注于裸K形态识别趋势反转信号
|
|
// 主要识别形态:PinBar、吞没线、内包线、孕线、乌云盖顶、刺透形态
|
|
// 结合趋势确认和成交量验证,提高信号可靠性
|