This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprofile.pine
117 lines (96 loc) · 4.07 KB
/
profile.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ildar Akhmetgaleev (AkhIL)
// See https://github.com/akhilman/vol-poorofile
// vim: shiftwidth=2 tabstop=2
{# Template settings -#}
{% set n_rows = (n_rows or 40) | int -%}
{% set block_size = (block_size or 200) | int -%}
{% set profile_color = profile_color or 'gray' -%}
{% set peak_size = peak_size or 5 | int -%}
{% set peak_color = peak_color or 'orange' -%}
{% set visual_row_width = (visual_row_width or 100) | int -%}
{% set max_bars_back = (max_bars_back or 500) | int -%}
//@version=4
study("Poor man's volume profile", "PMVolProf", overlay=true, max_bars_back={{max_bars_back}})
block_size = input({{block_size}}, "Bars in profile block", minval=10, maxval={{max_bars_back}})
visual_row_width = input({{visual_row_width}}, "Visual row width", minval=10)
show_peaks = input(true, "Show peaks")
use_custom_volume_source = input(false, "Use custom volume source")
volume_source_symbol = input("", "Custom volume source symbol", input.symbol)
BOUNDS_COLOR = color.{{profile_color}}
PROFILE_COLOR = color.new(color.{{profile_color}}, 50)
PEAK_COLOR = color.new(color.{{peak_color}}, 50)
custom_volume = use_custom_volume_source ? security(volume_source_symbol, timeframe.period, volume, lookahead=barmerge.lookahead_on) : volume
////
// Calculate profile
///
block_high = highest(high, block_size)
block_low = lowest(low, block_size)
float highest_row_value = 0
{% for n in range(n_rows) -%}
float row{{n}}_price = na
int row{{n}}_width = 0
float row{{n}}_value = 0.0
row{{n}}_is_peak = false
{% endfor %}
if barstate.islast
block_height = block_high - block_low
row_height = block_height / {{n_rows}}
{% for n in range(n_rows) -%}
row{{n}}_low = block_low + row_height * {{n}}
row{{n}}_high = block_low + row_height * {{n+1}}
{% endfor %}
for i = 0 to block_size + 1
n_rows_affected = 0
{% for n in range(n_rows) -%}
bar_row{{n}}_value = 0.0
if low[i] < row{{n}}_high and high[i] > row{{n}}_low
bar_row{{n}}_value := nz(custom_volume[i])
n_rows_affected := n_rows_affected + 1
{% endfor %}
{% for n in range(n_rows) -%}
row{{n}}_value := row{{n}}_value + (n_rows_affected != 0 ? bar_row{{n}}_value / n_rows_affected : 0)
{% endfor %}
{% for n in range(n_rows) -%}
highest_row_value := max(highest_row_value, row{{n}}_value)
{% endfor %}
{% for n in range(n_rows) -%}
row{{n}}_price := (row{{n}}_low + row{{n}}_high ) / 2
row{{n}}_width := floor(visual_row_width * row{{n}}_value / highest_row_value)
{% endfor %}
{% for n in range(n_rows) -%}
if {% for m in range([n-peak_size, 0]|max, [n+peak_size+1, n_rows]|min) | reject("eq", n) -%}
{%- if not loop.first %} and {% endif -%}
row{{n}}_value >= row{{m}}_value
{%- endfor %}
row{{n}}_is_peak := true
{% endfor %}
////
// Draw profile
///
var int first_bar_time = time
if barstate.isfirst
first_bar_time := time
var line block_high_line = line.new(bar_index, high, bar_index, high, color=BOUNDS_COLOR)
var line block_low_line = line.new(bar_index, low, bar_index, low, color=BOUNDS_COLOR)
{% for n in range(n_rows) -%}
var label row{{n}}_label = label.new(time_close, close, "", xloc=xloc.bar_time, yloc=yloc.price, color=PROFILE_COLOR, style=label.style_label_left, size=size.auto)
{% endfor %}
if barstate.islast
line.set_xloc(block_high_line, max(time[block_size], first_bar_time), time_close, xloc.bar_time)
line.set_y1(block_high_line, block_high)
line.set_y2(block_high_line, block_high)
line.set_xloc(block_low_line, max(time[block_size], first_bar_time), time_close, xloc.bar_time)
line.set_y1(block_low_line, block_low)
line.set_y2(block_low_line, block_low)
{% for n in range(n_rows) -%}
row{{n}}_text = ""
for i = 0 to row{{n}}_width + 1
row{{n}}_text := row{{n}}_text + "#"
row{{n}}_color = show_peaks and row{{n}}_is_peak ? PEAK_COLOR : PROFILE_COLOR
label.set_x(row{{n}}_label, time_close)
label.set_y(row{{n}}_label, row{{n}}_price)
label.set_color(row{{n}}_label, row{{n}}_color)
label.set_textcolor(row{{n}}_label, row{{n}}_color)
label.set_text(row{{n}}_label, row{{n}}_text)
{% endfor %}