Editors' picksOPEN-SOURCE SCRIPT

Volume Profile with a few polylines

Updated
The base of "Volume Profile with a few polylines" is another script of mine, Volume Profile (Maps) [LuxAlgo].

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.
snapshot


🔶 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.
snapshot

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

  1. 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.

    Example: 123456.789

      0->123456.789
      1->123456.79
      2->123456.8
      3->123457
    -1->123460
    -2->123500

    snapshot
  2. Step

    Another option is custom steps.
    After setting "Round" to "Step", choose the desired steps in price,

    Examples
    •     2  -> 1234.00, 1236.00, 1238.00, 1240.00
    •     5  -> 1230.00, 1235.00, 1240.00, 1245.00
    • 100  -> 1200.00, 1300.00, 1400.00, 1500.00
    • 0.05 -> 1234.00, 1234.05, 1234.10, 1234.15
    •••



🔶 FEATURES

🔹 Volume * currency

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:

snapshot


🔶 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.


-> At the last bar, all prices (source) are now associated with volume.


🔶 SETTINGS

  • Source: Set source of choice; default close, can be set as high, low, open, ...
  • Volume & currency: Enable to multiply volume with price (see Features)
  • Amount of bars: Set amount of bars which you want to include in the Volume Profile


    🔹 Round -> 'Round/Step'

  • Round -> see Concepts
  • Step    -> see Concepts

    🔹 Display Volume Profile

  • Offset: shifts the Volume Profile (max. 500 bars to the right of last bar, see Features)
  • Max width Volume Profile: largest volume will be x bars wide, the rest is displayed as a ratio against largest volume (see Features)
  • Colours
  • Curved: make lines curved
  • Closed: connect last with first point


🔶 LIMITATIONS

• Lines won't go further than first bar (coded).
• The Volume Profile can be placed maximum 500 bar to the right of last price.
Release Notes
Update: polylines are removed now when there is a new bar (fixes double patterns)
educationalVolume

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


PineCoder: pinecoders.com

- We cannot control our emotions,
but we can control our keyboard -

Disclaimer