-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy-rsi-stoch-trailing-stop.pine
97 lines (80 loc) · 4.49 KB
/
strategy-rsi-stoch-trailing-stop.pine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ossy1717
//@version=4
strategy("(Strategy) RSI + STOCH Overb/s (trailing stop)", overlay = true, initial_capital = 1000, default_qty_value = 1000, default_qty_type = strategy.cash, process_orders_on_close=true)
// Inputs //
tradeDirection = input(defval = "All", title = "Long, Short or All Setups?", options = ["All", "Longs", "Shorts"])
fromDate = input(title="From Date", type=input.time, defval=timestamp("01 Jan 2021 00:00 -0400"))
showTrailingStopLine = input(defval = "No", title = "Show Trailing Stop Line", options = ["Yes", "No"])
trailingStopPerc = input(defval = 1, title = "Trailing SL %", minval = .1, maxval = 100, step = .1) / 100
checkRsiIndicator = input(defval = "Yes", title = "Check RSI Div Indicator", options = ["Yes", "No"])
lenFast = input(5, minval=1, title="Length Fast RSI")
lenSlow = input(14, minval=1, title="Length Slow RSI")
stochLen = input(14, minval = 1, title = "Stoch Length")
periodK = input(3, title="K", minval=1)
periodD = input(3, title="D", minval=1)
rsiLen = input(14, minval = 1, title = "RSI Length")
rsiOverbought = input(defval = 70, title = "RSI Overbought", minval = 1, maxval = 100, step = 1)
rsiOversold = input(defval = 30, title = "RSI Oversold", minval = 1, maxval = 100, step = 1)
// Inputs //
// RSI
rsi = rsi(close, rsiLen)
// Stoch
stk = sma(stoch(close, high, low, stochLen), 3)
k = sma(stoch(rsi, rsi, rsi, stochLen), periodK) // Blue
d = sma(k, periodD)
// RSI DIV Indicator
upFast = rma(max(change(close), 0), lenFast)
downFast = rma(-min(change(close), 0), lenFast)
rsiFast = downFast == 0 ? 100 : upFast == 0 ? 0 : 100 - (100 / (1 + upFast / downFast))
upSlow = rma(max(change(close), 0), lenSlow)
downSlow = rma(-min(change(close), 0), lenSlow)
rsiSlow = downSlow == 0 ? 100 : upSlow == 0 ? 0 : 100 - (100 / (1 + upSlow / downSlow))
divergence = rsiFast - rsiSlow
isRSIDivBullish = divergence > 0
isRSIDivBearish = divergence < 0
// Conditions
isRSIOverbought = rsi >= rsiOverbought
isRSIOversold = rsi <= rsiOversold
isStochOverbought = k >= rsiOverbought and d >= rsiOverbought
isStochOversold = k <= rsiOversold and d <= rsiOversold
timePeriod = time >= fromDate
notInTrade = strategy.position_size == 0
goLong = notInTrade and timePeriod and isRSIOversold and isStochOversold and (checkRsiIndicator == 'No' or (checkRsiIndicator == 'Yes' and isRSIDivBullish)) and (tradeDirection == "All" or tradeDirection == "Longs")
goShort = notInTrade and timePeriod and isRSIOverbought and isStochOverbought and (checkRsiIndicator == 'No' or (checkRsiIndicator == 'Yes' and isRSIDivBearish)) and (tradeDirection == "All" or tradeDirection == "Shorts")
// Visuals
// RSI Debug //
// label.new(bar_index, goLong ? low : high, yloc = goLong ? yloc.belowbar : yloc.abovebar, text=tostring(rsi, '#'), textcolor=color.white, color = color.green, style = goLong ? label.style_label_up : label.style_label_down, size = size.small)
// RSI Debug //
plotshape(goLong, style = shape.arrowup, location = location.belowbar, color = color.green)
plotshape(goShort, style = shape.arrowdown, location = location.abovebar, color = color.red)
// Determine trail stop loss prices
longStopPrice = 0.0, shortStopPrice = 0.0
longStopPrice := if (strategy.position_size > 0)
stopValue = low * (1 - trailingStopPerc)
max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if (strategy.position_size < 0)
stopValue = high * (1 + trailingStopPerc)
min(stopValue, shortStopPrice[1])
else
999999
// Long entries
strategy.entry(id = "long", long = strategy.long, when = goLong)
strategy.exit("exit", "long", stop=longStopPrice)
// Short entries
strategy.entry(id = "short", long = strategy.short, when = goShort)
strategy.exit("exit", "short", stop=shortStopPrice)
// Plot stop loss values for confirmation
longTrailingStopLine = plot(series=(showTrailingStopLine == "Yes" and strategy.position_size > 0) ? longStopPrice : na,
color=color.orange, style=plot.style_stepline,
linewidth=1, title="Long Trail Stop")
shortTrailingStopLine = plot(series=(showTrailingStopLine == "Yes" and strategy.position_size < 0) ? shortStopPrice : na,
color=color.orange, style=plot.style_stepline,
linewidth=1, title="Short Trail Stop")
// Entry line
entryPlot = plot(low, color=color.rgb(0, 0, 0, 100), style=plot.style_linebr, linewidth=1)
// SL area fill
fill(longTrailingStopLine, entryPlot, color=color.orange)
fill(entryPlot, shortTrailingStopLine, color=color.orange)