本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(title="Support/Resistance & EMA Crossovers with Alerts", shorttitle="SR EMA", overlay=true, max_bars_back=1000)
// ========== EMA Settings ==========
ST_EMA = input.int(title="Short term EMA", defval=5)
MT_EMA = input.int(title="Medium term EMA", defval=12)
LT_EMA = input.int(title="Long term EMA", defval=34)
src = input.source(close, title="Source")
// ========== Support/Resistance Settings ==========
toggleBreaks = input(true, title="Show Breaks")
leftBars = input.int(15, title="Left Bars")
rightBars = input.int(15, title="Right Bars")
volumeThresh = input.float(20, title="Volume Threshold")
// ========== EMA Calculations ==========
Signal_ST_EMA = ta.ema(src, ST_EMA)
Signal_MT_EMA = ta.ema(src, MT_EMA)
Signal_LT_EMA = ta.ema(src, LT_EMA)
// ========== Support/Resistance Calculations ==========
highUsePivot = fixnan(ta.pivothigh(leftBars, rightBars)[1])
lowUsePivot = fixnan(ta.pivotlow(leftBars, rightBars)[1])
// Volume %
short = ta.ema(volume, 5)
long = ta.ema(volume, 10)
osc = 100 * (short - long) / long
// ========== Trend Detection ==========
upTrend = ta.crossover(Signal_ST_EMA, Signal_MT_EMA)
downTrend = ta.crossunder(Signal_ST_EMA, Signal_MT_EMA)
buySignal = ta.crossover(Signal_MT_EMA, Signal_LT_EMA)
sellSignal = ta.crossunder(Signal_MT_EMA, Signal_LT_EMA)
// ========== Plot EMA Lines ==========
//plot(Signal_ST_EMA, title="Short term EMA", color=color.purple, linewidth=1)
//plot(Signal_MT_EMA, title="Medium term EMA", color=color.orange, linewidth=1)
//plot(Signal_LT_EMA, title="Long term EMA", color=color.blue, linewidth=1)
// ========== Plot Support/Resistance Levels ==========
r1 = plot(highUsePivot, color=ta.change(highUsePivot) != 0 ? na : #FF0000, linewidth=3, offset=-(rightBars+1), title="Resistance")
s1 = plot(lowUsePivot, color=ta.change(lowUsePivot) != 0 ? na : #233dee, linewidth=3, offset=-(rightBars+1), title="Support")
// ========== EMA Visual Signals ==========
plotshape(upTrend, title="Uptrend start signal", text="Up", color=color.purple, textcolor=color.white, style=shape.labelup, size=size.normal, location=location.belowbar)
plotshape(downTrend, title="Downtrend start signal", text="Down", color=color.orange, textcolor=color.white, style=shape.labeldown, size=size.normal, location=location.abovebar)
plotshape(buySignal, title="Buy start signal", text="Buy", color=color.green, textcolor=color.white, style=shape.labelup, size=size.normal, location=location.belowbar)
plotshape(sellSignal, title="Sell start signal", text="Sell", color=color.red, textcolor=color.white, style=shape.labeldown, size=size.normal, location=location.abovebar)
// ========== Support/Resistance Break Signals ==========
// For breaks with volume
plotshape(toggleBreaks and ta.crossunder(close, lowUsePivot) and not (open - close < high - open) and osc > volumeThresh, title="Break", text="B", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)
plotshape(toggleBreaks and ta.crossover(close, highUsePivot) and not(open - low > close - open) and osc > volumeThresh, title="Break", text="B", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
// For bull / bear wicks
plotshape(toggleBreaks and ta.crossover(close, highUsePivot) and open - low > close - open, title="Break", text="Bull Wick", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(toggleBreaks and ta.crossunder(close, lowUsePivot) and open - close < high - open, title="Break", text="Bear Wick", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)
// ========== EMA Alerts ==========
if upTrend
content = '{"content": "UP Curl ==> Crossed over EMA (' + str.tostring(ST_EMA) + '=' + str.tostring(MT_EMA) + ')", "tts": false, "embeds": [{ "title": "' + syminfo.prefix + ' = ' + syminfo.ticker + ' Type = ' + syminfo.type + '", "description": "Price= ' + str.tostring(open) + ' Volume= ' + str.tostring(volume) + ' Interval= ' + str.tostring(timeframe.period) + '" }]}'
alert(content, alert.freq_once_per_bar)
if downTrend
content = '{"content": "DOWN Curl ==> Crossed under EMA (' + str.tostring(ST_EMA) + '=' + str.tostring(MT_EMA) + ')", "tts": false, "embeds": [{ "title": "' + syminfo.prefix + ' = ' + syminfo.ticker + ' Type = ' + syminfo.type + '", "description": "Price= ' + str.tostring(open) + ' Volume= ' + str.tostring(volume) + ' Interval= ' + str.tostring(timeframe.period) + '" }]}'
alert(content, alert.freq_once_per_bar)
if buySignal
content = '{"content": "BUY Signal ==> Crossed over EMA (' + str.tostring(MT_EMA) + '=' + str.tostring(LT_EMA) + ')", "tts": false, "embeds": [{ "title": "' + syminfo.prefix + ' = ' + syminfo.ticker + ' Type = ' + syminfo.type + '", "description": "Price= ' + str.tostring(open) + ' Volume= ' + str.tostring(volume) + ' Interval= ' + str.tostring(timeframe.period) + '" }]}'
alert(content, alert.freq_once_per_bar)
if sellSignal
content = '{"content": "SELL Signal ==> Crossed under EMA (' + str.tostring(MT_EMA) + '=' + str.tostring(LT_EMA) + ')", "tts": false, "embeds": [{ "title": "' + syminfo.prefix + ' = ' + syminfo.ticker + ' Type = ' + syminfo.type + '", "description": "Price= ' + str.tostring(open) + ' Volume= ' + str.tostring(volume) + ' Interval= ' + str.tostring(timeframe.period) + '" }]}'
alert(content, alert.freq_once_per_bar)
// ========== Support/Resistance Alerts ==========
if ta.crossunder(close, lowUsePivot) and osc > volumeThresh
content = '{"content": "Support Broken", "tts": false, "embeds": [{ "title": "' + syminfo.prefix + ' = ' + syminfo.ticker + ' Type = ' + syminfo.type + '", "description": "Price= ' + str.tostring(open) + ' Volume= ' + str.tostring(volume) + ' Interval= ' + str.tostring(timeframe.period) + '" }]}'
alert(content, alert.freq_once_per_bar)
if ta.crossover(close, highUsePivot) and osc > volumeThresh
content = '{"content": "Resistance Broken", "tts": false, "embeds": [{ "title": "' + syminfo.prefix + ' = ' + syminfo.ticker + ' Type = ' + syminfo.type + '", "description": "Price= ' + str.tostring(open) + ' Volume= ' + str.tostring(volume) + ' Interval= ' + str.tostring(timeframe.period) + '" }]}'
alert(content, alert.freq_once_per_bar)