Pre-defined Functions in PineScript


Posted by: Invostock.com
Published on: January 16, 2023
Pre-defined Functions in PineScript

Pine Script has a number of predefined functions that can be used to perform various operations such as mathematical calculations, data analysis, and charting. Here are some examples of commonly used predefined functions in Pine Script:

Mathematical Functions:

  • abs(x): returns the absolute value of x.
  • acos(x): returns the arc cosine of x.
  • asin(x): returns the arc sine of x.
  • atan(x): returns the arc tangent of x.
  • ceil(x): returns the smallest integer that is greater than or equal to x.
  • cos(x): returns the cosine of x.
  • exp(x): returns e raised to the power of x.
  • floor(x): returns the largest integer that is less than or equal to x.
  • log(x): returns the natural logarithm (base e) of x.
  • ln(x): returns the natural logarithm (base e) of x.
  • max(x1, x2, ...): returns the maximum value among its arguments.
  • min(x1, x2, ...): returns the minimum value among its arguments.
  • pow(x, y): returns x raised to the power of y.
  • round(x): returns x rounded to the nearest integer.
  • sin(x): returns the sine of x.
  • sqrt(x): returns the square root of x.
  • tan(x): returns the tangent of x.

Technical Indicator Functions:

  • sma(source, length): returns the Simple Moving Average of the source data with a specified length.
  • ema(source, length): returns the Exponential Moving Average of the source data with a specified length.
  • macd(source, fast_length, slow_length, signal_length): returns the Moving Average Convergence Divergence of the source data with specified fast_length, slow_length and signal_length.
  • rsi(source, length): returns the Relative Strength Index of the source data with a specified length.
  • stoch(source, fast_length, slow_length): returns the Stochastic Oscillator of the source data with specified fast_length and slow_length.
  • adx(source, length): returns the Average Directional Index of the source data with a specified length.
  • cci(source, length): returns the Commodity Channel Index of the source data with a specified length.

Data Access Functions:

  • open, high, low, close, volume : returns the open, high, low, close, and volume data of the current bar respectively.
  • bar_index : returns the index of the current bar.
  • time : returns the timestamp of the current bar.
  • security(ticker, resolution, lookahead = barmerge.lookahead_on) : returns the security with the specified ticker symbol, resolution, and lookahead mode.

Control Flow Functions:

  • input(def, type=input.type.real, title="") : creates an input parameter with a default value, input type, and title.
  • strategy.entry(id, strategy.long, qty=1, when=strategy.entry.at_market, comment="") : creates a long entry order with an id, quantity, order type, and comment.
  • strategy.exit(id, comment="") : closes a position with a specified id and comment.

These are just a few examples of the many predefined functions available in Pine Script. Pine Script has a vast library of functions that can be used to perform various types of calculations, data analysis, and charting. It's worth noting that Pine Script is an open-source language and new functions are continuously being added by the community