Home » Input Functions » Input.session function In Pine Script

Input.session function In Pine Script

Photo of author
Published on

A key part of developing custom scripts involves the use of the input.session function. This function adds a level of interactivity and customization to your scripts, allowing users to adjust settings as per their preferences.

What is the input.session Function?

The input.session function is used to add an input to the Inputs tab of your script’s Settings. This offers a way for you to provide configuration options to the users of your script. The function adds two dropdowns that allow the user to specify the beginning and the end of a trading session using the session selector, and the result is returned as a string.

Syntax and Arguments

The function takes several arguments that allow you to control its behaviour:

input.session(defval, title, options, tooltip, inline, group, confirm) → input string

Let’s break down these arguments:

  • defval: This argument determines the default value of the input variable shown in the script’s “Settings/Inputs” tab, where the user can modify it.
  • title: This sets the title of the input. If not specified, the variable name will be used as the title.
  • options: This argument provides a list of options for the user to choose from.
  • tooltip: The string that will be displayed to the user when hovering over the tooltip icon.
  • inline: This argument combines all the input calls using the same argument in one line.
  • group: This argument creates a header above all inputs using the same group argument string.
  • confirm: If set to true, then the user will be asked to confirm the input value before the indicator is added to the chart. The default value is false.

An Example of input.session

Here’s a simple example that uses the input.session function:

pinescriptCopy code
//@version=5
indicator("input.session", overlay=true)
i_sess = input.session("1300-1700", "Session", options=["0930-1600", "1300-1700", "1700-2100"])
t = time(timeframe.period, i_sess)
bgcolor(time == t ? color.green : na)
input.sessions in pine script

In this example:

  • The input.session function is used to create a dropdown with three different session options: “0930-1600”, “1300-1700”, and “1700-2100”. The default session is “1300-1700”.
  • The time(timeframe.period, i_sess) function uses the user’s selected session to calculate the current time.
  • The bgcolor(time == t ? color.green : na) function changes the background color of the chart to green during the selected session.

Key Takeaway

The input.session function provides an essential way to make your Pine Script indicators and strategies more customizable and user-friendly. By using this function, you can provide your users with configurable options that help them tailor your scripts to their specific needs and preferences.

Conclusion

In conclusion, the input.session function is a powerful feature in Pine Script that allows developers to create more flexible and interactive scripts. The ability to define various inputs and provide users with the ability to choose their preferred sessions is invaluable in creating a more user-friendly experience. Understanding and using this function effectively can significantly enhance your script’s functionality and user experience.

Leave a Comment