
Pine Script libraries have always been about sharing reusable code. Since their introduction, libraries could export functions, user-defined types, and enum types for other scripts to import and use1. But there was a gap: you could not export a simple constant value. If you wanted to share a mathematical ratio, a threshold number, or a color value across multiple scripts, you had to wrap it in a function that returned the value. The June 2025 update closes that gap by allowing libraries to export user-defined constant variables directly2.
This guide covers the new syntax, the supported types, practical use cases, and how exported constants differ from exported functions.
Before the June 2025 update, the export keyword in Pine Script libraries could only be applied to functions, methods, user-defined type definitions, and enum definitions1. If you wanted to share a constant value like the golden ratio or a standard RSI threshold with other scripts, you had two options: define it directly in every script that needed it, or create an exported function that returned the value.
Neither approach was ideal. Duplicating constants across scripts creates maintenance headaches when values need to change. Wrapping a constant in a function adds unnecessary syntax overhead and obscures what is really just a simple value.
The update adds support for exporting constant variables from libraries2. The syntax uses the existing export keyword combined with the const qualifier to declare a variable that other scripts can import and use directly, without calling a function.
The syntax for exporting a constant from a library is straightforward. Place the export keyword before the variable declaration and include the const qualifier before the type:
export const float SILVER_RATIO = 1.0 + math.sqrt(2)
This declares a constant named SILVER_RATIO with the value of 1 + the square root of 2 (approximately 2.414), and makes it available to any script that imports the library.
The general pattern is:
export const <type> <NAME> = <value>
There are several rules governing this declaration:
Exported constant variables are limited to five primitive types3. Each type has clear use cases for shared constants.
Integer constants are useful for default lengths, bar counts, and threshold values that do not require decimal precision.
export const int DEFAULT_RSI_LENGTH = 14
export const int MAX_LOOKBACK = 500
export const int OVERBOUGHT_LEVEL = 70
export const int OVERSOLD_LEVEL = 30
Float constants are ideal for mathematical ratios, multipliers, and precise threshold values. The value can include calls to const-compatible math functions like math.sqrt().
export const float GOLDEN_RATIO = 1.618033988749895
export const float SILVER_RATIO = 1.0 + math.sqrt(2)
export const float FIB_0236 = 0.236
export const float FIB_0382 = 0.382
export const float FIB_0618 = 0.618
Boolean constants can serve as feature flags or default settings that importing scripts reference.
export const bool ENABLE_DEBUG = false
export const bool USE_CONFIRMED_BARS = true
Color constants let you define a shared palette that multiple indicators and strategies use. This ensures visual consistency across a suite of scripts.
export const color BULLISH_COLOR = #26a69a
export const color BEARISH_COLOR = #ef5350
export const color NEUTRAL_COLOR = #787b86
export const color SIGNAL_COLOR = #2196f3
String constants are useful for timeframe identifiers, formatting patterns, and label text that multiple scripts share.
export const string DEFAULT_TIMEFRAME = "1D"
export const string VOLUME_FORMAT = "{0,number,###,###}"
export const string VERSION_TAG = "v2.1.0"
A practical constants library combines related values into a single importable package. Here is a complete example of a library that exports mathematical constants, Fibonacci levels, and a color palette for trading indicators:
//@version=6
// @description Provides shared constants for trading indicators.
library("TradingConstants")
// Mathematical constants
export const float GOLDEN_RATIO = 1.618033988749895
export const float SILVER_RATIO = 1.0 + math.sqrt(2)
export const float SQRT_TWO = math.sqrt(2)
// Fibonacci retracement levels
export const float FIB_0236 = 0.236
export const float FIB_0382 = 0.382
export const float FIB_0500 = 0.500
export const float FIB_0618 = 0.618
export const float FIB_0786 = 0.786
// RSI thresholds
export const int RSI_OVERBOUGHT = 70
export const int RSI_OVERSOLD = 30
export const int RSI_LENGTH = 14
// Shared color palette
export const color COLOR_BULL = #26a69a
export const color COLOR_BEAR = #ef5350
export const color COLOR_NEUTRAL = #787b86
This library must be published (privately or publicly) before other scripts can import it. Once published, any script can access these constants using the standard import statement.
Importing constants from a library works the same way as importing functions or types1. Use the import statement with the publisher's username, library name, and version number. Then reference the constants using the library's namespace or alias.
//@version=6
indicator("Using library constants", overlay = true)
import userName/TradingConstants/1 as tc
// Use Fibonacci constants for retracement levels.
float priceRange = ta.highest(high, 50) - ta.lowest(low, 50)
float highPoint = ta.highest(high, 50)
plot(highPoint - priceRange * tc.FIB_0382, "38.2% Retracement", tc.COLOR_BULL)
plot(highPoint - priceRange * tc.FIB_0618, "61.8% Retracement", tc.COLOR_BEAR)
// Use RSI thresholds for overbought/oversold detection.
float rsi = ta.rsi(close, tc.RSI_LENGTH)
hline(tc.RSI_OVERBOUGHT, "Overbought")
hline(tc.RSI_OVERSOLD, "Oversold")
The imported constants behave exactly like locally defined constants. They have the "const" qualifier, which means they can be used anywhere a const value is accepted, including in function parameters that require "const" or "input" arguments4. This is a significant advantage over exported functions, which always return "simple" or "series" values1.
Before this update, the common workaround for sharing constant values was to create an exported function that returned the value:
// Old approach: function that returns a constant
export goldenRatio() =>
1.618033988749895
This works, but it has a fundamental limitation. Library functions always return "simple" or "series" results1. They cannot return values with the "const" or "input" qualifier. This means you cannot use the result of a library function in places that require a compile-time constant.
For example, the show_last parameter of plot() requires a "const int" or "input int" value3. A library function returning an integer cannot satisfy this requirement. But an exported constant can:
// This works with exported constants
export const int SHOW_LAST_BARS = 50
// In the importing script:
plot(close, show_last = tc.SHOW_LAST_BARS) // Valid: const int accepted
// This would NOT work with a function
// plot(close, show_last = tc.getShowLast()) // Error: simple int not accepted
The key differences between exported constants and exported functions are:
Exported constants are most valuable when you maintain multiple scripts that share common values.
If you publish a suite of indicators that all use the same RSI length, moving average periods, or volatility thresholds, define those values once in a constants library. When you need to adjust a threshold, update the library and increment its version. All importing scripts will use the new value once they update their import statement.
Visual consistency across a set of related indicators improves readability. Export your color scheme from a single library so that bullish, bearish, and neutral colors are identical across every chart tool you build.
Fibonacci ratios, Gann angles, standard deviation multipliers, and other mathematical values that appear repeatedly across trading algorithms belong in a shared library. This eliminates typos and ensures every script uses the same precise values.
Boolean constants can serve as feature flags during development. Set a debug constant to true in your library to enable verbose logging across all your scripts during testing, then flip it to false for production use.
Shared constants ensure that all your indicators and strategies use the same thresholds, ratios, and configuration values. Once your Pine Script strategies are producing consistent signals based on well-defined constants, you can automate execution through TradersPost. Set up webhook alerts in TradingView and let TradersPost route orders to your brokerage account automatically.
Visit TradersPost to connect your TradingView strategies to live and paper trading accounts across stocks, options, futures, and crypto5.
1 Pine Script User Manual
2 Pine Script Release Notes
3 Pine Script v6 Language Reference
4 Pine Script v5 to v6 Migration Guide
5 TradersPost - Automated Trading Platform