Files
tradingview-pine/srbr1.pine

317 lines
16 KiB
Plaintext
Raw Normal View History

2025-08-02 04:03:35 +00:00
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartPrime
//@version=6
indicator('Support and Resistance (High Volume Boxes) [ChartPrime]2', shorttitle = 'SR Breaks and Retests [ChartPrime]2', overlay = true, max_boxes_count = 50)
// ---------------------------------------------------------------------------------------------------------------------}
// 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎
// ---------------------------------------------------------------------------------------------------------------------{
int lookbackPeriod = input.int(20, 'Lookback Period', minval = 1, group = 'Settings')
int vol_len = input.int(2, 'Delta Volume Filter Length', tooltip = 'Higher input, will filter low volume boxes', group = 'Settings')
float box_withd = input.float(1, 'Adjust Box Width', maxval = 1000, minval = 0, step = 0.1)
// ---------------------------------------------------------------------------------------------------------------------}
// 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
// ---------------------------------------------------------------------------------------------------------------------{
// Delta Volume Function
upAndDownVolume() =>
posVol = 0.0
negVol = 0.0
var isBuyVolume = true
switch
close > open =>
isBuyVolume := true
isBuyVolume
close < open =>
isBuyVolume := false
isBuyVolume
if isBuyVolume
posVol := posVol + volume
posVol
else
negVol := negVol - volume
negVol
posVol + negVol
// Function to identify support and resistance boxes
calcSupportResistance(src, lookbackPeriod) =>
// Volume
Vol = upAndDownVolume()
vol_hi = ta.highest(Vol / 2.5, vol_len)
vol_lo = ta.lowest(Vol / 2.5, vol_len)
var float supportLevel = na
var float supportLevel_1 = na
var float resistanceLevel = na
var float resistanceLevel_1 = na
var box sup = na
var box res = na
var color res_color = na
var color sup_color = na
var float multi = na
var bool brekout_res = false
var bool brekout_sup = false
var bool res_holds = false
var bool sup_holds = false
// Find pivot points
pivotHigh = ta.pivothigh(src, lookbackPeriod, lookbackPeriod)
pivotLow = ta.pivotlow(src, lookbackPeriod, lookbackPeriod)
// Box width
atr = ta.atr(200)
withd = atr * box_withd
// Volume range for color gradient
vol_highest = ta.highest(Vol, 25)
vol_lowest = ta.lowest(Vol, 25)
// Find support levels with Positive Volume
if not na(pivotLow) and Vol > vol_hi
supportLevel := pivotLow
supportLevel_1 := supportLevel - withd
topLeft = chart.point.from_index(bar_index - lookbackPeriod, supportLevel)
bottomRight = chart.point.from_index(bar_index, supportLevel_1)
sup_color := color.from_gradient(Vol, 0, vol_highest, color(na), color.new(color.green, 30))
sup := box.new(top_left = topLeft, bottom_right = bottomRight, border_color = color.green, border_width = 1, bgcolor = sup_color, text = 'Vol: ' + str.tostring(math.round(Vol, 2)), text_color = chart.fg_color, text_size = size.small)
sup
// Find resistance levels with Negative Volume
if not na(pivotHigh) and Vol < vol_lo
resistanceLevel := pivotHigh
resistanceLevel_1 := resistanceLevel + withd
topLeft = chart.point.from_index(bar_index - lookbackPeriod, resistanceLevel)
bottomRight = chart.point.from_index(bar_index, resistanceLevel_1)
res_color := color.from_gradient(Vol, vol_lowest, 0, color.new(color.red, 30), color(na))
res := box.new(top_left = topLeft, bottom_right = bottomRight, border_color = color.red, border_width = 1, bgcolor = res_color, text = 'Vol: ' + str.tostring(math.round(Vol, 2)), text_color = chart.fg_color, text_size = size.small)
res
// Adaptive Box Len
sup.set_right(bar_index + 1)
res.set_right(bar_index + 1)
// Break of support or resistance conditions
brekout_res := ta.crossover(low, resistanceLevel_1)
res_holds := ta.crossunder(high, resistanceLevel)
sup_holds := ta.crossover(low, supportLevel)
brekout_sup := ta.crossunder(high, supportLevel_1)
// Change Color of Support to red if it was break, change color of resistance to green if it was break
if brekout_sup
sup.set_bgcolor(color.new(color.red, 80))
sup.set_border_color(color.red)
sup.set_border_style(line.style_dashed)
if sup_holds
sup.set_bgcolor(sup_color)
sup.set_border_color(color.green)
sup.set_border_style(line.style_solid)
if brekout_res
res.set_bgcolor(color.new(color.green, 80))
res.set_border_color(color.new(color.green, 0))
res.set_border_style(line.style_dashed)
if res_holds
res.set_bgcolor(res_color)
res.set_border_color(color.new(color.red, 0))
res.set_border_style(line.style_solid)
[supportLevel, resistanceLevel, brekout_res, res_holds, sup_holds, brekout_sup]
// Calculate support and resistance levels and their breakouts
[supportLevel, resistanceLevel, brekout_res, res_holds, sup_holds, brekout_sup] = calcSupportResistance(close, lookbackPeriod)
// Check if Resistance become Support or Support Become Resistance
var bool res_is_sup = false
var bool sup_is_res = false
switch
brekout_res =>
res_is_sup := true
res_is_sup
res_holds =>
res_is_sup := false
res_is_sup
switch
brekout_sup =>
sup_is_res := true
sup_is_res
sup_holds =>
sup_is_res := false
sup_is_res
// ---------------------------------------------------------------------------------------------------------------------}
// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
// ---------------------------------------------------------------------------------------------------------------------{
// Plot Res and Sup breakouts and holds
plotchar(res_holds, 'Resistance Holds', '◆', color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
plotchar(sup_holds, 'Support Holds', '◆', color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
plotchar(brekout_res and res_is_sup[1], 'Resistance as Support Holds', '◆', color = #20ca26, size = size.tiny, location = location.belowbar, offset = -1)
plotchar(brekout_sup and sup_is_res[1], 'Support as Resistance Holds', '◆', color = #e92929, size = size.tiny, location = location.abovebar, offset = -1)
// Break Out Labels
if brekout_sup and not sup_is_res[1]
label.new(bar_index[1], supportLevel[1], text = 'Break Sup', style = label.style_label_down, color = #7e1e1e, textcolor = chart.fg_color, size = size.small)
if brekout_res and not res_is_sup[1]
label.new(bar_index[1], resistanceLevel[1], text = 'Break Res', style = label.style_label_up, color = #2b6d2d, textcolor = chart.fg_color, size = size.small)
// 警报标记 - 醒目的视觉提示
// 支撑位保持警报标记
if sup_holds
label.new(bar_index, low, text = '🔔SUP', style = label.style_label_down, color = color.new(color.lime, 0), textcolor = color.black, size = size.large, tooltip = '支撑位保持警报')
// 阻力位保持警报标记
if res_holds
label.new(bar_index, high, text = '🔔RES', style = label.style_label_up, color = color.new(color.red, 0), textcolor = color.white, size = size.large, tooltip = '阻力位保持警报')
// 支撑位突破警报标记
if brekout_sup
label.new(bar_index[1], supportLevel[1], text = '🚨SUP⬇', style = label.style_label_down, color = color.new(color.orange, 0), textcolor = color.black, size = size.large, tooltip = '支撑位突破警报')
// 阻力位突破警报标记
if brekout_res
label.new(bar_index[1], resistanceLevel[1], text = '🚨RES⬆', style = label.style_label_up, color = color.new(color.aqua, 0), textcolor = color.black, size = size.large, tooltip = '阻力位突破警报')
// 阻力转支撑警报标记
if brekout_res and res_is_sup[1]
label.new(bar_index[1], resistanceLevel[1], text = '⭐R→S', style = label.style_label_down, color = color.new(color.purple, 0), textcolor = color.white, size = size.large, tooltip = '阻力转支撑保持警报')
// 支撑转阻力警报标记
if brekout_sup and sup_is_res[1]
label.new(bar_index[1], supportLevel[1], text = '⭐S→R', style = label.style_label_up, color = color.new(color.maroon, 0), textcolor = color.white, size = size.large, tooltip = '支撑转阻力保持警报')
// 全局作用域的plotshape - 警报形状标记
plotshape(sup_holds, title = '支撑保持警报', style = shape.triangleup, location = location.belowbar, color = color.new(color.lime, 0), size = size.large)
plotshape(res_holds, title = '阻力保持警报', style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.large)
plotshape(brekout_sup, title = '支撑突破警报', style = shape.xcross, location = location.belowbar, color = color.new(color.orange, 0), size = size.large)
plotshape(brekout_res, title = '阻力突破警报', style = shape.xcross, location = location.abovebar, color = color.new(color.aqua, 0), size = size.large)
plotshape(brekout_res and res_is_sup[1], title = '阻力转支撑警报', style = shape.diamond, location = location.belowbar, color = color.new(color.purple, 0), size = size.large)
plotshape(brekout_sup and sup_is_res[1], title = '支撑转阻力警报', style = shape.diamond, location = location.abovebar, color = color.new(color.maroon, 0), size = size.large)
// ◆
// ---------------------------------------------------------------------------------------------------------------------}
// 𝙋𝙍𝙄𝘾𝙀 𝙎𝙏𝘼𝙏𝙐𝙎 & 𝙈𝘼𝙍𝙆𝙀𝙏 𝙊𝙐𝙏𝙇𝙊𝙊𝙆
// ---------------------------------------------------------------------------------------------------------------------{
// ═════════ 当前图表时间周期价格状态分析 ════════
get_current_price_status() =>
current_price = close
support_level = supportLevel
resistance_level = resistanceLevel
// 计算价格相对位置
if na(support_level) and na(resistance_level)
"无明确支撑阻力"
else if na(support_level)
if current_price > resistance_level
"突破阻力位上方"
else if current_price >= resistance_level * 0.995
"接近阻力位"
else
"阻力位下方"
else if na(resistance_level)
if current_price < support_level
"跌破支撑位下方"
else if current_price <= support_level * 1.005
"接近支撑位"
else
"支撑位上方"
else
range_size = resistance_level - support_level
price_position = (current_price - support_level) / range_size
if current_price > resistance_level
"突破阻力位上方"
else if current_price < support_level
"跌破支撑位下方"
else if price_position >= 0.8
"接近阻力位"
else if price_position <= 0.2
"接近支撑位"
else
"区间中部运行"
// ═════════ 市场观点生成 ════════
get_market_outlook() =>
current_status = get_current_price_status()
// 基于价格状态和突破情况生成观点
if brekout_res and res_is_sup[1]
"看涨:阻力转支撑确认,建议关注回调买入机会"
else if brekout_sup and sup_is_res[1]
"看跌:支撑转阻力确认,建议关注反弹卖出机会"
else if brekout_res
"看涨:突破阻力位,建议关注回踩确认后的追涨机会"
else if brekout_sup
"看跌:跌破支撑位,建议关注反弹确认后的追跌机会"
else if res_holds
"中性偏空:阻力位有效,短期上涨受阻"
else if sup_holds
"中性偏多:支撑位有效,短期下跌受限"
else if current_status == "接近阻力位"
"谨慎:接近关键阻力位,注意突破或回落"
else if current_status == "接近支撑位"
"谨慎:接近关键支撑位,注意反弹或破位"
else if current_status == "区间中部运行"
"中性:价格在支撑阻力区间内震荡"
else if current_status == "突破阻力位上方"
"看涨:已突破阻力位,关注持续性"
else if current_status == "跌破支撑位下方"
"看跌:已跌破支撑位,关注反弹力度"
else
"观望:等待明确的支撑阻力信号"
// 获取当前价格状态和市场观点
current_price_status = get_current_price_status()
market_outlook = get_market_outlook()
// ---------------------------------------------------------------------------------------------------------------------}
// 𝘼𝙇𝙀𝙍𝙏 𝘾𝙊𝙉𝘿𝙄𝙏𝙄𝙊𝙉𝙎
// ---------------------------------------------------------------------------------------------------------------------{
// 支撑位保持警报
alertcondition(sup_holds, title = '支撑位保持', message = '{"指标名称":"SRBR","交易对":"{{ticker}}","周期":"{{interval}}","支撑位":"{{plot("supportLevel")}}","阻力位":"{{plot("resistanceLevel")}}","开盘价":"{{open}}","收盘价":"{{close}}","最高价":"{{high}}","最低价":"{{low}}","触发时间":"{{timenow}}","时间":"{{time}}","事件":"支撑位保持","位置":"支撑位","信号":"support_holds","价格状态":"' + current_price_status + '","市场观点":"' + market_outlook + '"}')
// 阻力位保持警报
alertcondition(res_holds, title = '阻力位保持', message = '{"指标名称":"SRBR","交易对":"{{ticker}}","周期":"{{interval}}","支撑位":"{{plot("supportLevel")}}","阻力位":"{{plot("resistanceLevel")}}","开盘价":"{{open}}","收盘价":"{{close}}","最高价":"{{high}}","最低价":"{{low}}","触发时间":"{{timenow}}","时间":"{{time}}","事件":"阻力位保持","位置":"阻力位","信号":"resistance_holds","价格状态":"' + current_price_status + '","市场观点":"' + market_outlook + '"}')
// 支撑位突破警报
alertcondition(brekout_sup, title = '支撑位突破', message = '{"指标名称":"SRBR","交易对":"{{ticker}}","周期":"{{interval}}","支撑位":"{{plot("supportLevel")}}","阻力位":"{{plot("resistanceLevel")}}","开盘价":"{{open}}","收盘价":"{{close}}","最高价":"{{high}}","最低价":"{{low}}","触发时间":"{{timenow}}","时间":"{{time}}","事件":"支撑位突破","位置":"支撑位下方","信号":"support_breakout","价格状态":"' + current_price_status + '","市场观点":"' + market_outlook + '"}')
// 阻力位突破警报
alertcondition(brekout_res, title = '阻力位突破', message = '{"指标名称":"SRBR","交易对":"{{ticker}}","周期":"{{interval}}","支撑位":"{{plot("supportLevel")}}","阻力位":"{{plot("resistanceLevel")}}","开盘价":"{{open}}","收盘价":"{{close}}","最高价":"{{high}}","最低价":"{{low}}","触发时间":"{{timenow}}","时间":"{{time}}","事件":"阻力位突破","位置":"阻力位上方","信号":"resistance_breakout","价格状态":"' + current_price_status + '","市场观点":"' + market_outlook + '"}')
// 阻力转支撑警报
alertcondition(brekout_res and res_is_sup[1], title = '阻力转支撑保持', message = '{"指标名称":"SRBR","交易对":"{{ticker}}","周期":"{{interval}}","支撑位":"{{plot("supportLevel")}}","阻力位":"{{plot("resistanceLevel")}}","开盘价":"{{open}}","收盘价":"{{close}}","最高价":"{{high}}","最低价":"{{low}}","触发时间":"{{timenow}}","时间":"{{time}}","事件":"阻力转支撑保持","位置":"前阻力位","信号":"resistance_as_support","价格状态":"' + current_price_status + '","市场观点":"' + market_outlook + '"}')
// 支撑转阻力警报
alertcondition(brekout_sup and sup_is_res[1], title = '支撑转阻力保持', message = '{"指标名称":"SRBR","交易对":"{{ticker}}","周期":"{{interval}}","支撑位":"{{plot("supportLevel")}}","阻力位":"{{plot("resistanceLevel")}}","开盘价":"{{open}}","收盘价":"{{close}}","最高价":"{{high}}","最低价":"{{low}}","触发时间":"{{timenow}}","时间":"{{time}}","事件":"支撑转阻力保持","位置":"前支撑位","信号":"support_as_resistance","价格状态":"' + current_price_status + '","市场观点":"' + market_outlook + '"}')
// ---------------------------------------------------------------------------------------------------------------------}