-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy-ha-confluence-fixed-stop.pine
103 lines (86 loc) · 5.29 KB
/
strategy-ha-confluence-fixed-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
// 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 (fixed 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"))
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)
slPerc = input(defval = 0.5, title = "Stop Loss %", minval = 0.5, step = 0.5, maxval = 100) / 100
tpPerc = input(defval = 1, title = "Take Profit %", minval = 0.5, step = 0.5, maxval = 100) / 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))
longSlLevel = strategy.position_avg_price * (1 - slPerc)
longTpLevel = strategy.position_avg_price * (1 + tpPerc)
shortSlLevel = strategy.position_avg_price * (1 + slPerc)
shortTpLevel = strategy.position_avg_price * (1 - tpPerc)
// Long entries
strategy.entry(id = "long", long = strategy.long, when = goLong)
strategy.exit("exit", "long", stop = longSlLevel, limit = longTpLevel)
// Short entries
strategy.entry(id = "short", long = strategy.short, when = goShort)
strategy.exit("exit", "short", stop = shortSlLevel, limit = shortTpLevel)
// Entry line
entryPlot = plot(strategy.position_avg_price, color=color.gray, style=plot.style_linebr, linewidth=1)
// Long
longSlPlot = plot(strategy.position_size < 0 ? na : longSlLevel, color=color.red, style=plot.style_linebr, linewidth=1)
longTpPlot = plot(strategy.position_size < 0 ? na : longTpLevel, color=color.green, style=plot.style_linebr, linewidth=1)
fill(longSlPlot, entryPlot, color=color.red)
fill(entryPlot, longTpPlot, color=color.green)
// Short
shortSlPlot = plot(strategy.position_size > 0 ? na : shortSlLevel, color=color.red, style=plot.style_linebr, linewidth=1)
shortTpPlot = plot(strategy.position_size > 0 ? na : shortTpLevel, color=color.green, style=plot.style_linebr, linewidth=1)
fill(entryPlot, shortSlPlot, color=color.red)
fill(entryPlot, shortTpPlot, color=color.green)