1. **Purpose** - The script tries to capture the essential “shape” of price movement by selecting a limited number of “key points” (anchors) from the latest bars. - After selecting these anchors, it draws straight lines between them, effectively simplifying the price chart into a smaller set of points without losing major swings.
2. **How It Works, Step by Step** 1. We look back a certain number of bars (e.g., 50). 2. We start by drawing a straight line from the **oldest** bar in that range to the **newest** bar—just two points. 3. Next, we find the bar whose price is *farthest away* from that straight line. That becomes a new anchor point. 4. We “snap” (pin) the line to go exactly through that new anchor. Then we re-draw (re-interpolate) the entire line from the first anchor to the last, in segments. 5. We repeat the process (adding more anchors) until we reach the desired number of points. Each time, we choose the biggest gap between our line and the actual price, then re-draw the entire shape. 6. Finally, we connect these anchors on the chart with red lines, visually simplifying the price curve.
3. **Why It’s Useful** - It highlights the most *important* bends or swings in the price over the chosen window. - Instead of plotting every single bar, it condenses the information down to the “key turning points.”
4. **Key Takeaway** - You’ll see a small number of red line segments connecting the **most significant** points in the price data. - This is especially helpful if you want a simplified view of recent price action without minor fluctuations.
1. **Data Collection** - We collect the **most recent** `length` bars in `closeData`. Index 0 is the oldest bar in that window, index `length-1` is the newest bar.
2. **Initial Straight Line** - We create an array called `linesArr` that starts as a simple linear interpolation from `closeData[0]` (the oldest bar’s close) to `closeData[length-1]` (the newest bar’s close).
3. **Line Breaks** - We store “anchor points” in `lineBreaks`, initially `[0, length - 1]`. These are the start and end of our segment.
4. **Global Re-Interpolation** - Each time we want to add a new anchor, we **re-draw** (linear interpolation) for *every* subrange `[lineBreaks, lineBreaks[i+1]]`, ensuring we have a globally consistent line. - This avoids the “local subrange only” approach, which can cause clustering near existing anchors.
5. **Finding the Largest Distance** - After re-drawing, we compute the vertical distance for each bar `i` that isn’t already a line break. The bar with the biggest distance from the line is chosen as the next anchor (`maxDistIdx`).
6. **Snapping and Re-Interpolate** - We “snap” that bar’s line value to the actual close, i.e. `linesArr[maxDistIdx] = closeData[maxDistIdx]`. Then we globally re-draw all segments again.
7. **Repeat** - We repeat these insertions until we have the desired number of points (`num_points`).
8. **Drawing** - Finally, we connect each consecutive pair of anchor points (`lineBreaks`) with a `line.new(...)` call, coloring them red. - We offset the line’s `x` coordinate so that the anchor at index 0 lines up with `bar_index - (length - 1)`, and the anchor at index `length-1` lines up with `bar_index` (the current bar).
**Result**: You get a simplified representation of the price with a small set of line segments capturing the largest “jumps” or swings. By re-drawing the entire line after each insertion, the anchors tend to distribute more *evenly* across the data, mitigating the issue where anchors bunch up near each other.
Enjoy experimenting with different `length` and `num_points` to see how the simplified lines change!
진정한 TradingView 정신에 따라, 이 스크립트의 저자는 트레이더들이 이해하고 검증할 수 있도록 오픈 소스로 공개했습니다. 저자에게 박수를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 출판물에서 이 코드를 재사용하는 것은 하우스 룰에 의해 관리됩니다. 님은 즐겨찾기로 이 스크립트를 차트에서 쓸 수 있습니다.