Nothing Special   »   [go: up one dir, main page]

3M WR Dmi

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/
// © pravin18001

//@version=5
strategy(title="3M Sentiment DMI WR", overlay=true, margin_long=10000,
margin_short=10000)

lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50)


len = input.int(14, minval=1, title="DI Length")
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

indicator("Williams Percent Range", shorttitle="Williams WR", format=format.price,


precision=2, timeframe="", timeframe_gaps=true)
length = input(title="Length", defval=14)
src = input(close, "Source")
_pr(length) =>
max = ta.highest(length)
min = ta.lowest(length)
100 * (src - max) / (max - min)
percentR = _pr(length)
obPlot = hline(-20, title="Upper Band", color=#787B86)
hline(-50, title="Middle Level", linestyle=hline.style_dotted, color=#787B86)
osPlot = hline(-80, title="Lower Band", color=#787B86)
fill(obPlot, osPlot, title="Background", color=color.rgb(126, 87, 194, 90))
plot(percentR, title="WR", color=#7E57C2)

//RSI
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price,
precision=2, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>


switch type
"SMA" => ta.sma(source, length)
"Bollinger Bands" => 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)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")


rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger
Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings", display =
display.data_window)
maLengthInput = input.int(14, title="MA Length", group="MA Settings", display =
display.data_window)
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev",
group="MA Settings", display = display.data_window)
showDivergence = input.bool(false, title="Show Divergence", group="RSI Settings",
display = display.data_window)

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)


down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

rsiPlot = plot(rsi, "RSI", color=#7E57C2)


plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
midline = hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI
Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na,
title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na,
title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na,
title="Bollinger Bands Background Fill")

midLinePlot = plot(50, color = na, editable = false, display = display.none)


fill(rsiPlot, midLinePlot, 100, 70, top_color = color.new(color.green, 0),
bottom_color = color.new(color.green, 100), title = "Overbought Gradient Fill")
fill(rsiPlot, midLinePlot, 30, 0, top_color = color.new(color.red, 100),
bottom_color = color.new(color.red, 0), title = "Oversold Gradient Fill")

// Divergence
lookbackRight = 5
lookbackLeft = 5
rangeUpper = 60
rangeLower = 5
bearColor = color.red
bullColor = color.green
textColor = color.white
noneColor = color.new(color.white, 100)

plFound = na(ta.pivotlow(rsi, lookbackLeft, lookbackRight)) ? false : true


phFound = na(ta.pivothigh(rsi, lookbackLeft, lookbackRight)) ? false : true
_inRange(cond) =>
bars = ta.barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// Regular Bullish
// rsi: Higher Low

rsiHL = rsi[lookbackRight] > ta.valuewhen(plFound, rsi[lookbackRight], 1) and


_inRange(plFound[1])

// Price: Lower Low

priceLL = low[lookbackRight] < ta.valuewhen(plFound, low[lookbackRight], 1)


bullCondAlert = priceLL and rsiHL and plFound
bullCond = showDivergence and bullCondAlert
plot(
plFound ? rsi[lookbackRight] : na,
offset=-lookbackRight,
title="Regular Bullish",
linewidth=2,
color=(bullCond ? bullColor : noneColor)
)

plotshape(
bullCond ? rsi[lookbackRight] : na,
offset=-lookbackRight,
title="Regular Bullish Label",
text=" Bull ",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor
)

//------------------------------------------------------------------------------
// Regular Bearish
// rsi: Lower High

rsiLH = rsi[lookbackRight] < ta.valuewhen(phFound, rsi[lookbackRight], 1) and


_inRange(phFound[1])

// Price: Higher High

priceHH = high[lookbackRight] > ta.valuewhen(phFound, high[lookbackRight], 1)

bearCondAlert = priceHH and rsiLH and phFound


bearCond = showDivergence and bearCondAlert

plot(
phFound ? rsi[lookbackRight] : na,
offset=-lookbackRight,
title="Regular Bearish",
linewidth=2,
color=(bearCond ? bearColor : noneColor)
)

plotshape(
bearCond ? rsi[lookbackRight] : na,
offset=-lookbackRight,
title="Regular Bearish Label",
text=" Bear ",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor
)

alertcondition(bullCondAlert, title='Regular Bullish Divergence', message="Found a


new Regular Bullish Divergence, `Pivot Lookback Right` number of bars to the left
of the current bar.")
alertcondition(bearCondAlert, title='Regular Bearish Divergence', message='Found a
new Regular Bearish Divergence, `Pivot Lookback Right` number of bars to the left
of the current bar.')
// Long Entry Conditions

longCondition1 = ta.crossover(plusDM, 21) and adx > 19.5


longCondition2 = minusDM > 21 and ta.crossover(minusDM, adx) and adx > 19.5
longCondition3 = minusDM > 21 and ta.crossunder(adx, minusDM) and adx > 19.5
longCondition4 = ta.crossover(plusDM, adx) and adx > 19.5
longCondition5 = ta.crossover(plusDM, minusDM) and adx > 19.5
longCondition6 = WR < -80 and ta.crossover(WR, -80) and adx > 19.5
longCondition7 = WR > -20 and ta.crossover(WR, -20) and adx > 19.5
longCondition8 = RSI < 40 and ta.crossover(close, ma5) and adx > 19.5
longCondition9 = RSI > 57 and ta.crossover(close, ma5) and adx > 19.5

strategy.entry("Long Entry", strategy.long, when = (long_condition1 or


long_condition2, long_condition3 or long_condition4 or long_condition5 or
long_condition6 or long_condition7 or long_condition8 or long_condition9))

// Short Entry Conditions


short_entry1 = ta.crossover(minusDM, 21) and adx > 19.5
short_entry2 = plusDM > 21 and ta.crossunder(plusDM, adx) and adx > 19.5
short_entry3 = plusDM > 21 and ta.crossunder(adx, plusDM) and adx > 19.5
short_entry4 = ta.crossunder(minusDM, adx) and adx > 19.5
short_entry5 = ta.crossunder(minusDM, plusDM) and adx > 19.5
short_entry6 = WR > -20 and ta.crossunder(WR, -20) and adx > 19.5
short_entry7 = WR > 80 and ta.crossunder(WR, 80) and adx > 19.5
short_entry8 = RSI > 60 and ta.crossunder(close, ma5) and adx > 19.5
short_entry9 = RSI < 19 and ta.crossunder(close, ma5) and adx > 19.5

strategy.entry("Short Entry", strategy.short, when = (short_condition1) or


short_condition2 or short_condition3 or short_condition4 or short_condition5 or
short_condition6 or short_condition7 or short_condition8 or short_condition9))

// Calculate target and stop loss levels


long_target = strategy.position_avg_price + target_points
long_stop_loss = strategy.position_avg_price - stop_loss_points
short_target = strategy.position_avg_price - target_points
short_stop_loss = strategy.position_avg_price + stop_loss_points

// Strategy exit
strategy.exit("Long Exit", "Long", limit=long_target, stop=long_stop_loss)
strategy.exit("Short Exit", "Short", limit=short_target, stop=short_stop_loss)

// Strategy exit
strategy.exit("Long Exit", "Long", limit=long_target, stop=long_stop_loss)
strategy.exit("Short Exit", "Short", limit=short_target, stop=short_stop_loss)

You might also like