Home » Pinescript Syntax » Fundamental Types in Pine Script

Fundamental Types in Pine Script

Photo of author
Published on

Pine Script uses various data types to handle different kinds of values. Understanding these fundamental types is key to writing efficient and effective scripts for custom indicators and strategies.

Understanding Pine Script Types

In Pine Script, each data type has a specific role, determining how values are stored, manipulated, and interacted with. The types you’ll frequently encounter are int, float, bool, color, and string.

The Fundamental Types Explained

1. Integer (int)
  • Definition: Represents whole numbers without fractional components.
  • Usage: Commonly used in counting operations, indexing, and time-based calculations.
  • Examples:
  • bar_index (index of the current bar)
  • dayofmonth (day of the month as an integer)
  • Syntax:
  int lengthInput = input.int(100, "Length", minval = 2)
  • Explanation: Here, lengthInput is an integer variable used to determine the length of a moving average.
2. Floating-Point (float)
  • Definition: Represents numbers with fractional parts.
  • Usage: Essential for calculations involving price data, averages, and mathematical computations.
  • Examples:
  • close (closing price of a bar)
  • ta.vwap (volume-weighted average price)
  • Syntax:
  float ma = ta.sma(close, lengthInput)
  • Explanation: ma calculates the Simple Moving Average (SMA) of closing prices over a specified period.
3. Boolean (bool)
  • Definition: Represents logical values, either true or false.
  • Usage: Used in conditional statements and logical expressions.
  • Examples:
  • ta.crossover(close, ma) (checks if the close price crosses over the moving average)
  • Syntax:
  bool crossUp = ta.crossover(close, ma)
  • Explanation: crossUp is a boolean that becomes true if the close price crosses over the moving average.
4. Color (color)
  • Definition: Used to define colors in scripts, particularly for visual elements like indicators.
  • Usage: Specifying plot colors, background colors, and other visual elements.
  • Examples:
  • color.red (predefined red color)
  • #FF0000 (hexadecimal representation of red)
  • Syntax:
  color maColor = close > ma ? color.lime : color.fuchsia
  • Explanation: maColor changes between lime and fuchsia based on the relationship between the close price and the moving average.
5. String (string)
  • Definition: Represents sequences of characters, including letters, numbers, and symbols.
  • Usage: Used for text in labels, titles, and dynamic text generation.
  • Examples:
  • "MA" (a simple string for a title)
  • Syntax:
  string MA_TITLE = "MA"
  • Explanation: MA_TITLE is a string variable used as a title for a moving average plot.

Key Takeaway

The proper use of fundamental types in Pine Script is crucial for creating scripts that are not only functional but also efficient and easy to read. Each type serves a specific purpose, and understanding these purposes allows for better script design and implementation.

Conclusion

In conclusion, mastering the fundamental types in Pine Script is a foundational skill for anyone looking to develop custom indicators and strategies on TradingView. By understanding and utilizing these types, your scripts can become more powerful, flexible, and user-friendly.

Leave a Comment