Home » Table Functions » table.delete function in Pine Script

table.delete function in Pine Script

Photo of author
Published on

In this tutorial, we will explore the table.delete function in Pine Script. This is an essential function in Pine Script that helps in managing the lifecycle of table objects.

What is the table.delete function?

The table.delete function in Pine Script is used to delete a table. It works by calling table.delete(table_id) where table_id is the ID of the table that you want to delete. After the function has been called, the table is removed from the chart. It’s a straightforward, but powerful function that gives you control over the tables that you create in your script.

Syntax

The syntax for table.delete function is quite simple:

table.delete(table_id)

Here, table_id is the object of the table that we want to delete. It must be a table series data type.

Example of the table.delete function

Let’s take a look at an example to better understand the table.delete function in Pine Script. Here, we’ll create a table that displays the open and close prices of a security. Then, we’ll delete the table.

//@version=5
indicator("table.delete example")
var testTable = table.new(position = position.bottom_right, columns = 2, rows = 1, bgcolor = color.yellow, border_width = 1)
if barstate.islast
    table.cell(table_id = testTable, column = 0, row = 0, text = "Open is " + str.tostring(open))
    table.cell(table_id = testTable, column = 1, row = 0, text = "Close is " + str.tostring(close), bgcolor=color.teal)
if barstate.isrealtime
    table.delete(testTable)
table.delete function

Explanation of the Code

Let’s go through the code line by line:

  • We start by specifying the version of Pine Script that we’re using, which is version 5.
  • We then declare an indicator with the title “table.delete example”.
  • We create a new table named testTable with the table.new function. The table is placed at the top right of the chart. It has two columns and one row. The background color of the table is yellow, and it has a border width of 1.
  • In the next section of the code, we check if the current bar is the last one using barstate.islast. If it is, we populate the table cells with the opening and closing prices of the bar. The str.tostring function is used to convert the prices to strings so they can be displayed in the table.
  • We then use table.cell to set the text in each cell of the table. The first cell will display the open price, and the second cell will display the close price with a teal background color.
  • Finally, we check if we’re in real-time using barstate.isrealtime. If we are, then we delete the table using table.delete(testTable). This removes the table from the chart.

Key Takeaway

The table.delete function is a crucial tool in Pine Script for managing the tables that you create. It allows you to delete tables based on certain conditions, giving you the flexibility to control when and where tables appear in your chart. Remember that the table_id parameter is the ID of the table that you want to delete.

Conclusion

In this tutorial, we have explored the table.delete function in Pine Script. We’ve seen its syntax, usage, and walked through a detailed example demonstrating its use in creating and deleting a table from a chart. Understanding this function is important for anyone looking to create interactive and dynamic scripts in Pine Script. Happy coding!

Leave a Comment