rsi supertrend

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.

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)

[supertrend, direction] = request.security(syminfo.tickerid, "60",


ta.supertrend(factor, atrPeriod))

//handle supertrend {
supertrend := barstate.isfirst ? na : supertrend

detectUptrend = direction[1] > direction


detectDowntrend = direction[1] < direction

var isUptrend = false


var isOpenLongSupertrendPosition = false
var isOpenShortSupertrendPosition = false
if detectDowntrend and direction[1] != direction
isUptrend := false
else if detectUptrend and direction[1] != direction
isUptrend := true

//alert supertrend

//supertrend
atrPeriod1 = input.int(10, "ATR Length", minval = 1)
factor1 = input.float(1, "Factor", minval = 0.01, step = 0.01)

[supertrend1, direction1] = request.security(syminfo.tickerid, "60",


ta.supertrend(factor1, atrPeriod1))

//handle supertrend {
supertrend1 := barstate.isfirst ? na : supertrend1

detectUptrend1 = direction1[1] > direction1


detectDowntrend1 = direction1[1] < direction1

longSupertrend = not isOpenLongSupertrendPosition and isUptrend and osc < 40


exitLongSupertrend = isOpenLongSupertrendPosition and isUptrend and
detectDowntrend1
shortSupertrend = not isOpenShortSupertrendPosition and not isUptrend and osc > 60
exitShortSupertrend = isOpenShortSupertrendPosition and not isUptrend and
detectUptrend1

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

//}

//set up for ADX


isShowADX = input(title = 'Show ADX plots', defval = false, inline = "A", group =
"ADX settings")
isUptrendADX = input(title = 'Is uptrend or downtrend', defval = true, inline =
"A", group = "ADX settings")
valueADX = input.int(0, minval = 0, title = "ADX offset value", inline='A', group =
"ADX settings")

adxlen = input(14, title="ADX Smoothing")


dilen = input(14, title="DI Length")
dirmov(len) =>
upADX = ta.change(high)
downADX = -ta.change(low)
plusDM = na(upADX) ? na : (upADX > downADX and upADX > 0 ? upADX : 0)
minusDM = na(down) ? na : (downADX > upADX and downADX > 0 ? downADX : 0)
truerange = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)

//plot(isShowADX ? sig : na, color=color.red, title="ADX")

_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")

// plot(osc, title = 'RSI', linewidth = 1, color = color.rgb(255, 189, 68))


// plot(plFound ? osc[lbR] : na, offset = -lbR, title = 'Regular Bullish',
linewidth = 2, color = bullCond ? bullColor : noneColor)
// plot(plFound ? osc[lbR] : na, offset = -lbR, title = 'Hidden Bullish', linewidth
= 2, color = hiddenBullCond ? hiddenBullColor : noneColor)
// plot(phFound ? osc[lbR] : na, offset = -lbR, title = 'Regular Bearish',
linewidth = 2, color = bearCond ? bearColor : noneColor)
// plot(phFound ? osc[lbR] : na, offset = -lbR, title = 'Hidden Bearish', linewidth
= 2, color = hiddenBearCond ? hiddenBearColor : noneColor)//}

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")
//}

You might also like