-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy-ha-confluence-trailing-stop.pine
116 lines (94 loc) · 5.32 KB
/
strategy-ha-confluence-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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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) HA + Confluence (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"])
checkSupertrend = input(defval = "Yes", title = "Check Supertrend Indicator", options = ["Yes", "No"])
checkRsiIndicator = input(defval = "Yes", title = "Check RSI Div Indicator", options = ["Yes", "No"])
checkRsiLevel = input(defval = "Yes", title = "Check RSI Level", options = ["Yes", "No"])
rsiBullLevel = input(defval = 50, title = "RSI Bull Level", minval = 1, maxval = 100, step = 1)
rsiBearLevel = input(defval = 50, title = "RSI Bear Level", minval = 1, maxval = 100, step = 1)
trailingStopPerc = input(defval = 1, title = "Stop Loss %", minval = .1, maxval = 100, step = .1) / 100
// Supertrend
atrPeriod = input(10, "Supertrend ATR Length")
factor = input(3, "Supertrend Factor")
// RSI DIV
lenFast = input(5, minval=1, title="Length Fast RSI")
lenSlow = input(14, minval=1, title="Length Slow RSI")
// Inputs //
// Calculation HA Values
haopen = 0.0
haclose = ((open + high + low + close)/4)
haopen := na(haopen[1]) ? (open + close)/2 : (haopen[1] + haclose[1]) / 2
hahigh = max(high, max(haopen, haclose))
halow = min(low, min(haopen, haclose))
// HA colors
hacolor = haclose > haopen ? color.green : color.red
// Signals
turnGreen = haclose > haopen and haclose[1] <= haopen[1]
turnRed = haclose <= haopen and haclose[1] > haopen[1]
plotshape(turnGreen, style = shape.arrowup, location = location.belowbar, color = color.green)
plotshape(turnRed, style = shape.arrowdown, location = location.abovebar, color = color.red)
// Supertrend Indicator
[supertrend, direction] = supertrend(factor, atrPeriod)
isSupertrendBullish = direction < 0
isSupertrendBearish = direction > 0
// Debug
// plotshape(isSupertrendBullish, style = shape.triangleup, location = location.belowbar, color = color.green)
// plotshape(isSupertrendBearish, style = shape.triangledown, location = location.abovebar, color = color.red)
// 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))
//plotfast = plot(rsiFast, color=blue)
//plotslow = plot(rsiSlow, color=orange)
divergence = rsiFast - rsiSlow
isDivBullish = divergence > 0
isDivBearish = divergence < 0
// plotdiv = plot(divergence, color = divergence > 0 ? color.lime: color.red, linewidth = 2)
//band1 = hline(70,color=green)
//band0 = hline(30,color=red)
// band = hline(0)
// Normal RSI
rsi = rsi(close, lenSlow)
// Conditions
timePeriod = time >= fromDate
notInTrade = strategy.position_size == 0
goLong = turnGreen and notInTrade and timePeriod and (checkSupertrend == 'No' or (checkSupertrend == 'Yes' and isSupertrendBullish)) and (checkRsiIndicator == 'No' or (checkRsiIndicator == 'Yes' and isDivBullish)) and (tradeDirection == "All" or tradeDirection == "Longs") and (checkRsiLevel == "No" or (checkRsiLevel == "Yes" and rsi < rsiBullLevel))
goShort = turnRed and notInTrade and timePeriod and (checkSupertrend == 'No' or (checkSupertrend == 'Yes' and isSupertrendBearish)) and (checkRsiIndicator == 'No' or (checkRsiIndicator == 'Yes' and isDivBearish)) and (tradeDirection == "All" or tradeDirection == "Shorts") and (checkRsiLevel == "No" or (checkRsiLevel == "Yes" and rsi > rsiBearLevel))
// 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)