-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstructure-aware-volume-profiling-per-swing.pine
More file actions
424 lines (311 loc) · 16.9 KB
/
Copy pathstructure-aware-volume-profiling-per-swing.pine
File metadata and controls
424 lines (311 loc) · 16.9 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga
//@version=6
indicator("Swing Profile [BigBeluga]", overlay = true, max_labels_count = 500, max_boxes_count = 500, calc_bars_count = 2000, max_bars_back = 5000)
// INPUTS ---------------------------------------------------------------------
swingLen = input.int(50, "Swing Length", minval = 10)
swingColorUp = input.color(color.lime, "▲", inline = "colors")
swingColorDown = input.color(color.orange, "▼", inline = "colors")
showProfile = input.bool(true, "Profile", group = "Swing Volume Profile")
showHeatMap = input.bool(false, "HeatMap", group = "Swing Volume Profile")
showZigZag = input.bool(true, "ZigZag", inline = "Zz")
zigZagStyleInput = input.string("Dotted", "", ["Dotted", "Dashed", "Solid"], inline = "Zz")
showPOC = input.bool(true, "PoC", "", inline = "Poc", group = "Point of Control")
pocWidthInput = input.int(2, "", inline = "Poc", group = "Point of Control")
pocColor = input.color(color.red, "", inline = "Poc", group = "Point of Control")
transparentNA = color.new(color.black, 100)
string dataSizeInput = input.string("Small", title = "Data Size", options = ["Tiny", "Small", "Normal", "Large", "Huge"])
// Size switcher for labels
getLabelSize() =>
switch dataSizeInput
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
"Huge" => size.huge
=> size.tiny
getZigZagLineStyle() =>
switch zigZagStyleInput
"Dotted" => line.style_dotted
"Dashed" => line.style_dashed
"Solid" => line.style_solid
// Persistent storage containers
var polyline profilePoly = na
var dataLabel = label(na)
var pocLineHandle = line(na)
var profileBoxes = array.new<box>()
// Structs for swing points
type SwingData
float price
int index
var highSwing = SwingData.new(na, na)
var lowSwing = SwingData.new(na, na)
// Direction flag: true = down move, false = up move
var isDownMove = false
// CALCULATIONS ---------------------------------------------------------------
// Detect swing highs and lows
highestSwingHigh = ta.highest(swingLen)
lowestSwingLow = ta.lowest(swingLen)
if high == highestSwingHigh
isDownMove := true
if low == lowestSwingLow
isDownMove := false
// Store completed swing high
if high[1] == highestSwingHigh[1] and high < highestSwingHigh
highSwing.index := bar_index[1]
highSwing.price := high[1]
// Store completed swing low
if low[1] == lowestSwingLow[1] and low > lowestSwingLow
lowSwing.index := bar_index[1]
lowSwing.price := low[1]
// Default bin size based on ATR
priceBinSize = ta.atr(200) * 0.5
// HISTORICAL SWING LEG -------------------------------------------------------
// When direction flips we finalize previous swing leg and build full profile
if isDownMove != isDownMove[1]
// Clear previous drawings
profilePoly.delete()
dataLabel.delete()
for b in profileBoxes
b.delete()
profileBoxes.clear()
swingMainColor = isDownMove ? swingColorDown : swingColorUp
// Draw zigzag connector
if showZigZag
line.new(highSwing.index, highSwing.price, lowSwing.index, lowSwing.price, color = swingMainColor, style = getZigZagLineStyle())
// Determine swing boundaries
swingBottom = math.min(highSwing.price, lowSwing.price)
swingTop = math.max(highSwing.price, lowSwing.price)
// Number of volume bins
binCount = int(math.abs(highSwing.price - lowSwing.price) / priceBinSize)
binStep = math.abs(highSwing.price - lowSwing.price) / binCount
lastSwingIndex = math.max(highSwing.index, lowSwing.index)
prevSwingIndex = math.min(highSwing.index, lowSwing.index)
// Arrays for volume accumulation
volumeBins = array.new<float>(binCount, 0.)
profilePoints = array.new<chart.point>()
buyVolumeBins = array.new<float>(binCount, 0.)
sellVolumeBins = array.new<float>(binCount, 0.)
barOffset = bar_index - lastSwingIndex
// Accumulate volume per price bin
for i = barOffset to (lastSwingIndex - prevSwingIndex) + barOffset
candleClose = close[i]
candleOpen = open[i]
candleVol = volume[i]
for k = 0 to binCount - 1
binMidPrice = swingBottom + (k * binStep) + (binStep / 2)
if math.abs(binMidPrice - candleClose) < binStep
volumeBins.set(k, volumeBins.get(k) + candleVol)
if candleClose > candleOpen
buyVolumeBins.set(k, buyVolumeBins.get(k) + candleVol)
else
sellVolumeBins.set(k, sellVolumeBins.get(k) + candleVol)
// Draw boxes and build profile outline
for k = 0 to binCount - 1
binLow = swingBottom + (k * binStep)
binHigh = binLow + binStep
volumeRatio = volumeBins.get(k) / volumeBins.max()
profileWidth = int(volumeRatio * ((lastSwingIndex - prevSwingIndex) / 2))
// Draw POC line
if volumeRatio == 1 and showPOC
line.new(showProfile ? prevSwingIndex + profileWidth : prevSwingIndex, math.avg(binLow, binHigh), lastSwingIndex, math.avg(binLow, binHigh), color = pocColor, width = pocWidthInput)
// Create volume box
box.new(
showProfile ? prevSwingIndex + profileWidth : prevSwingIndex,
binHigh,
showHeatMap ? lastSwingIndex : prevSwingIndex,
binLow,
transparentNA,
0,
bgcolor = showProfile or showHeatMap ? color.from_gradient(volumeRatio, 0, 1, color.new(swingMainColor, 90), color.new(volumeRatio == 1 and showPOC ? pocColor : swingMainColor, 50)) : transparentNA,
text = showHeatMap ? "" : volumeRatio == 1 ? str.tostring(volumeBins.max(), format.volume) : ""
)
// Build polygon outline
if k == 0
profilePoints.push(chart.point.from_index(prevSwingIndex, binLow))
profilePoints.push(chart.point.from_index(prevSwingIndex + profileWidth, binLow))
profilePoints.push(chart.point.from_index(prevSwingIndex + profileWidth, binHigh))
if k == binCount - 1
profilePoints.push(chart.point.from_index(prevSwingIndex, binHigh))
// Draw outer polyline
if showProfile
polyline.new(profilePoints, false, true, line_color = showHeatMap ? color.new(swingMainColor, 50) : swingMainColor)
// Summary label for historical swing
swingArrowText = isDownMove ? "▼" : "▲"
swingTooltip =
"Total Volume: " + str.tostring(volumeBins.sum(), format.volume)
+ "\nBuy Volume: " + str.tostring(buyVolumeBins.sum(), format.volume)
+ "\nSell Volume: " + str.tostring(sellVolumeBins.sum(), format.volume)
+ "\nDelta Volume: " + str.tostring((buyVolumeBins.sum() - sellVolumeBins.sum()) / volumeBins.sum() * 100, format.percent)
label.new(prevSwingIndex, isDownMove ? swingTop : swingBottom, swingArrowText,
style = isDownMove ? label.style_label_down : label.style_label_up,
color = transparentNA,
textcolor = swingMainColor,
size = getLabelSize(),
tooltip = swingTooltip)
// REAL-TIME SWING LEG --------------------------------------------------------
// Real-time profile rebuilding while swing is still forming
if not isDownMove
// Clear boxes on every update
for b in profileBoxes
b.delete()
profileBoxes.clear()
if barstate.islast
swingMainColor = chart.fg_color
// Remove previous zigzag preview line
line.delete(line.new(highSwing.index, highSwing.price, lowSwing.index, lowSwing.price, color = swingMainColor, style = line.style_dashed)[1])
swingBottom = math.min(highSwing.price, lowSwing.price)
swingTop = math.max(highSwing.price, lowSwing.price)
binCount = int(math.abs(highSwing.price - lowSwing.price) / priceBinSize)
binStep = math.abs(highSwing.price - lowSwing.price) / binCount
lastSwingIndex = math.max(highSwing.index, lowSwing.index)
prevSwingIndex = math.min(highSwing.index, lowSwing.index)
volumeBins = array.new<float>(binCount, 0.)
profilePoints = array.new<chart.point>()
buyVolumeBins = array.new<float>(binCount, 0.)
sellVolumeBins = array.new<float>(binCount, 0.)
barOffset = bar_index - lastSwingIndex
// Accumulate volume in real time
for i = barOffset to (lastSwingIndex - prevSwingIndex) + barOffset
candleClose = close[i]
candleOpen = open[i]
candleVol = volume[i]
for k = 0 to binCount - 1
binMidPrice = swingBottom + (k * binStep) + (binStep / 2)
if math.abs(binMidPrice - candleClose) < binStep
volumeBins.set(k, volumeBins.get(k) + candleVol)
if candleClose > candleOpen
buyVolumeBins.set(k, buyVolumeBins.get(k) + candleVol)
else
sellVolumeBins.set(k, sellVolumeBins.get(k) + candleVol)
// Draw real-time boxes
for k = 0 to binCount - 1
binLow = swingBottom + (k * binStep)
binHigh = binLow + binStep
volumeRatio = volumeBins.get(k) / volumeBins.max()
profileWidth = int(volumeRatio * ((lastSwingIndex - prevSwingIndex) / 2))
if volumeRatio == 1 and showPOC
pocLineHandle := line.new(showProfile ? prevSwingIndex + profileWidth : prevSwingIndex, math.avg(binLow, binHigh), lastSwingIndex, math.avg(binLow, binHigh), color = pocColor, width = pocWidthInput)
line.delete(pocLineHandle[1])
profileBoxes.push(box.new(
showProfile ? prevSwingIndex + profileWidth : prevSwingIndex,
binHigh,
showHeatMap ? lastSwingIndex : prevSwingIndex,
binLow,
transparentNA,
0,
bgcolor = showProfile or showHeatMap ? color.from_gradient(volumeRatio, 0, 1, color.new(swingMainColor, 90), color.new(volumeRatio == 1 and showPOC ? pocColor : swingMainColor, 50)) : transparentNA,
text = showHeatMap ? "" : volumeRatio == 1 ? str.tostring(volumeBins.max(), format.volume) : ""
))
if k == 0
profilePoints.push(chart.point.from_index(prevSwingIndex, binLow))
profilePoints.push(chart.point.from_index(prevSwingIndex + profileWidth, binLow))
profilePoints.push(chart.point.from_index(prevSwingIndex + profileWidth, binHigh))
if k == binCount - 1
profilePoints.push(chart.point.from_index(prevSwingIndex, binHigh))
// Update polyline in real time
if showProfile
polyline.delete(profilePoly[1])
profilePoly := polyline.new(profilePoints, false, true, line_color = showHeatMap ? color.new(swingMainColor, 50) : swingMainColor)
// Label with summary data
swingTooltip = "T - Total Volume\nB - Buy Volume\nS - Sell Volume\nD - Delta Volume"
swingDataText =
"T: " + str.tostring(volumeBins.sum(), format.volume)
+ "\nB: " + str.tostring(buyVolumeBins.sum(), format.volume)
+ "\nS: " + str.tostring(sellVolumeBins.sum(), format.volume)
+ "\nD: " + str.tostring((buyVolumeBins.sum() - sellVolumeBins.sum()) / volumeBins.sum() * 100, format.percent)
dataLabel := label.new(prevSwingIndex, not isDownMove ? swingTop : swingBottom, swingDataText,
style = not isDownMove ? label.style_label_down : label.style_label_up,
color = transparentNA,
textcolor = swingMainColor,
size = getLabelSize(),
tooltip = swingTooltip,
textalign = text.align_left)
label.delete(dataLabel[1])
// MIRROR OF ABOVE FOR isDownMove == true ------------------------------------------
// Mirror logic for bullish direction
else
for b in profileBoxes
b.delete()
profileBoxes.clear()
if barstate.islast
swingMainColor = chart.fg_color
line.delete(line.new(highSwing.index, highSwing.price, lowSwing.index, lowSwing.price, color = swingMainColor, style = line.style_dashed)[1])
swingBottom = math.min(highSwing.price, lowSwing.price)
swingTop = math.max(highSwing.price, lowSwing.price)
binCount = int(math.abs(highSwing.price - lowSwing.price) / priceBinSize)
binStep = math.abs(highSwing.price - lowSwing.price) / binCount
lastSwingIndex = math.max(highSwing.index, lowSwing.index)
prevSwingIndex = math.min(highSwing.index, lowSwing.index)
volumeBins = array.new<float>(binCount, 0.)
profilePoints = array.new<chart.point>()
buyVolumeBins = array.new<float>(binCount, 0.)
sellVolumeBins = array.new<float>(binCount, 0.)
barOffset = bar_index - lastSwingIndex
for i = barOffset to (lastSwingIndex - prevSwingIndex) + barOffset
candleClose = close[i]
candleOpen = open[i]
candleVol = volume[i]
for k = 0 to binCount - 1
binMidPrice = swingBottom + (k * binStep) + (binStep / 2)
if math.abs(binMidPrice - candleClose) < binStep
volumeBins.set(k, volumeBins.get(k) + candleVol)
if candleClose > candleOpen
buyVolumeBins.set(k, buyVolumeBins.get(k) + candleVol)
else
sellVolumeBins.set(k, sellVolumeBins.get(k) + candleVol)
for k = 0 to binCount - 1
binLow = swingBottom + (k * binStep)
binHigh = binLow + binStep
volumeRatio = volumeBins.get(k) / volumeBins.max()
profileWidth = int(volumeRatio * ((lastSwingIndex - prevSwingIndex) / 2))
if volumeRatio == 1 and showPOC
pocLineHandle := line.new(showProfile ? prevSwingIndex + profileWidth : prevSwingIndex, math.avg(binLow, binHigh), lastSwingIndex, math.avg(binLow, binHigh), color = pocColor, width = pocWidthInput)
line.delete(pocLineHandle[1])
profileBoxes.push(box.new(
showProfile ? prevSwingIndex + profileWidth : prevSwingIndex,
binHigh,
showHeatMap ? lastSwingIndex : prevSwingIndex,
binLow,
transparentNA,
0,
bgcolor = showProfile or showHeatMap ? color.from_gradient(volumeRatio, 0, 1, color.new(swingMainColor, 90), color.new(volumeRatio == 1 and showPOC ? pocColor : swingMainColor, 50)) : transparentNA,
text = showHeatMap ? "" : volumeRatio == 1 ? str.tostring(volumeBins.max(), format.volume) : ""
))
if k == 0
profilePoints.push(chart.point.from_index(prevSwingIndex, binLow))
profilePoints.push(chart.point.from_index(prevSwingIndex + profileWidth, binLow))
profilePoints.push(chart.point.from_index(prevSwingIndex + profileWidth, binHigh))
if k == binCount - 1
profilePoints.push(chart.point.from_index(prevSwingIndex, binHigh))
if showProfile
polyline.delete(profilePoly[1])
profilePoly := polyline.new(profilePoints, false, true, line_color = showHeatMap ? color.new(swingMainColor, 50) : swingMainColor)
swingTooltip = "T - Total Volume\nB - Buy Volume\nS - Sell Volume\nD - Delta Volume"
swingDataText =
"T: " + str.tostring(volumeBins.sum(), format.volume)
+ "\nB: " + str.tostring(buyVolumeBins.sum(), format.volume)
+ "\nS: " + str.tostring(sellVolumeBins.sum(), format.volume)
+ "\nD: " + str.tostring((buyVolumeBins.sum() - sellVolumeBins.sum()) / volumeBins.sum() * 100, format.percent)
dataLabel := label.new(prevSwingIndex, not isDownMove ? swingTop : swingBottom, swingDataText,
style = not isDownMove ? label.style_label_down : label.style_label_up,
color = transparentNA,
textcolor = swingMainColor,
size = getLabelSize(),
tooltip = swingTooltip,
textalign = text.align_left)
label.delete(dataLabel[1])
// Smoothing MA inputs
GRP = "Moving Average"
maTypeInput = input.string("None", "Type", options = ["None", "SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group = GRP, display = display.none)
maLengthInput = input.int(25, "Length", group = GRP, display = display.none, active = maTypeInput != "None")
src = input(close, title="Source", group = GRP, active = maTypeInput != "None")
// Smoothing MA Calculation
ma(source, length, MAtype) =>
switch MAtype
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
plot(ma(src, maLengthInput, maTypeInput), color=color.yellow, title="MA")