Appearance
Imports and libraries
How do you use functions from a library in your script? Write import <publisher>/<name>/<version> as <alias> after the declaration, then call each exported function as <alias>.<function>(). The import finds the library by the name you saved it under in your script store, which is the path's last part that isn't a number, usually the middle one. The title in its library() call doesn't count.
pine
//@version=6
library("Kit")
// Each exported function is what an importer can call.
export double(float x) => x * 2pine
//@version=6
indicator("Uses Kit")
import Me/Kit/1 as kit
plot(kit.double(close))What an import finds
An import resolves to the first of these that matches:
| Path | What it links |
|---|---|
TradingView/ta/<any version> | TradingView's own ta library, its v14 text, whatever version the path names |
TradingView/RelativeValue/<any version> | A built-in port of TradingView's RelativeValue library |
| Anything else | The newest library saved in your script store under that name, whatever publisher and version the path names |
Because only the name counts, two libraries with the same name can't be used side by side. An import the store doesn't have stops the run with "Library <path> is not in the script store", so save the library as a script and run again.
Left out, the alias is the path's last part that isn't a number: import TradingView/ta/7 reads as ta.
What a library brings
The importer gets the library's exported functions, types, enums and constants under the alias. Nothing else of the library runs in the importer: its plots and its library() call stay with the library itself. The linker writes the library's text into the script before any engine reads it, so every engine runs the same plain text.
In AmenoScript, an import can also bring in a whole saved indicator or strategy, and <alias>.run() runs its text in your context. See AmenoScript's extensions.
Built-in helpers
A few functions from TradingView's Strategy, RiskMetrics and colour libraries are always available, with no import, under their own prefixes. They aren't TradingView names: a script that uses them won't compile on TradingView.
| Function | Returns |
|---|---|
tvstrat.priceToTicks(price) | price in ticks |
tvstrat.ticksToPrice(ticks, from) | The price ticks from from, the position's average price by default. na when flat |
tvstrat.ticksToCurrency(ticks) | ticks times the tick, as money per unit |
tvstrat.percentToTicks(percent, from) | percent of from in ticks. na when flat |
tvstrat.percentToPrice(percent, from) | percent of from as a price distance. na when flat |
tvstrat.percentToCurrency(price, percent) | percent of price |
tvstrat.ticksToStopLevel(ticks) | The stop price ticks against the open position |
tvstrat.ticksToTpLevel(ticks) | The target price ticks in favour of the open position |
tvstrat.percentProfit(exitPrice) | The open position's profit in percent at exitPrice |
tvstrat.calcPositionSizeByStopLossTicks(stopLossTicks, riskPercent) | Units that lose riskPercent of equity at a stop stopLossTicks away, floored |
tvstrat.calcPositionSizeByStopLossPercent(stopLossPercent, riskPercent, entryPrice) | Units that lose riskPercent of equity at a stop stopLossPercent from entryPrice, the close by default, floored |
tvrisk.sharpeRatio(returnsArray, annualBenchmark, forceCalc, periodsPerYear) | The mean return less the benchmark's share per period, over the sample standard deviation |
tvrisk.sortinoRatio(returnsArray, annualBenchmark, forceCalc, periodsPerYear) | The same over the downside deviation |
tvrisk.ratingStatus(ratingValue, strongBound, weakBound) | A rating's word: strong or weak, buy or sell, or neutral |
tvutil.clamp01(x), tvutil.clamp255(x), tvutil.clamp_alpha(a), tvutil.clamp_int(x, lo, hi) | x held inside a range |
tvutil.safe_div(num, den, fallback) | num / den, or fallback when den is 0 or either is na |
tvutil.order2(a, b) | [lower, higher] |
tvta.* holds sixteen helpers from TradingView's ta library under a prefix of their own: ao, aroon, atr2, changePercent, dema, donchian, ema2, er, kvo, ppo, rms, tema, trima, trix, ulcerIndex and vi. An import TradingView/ta/<v> gives you the whole library instead.