The structure of maps is used to gather the data. However, the drawings is done with polylines. This enables coders to draw an entire volume profile with just a few polylines, while the range is broader. This results in the benefit to draw more "lines" than with line.new() / box.new() alone.
🔶 CONCEPTS
🔹 Polylines
polyline.new creates a new polyline instance and displays it on the chart, sequentially connecting all of the points in the `points` array with line segments. The segments in the drawing can be straight or curved depending on the `curved` parameter.
In this script, points are connected, starting from the bottom. The created line moves up until there is a price level where a volume value needs to be displayed, at which the line goes to the left to the concerning volume value, coming back at the same price level until the line returns to its initial x-axis, after which the line will continue to rise until all values are displayed.
A polyline can contain maximum 10000 points (10K). Since the line has to go back and forth, each price/volume line takes 3 points. In the case that 20K bars all have a different price, we would need 60K points, or just 6 polylines. A maximum of 100 polylines can be displayed.
The 3 highest volume values are displayed with line.new(), each with their own colour.
🔹 Maps
A map object is a collection that consists of key-value pairs
Each key is unique and can only appear once. When adding a new value with a key that the map already contains, that value replaces the old value associated with the key. You can change the value of a particular key though, for example adding volume (value) at the same price (key), the latter technique is used in this script.
Volume is added to the map, associated with a particular price (default close, can be set at high, low, open,...)
When the map already contains the same price (key), the value (volume) is added to the existing volume at the associated price.
A map can contain maximum 50K values, which is more than enough to hold 20K bars (Basic 5K - Premium plan 20K), so the whole history can be put into a map.
🔹 Rounding function
This publication contains 2 round functions, which can be used to widen the Volume Profile
Round
• "Round" set at zero -> nothing changes to the source number • "Round" set below zero -> x digit(s) after the decimal point, starting from the right side, and rounded. • "Round" set above zero -> x digit(s) before the decimal point, starting from the right side, and rounded.
Let's take as example BTCUSD, relative to USD, 10 volume at a price of 100 BTCUSD will be very different than 10 volume at a price of 30000 (1K vs. 300K) If you want volume to be associated with USD, enable Volume * currency. Volume will then be multiplied by the price: • 10 volume, 1 BTC = 100 -> 1000 • 10 volume, 1 BTC = 30K -> 300K
Polylines has the attributes curved & closed. When "curved" is enabled the drawing will connect all points from the `points` array using curved line segments. When "closed" is enabled the drawing will also connect the first point to the last point from the `points` array, resulting in a closed polyline. They are default disabled, but can be enabled:
🔶 DETAILS
🔹 Put
When the map doesn't contain a price, it will be added, using map.put(id, key, value) In our code:
A key (price) is now associated with a value (volume) -> key : value
Since all keys are unique, we don't have to know its position to extract the value, we just need to know the key -> map.get(id, key) We use map.get() when a certain key already exists in the map, and we want to add volume with that value.