-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathvp-v2.txt
492 lines (395 loc) · 19.9 KB
/
vp-v2.txt
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//@version=5
indicator("VP", overlay=true, max_boxes_count=500, max_bars_back=1000)
type Box_row
box [] g_box
box [] r_box
type Box_bar
Box_row [] box_rows
type Line_row
line [] l_line
type Pv_row
float poc
float val
float vah
type Line_bar
Line_row [] line_rows
Pv_row [] pv_rows
//==========================
//Inputs
//==========================
sessionType = input.string('Daily')
showProf = input.bool(true, 'Show Volume Profile', group='Volume Profile Display')
// showSbox = input.bool(true, 'Show Session Box', group='Display')
showPoc = input.bool(true, 'Show POC', group='Volume Profile Display')
showVA = input.bool(true, 'Show VAH and VAL', group='Volume Profile Display')
// showVAb = input.bool(false, 'Show Value Area Box', group='Display')
showCur = input.bool(false, 'Show Live Volume', group='Volume Profile Display')
bar_show = input.int(2, 'Number VP', minval=1, group='Volume Profile Settings')
// showLabels = input.bool(true, 'Show Session Lables', group='Display')
// showFx = input.bool(false, 'Show Forex Sessions (no profile)', group='Display')
row = input.int(30, 'Row Size', minval=5, tooltip='The higher the value, the more refined of a profile, but less profiles shown on chart', group='Volume Profile Settings')
VAwid = input.int(70, 'Value Area Volume', minval=1, maxval=100, group='Volume Profile Settings')
dispMode = input.string('Up/Down', 'Volume', ['Total', 'Up/Down', 'Delta'], group='Volume Profile Settings')
volType = input.string('Volume', 'Profile Data Type', group='Volume Profile Settings')
smoothVol = input.bool(false, 'Smooth Volume Data', tooltip='Useful for assets that have very large spikes in volume over large bars - helps create better profiles', group='Volume Profile Settings')
dataTf = ''
bullCol = input.color(color.new(#2962ff,30), 'Up Volume', group='Appearance')
bearCol = input.color(color.new(#fbc02d,30), 'Down Volume', group='Appearance')
// VAbCol = input.color(color.rgb(107, 159, 255, 90), 'Value Area Box', group='Appearance' )
pocCol = input.color(#f44336, 'POC', inline='p', group='Appearance')
pocWid = input.int(1, 'Thickness', inline='p', group='Appearance')
vahCol = input.color(#2962ff, 'VAH', inline='h', group='Appearance')
vahWid = input.int(1, 'Thickness', inline='h', group='Appearance')
valCol = input.color(#2962ff, 'VAL', inline='l', group='Appearance')
valWid = input.int(1, 'Thickness', inline='l', group='Appearance')
// boxBg = input.color(color.rgb(255, 153, 0, 100), 'Box', inline='m', group='Appearance')
// boxWid = input.int(1, 'Thickness', inline='m', group='Appearance')
//==========================
//Constants / Variable Declaration
//==========================
var int zoneStart = 0
var int tokyoStart = 0
var int londonStart = 0
var int nyStart = 0
int lookback = bar_index - zoneStart
var activeZone = false
// Defining arrays that store the information
var vpGreen = array.new_float(row, 0) // Sum of volume on long bars
var vpRed = array.new_float(row, 0) // Same thing but with red bars
var zoneBounds = array.new_float(row, 0) // array that stores the highest value that can be in a zone
//Values to store current intra bar data
var float[] ltfOpen = array.new_float(0)
var float[] ltfClose = array.new_float(0)
var float[] ltfHigh = array.new_float(0)
var float[] ltfLow = array.new_float(0)
var float[] ltfVolume = array.new_float(0)
//Getting OI Data
string userSymbol = syminfo.prefix + ":" + syminfo.ticker
string openInterestTicker = str.format("{0}_OI", userSymbol)
string timeframe = syminfo.type == "futures" and timeframe.isintraday ? "1D" : timeframe.period
deltaOi = request.security(openInterestTicker, timeframe, close-close[1], ignore_invalid_symbol = true)
//Selecting what vol type to use
vol() =>
out = smoothVol ? ta.ema(volume, 5) : volume
if volType == 'Open Interest'
out := deltaOi
out
//Getting intrabar intial data
[dO, dC, dH, dL, dV] = request.security_lower_tf(syminfo.tickerid, dataTf, [open, close, high, low, vol()])
//==========================
//Functions
//==========================
resetProfile(enable) =>
if enable
array.fill(vpGreen, 0)
array.fill(vpRed, 0)
array.clear(ltfOpen)
array.clear(ltfHigh)
array.clear(ltfLow)
array.clear(ltfClose)
array.clear(ltfVolume)
profHigh = ta.highest(high, lookback+1)[1]
profLow = ta.lowest(low, lookback+1)[1]
tr = ta.atr(1)
atr = ta.atr(14)
get_vol(y11, y12, y21, y22, height, vol) =>
nz(math.max(math.min(math.max(y11, y12), math.max(y21, y22)) - math.max(math.min(y11, y12), math.min(y21, y22)), 0) * vol / height)
profileAdd(o, h, l, c, v, g, w) =>
//Array to store how much to distribute in each zone, on scale of 1 for full gap size to 0
zoneDist = array.new_float(row, 0)
distSum = 0.0
// Going over each zone
for i = 0 to array.size(vpGreen) - 1
// Checking to see if cur bar is in zone
zoneTop = array.get(zoneBounds, i)
zoneBot = zoneTop - g
body_top = math.max(c, o)
body_bot = math.min(c, o)
itsgreen = c >= o
topwick = h - body_top
bottomwick = body_bot - l
body = body_top - body_bot
bodyvol = body * v / (2 * topwick + 2 * bottomwick + body)
topwickvol = 2 * topwick * v / (2 * topwick + 2 * bottomwick + body)
bottomwickvol = 2 * bottomwick * v / (2 * topwick + 2 * bottomwick + body)
if volType == 'Volume'
array.set(vpGreen, i, array.get(vpGreen, i) + (itsgreen ? get_vol(zoneBot, zoneTop, body_bot, body_top, body, bodyvol) : 0) + get_vol(zoneBot, zoneTop, body_top, h, topwick, topwickvol) / 2 + get_vol(zoneBot, zoneTop, body_bot, l, bottomwick, bottomwickvol) / 2)
array.set(vpRed, i, array.get(vpRed, i) + (itsgreen ? 0 : get_vol(zoneBot, zoneTop, body_bot, body_top, body, bodyvol)) + get_vol(zoneBot, zoneTop, body_top, h, topwick, topwickvol) / 2 + get_vol(zoneBot, zoneTop, body_bot, l, bottomwick, bottomwickvol) / 2)
// else if volType == 'Open Interest'
// if v > 0
// array.set(vpGreen, i, array.get(vpGreen, i) + get_vol(zoneBot, zoneTop, body_bot, body_top, body, v))// + get_vol(zoneBot, zoneTop, body_top, h, topwick, topwickvol) / 2 + get_vol(zoneBot, zoneTop, body_bot, l, bottomwick, bottomwickvol) / 2)
// if v < 0
// array.set(vpRed, i, array.get(vpRed, i) + get_vol(zoneBot, zoneTop, body_bot, body_top, body, -v))// + get_vol(zoneBot, zoneTop, body_top, h, topwick, topwickvol) / 2 + get_vol(zoneBot, zoneTop, body_bot, l, bottomwick, bottomwickvol) / 2)
calcSession(update) =>
array.fill(vpGreen, 0)
array.fill(vpRed, 0)
if bar_index > lookback and update
gap = (profHigh - profLow) / row
// Defining profile bounds
for i = 0 to row - 1
array.set(zoneBounds, i, profHigh - gap * i)
// Putting each bar inside zone into the volume profile array
if array.size(ltfOpen) > 0
for j = 0 to array.size(ltfOpen) - 1
profileAdd(array.get(ltfOpen, j), array.get(ltfHigh, j), array.get(ltfLow, j), array.get(ltfClose, j), array.get(ltfVolume, j), gap, 1)
pocLevel() =>
float maxVol = 0
int levelInd = 0
for i = 0 to array.size(vpRed) - 1
if array.get(vpRed, i) + array.get(vpGreen, i) > maxVol
maxVol := array.get(vpRed, i) + array.get(vpGreen, i)
levelInd := i
float outLevel = na
if levelInd != array.size(vpRed) - 1
outLevel := array.get(zoneBounds, levelInd) - (array.get(zoneBounds, levelInd) - array.get(zoneBounds, levelInd+1)) / 2
outLevel
valueLevels(poc) =>
float gap = (profHigh - profLow) / row
float volSum = array.sum(vpRed) + array.sum(vpGreen)
float volCnt = 0
float vah = profHigh
float val = profLow
//Finding poc index
int pocInd = 0
for i = 0 to array.size(zoneBounds)-2
if array.get(zoneBounds, i) >= poc and array.get(zoneBounds, i + 1) < poc
pocInd := i
volCnt += (array.get(vpRed, pocInd) + array.get(vpGreen, pocInd))
for i = 1 to array.size(vpRed)
if pocInd + i >= 0 and pocInd + i < array.size(vpRed)
volCnt += (array.get(vpRed, pocInd + i) + array.get(vpGreen, pocInd + i))
if volCnt >= volSum * (VAwid/100)
break
else
val := array.get(zoneBounds, pocInd + i) - gap
if pocInd - i >= 0 and pocInd - i < array.size(vpRed)
volCnt += (array.get(vpRed, pocInd - i) + array.get(vpGreen, pocInd - i))
if volCnt >= volSum * (VAwid/100)
break
else
vah := array.get(zoneBounds, pocInd - i)
[val, vah]
drawNewZone(Box_bar box_bar, Line_bar line_bar, update, able_add = false) =>
float _vah = 0.0
float _val = 0.0
float _poc = 0.0
if bar_index > lookback and update and array.sum(vpGreen) + array.sum(vpRed) > 0 and able_add == true
gap = (profHigh - profLow) / row
float leftMax = bar_index[lookback]
float rightMax = bar_index[int(lookback / 1.4)]
float rightMaxVol = array.max(vpGreen)+array.max(vpRed)
float buffer = gap / 10
// if showLabels
// label.new((bar_index - 1 + int(leftMax))/2, profHigh, sessionType, color=color.rgb(0,0,0,100), textcolor=chart.fg_color)
if showProf
green_boxs = array.new_box(array.size(vpRed), na)
red_boxs = array.new_box(array.size(vpRed), na)
for i = 0 to array.size(vpRed) - 1
greenEnd = int(leftMax + (rightMax - leftMax) * (array.get(vpGreen, i) / rightMaxVol))
redEnd = int(greenEnd + (rightMax - leftMax) * (array.get(vpRed, i) / rightMaxVol))
if dispMode == 'Up/Down'
greenBox = box.new(int(leftMax), array.get(zoneBounds, i) - buffer, greenEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bullCol, border_width=0)
redBox = box.new(greenEnd, array.get(zoneBounds, i) - buffer, redEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bearCol, border_width=0)
array.set(green_boxs, i, greenBox)
array.set(red_boxs, i, redBox)
else if dispMode == 'Total'
greenBox = box.new(int(leftMax), array.get(zoneBounds, i) - buffer, greenEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bullCol, border_width=0)
array.set(green_boxs, i, greenBox)
else
greenBox = box.new(int(leftMax), array.get(zoneBounds, i) - buffer, greenEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bullCol, border_width=0)
redBox = box.new(int(leftMax)-redEnd+greenEnd, array.get(zoneBounds, i) - buffer, int(leftMax), array.get(zoneBounds, i) - gap + buffer, bgcolor=bearCol, border_width=0)
array.set(green_boxs, i, greenBox)
array.set(red_boxs, i, redBox)
// add to array
if green_boxs.size() > 0
box_bar.box_rows.push(Box_row.new(green_boxs, red_boxs))
// box.new(int(leftMax), profHigh, bar_index-1, profLow, chart.fg_color, boxWid, line.style_dashed, bgcolor=boxBg)
poc = pocLevel()
[val, vah] = valueLevels(poc)
l_lines = array.new_line()
if showPoc
poc_line = line.new(int(leftMax), poc, bar_index-1, poc, color=pocCol, width=pocWid)
array.push(l_lines, poc_line)
if showVA
vah_line = line.new(int(leftMax), vah, bar_index-1, vah, color=vahCol, width=vahWid)
array.push(l_lines, vah_line)
val_line = line.new(int(leftMax), val, bar_index-1, val, color=valCol, width=1)
array.push(l_lines, val_line)
if l_lines.size() > 0
line_bar.line_rows.push(Line_row.new(l_lines))
line_bar.pv_rows.push(Pv_row.new(poc, val, vah))
//if update
// resetProfile(true)
drawCurZone(update, delete) =>
var line pocLine = na
var line vahLine = na
var line valLine = na
var box outBox = na
var label sessionLab = na
var redBoxes = array.new_box(array.size(vpRed), na)
var greenBoxes = array.new_box(array.size(vpRed), na)
if bar_index > lookback and update and array.sum(vpGreen) + array.sum(vpRed) > 0
//Clearing the previous boxes and array
if not na(pocLine)
line.delete(pocLine)
if not na(vahLine)
line.delete(vahLine)
if not na(valLine)
line.delete(valLine)
if not na(outBox)
box.delete(outBox)
if not na(sessionLab)
label.delete(sessionLab)
for i = 0 to array.size(redBoxes) - 1
if not na(array.get(redBoxes, i))
box.delete(array.get(redBoxes, i))
box.delete(array.get(greenBoxes, i))
gap = (profHigh - profLow) / row
float leftMax = bar_index[lookback]
float rightMax = bar_index[int(lookback / 1.4)]
float rightMaxVol = array.max(vpGreen)+array.max(vpRed)
float buffer = gap / 10
// if showLabels
// sessionLab := label.new((bar_index - 1 + int(leftMax))/2, profHigh, sessionType, color=color.rgb(0,0,0,100), textcolor=chart.fg_color)
if showProf
for i = 0 to array.size(vpRed) - 1
greenEnd = int(leftMax + (rightMax - leftMax) * (array.get(vpGreen, i) / rightMaxVol))
redEnd = int(greenEnd + (rightMax - leftMax) * (array.get(vpRed, i) / rightMaxVol))
if dispMode == 'Up/Down'
array.set(greenBoxes, i, box.new(int(leftMax), array.get(zoneBounds, i) - buffer, greenEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bullCol, border_width=0))
array.set(redBoxes, i, box.new(greenEnd, array.get(zoneBounds, i) - buffer, redEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bearCol, border_width=0))
else if dispMode == 'Total'
array.set(greenBoxes, i, box.new(int(leftMax), array.get(zoneBounds, i) - buffer, greenEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bullCol, border_width=0))
else
array.set(greenBoxes, i, box.new(int(leftMax), array.get(zoneBounds, i) - buffer, greenEnd, array.get(zoneBounds, i) - gap + buffer, bgcolor=bullCol, border_width=0))
array.set(redBoxes, i, box.new(int(leftMax)-redEnd+greenEnd, array.get(zoneBounds, i) - buffer, int(leftMax), array.get(zoneBounds, i) - gap + buffer, bgcolor=bearCol, border_width=0))
// outBox := box.new(int(leftMax), profHigh, bar_index-1, profLow, chart.fg_color, boxWid, line.style_dashed, bgcolor=boxBg)
poc = pocLevel()
[val, vah] = valueLevels(poc)
if showPoc
line.delete(pocLine)
pocLine := line.new(int(leftMax), poc, bar_index-1, poc, color=pocCol, width=pocWid)
if showVA
line.delete(vahLine)
line.delete(valLine)
vahLine := line.new(int(leftMax), vah, bar_index-1, vah, color=vahCol, width=vahWid)
valLine := line.new(int(leftMax), val, bar_index-1, val, color=valCol, width=valWid)
if delete
box.delete(outBox)
line.delete(pocLine)
line.delete(vahLine)
line.delete(valLine)
for i = 0 to array.size(greenBoxes)-1
box.delete(array.get(greenBoxes, i))
for i = 0 to array.size(redBoxes)-1
box.delete(array.get(redBoxes, i))
combArray(arr1, arr2) =>
out = array.copy(arr1)
if array.size(arr2) > 0
for i = 0 to array.size(arr2) - 1
array.push(out, array.get(arr2, i))
out
updateIntra(o, h, l, c, v) =>
if array.size(o) > 0
for i = 0 to array.size(o) - 1
array.push(ltfOpen, array.get(o, i))
array.push(ltfHigh,array.get(h, i))
array.push(ltfLow,array.get(l, i))
array.push(ltfClose,array.get(c, i))
array.push(ltfVolume,array.get(v, i))
//==========================
//Calculations
//==========================
//Detecting different start dates
newDaily = dayofweek != dayofweek[1]
newWeekly = weekofyear != weekofyear[1]
newMonthly = (dayofmonth != dayofmonth[1] + 1) and (dayofmonth != dayofmonth[1])
newQuarterly = month != month[1] and (month - 1) % 3 == 0
utcHour = hour(time(timeframe.period, '0000-2400', 'GMT'), 'GMT')
newSession = switch sessionType
'Daily' => newDaily
'Weekly' => newWeekly
'Monthly' => newMonthly
'Quarterly' => newQuarterly
=> newDaily
zoneEnd = switch sessionType
'Daily' => newDaily
'Weekly' => newWeekly
'Monthly' => newMonthly
'Quarterly' => newQuarterly
=> newDaily
//Re calculating and drawing zones
calcSession(zoneEnd or (barstate.islast and showCur))
/////////////////////////////////////////////////////
// Doan Modfied Start
bar_day = dayofmonth(time_close)
bar_month = month(time_close)
bar_year = year(time_close)
cur_day = dayofmonth(timenow)
cur_month = month(timenow)
cur_year = year(timenow)
able_add = false
if cur_year == bar_year and (bar_month == cur_month or (bar_month+1) == cur_month or (bar_month+2) == cur_month or cur_month == 1 and (bar_month == 12 or bar_month == 11)) and str.tostring(timeframe.period) != 'D'
able_add := true
// if cur_year == bar_year //and (bar_month == cur_month or (bar_month+1) == cur_month or (bar_month+2) == cur_month or (bar_month+3) == cur_month or cur_month == 1 and (bar_month == 12 or bar_month == 11 or bar_month == 10) or cur_month == 2 and (bar_month == 12 or bar_month == 11)) and str.tostring(timeframe.period) == 'D'
// //if bar_month == cur_month or (bar_month+1) == cur_month
// able_add := true
var box_bar = Box_bar.new(
box_rows = array.new<Box_row>()
)
var line_bar = Line_bar.new(
line_rows = array.new<Line_row>(),
pv_rows = array.new<Pv_row>()
)
// get value return poc, val, vah to alert
drawNewZone(box_bar, line_bar, zoneEnd, able_add)
//[poc, val, vah] =
// debug
// if val != 0.0
// label.new(bar_index, val, str.format("{0} == {1} *** {2} == {3}", dayofmonth(time_close), dayofmonth(timenow), month(time_close), month(timenow)), color=color.rgb(0,0,0,100), textcolor=chart.fg_color)
poc = 0.0
val = 0.0
vah = 0.0
if able_add == true
if box_bar.box_rows.size() > (bar_show+1)
for [index,box_row] in box_bar.box_rows
if index > box_bar.box_rows.size() - (bar_show+1)
break
g_boxs = box_row.g_box
for [g_index,g_box_row] in g_boxs
box.delete(g_box_row)
r_boxs = box_row.r_box
for [r_index,r_box_row] in r_boxs
box.delete(r_box_row)
if line_bar.line_rows.size() > (bar_show+1)
for [index,line_row] in line_bar.line_rows
if index > line_bar.line_rows.size() - (bar_show+1)
break
l_lines = line_row.l_line
for [l_index,l_line_row] in l_lines
line.delete(l_line_row)
if line_bar.pv_rows.size() > 1
Pv_row pv = array.get(line_bar.pv_rows, array.size(line_bar.pv_rows)-1)
poc := pv.poc
val := pv.val
vah := pv.vah
// Doan Modified End
/////////////////////////////////////////////////////
drawCurZone(barstate.islast and not zoneEnd and showCur and activeZone, zoneEnd)
//Reseting profie at start of new zone
resetProfile(newSession)
//Updating data arrays
updateIntra(dO, dH, dL, dC, dV)
//Reseting zone start value
if zoneEnd
activeZone := false
if newSession
zoneStart := bar_index
activeZone := true
londonHigh = ta.highest(high, bar_index-londonStart+1)
tokyoHigh = ta.highest(high, bar_index-tokyoStart+1)
nyHigh = ta.highest(high, bar_index-nyStart+1)
//if bar_index > lookback and zoneEnd and array.sum(vpGreen) + array.sum(vpRed) > 0 //and able_add == true
// debug
if barstate.islast
label.new(bar_index, low, text=str.format("POC: {0} \n VAL: {1} \nVAH: {2}", poc, val, vah), textcolor = color.white)