Rsi Supertrend
Rsi Supertrend
Rsi Supertrend
0
at https://mozilla.org/MPL/2.0/
// © doanhdp
//@version=5
strategy("RSI & Supertrend v1.0", overlay=true, margin_long=10, margin_short=10)
// FUNCTION {
ma(source, length, type)=>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)//}
// MENU {
oscSrc = input.source(close, "Source", inline='A', group = "Oscillator")
oscLen = input.int(14, minval = 1, title = "Length", inline='A', group =
"Oscillator")
maTypeInput = input.string("SMA", title = "Type", options = ["SMA", "EMA", "SMMA
(RMA)", "WMA", "VWMA"], inline='B', group = "Moving Average")
maLengthInput = input.int(14, title = "Length", inline="B", group = "Moving
Average")
plotBull = input(title = 'Bullish', defval = true, inline = "A", group =
"Divergence Plots")
plotBear = input(title = 'Bearish', defval = true, inline = "A", group =
"Divergence Plots")
plotHiddenBull = input(title = 'Hidden Bull', defval = false, inline = "B", group
= "Divergence Plots")
plotHiddenBear = input(title = 'Hidden Bear', defval = false, inline = "B", group =
"Divergence Plots")
lbR = 4
lbL = 4
rangeUpper = 60
rangeLower = 5//}
up = ta.rma(math.max(ta.change(oscSrc), 0), oscLen)
down = ta.rma(-math.min(ta.change(oscSrc), 0), oscLen)
osc = ta.rsi(oscSrc,oscLen)
oscMA = ma(osc, maLengthInput, maTypeInput)
//supertrend
atrPeriod = input.int(10, "ATR Length", minval = 1)
factor = input.float(2, "Factor", minval = 0.01, step = 0.01)
//handle supertrend {
supertrend := barstate.isfirst ? na : supertrend
//alert supertrend
//supertrend
atrPeriod1 = input.int(10, "ATR Length", minval = 1)
factor1 = input.float(1, "Factor", minval = 0.01, step = 0.01)
//handle supertrend {
supertrend1 := barstate.isfirst ? na : supertrend1
if exitLongSupertrend
isOpenLongSupertrendPosition := false
else if longSupertrend
isOpenLongSupertrendPosition := true
if exitShortSupertrend
isOpenShortSupertrendPosition := false
else if shortSupertrend
isOpenShortSupertrendPosition := true
// if direction[1] != direction
// if isOpenLongSupertrendPosition
// strategy.close("BarUp")
// isOpenLongSupertrendPosition := false
// if isOpenShortSupertrendPosition
// strategy.close("BarDown")
// isOpenShortSupertrendPosition := false
//}
_inRange(cond) =>
bars = ta.barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper
// TREND ID{
plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true
oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)
priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)
priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)
bullCond = plotBull and priceLL and oscHL and plFound
hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
bearCond = plotBear and priceHH and oscLH and phFound
hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound//}
// COLORS {
bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.green, 0)
hiddenBearColor = color.new(color.red, 0)
textColor = color.white
noneColor = color.new(color.white, 100)//}
// PLOTTING {
oscUB = hline(60, "Upper Bound", color = #787B86)
oscUBB = hline(70, "UpUpper Bound", color = #787B86)
oscLB = hline(40, "Lower Bound", color = #787B86)
oscLBB = hline(30, "LLower Bound", color = #787B86)
fill(oscUB, oscLB, color = color.rgb(64, 47, 86), title = "Background Fill")
fill(oscUB, oscUBB, color = color.rgb(87, 87, 102), title = "Background Fill")
fill(oscLB, oscLBB, color = color.rgb(87, 87, 102), title = "Background Fill")
adxLongAlert = isShowADX and isUptrendADX and sig >= valueADX and osc <= 40
adxShortAlert = isShowADX and not isUptrendADX and sig <= valueADX and osc >= 60
// ALERT CONDITIONS {
alertcondition(bullCond,title = 'Bullish Divergence',message = 'Bullish
Divergence')
alertcondition(hiddenBullCond,title = 'Hidden Bull Divergence',message = 'Hidden
Bull Divergence')
alertcondition(bearCond,title = 'Bearish Divergence',message = 'Bearish
Divergence')
alertcondition(hiddenBearCond,title = 'Hidden Bear Divergence',message = 'Hidden
Bearish Divergence')
alertcondition(adxLongAlert,title = 'RSI & ADX',message = 'Long alert')
alertcondition(adxShortAlert,title = 'RSI & ADX',message = 'Short alert')
//}
//only alert {
if adxLongAlert
alert(syminfo.ticker + "Long alert at: " + str.tostring(close),
alert.freq_once_per_bar_close)
else if adxShortAlert
alert(syminfo.ticker + "Short alert at: " + str.tostring(close),
alert.freq_once_per_bar_close)
else if longSupertrend
alert(syminfo.ticker + "🟢 Long alert at: " + str.tostring(close),
alert.freq_once_per_bar_close)
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", from_entry="Long", stop=close * 0.99, limit =
1.08*close)
else if shortSupertrend
alert(syminfo.ticker + "🔴 Short alert at: " + str.tostring(close),
alert.freq_once_per_bar_close)
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", from_entry="Short", stop=close * 1.01, limit =
0.925*close)
else if exitLongSupertrend
alert(syminfo.ticker + "🔔 Exit long alert at: " + str.tostring(close),
alert.freq_once_per_bar_close)
strategy.close("Long")
else if exitShortSupertrend
alert(syminfo.ticker + "🔔 Exit short alert at: " + str.tostring(close),
alert.freq_once_per_bar_close)
strategy.close("BarDown")
//}