-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPinBarReversal.pine
More file actions
188 lines (166 loc) Β· 10.4 KB
/
Copy pathPinBarReversal.pine
File metadata and controls
188 lines (166 loc) Β· 10.4 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//@version=6
indicator("π Pin Bar Reversal", shorttitle="PBR", overlay=true,
max_lines_count=50, max_labels_count=100, max_boxes_count=10)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// βοΈ INPUTS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
wickRatio = input.float(1.5, "Wick/Body Ratio", minval=0.5, maxval=5.0, step=0.1, group="Pin Bar Geometry")
bodyPct = input.float(0.50, "Body Zone (max %)", minval=0.1, maxval=0.9, step=0.05, group="Pin Bar Geometry")
cooldown = input.int(3, "Cooldown Bars", minval=1, maxval=50, group="Pin Bar Geometry",
tooltip="Minimum bars between signals β prevents stacking")
swingStr = input.int(2, "Swing Strength", minval=1, maxval=50, group="Support and Resistance")
srLookback = input.int(100, "S/R Lookback Bars", minval=3, maxval=500, group="Support and Resistance")
srProximity = input.float(2.00, "Proximity x ATR", minval=0.05, maxval=5.0, step=0.05, group="Support and Resistance")
atrLen = input.int(14, "ATR Period", minval=3, maxval=50, group="Support and Resistance")
bullCol = input.color(#089981, "CALL Color", group="Display")
bearCol = input.color(#f23645, "PUT Color", group="Display")
showLines = input.bool(true, "Show S/R Lines", group="Display")
showWatch = input.bool(true, "Show Live Signal Watch", group="Display")
useTrend = input.bool(false, "EMA Trend Filter", group="Display",
tooltip="CALL only above EMA, PUT only below EMA")
emaLen = input.int(50, "EMA Length", minval=5, maxval=500, group="Display")
useRSI = input.bool(false, "RSI Filter", group="Display",
tooltip="CALL only when RSI oversold, PUT only when overbought")
rsiLen = input.int(14, "RSI Length", minval=2, maxval=50, group="Display")
rsiOS = input.int(45, "RSI Oversold (CALL <)", minval=10, maxval=60, group="Display")
rsiOB = input.int(55, "RSI Overbought (PUT >)", minval=40, maxval=90, group="Display")
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// π GEOMETRY
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
atr = ta.atr(atrLen)
ema = ta.ema(close, emaLen)
rsi = ta.rsi(close, rsiLen)
eps = 1e-10
body = math.max(math.abs(close - open), eps)
upWick = high - math.max(close, open)
loWick = math.min(close, open) - low
cRange = math.max(high - low, eps)
bodyTop = math.max(close, open)
bodyBot = math.min(close, open)
prox = srProximity * atr
aboveEma = close > ema
belowEma = close < ema
rsiOkBull = not useRSI or rsi < rsiOS
rsiOkBear = not useRSI or rsi > rsiOB
bullPin = loWick >= wickRatio * body and (bodyBot - low) / cRange <= bodyPct and loWick > upWick
bearPin = upWick >= wickRatio * body and (high - bodyTop) / cRange <= bodyPct and upWick > loWick
plot(useTrend ? ema : na, "EMA", color=color.new(#d4af37, 30), linewidth=2)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// π SWING LEVEL ARRAYS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rawPH = ta.pivothigh(swingStr, swingStr)
rawPL = ta.pivotlow(swingStr, swingStr)
var array<float> resP = array.new_float()
var array<int> resB = array.new_int()
var array<float> supP = array.new_float()
var array<int> supB = array.new_int()
if not na(rawPH)
array.push(resP, rawPH)
array.push(resB, bar_index - swingStr)
if array.size(resP) > 60
array.shift(resP)
array.shift(resB)
if not na(rawPL)
array.push(supP, rawPL)
array.push(supB, bar_index - swingStr)
if array.size(supP) > 60
array.shift(supP)
array.shift(supB)
findNearest(lvlArr, barArr, tipPrice, p) =>
result = float(na)
bestDist = 999999.0
sz = array.size(lvlArr)
if sz > 0
for k = 0 to sz - 1
pvP = array.get(lvlArr, k)
pvB = array.get(barArr, k)
barsAgo = bar_index - pvB
dist = math.abs(tipPrice - pvP)
if barsAgo > 0 and barsAgo <= srLookback and dist <= p and dist < bestDist
bestDist := dist
result := pvP
result
nearestSup = findNearest(supP, supB, low, prox)
nearestRes = findNearest(resP, resB, high, prox)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// π¦ SIGNALS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
trendOkBull = not useTrend or aboveEma
trendOkBear = not useTrend or belowEma
var int lastSigBar = -999
cooledDown = (bar_index - lastSigBar) >= cooldown
callSig = bullPin and not na(nearestSup) and trendOkBull and rsiOkBull and cooledDown
putSig = bearPin and not na(nearestRes) and trendOkBear and rsiOkBear and cooledDown
if callSig or putSig
lastSigBar := bar_index
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// π ARROWS + BAR GLOW
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
plotshape(callSig, "CALL", shape.arrowup, location.belowbar, bullCol, size=size.large, text="CALL", textcolor=bullCol)
plotshape(putSig, "PUT", shape.arrowdown, location.abovebar, bearCol, size=size.large, text="PUT", textcolor=bearCol)
bgcolor(callSig ? color.new(bullCol, 82) : na, title="CALL Glow")
bgcolor(putSig ? color.new(bearCol, 82) : na, title="PUT Glow")
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// π S/R LINES (max 5 each side)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
var array<line> rLns = array.new_line()
var array<line> sLns = array.new_line()
if showLines and barstate.isconfirmed
if not na(rawPH)
ln = line.new(bar_index - swingStr, rawPH, bar_index, rawPH,
color=color.new(bearCol, 50), width=1, style=line.style_dashed, extend=extend.right)
array.push(rLns, ln)
if array.size(rLns) > 5
line.delete(array.shift(rLns))
if not na(rawPL)
ln = line.new(bar_index - swingStr, rawPL, bar_index, rawPL,
color=color.new(bullCol, 50), width=1, style=line.style_dashed, extend=extend.right)
array.push(sLns, ln)
if array.size(sLns) > 5
line.delete(array.shift(sLns))
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// ποΈ LIVE SIGNAL WATCH
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
var label watchLbl = na
if showWatch and barstate.islast
label.delete(watchLbl)
lb = math.max(math.abs(close - open), eps)
lu = high - math.max(close, open)
ll = math.min(close, open) - low
lr = math.max(high - low, eps)
lbt = math.max(close, open)
lbb = math.min(close, open)
lBull = ll >= wickRatio * lb and (lbb - low) / lr <= bodyPct
lBear = lu >= wickRatio * lb and (high - lbt) / lr <= bodyPct
lSup = findNearest(supP, supB, low, prox)
lRes = findNearest(resP, resB, high, prox)
lCall = lBull and not na(lSup)
lPut = lBear and not na(lRes)
var int pct = 0
var string txt = ""
pct := 0
txt := ""
if lBull
pct := math.min(math.round(ll / (wickRatio * lb) * 100), 100)
else if lBear
pct := math.min(math.round(lu / (wickRatio * lb) * 100), 100)
if lCall
txt := "CALL FORMING " + str.tostring(pct) + "% | Pin + Support"
else if lPut
txt := "PUT FORMING " + str.tostring(pct) + "% | Pin + Resistance"
else if lBull
txt := "Bull Pin forming (no S/R nearby)"
else if lBear
txt := "Bear Pin forming (no S/R nearby)"
if txt != ""
col = lCall ? bullCol : lPut ? bearCol : color.gray
yPos = lCall or lBull ? low - atr * 0.6 : high + atr * 0.6
stl = lCall or lBull ? label.style_label_up : label.style_label_down
watchLbl := label.new(bar_index, yPos, txt,
style=stl, color=color.new(col, 15), textcolor=color.white, size=size.normal)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// π ALERTS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
alertcondition(callSig, "CALL Signal", "Pin Bar CALL - enter on next candle open")
alertcondition(putSig, "PUT Signal", "Pin Bar PUT - enter on next candle open")
alertcondition(callSig or putSig, "Any Signal", "Pin Bar signal fired")