library() - Pine Script Function
library()
Section titled “library()”Overview
Section titled “Overview”Use library() at the top of a script to publish reusable functions, constants, or variables. Library scripts cannot plot or trade; they only expose exports for other scripts to import.
Syntax
Section titled “Syntax”library(title, overlay, dynamic_requests) → voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
title | const string | Identifier used in import. Must be unique and contain no spaces. Required. |
overlay | const bool | Historical flag for compatibility; has no effect in libraries. |
dynamic_requests | const bool | Allow the library to use dynamic request.* calls (Pine v5). |
Remarks
Section titled “Remarks”- Functions, variables, or constants must be prefixed with
exportto be visible to consumers. - Library scripts cannot call order or plot functions (no visual output). Use indicators/strategies to display results.
- Import syntax:
import authorid/libraryname/1(where1is the version number).
Example
Section titled “Example”//@version=5// @description Utility math librarylibrary("math_utils", overlay=false)
export hypotenuse(float a, float b) => math.sqrt(a * a + b * b)export double(float x) => x * 2Usage in another script:
//@version=5import authorid/math_utils/1 as mu
indicator("Library consumer", overlay=false)plot(mu.hypotenuse(close, open))