本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Mo3ty Stochastic Reversal Signals", shorttitle="Mo3ty Stoch Signals", overlay=true)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
kLength = input.int(21, "Stochastic K Length", minval=1)
kSmooth = input.int(5, "K Smoothing", minval=1)
overbought = input.float(80.0, "Overbought Level", minval=50.0, maxval=100.0)
oversold = input.float(20.0, "Oversold Level", minval=0.0, maxval=50.0)
useEMA = input.bool(true, "Use EMA Filter")
emaLength = input.int(50, "EMA Length", minval=1)
useCandleFilter = input.bool(true, "Use Candle Confirmation")
// Minimum number of closed candles between
// two signals of the SAME type
signalCooldown = input.int(5, "Same Signal Cooldown (Bars)", minval=1)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// STOCHASTIC CALCULATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
lowestLow = ta.lowest(low, kLength)
highestHigh = ta.highest(high, kLength)
priceRange = highestHigh - lowestLow
rawK = priceRange != 0 ?
100.0 * (close - lowestLow) / priceRange :
0.0
// K only — D% intentionally removed
k = ta.sma(rawK, kSmooth)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// EMA
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
emaValue = ta.ema(close, emaLength)
plot(
emaValue,
title="EMA",
color=color.orange,
linewidth=2
)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// STOCHASTIC REVERSAL
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY:
// Previous bar was in Oversold
// Current bar shows K turning upward
stochBuy =
k[1] <= oversold and
k > k[1]
// SELL:
// Previous bar was in Overbought
// Current bar shows K turning downward
stochSell =
k[1] >= overbought and
k < k[1]
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CANDLE CONFIRMATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullishCandle = close > close[1]
bearishCandle = close < close[1]
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// EMA FILTER
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
buyEMACondition =
not useEMA or close > emaValue
sellEMACondition =
not useEMA or close < emaValue
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SIGNAL LOCK SYSTEM
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var bool buyArmed = true
var bool sellArmed = true
// Store the bar where the last BUY/SELL occurred
var int lastBuyBar = na
var int lastSellBar = na
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// RE-ARM LOGIC
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY becomes available again only after
// Stochastic exits Oversold.
if barstate.isconfirmed and k > oversold
buyArmed := true
// SELL becomes available again only after
// Stochastic exits Overbought.
if barstate.isconfirmed and k < overbought
sellArmed := true
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SAME SIGNAL COOLDOWN
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY cooldown
buyCooldownOK =
na(lastBuyBar) or
(bar_index - lastBuyBar > signalCooldown)
// SELL cooldown
sellCooldownOK =
na(lastSellBar) or
(bar_index - lastSellBar > signalCooldown)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SIGNAL SETUPS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
buySetup =
stochBuy and
buyEMACondition and
(not useCandleFilter or bullishCandle)
sellSetup =
stochSell and
sellEMACondition and
(not useCandleFilter or bearishCandle)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CONFIRMED SIGNALS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
confirmedBuy =
barstate.isconfirmed and
buySetup and
buyArmed and
buyCooldownOK
confirmedSell =
barstate.isconfirmed and
sellSetup and
sellArmed and
sellCooldownOK
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LOCK + RECORD SIGNAL BAR
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if confirmedBuy
buyArmed := false
lastBuyBar := bar_index
if confirmedSell
sellArmed := false
lastSellBar := bar_index
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SIGNAL PLOTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plotshape(
confirmedBuy,
title="BUY",
style=shape.labelup,
location=location.belowbar,
color=color.green,
text="BUY",
textcolor=color.white,
size=size.small
)
plotshape(
confirmedSell,
title="SELL",
style=shape.labeldown,
location=location.abovebar,
color=color.red,
text="SELL",
textcolor=color.white,
size=size.small
)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(
confirmedBuy,
title="Mo3ty BUY Signal",
message="Mo3ty Stochastic Reversal Signals: BUY confirmed on candle close."
)
alertcondition(
confirmedSell,
title="Mo3ty SELL Signal",
message="Mo3ty Stochastic Reversal Signals: SELL confirmed on candle close."
)