```
//@version=4
study(title="MarketCipher_FREE", shorttitle="MarketCipher_FREE")
// FUNCTIONS {
// MovingAverageTypes
ma(matype, src, length) =>
if matype == "RMA"
rma(src, length)
else
if matype == "SMA"
sma(src, length)
else
if matype == "EMA"
ema(src, length)
else
if matype == "WMA"
wma(src, length)
else
src
// } FUNCTIONS
// PARAMETERS for lightweight version 1.0 {
// Wavetrend Channel Length
n1 = input(7, "WaveTrend Channel Length")
// Wavetrend Average Length
n2 = input(12, "WaveTrend Average Length")
// Wavetrend MovingAverage Parameters Exponential weighted, weighted , exponential, simple
wtMA = input(defval="EMA", title="Wavetrend MovingAverage Type", options=["RMA", "SMA", "EMA", "WMA"])
wtMA1 = input(defval="EMA", title="Wavetrend MovingAverage Type 1", options=["RMA", "SMA", "EMA", "WMA"])
wtMA2 = input(defval="EMA", title="Wavetrend MovingAverage Type 2", options=["RMA", "SMA", "EMA", "WMA"])
wtMA3 = input(defval="SMA", title="Wavetrend MovingAverage Type 3", options=["RMA", "SMA", "EMA", "WMA"])
// WaveTrend Overbought and Oversold zones
obLevel = input(55, "WaveTrend Overbought Level 1")
obLevel2 = input(60, "WaveTrendOverbought Level 2")
osLevel = input(-55, "WaveTrend Oversold Level 1")
osLevel2 = input(-60, "WaveTrend Oversold Level 2")
osLevel3 = input(-80, "WaveTrend Oversold Level 3")
// WaveTrend MA Source
ap = input(ohlc4, "WaveTrend MovingAverage Source")
// WaveTrend MA Length
sp = input(3, "WaveTrend MovingAverage Length")
// RSI Divergence bull bear detector
bearish_div_rsi = input(61, "Minimal Bearish RSI", input.integer, minval=50, maxval=100)
bullish_div_rsi = input(31, "Maximum Bullish RSI", input.integer, minval=0, maxval=50)
// Colors
colorRed = #ff0000
colorPurple = #da00ff
colorGreen = #03ff00
colorPink = color.black
// Divergence WT
WTDivOBLevel = input(55)
WTDivOSLevel = input(-63)
// Divergence RSI
RSIDivOBLevel = input(70)
RSIDivOSLevel = input(30)
// } PARAMETERS
// CALCULATE INDICATORS {
// RSI
up = rma(max(change(close), 0), 14)
down = rma(-min(change(close), 0), 14)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
rsiColor = rsi < bullish_div_rsi ? color.green : rsi > bearish_div_rsi ? color.red : color.purple
// Calculates WaveTrend
esa = ma(wtMA, ap, n1)
de = ma(wtMA1, abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * de)
tci = ma(wtMA2, ci, n2)
wt1 = tci
wt2 = ma(wtMA3, wt1, sp)
// WaveTrend Conditions
WTCross = cross(wt1, wt2)
WTCrossUp = wt2 - wt1 <= 0
WTCrossDown = wt2 - wt1 >= 0
WTOverSold = wt2 <= osLevel
WTOverBought = wt2 >= obLevel
FallingKnife = wt2 <= osLevel3
//Divergences
f_top_fractal(_src) => _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
f_bot_fractal(_src) => _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
f_fractalize(_src) => f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
//Diverges Wavetrend
WTFractal_top = f_fractalize(wt2) > 0 and wt2[2] >= WTDivOBLevel ? wt2[2] : na
WTFractal_bot = f_fractalize(wt2) < 0 and wt2[2] <= WTDivOSLevel ? wt2[2] : na
WTHigh_prev = valuewhen(WTFractal_top, wt2[2], 0)[2]
WTHigh_price = valuewhen(WTFractal_top, high[2], 0)[2]
WTLow_prev = valuewhen(WTFractal_bot, wt2[2], 0)[2]
WTLow_price = valuewhen(WTFractal_bot, low[2], 0)[2]
WTRegular_bearish_div = WTFractal_top and high[2] > WTHigh_price and wt2[2] < WTHigh_prev
WTRegular_bullish_div = WTFractal_bot and low[2] < WTLow_price and wt2[2] > WTLow_prev
bearWTSignal = WTRegular_bearish_div and WTCrossDown
bullWTSignal = WTRegular_bullish_div and WTCrossUp
WTCol1 = bearWTSignal ? color.red : na
WTCol2 = bullWTSignal ? #00FF00EB : na
//DIVERGENCE RSI
RSIFractal_top = f_fractalize(rsi) > 0 and rsi[2] >= RSIDivOBLevel ? rsi[2] : na
RSIFractal_bot = f_fractalize(rsi) < 0 and rsi[2] <= RSIDivOSLevel ? rsi[2] : na
RSIHigh_prev = valuewhen(RSIFractal_top, rsi[2], 0)[2]
RSIHigh_price = valuewhen(RSIFractal_top, high[2], 0)[2]
RSILow_prev = valuewhen(RSIFractal_bot, rsi[2], 0)[2]
RSILow_price = valuewhen(RSIFractal_bot, low[2], 0)[2]
bearRSISignal = RSIFractal_top and high[2] > RSIHigh_price and rsi[2] < RSIHigh_prev
bullRSISignal = RSIFractal_bot and low[2] < RSILow_price and rsi[2] > RSILow_prev
RSICol1 = bearRSISignal ? #f802fc: na
RSICol2 = bullRSISignal ? #00FF00EB : na
// Small Circles WT Cross
signalColor = wt2 - wt1 > 0 ? color.red : color.lime
// Buy signal
buySignal = WTCross and WTCrossUp and WTOverSold
bigBuySignal = WTCross and (bullRSISignal or bullWTSignal)
buySignalColor = bigBuySignal ? colorPurple : buySignal ? colorGreen : na
plotBuy = bigBuySignal ? bigBuySignal : buySignal
// Sell signal
sellSignal = WTCross and WTCrossDown and WTOverBought
bigSellSignal = WTCross and (bearRSISignal or bearWTSignal)
sellSignalColor = bigSellSignal ? colorPurple : sellSignal ? colorRed : na
plotSell = bigSellSignal ? bigSellSignal : sellSignal
// fallingKnife
plotFallingKnife = WTCross and WTCrossUp and FallingKnife
// } CALCULATE INDICATORS
// DRAW {
// basis
plot(0, title="basis", color=color.lime)
// Draw Overbought & Oversold lines
plot(obLevel, title="OverBought Level 1", color=color.gray, linewidth=2, style=plot.style_line, transp=0)
plot(obLevel2, title="OverBought Level 2", color=color.gray, style=plot.style_cross, transp=0)
plot(osLevel, title="OverSold Level 1", color=color.gray, linewidth=2, style=plot.style_line, transp=0)
plot(osLevel2, title="OverSold Level 2", color=color.gray, style=plot.style_cross, transp=0)
// DIVERGENCES
plot(series = WTFractal_top ? wt2[2] : na, title='WaveTrend Regular Bearish Divergence', color=WTCol1, linewidth=3, transp=0)
plot(series = WTFractal_bot ? wt2[2] : na, title='WaveTrend Regular Bullish Divergence', color=WTCol2, linewidth=3, transp=0)
plot(series = RSIFractal_top ? rsi[2] : na, title='RSI Bearish Divergence', color=RSICol1, linewidth=3, transp=0)
plot(series = RSIFractal_bot ? rsi[2] : na, title='RSI Bullish Divergence', color=RSICol2, linewidth=3, transp=0)
// WT Area 1
plot(wt1, style=plot.style_area, title="WaveTrend Wave 1", color=#0ae6ff, transp=10)
// WT Area 2
plot(wt2, style=plot.style_area, title="WaveTrend Wave 2", color=#0042ff, transp=10)
plot(WTCross ? wt2 : na, title="Low probability Buy and sell signals", color=signalColor, style=plot.style_circles, linewidth=3, transp=0)
plot(plotBuy ? -90 : na, title="High probability buy signal", color=buySignalColor, style=plot.style_cross, linewidth=4, transp=0)
plot(plotSell ? 90 : na, title="High probability Sell signal", color=sellSignalColor, style=plot.style_cross, linewidth=4, transp=0)
plot(plotFallingKnife ? -110 : na, title="FallingKnife", color=color.black, style=plot.style_cross, linewidth=4, transp=0)
// } DRAW
// ALERTS {
// BUY
alertcondition(plotBuy != 0, "High probability buy signal", "High probability buy signal")
alertcondition(bigBuySignal != 0, "High probability buy signal with WT divergence", "High probability buy signal with Wavetrend Divergence ")
// SELL
alertcondition(plotSell != 0, "High probability sell signal", "High probability buy signal")
alertcondition(bigSellSignal != 0, "High probability sell signal with WT divergence", "High probability sell signal with Wavetrend Divergence ")
alertcondition(plotFallingKnife != 0, "FallingKnife", "Possible FallingKnife")
// } ALERTS
```