|
1 |
| -//@version=4 |
| 1 | +//@version=6 |
2 | 2 | // Copyright (c) 2021-present, Alex Orekhov (everget)
|
3 | 3 | // Nick Rypock Trailing Reverse (NRTR) script may be freely distributed under the terms of the GPL-3.0 license.
|
4 |
| -study("Nick Rypock Trailing Reverse", shorttitle="NRTR", overlay=true) |
| 4 | +indicator('Nick Rypock Trailing Reverse', shorttitle = 'NRTR', overlay = true) |
5 | 5 |
|
6 |
| -k = input(title="Coefficient of Correction, %", type=input.float, minval=0, maxval=100, step=0.1, defval=2) |
7 |
| -showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true) |
8 |
| -applyRibbon = input(title="Apply Ribbon ?", type=input.bool, defval=true) |
| 6 | +const string calcGroup = 'Calculation' |
| 7 | +percentage = input.float(2, minval = 0, maxval = 100, step = 0.1, title = 'Coefficient of Correction, %', group = calcGroup) / 100 |
| 8 | + |
| 9 | +const string visualGroup = 'Visuals' |
| 10 | +showLabels = input.bool(true, title = 'Show Buy/Sell Labels', group = visualGroup) |
| 11 | +highlightState = input.bool(true, title = 'Highlight State', group = visualGroup) |
| 12 | + |
| 13 | +//--- |
9 | 14 |
|
10 | 15 | var int trend = 0
|
11 | 16 | var float hp = close
|
12 | 17 | var float lp = close
|
13 |
| -float nrtr = close |
14 |
| - |
15 |
| -percentage = k * 0.01 |
| 18 | +nrtr = close |
16 | 19 |
|
17 | 20 | if trend >= 0
|
18 |
| - if close > hp |
19 |
| - hp := close |
20 |
| - hp |
21 |
| - nrtr := hp * (1 - percentage) |
22 |
| - if close <= nrtr |
23 |
| - trend := -1 |
24 |
| - lp := close |
25 |
| - nrtr := lp * (1 + percentage) |
26 |
| - nrtr |
| 21 | + if close > hp |
| 22 | + hp := close |
| 23 | + hp |
| 24 | + nrtr := hp * (1 - percentage) |
| 25 | + if close <= nrtr |
| 26 | + trend := -1 |
| 27 | + lp := close |
| 28 | + nrtr := lp * (1 + percentage) |
| 29 | + nrtr |
27 | 30 | else
|
28 |
| - if close < lp |
29 |
| - lp := close |
30 |
| - lp |
31 |
| - nrtr := lp * (1 + percentage) |
32 |
| - if close > nrtr |
33 |
| - trend := 1 |
34 |
| - hp := close |
35 |
| - nrtr := hp * (1 - percentage) |
36 |
| - nrtr |
37 |
| - |
38 |
| -var color longColor = color.green |
39 |
| -var color shortColor = color.red |
40 |
| -var color textColor = color.white |
41 |
| - |
42 |
| -longStopPlot = plot(trend == 1 ? nrtr : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor) |
| 31 | + if close < lp |
| 32 | + lp := close |
| 33 | + lp |
| 34 | + nrtr := lp * (1 + percentage) |
| 35 | + if close > nrtr |
| 36 | + trend := 1 |
| 37 | + hp := close |
| 38 | + nrtr := hp * (1 - percentage) |
| 39 | + nrtr |
| 40 | + |
| 41 | +const color textColor = color.white |
| 42 | +const color longColor = color.green |
| 43 | +const color shortColor = color.red |
| 44 | +const color longFillColor = color.new(color.green, 85) |
| 45 | +const color shortFillColor = color.new(color.red, 85) |
| 46 | + |
| 47 | +longStopPlot = plot(trend == 1 ? nrtr : na, title = 'Long Stop', style = plot.style_linebr, linewidth = 2, color = longColor) |
43 | 48 | buySignal = trend == 1 and trend[1] == -1
|
44 |
| -plotshape(buySignal ? nrtr : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor, transp=0) |
45 |
| -plotshape(buySignal and showLabels ? nrtr : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=textColor, transp=0) |
| 49 | +plotshape(buySignal ? nrtr : na, title = 'Long Stop Start', location = location.absolute, style = shape.circle, size = size.tiny, color = longColor) |
| 50 | +plotshape(buySignal and showLabels ? nrtr : na, title = 'Buy Label', text = 'Buy', location = location.absolute, style = shape.labelup, size = size.tiny, color = longColor, textcolor = textColor) |
46 | 51 |
|
47 |
| -shortStopPlot = plot(trend == 1 ? na : nrtr, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor) |
| 52 | +shortStopPlot = plot(trend == 1 ? na : nrtr, title = 'Short Stop', style = plot.style_linebr, linewidth = 2, color = shortColor) |
48 | 53 | sellSignal = trend == -1 and trend[1] == 1
|
49 |
| -plotshape(sellSignal ? nrtr : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor, transp=0) |
50 |
| -plotshape(sellSignal and showLabels ? nrtr : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=textColor, transp=0) |
| 54 | +plotshape(sellSignal ? nrtr : na, title = 'Short Stop Start', location = location.absolute, style = shape.circle, size = size.tiny, color = shortColor) |
| 55 | +plotshape(sellSignal and showLabels ? nrtr : na, title = 'Sell Label', text = 'Sell', location = location.absolute, style = shape.labeldown, size = size.tiny, color = shortColor, textcolor = textColor) |
51 | 56 |
|
52 |
| -midPricePlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0, display=display.none, editable=false) |
| 57 | +midPricePlot = plot(ohlc4, title = '', display = display.none, editable = false) |
53 | 58 |
|
54 |
| -longFillColor = applyRibbon ? (trend == 1 ? longColor : na) : na |
55 |
| -shortFillColor = applyRibbon ? (trend == -1 ? shortColor : na) : na |
56 |
| -fill(midPricePlot, longStopPlot, title="Long Ribbon", color=longFillColor) |
57 |
| -fill(midPricePlot, shortStopPlot, title="Short Ribbon", color=shortFillColor) |
| 59 | +fill(midPricePlot, longStopPlot, title = 'Long State Filling', color = (highlightState and trend == 1 ? longFillColor : na)) |
| 60 | +fill(midPricePlot, shortStopPlot, title = 'Short State Filling', color = (highlightState and trend == -1 ? shortFillColor : na)) |
58 | 61 |
|
59 |
| -changeCond = trend != trend[1] |
60 |
| -alertcondition(changeCond, title="Alert: NRTR Direction Change", message="NRTR has changed direction!") |
61 |
| -alertcondition(buySignal, title="Alert: NRTR Buy", message="NRTR Buy!") |
62 |
| -alertcondition(sellSignal, title="Alert: NRTR Sell", message="NRTR Sell!") |
| 62 | +alertcondition(trend != trend[1], title = 'NRTR Direction Change', message = 'NRTR has changed direction, {{exchange}}:{{ticker}}') |
| 63 | +alertcondition(buySignal, title = 'NRTR Buy', message = 'NRTR Buy, {{exchange}}:{{ticker}}') |
| 64 | +alertcondition(sellSignal, title = 'NRTR Sell', message = 'NRTR Sell, {{exchange}}:{{ticker}}') |
0 commit comments