Pine Script™ is TradingView’s programming language. It allows traders to create their trading tools and run them on our servers. We designed Pine Script™ as a lightweight, yet powerful, language for developing indicators and strategies that you can then backtest. Most of TradingView’s built-in indicators are written in Pine Script™, and our thriving community of Pine Script™ programmers has published more than 100,000 Community Scripts.
//version=5 indicator("Cloning boxes demo", overlay = true, max_boxes_count = 500) //variable The number of bars between each new mainLine assignment. int length = input.int(20, "Length", 2) //variable The `chart.point` for the top-left of the `originalBox`. Contains `time` ,and `index` information. topLeft = chart.point.now(high) //variable The `chart.point` for the bottom-right of the `originalBox`. Does not ,contain `time` information. bottomRight = chart.point.from_index(bar_index + 1, low) //variable A new box with `topLeft` and `bottomRight` corners on every `length` bars. var box originalBox = na //variable Is teal when the bar is rising, maroon when it's falling. color originalColor = close > open ? color.teal : color.maroon if bar_index % length == 0 // Assign a new box using the `topLeft` and `bottomRight` info to the
id: 8b8eea6e3b33263b627e0ebb8c38d9fa - page: 274
,`originalBox`. // This box uses the `index` fields from the points as x-coordinates. originalBox := box.new(topLeft, bottomRight, originalColor, 2, bgcolor = color. ,new(originalColor, 60)) else //variable A clone of the `originalBox`. box copiedBox = box.copy(originalBox) // Modify the `copiedBox`. These changes do not affect the `originalBox`. box.set_top(copiedBox, high) box.set_bottom_right_point(copiedBox, bottomRight) box.set_border_color(copiedBox, color.gray) box.set_border_width(copiedBox, 1) box.set_bgcolor(copiedBox, na) 262 Chapter 4. Concepts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
id: 2e46418f019fe3b9eb02bd3aa40f09be - page: 274
Pine Script v5 User Manual Deleting boxes To delete boxes drawn by a script, use box.delete(). As with *.delete() functions in other drawing namespaces, this function is handy for conditionally removing boxes or maintaining a specific number of boxes on the chart. This example displays boxes representing periodic cumulative volume values. The script creates a new box ID and stores it in a boxes array once every length bars. If the arrays size exceeds the specified numberOfBoxes, the script removes the oldest box from the array using array.shift() and deletes it using box.delete(). On other bars, it accumulates volume over each period by modifying the top of the last box in the boxes array. The script then uses for loops to find the highestTop of all the arrays boxes and set the bgcolor of each box with a gradient color based on its box.get_top() value relative to the highestTop:
id: c39af04be377d8214c3da8751a4f26c8 - page: 275
//version=5 //variable The maximum number of boxes to show on the chart. const int MAX_BOXES_COUNT = 500 indicator("Deleting boxes demo", "Cumulative volume boxes", format = format.volume, ,max_boxes_count = MAX_BOXES_COUNT) //variable The number of bars in each period. int length = input.int(20, "Length", 1) //variable The maximum number of volume boxes in the calculation. int numberOfBoxes = input.int(10, "Number of boxes", 1, MAX_BOXES_COUNT) //variable An array containing the ID of each box displayed by the script. var boxes = array.new<box>() if bar_index % length == 0 // Push a new box into the `boxes` array. The box has the default `xloc.bar_ ,index` property. boxes.push(box.new(bar_index, 0, bar_index + 1, 0, #000000, 2, text_color = ,#000000)) // Shift the oldest box out of the array and delete it when the array's size ,exceeds the `numberOfBoxes`. if boxes.size() > numberOfBoxes box.delete(boxes.shift()) (continues on next page)
id: 46b110675d1eff4672c19f25ca6ed7c3 - page: 275