-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvolume-density-candle.pine
49 lines (46 loc) · 2.27 KB
/
volume-density-candle.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
//@version=5
indicator("Volume Density Candle", "VDC", overlay=true)
DEF_MAX_DEV_RATE = 1
MAX_TRANS = 100
avgLength = input.int(20, "Average length", minval=1, maxval=200)
maxDeviationRate = input.float(DEF_MAX_DEV_RATE, "Max deviation rate", minval=0.1, maxval=10, step=0.1)
excludeFirst = input.bool(false, "Exclude the first candle of the intraday session from the average")
excludeLast = input.bool(true, "Exclude the last candle of the intraday session from the average")
maxTransLimit = input.int(80, "Max transparency", minval=70, maxval=100)
upColor = input.color(color.teal, "Up color")
downColor = input.color(color.red, "Down color")
float trans = 0
if ta.tr(true) > 0 and not na(volume) and volume > 0
float sum = 0
count = 0
for i = 0 to avgLength - 1
if timeframe.isintraday and i > 0
if excludeFirst
if not na(time_tradingday[i + 1])
if time_tradingday[i] != time_tradingday[i + 1]
continue
if excludeLast
if time_tradingday[i] != time_tradingday[i - 1]
continue
if ta.tr(true)[i] > 0
volumeDensity = volume[i] / ta.tr(true)[i]
sum := sum + volumeDensity
count += 1
float avgVolumeDensity = 0
if count > 0
avgVolumeDensity := sum / count
currentVolumeDensity = volume / ta.tr(true)
float deviationRate = maxDeviationRate
if avgVolumeDensity > 0
deviationRate := (currentVolumeDensity - avgVolumeDensity) / avgVolumeDensity
deviationRate := deviationRate > maxDeviationRate ? maxDeviationRate : deviationRate
deviationRate := deviationRate < -maxDeviationRate ? -maxDeviationRate : deviationRate
minusDeviationRate = maxDeviationRate > DEF_MAX_DEV_RATE ? DEF_MAX_DEV_RATE : maxDeviationRate
deviationRateAbsRange = minusDeviationRate + maxDeviationRate
deviationRateAbsValue = minusDeviationRate + deviationRate
trans := MAX_TRANS - MAX_TRANS * deviationRateAbsValue / deviationRateAbsRange
trans := trans > maxTransLimit ? maxTransLimit : trans
upColor := color.new(upColor, trans)
downColor := color.new(downColor, trans)
color = close >= open ? upColor : downColor
plotcandle(open, high, low, close, color=color, wickcolor=color, bordercolor=color)