I want to add button on chart on top right side to indicator on tradingview that can…
- User
I want to add button on chart on top right side to indicator on tradingview that can turn on/off indicator without Opening Setting
- Assistant
I want to add button on chart on top right side to indicator on tradingview that can turn on/off indicator without Opening Setting
To add a button on your TradingView chart that can toggle your indicator on and off without opening the settings, you can use Pine Script to create an interactive label that acts as a button. Here's how you can do it:
Use a Persistent Variable: You'll need a variable that keeps track of whether your indicator is currently visible. This variable should be declared with the var keyword to make it persistent across script executions.
Create a Toggle Button Using a Label: Use the label.new() function to create a label that will act as your toggle button. Position it on the chart where you want it to appear, such as the top-right corner.
Detect Clicks on the Label: Use the label.is_clicked() function to detect when the label is clicked. When a click is detected, toggle the visibility variable.
Update the Label Text: Change the label text to reflect the current state (e.g., "Hide" when the indicator is visible and "Show" when it's hidden).
Control the Indicator Plot: Use the visibility variable in an if statement to control whether the indicator is plotted.
Here's a complete example script that demonstrates how to implement this:
//@version=5
indicator("Toggle Indicator Button", overlay=true)
var bool is_visible = true // Persistent variable to keep track of visibility
var label toggle_button = na // Persistent variable for the toggle button
// Create the toggle button on the first bar
if barstate.isfirst
toggle_button := label.new(
x=bar_index,
y=high,
text="Hide Indicator",
style=label.style_label_upper_right,
textcolor=color.white,
bgcolor=color.blue,
size=size.normal)
// Update the button position to stay at the top-right corner
label.set_xy(toggle_button, bar_index + 1, highest(high, 500) * 1.02)
// Detect clicks on the toggle button
if label.is_clicked(toggle_button)
is_visible := not is_visible // Toggle the visibility state
// Update the button text based on the current state
label.set_text(toggle_button, is_visible ? "Hide Indicator" : "Show Indicator")
// Plot the indicator only when it is visible
if is_visible
plot(sma(close, 14), color=color.orange, linewidth=2)
Explanation:
Persistent Variables:
is_visible keeps track of whether the indicator should be displayed.toggle_button holds the reference to the label acting as a button.Creating the Toggle Button:
barstate.isfirst.label.style_label_upper_right to position it in the upper-right corner.Updating the Button Position:
x position is continuously updated to stay at the latest bar (bar_index + 1).y position is set slightly above the highest high over the last 500 bars to ensure it's always visible.Handling Clicks:
label.is_clicked(toggle_button) checks if the button was clicked.is_visible is toggled, and the button text is updated accordingly.Conditional Plotting:
plot function is wrapped in an if is_visible condition, so it only executes when is_visible is true.Notes:
Real-Time vs. Historical Bars: The click detection works immediately on the last bar (real-time data). For historical bars, the click is processed after a chart refresh.
Version Compatibility: This script uses features available in Pine Script version 5. Ensure your script is set to //@version=5 at the top.
Customization: You can modify the appearance of the button by changing the bgcolor, textcolor, size, and other properties in the label.new() function.
Limitations:
By implementing this script, you'll have a convenient toggle button on your chart that allows you to show or hide your indicator with a single click, without needing to open the settings menu. This enhances usability, especially when you frequently need to switch the indicator's visibility while analyzing the chart.
Feel free to adjust the script to suit your specific needs, and let me know if you have any questions or need further assistance!
Not yet included in a published release.