Skip to the content

Help / Indicators and scripts

Writing your own scripts

When no built-in indicator does what you want, write your own. Scripts are JavaScript, with functions that will feel familiar if you know Pine Script: indicator(), input.*, ta.*, plot(), and strategy.entry() for strategies. They run in your browser, in a sandbox, and update with every tick. Every function is in the scripting reference.

Your first script#

  1. Open the Script editor tab in the bottom panel.
  2. Press + New… and choose a starter template.
  3. Edit the code. The editor completes names as you type and underlines mistakes.
  4. Press Add to chart. Errors show below the editor, with their line numbers.
indicator('Fast/slow EMA', { overlay: true });

const fast = input.int('Fast', 9, { min: 1 });
const slow = input.int('Slow', 21, { min: 1 });

plot(ta.ema(close, fast), { title: 'Fast', color: '#4a8df0' });
plot(ta.ema(close, slow), { title: 'Slow', color: '#f5a524' });

Inputs appear in a panel beside the editor once the script is on the chart, so you can change them without touching the code.

Averages over gaps: ta.ema and ta.rma (and RSI, ATR and the others built on them) give no value where what they average has none, and start again from a simple average of the next values, as Pine does. Until October 2026 they carried the last value flat across a gap, so a script or backtest on a series with gaps (an indicator drawn in pieces, another symbol with fewer bars) may now show a break where it showed a flat line, and trade differently there.

Comparing and adding series#

close, open and what ta.* returns are series: a value for every bar. JavaScript’s operators don’t work on them, so use methods instead: close.gt(open) rather than close > open, close.add(1) rather than close + 1, and select(cond, a, b) rather than cond ? a : b. Writing close > open stops the script with an error that says which method to use. if (cond) can’t be caught that way and is always true for a series, so pass conditions to functions such as plotshape() or strategy.entry(), or use cond.last() for the newest bar.

In a pane of its own, plotshape() and plotchar() put above and below markers at the pane’s top and bottom edges. Use { location: 'absolute', at: rsi } to put them on a line instead, and offset to move them back to the bar they’re about.

Strategies#

A script that starts with strategy() and places trades with strategy.entry(), strategy.exit() and strategy.close() can be backtested in the Strategy tester tab. See Backtesting a strategy.

Let the AI write it#

Under + New…, choose Write with AI…, describe what you want (“an RSI strategy that buys below 30 with a 5% stop”), then Add to my scripts or Write again. Read what it wrote before you rely on it.

The script library#

+ New… › Browse the library… has ready-made scripts to search, read and copy into your own scripts to change.

Alerts from a script#

A script can declare alertcondition()s. When it’s on the chart, they appear as conditions in the alert dialog, so you can be told when your own logic fires.

Note:Your scripts are saved as you type and follow your account to your other devices.

TradingView and Pine Script are trademarks of TradingView, Inc. OpenTapefolio is not affiliated with or endorsed by TradingView.