-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTick-Based_OrderVolumeTally_template.pine
171 lines (141 loc) · 5.52 KB
/
Tick-Based_OrderVolumeTally_template.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
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
//@version=4
// LICENSE: AGPLv3 (https://www.gnu.org/licenses/agpl-3.0.en.html)
// Copyright (C) 2021 Lysergik Productions (https://github.com/LysergikProductions)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// set overlay == `false` if you want to plot data in its own pane
study(title="Real-Time Volume Analyzer Template", shorttitle="Real-Time Volume Analyzer", format=format.volume, precision=2, scale=scale.right, overlay=true)
i_posColor = input(color.rgb(38, 166, 154), title="Positive Color")
i_negColor = input(color.rgb(240, 83, 80), title="Negative Color")
var bool showPercent = input(true, title='Show Percentages')
var buCol = color.black
var beCol = color.black
varip int ticks = 0
varip lastPrice = close
varip lastVol = volume
varip float volChange = 0.0
varip bool isLate = false // This will be true when the script recognizes it has not seen data for the current candle's first tick
varip string lastDir = 'null' // This is used to store the previous tick's affect on the current candle; up or down
varip float diff = 0.0 // This will equal `bullOrder - bearOrder`
varip float bullOrder = 0.0
varip float bearOrder = 0.0
varip float bullPercent = 0.0
varip float bearPercent = 0.0
if barstate.isnew and volume != 0
isLate := true
buCol := color.black
beCol := color.black
if barstate.isrealtime and lastPrice != close and lastVol != volume
ticks += 1
if not isLate
volChange := abs(volume-lastVol)
if close < lastPrice
lastDir := 'down'
bearOrder += volChange
bearPercent := bearOrder / volume * 100
buCol := color.black
beCol := i_negColor
else if close > lastPrice
lastDir := 'up'
bullOrder += volChange
bullPercent := bullOrder / volume * 100
buCol := i_posColor
beCol := color.black
lastPrice := close
lastVol := volume
isLate := false
// there was a tick but price did not change, so add half of volChange to each tally, unless last direction was known, then add it to that tally
else if barstate.isrealtime and lastPrice == close and lastVol != volume
ticks += 1
if not isLate
volChange := abs(volume-lastVol)
if lastDir == 'up'
bullOrder += volChange
buCol := i_posColor
beCol := color.black
else if lastDir == 'down'
bearOrder += volChange
buCol := color.black
beCol := i_negColor
else if lastDir != 'up' and lastDir != 'down'
bullOrder += volChange/2
bearOrder += volChange/2
buCol := color.black
beCol := color.black
lastPrice := close
lastVol := volume
isLate := false
// reset values when a new candle is detected
if barstate.isnew
ticks := 0
lastVol := 0
bullOrder := 0
bearOrder := 0
bullPercent := 0
bearPercent := 0
diff := bullOrder-bearOrder
var table perfTable = table.new(position.bottom_right, 1, 4, border_width = 3)
text_size = input("AUTO", "Text Size:", options=["AUTO", "TINY","SMALL", "NORMAL", "LARGE", "HUGE"])
table_size = input(6, "Table Width:")
text = size.auto
if text_size == "TINY"
text := size.tiny
else if text_size == "SMALL"
text := size.small
else if text_size == "NORMAL"
text := size.normal
else if text_size == "LARGE"
text := size.large
else if text_size == "HUGE"
text := size.huge
x = "k"
vol1 = volume
bu1 = bullOrder
be1 = bearOrder
if lastVol < 1000
x := ""
if lastVol >= 1000
vol1 := volume / 1000
bu1 := bullOrder / 1000
be1 := bearOrder / 1000
if lastVol >= 1000000
x := "M"
vol1 := volume / 1000000
bu1 := bullOrder / 1000000
be1 := bearOrder / 10000000
if lastVol >= 1000000000
x := "B"
vol1 := volume / 1000000000
bu1 := bullOrder / 1000000000
be1 := bearOrder / 1000000000
a = array.new_float(0)
for i = 1 to 20
array.push(a, vol1[i])
m1c = security(syminfo.tickerid, '1', close)
m1o = security(syminfo.tickerid, '1', open)
f_fillCell(_table, _column, _row, _value, _timeframe) =>
_c_color = m1c > m1o ? i_posColor : i_negColor
_transp = 85
_cellText = tostring(_value, "#.##") + x
if showPercent == true
_cellText := tostring(_value, "#.##") + x + ' (' + tostring(_value/vol1*100, "0.0") + '%)'
table.cell(_table, _column, _row, _cellText, text_color = color.white, width = table_size)
table.cell_set_text_size(perfTable, 0, 0, text)
table.cell_set_text_size(perfTable, 0, 1, text)
table.cell_set_text_size(perfTable, 0, 2, text)
table.cell_set_bgcolor(perfTable, 0, 0, close > open ? color.new(i_posColor, _transp) : color.new(i_negColor, _transp))
table.cell_set_bgcolor(perfTable, 0, 1, color.new(buCol, _transp))
table.cell_set_bgcolor(perfTable, 0, 2, color.new(beCol, _transp))
if barstate.islast
f_fillCell(perfTable, 0, 0, vol1 , "vol1")
f_fillCell(perfTable, 0, 1, bu1, "bu1")
f_fillCell(perfTable, 0, 2, be1, "be1")