One of important features of Pinescript the ability to work with tables. In this tutorial, we’ll delve deep into the functionality and use-cases of the table.clear
function.
Overview of the table.clear Function
The table.clear
function serves to remove a cell or a sequence of cells from the table. The cells are cleared in a rectangular pattern, defined by the start_column and start_row as the top-left corner, and the end_column and end_row as the bottom-right corner. The function syntax is as follows:
table.clear(table_id, start_column, start_row, end_column, end_row) → void
Arguments of table.clear Function
The table.clear
function accepts five arguments:
table_id (series table)
: This is a table object that you wish to manipulate.start_column (series int)
: This specifies the column index of the first cell to delete, with numbering starting at 0.start_row (series int)
: Similarly, this denotes the row index of the first cell to delete, starting from 0.end_column (series int)
: This is the index of the column of the last cell to delete. It’s optional and defaults to the value provided for start_column.end_row (series int)
: Lastly, this argument is the index of the row of the last cell to delete. It’s also optional, and defaults to the start_row value.
Using the table.clear Function: A Unique Example
Now that we have a basic understanding of what the table.clear
function is and how it works, let’s see it in action. In this example, we will create a simple “donut” shape using tables, and clear out the center using table.clear
.
//@version=5 indicator("A donut", overlay=true) if barstate.islast colNum = 8, rowNum = 8 padding = "◯" donutTable = table.new(position.middle_right, colNum, rowNum) for c = 0 to colNum - 1 for r = 0 to rowNum - 1 table.cell(donutTable, c, r, text=padding, bgcolor=#face6e, text_color=color.new(color.black, 100)) table.clear(donutTable, 2, 2, 5, 5)
Let’s break this down:
- We initialize our script with
//@version=5
to ensure we’re using the most current version of Pine Script. - The
indicator
function is used to name the indicator “A donut” and sets overlay to true to display it on the price chart. - We then check if the current bar is the last bar on the chart with
if barstate.islast
. - We initialize
colNum
androwNum
with a value of 8 to set the number of rows and columns in our table. padding
is a string variable containing a circular Unicode character that will be used as filler for our table cells.donutTable = table.new(position.middle_right, colNum, rowNum)
creates a new table in the middle right position of the chart.- The nested
for
loops fill each cell of the table with the padding symbol and gives it a background color and text color. - Finally,
table.clear(donutTable, 2, 2, 5, 5)
clears a section in the middle of our table to create the “donut” effect.
Key Takeaways
The table.clear
function in Pine Script is a powerful tool for creating dynamic visual elements on your charts. It offers precise control over table cells, allowing you to remove a single cell or a range of cells in a rectangular shape.
Conclusion
In conclusion, understanding and effectively utilizing the table.clear
function can significantly enrich your ability to present and analyze data visually in Pine Script. With this guide, you should now be well-equipped to leverage this function in your own scripts. Happy coding!