AlphaViz

Polyline Plus

This library introduces the `PolylinePlus` type, which is an enhanced version of the built-in PineScript `polyline`. It enables two features that are absent from the built-in type:

1. Developers can now efficiently add or remove points from the polyline. In contrast, the built-in `polyline` type is immutable, requiring developers to create a new instance of the polyline to make changes, which is cumbersome and incurs a significant performance penalty.
2. Each `PolylinePlus` instance can theoretically hold up to ~1M points, surpassing the built-in `polyline` type's limit of 10K points, as long as it does not exceed the memory limit of the PineScript runtime.

Internally, each `PolylinePlus` instance utilizes an array of `line`s and an array of `polyline`s. The `line`s array serves as a buffer to store lines formed by recently added points. When the buffer reaches its capacity, it flushes the contents and converts the lines into polylines. These polylines are expected to undergo fewer updates. This approach is similiar to the concept of "Buffered I/O" in file and network systems. By connecting the underlying lines and polylines, this library achieves an enhanced polyline that is dynamic, efficient, and capable of surpassing the maximum number of points imposed by the built-in polyline.

🔵 API

Step 1: Import this library

import algotraderdev/polylineplus/1 as pp
// remember to check the latest version of this library and replace the 1 above.

Step 2: Initialize the `PolylinePlus` type.

var p = pp.PolylinePlus.new()

There are a few optional params that developers can specify in the constructor to modify the behavior and appearance of the polyline instance.

var p = pp.PolylinePlus.new(
  // If true, the drawing will also connect the first point to the last point, resulting in a closed polyline.
  closed = false,
  // Determines the field of the chart.point objects that the polyline will use for its x coordinates. Either xloc.bar_index (default), or xloc.bar_time.
  xloc = xloc.bar_index,
  // Color of the polyline. Default is blue.
  line_color = color.blue,
  // Style of the polyline. Default is line.style_solid.
  line_style = line.style_solid,
  // Width of the polyline. Default is 1.
  line_width = 1,
  // The maximum number of points that each built-in `polyline` instance can contain.
  // NOTE: this is not to be confused with the maximum of points that each `PolylinePlus` instance can contain.
  max_points_per_builtin_polyline = 10000,
  // The number of lines to keep in the buffer. If more points are to be added while the buffer is full, then all the lines in the buffer will be flushed into the poylines. 
  // The higher the number, the less frequent we'll need to // flush the buffer, and thus lead to better performance.
  // NOTE: the maximum total number of lines per chart allowed by PineScript is 500. But given there might be other places where the indicator or strategy are drawing lines outside this polyline context, the default value is 50 to be safe.
  lines_bffer_size = 50)

Step 3: Push / Pop Points

// Push a single point
p.push_point(chart.point.now())

// Push multiple points
chart.point[] points = array.from(p1, p2, p3) // Where p1, p2, p3 are all chart.point type.
p.push_points(points)

// Pop point
p.pop_point()

// Resets all the points in the polyline.
p.set_points(points)

// Deletes the polyline.
p.delete()

🔵 Benchmark

Below is a simple benchmark comparing the performance between `PolylinePlus` and the native `polyline` type for incrementally adding 10K points to a polyline.

import algotraderdev/polylineplus/2 as pp

var t1 = 0
var t2 = 0

if bar_index < 10000
    int start = timenow
    var p = pp.PolylinePlus.new(xloc = xloc.bar_time, closed = true)
    p.push_point(chart.point.now())
    t1 += timenow - start

    start := timenow
    var polyline pl = na
    var points = array.new<chart.point>()
    points.push(chart.point.now())
    if not na(pl)
        pl.delete()
    pl := polyline.new(points)
    t2 += timenow - start

if barstate.islast
    log.info('{0} {1}', t1, t2)

For this benchmark, `PolylinePlus` took ~300ms, whereas the native `polyline` type took ~6000ms.

We can also fine-tune the parameters for `PolylinePlus` to have a larger buffer size for `line`s and a smaller buffer for `polyline`s.

var p = pp.PolylinePlus.new(xloc = xloc.bar_time, closed = true, lines_buffer_size = 500, max_points_per_builtin_polyline = 1000)

With the above optimization, it only took `PolylinePlus` ~80ms to process the same 10K points, which is ~75x the performance compared to the native `polyline`.

专业缠论指标: alphaviz.pro/chanlun
Email: contact@alphaviz.pro
Discord: discord.gg/w2fFtNega4
파인 라이브러리

트레이딩뷰 정신에 따라 오써는 이 파인 코드를 오픈 소스 라이브러리로 퍼블리쉬하여 당사 커뮤니티의 다른 파인 프로그래머들이 쓸 수 있도록 하였습니다. 오써에게 찬사를! 여러분은 이 라이브러리를 프라이빗 또는 오픈 소스 퍼블리케이션에 쓸 수 있지만 퍼블리케이션에 재사용은 하우스룰을 따릅니다.

면책사항

이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

이 라이브러리를 쓰시겠습니까?

텍스트를 클립보드에 카피한 뒤 님의 스크립트에 붙여 넣기.