Files
QuanTAlib/docs/pinescript.md
T

19 KiB

PineScript Guide: QuanTAlib Indicators for TradingView

"You do not need to understand the math. But the math does not care whether you understand it."

Welcome, Brave Copy-Paster

You are here because you want an indicator on your TradingView chart. Maybe someone on Crypto Twitter posted a screenshot with colored lines and you thought "I need that." Maybe you googled "best RSI Pine Script" at 2 AM. Maybe you clicked a link by accident. All valid paths to enlightenment.

Here is the good news: QuanTAlib provides Pine Script v6 source code for every one of its 393 indicators. Each script is self-contained, tested against the C# reference implementation, and ready to paste into TradingView's Pine Editor. No dependencies. No imports. No subscription to someone's Discord.

Here is the less-good news: the scripts contain actual mathematics. You do not have to read it. But it is there, silently judging.

How to Use a Pine Script (The Short Version)

  1. Find the indicator you want (see table below)
  2. Click the link to open the .pine file
  3. Copy the entire file contents
  4. Open TradingView. Click "Pine Editor" at the bottom
  5. Delete whatever is in there. Paste the code
  6. Click "Add to chart"
  7. Adjust the inputs in the indicator settings panel

That is it. You have deployed a mathematically rigorous indicator without writing a single line of code. Your ancestors would be proud.

Finding Your Indicator

Every indicator has a .pine file sitting next to its C# implementation and documentation. The folder structure is predictable:

lib/
  trends_IIR/
    ema/
      Ema.cs          ← the actual engine (C#, you can ignore this)
      Ema.md          ← documentation (math, history, validation)
      ema.pine        ← THIS IS WHAT YOU WANT

By category

Pick your category. Find your indicator. Click the .pine link. Copy. Paste. Done.

Core (price transforms)

These turn OHLC data into derived price series. If you do not know what "typical price" means, you probably want close instead.

Indicator What It Does Pine Script
AVGPRICE Average of OHLC avgprice.pine
HA Heikin-Ashi candles ha.pine
MEDPRICE Median of high and low medprice.pine
MIDPOINT Midpoint of highest and lowest midpoint.pine
MIDPRICE Midpoint of high and low midprice.pine
TYPPRICE Typical price: (H+L+C)/3 typprice.pine
WCLPRICE Weighted close: (H+L+2C)/4 wclprice.pine

Moving Averages: FIR (the "simple" ones)

FIR stands for Finite Impulse Response. It means the average uses a fixed window of bars. SMA is an FIR filter. You have been using FIR filters this whole time.

Indicator What It Does Pine Script
SMA Simple Moving Average (the one everyone knows) sma.pine
WMA Weighted Moving Average wma.pine
HMA Hull Moving Average (fast, smooth) hma.pine
ALMA Arnaud Legoux MA (offset + sigma tuning) alma.pine
TRIMA Triangular Moving Average trima.pine
LSMA Least Squares (linear regression line) lsma.pine
FWMA Fibonacci Weighted MA fwma.pine
GWMA Gaussian Weighted MA gwma.pine
SWMA Symmetric Weighted MA swma.pine
DWMA Double Weighted MA dwma.pine
SINEMA Sine-Weighted MA sinema.pine
HANMA Hann-Weighted MA hanma.pine
PARZEN Parzen-Weighted MA parzen.pine
SGMA Savitzky-Golay MA sgma.pine
TSF Time Series Forecast tsf.pine
BLMA Blackman MA blma.pine
PWMA Pascal Weighted MA pwma.pine
NLMA Non-Linear MA nlma.pine
ILRS Integral of Linear Regression Slope ilrs.pine
HAMMA Hamming MA hamma.pine
KAISER Kaiser-Windowed MA kaiser.pine
LANCZOS Lanczos MA lanczos.pine
PMA Polynomial MA pma.pine
NYQMA Nyquist MA nyqma.pine
QRMA QR Decomposition MA qrma.pine
RWMA Range Weighted MA rwma.pine
HEND Henderson MA hend.pine
CONV Convolution (custom kernel) conv.pine
BWMA Butterworth-Weighted MA bwma.pine
CRMA Crowley MA crma.pine
SP15 SP-15 MA sp15.pine
TUKEY_W Tukey-Windowed MA tukey_w.pine
RAIN RAIN MA rain.pine

Moving Averages: IIR (the "smart" ones)

IIR stands for Infinite Impulse Response. These use recursive feedback: the previous output affects the current output. Generally smoother, lower lag, and harder to understand. The indicator descriptions link to documentation if curiosity ever strikes.

Indicator What It Does Pine Script
EMA Exponential MA (the popular one) ema.pine
DEMA Double EMA (less lag) dema.pine
TEMA Triple EMA (even less lag) tema.pine
T3 Tillson T3 (six-stage cascade) t3.pine
JMA Jurik MA (adaptive, low noise) jma.pine
KAMA Kaufman Adaptive MA kama.pine
VIDYA Variable Index Dynamic Average vidya.pine
FRAMA Fractal Adaptive MA frama.pine
MAMA MESA Adaptive MA (Ehlers) mama.pine
HOLT Holt Exponential Smoothing holt.pine
HWMA Holt-Winters MA hwma.pine
RMA Wilder's MA (used inside RSI) rma.pine
ZLEMA Zero-Lag EMA zlema.pine
ZLDEMA Zero-Lag Double EMA zldema.pine
ZLTEMA Zero-Lag Triple EMA zltema.pine
MGDI McGinley Dynamic mgdi.pine
LEMA Leader EMA lema.pine
HEMA Hull EMA hema.pine
GDEMA Generalized Double EMA gdema.pine
DSMA Deviation-Scaled MA dsma.pine
CORAL Coral Trend Filter coral.pine
AHRENS Ahrens MA ahrens.pine
DECYCLER Ehlers Decycler decycler.pine
MCNMA McNicholl EMA mcnma.pine
MMA Modified MA mma.pine
NMA Natural MA nma.pine
QEMA Quad EMA qema.pine
REMA Regularized EMA rema.pine
RGMA Recursive Gaussian MA rgma.pine
TRAMA Trend Regularity Adaptive MA trama.pine
LTMA Linear Trend MA ltma.pine
HTIT Hilbert Transform Instantaneous Trend htit.pine
ADXVMA ADX Variable MA adxvma.pine
VAMA Volatility Adjusted MA vama.pine
YZVAMA Yang-Zhang Volatility Adjusted MA yzvama.pine
MAVP Moving Average Variable Period mavp.pine

Oscillators

Numbers that bounce between limits. Overbought, oversold, divergence. You know the drill.

Indicator What It Does Pine Script
RSI Relative Strength Index (the king) rsi.pine
STOCH Stochastic Oscillator stoch.pine
STOCHF Fast Stochastic stochf.pine
STOCHRSI Stochastic RSI stochrsi.pine
CCI Commodity Channel Index cci.pine
WILLR Williams %R willr.pine
FISHER Fisher Transform (Ehlers) fisher.pine
QQE Qualitative Quantitative Estimation qqe.pine
TRIX Triple EMA Rate of Change trix.pine
KDJ KDJ Indicator See oscillators
CTI Correlation Trend Indicator cti.pine
LRSI Laguerre RSI lrsi.pine
REFLEX Ehlers Reflex reflex.pine
SMI Stochastic Momentum Index smi.pine
STC Schaff Trend Cycle stc.pine

Momentum

How fast price is moving. Direction matters here.

Indicator What It Does Pine Script
MACD Moving Average Convergence Divergence macd.pine
ROC Rate of Change roc.pine
MOM Momentum (price change over N bars) mom.pine
TSI True Strength Index tsi.pine
CMO Chande Momentum Oscillator cmo.pine
PPO Percentage Price Oscillator ppo.pine
VEL Velocity vel.pine
BOP Balance of Power bop.pine
CFB Composite Force Balance cfb.pine

Dynamics (trend strength)

Is there a trend? How strong? These indicators answer that.

Indicator What It Does Pine Script
ADX Average Directional Index adx.pine
AROON Aroon Up/Down aroon.pine
SUPERTREND SuperTrend (ATR-based stops) super.pine
ICHIMOKU Ichimoku Cloud ichimoku.pine
VORTEX Vortex Indicator vortex.pine
CHOP Choppiness Index chop.pine
ALLIGATOR Williams Alligator alligator.pine
PSAR Parabolic SAR psar.pine

Volatility

How much price moves. Not direction. Just magnitude.

Indicator What It Does Pine Script
ATR Average True Range atr.pine
TR True Range tr.pine
BBW Bollinger Band Width bbw.pine
HV Historical Volatility hv.pine
NATR Normalized ATR natr.pine

Channels (bands around price)

Upper band, lower band, sometimes a middle. Price bounces between them. In theory.

Indicator What It Does Pine Script
BBANDS Bollinger Bands bbands.pine
KCHANNEL Keltner Channels kchannel.pine
DCHANNEL Donchian Channels dchannel.pine
PCHANNEL Price Channels pchannel.pine
ACCBANDS Acceleration Bands accbands.pine

Volume

What the crowd is doing with their money.

Indicator What It Does Pine Script
OBV On-Balance Volume obv.pine
VWAP Volume Weighted Average Price vwap.pine
MFI Money Flow Index (volume RSI) mfi.pine
CMF Chaikin Money Flow cmf.pine
ADL Accumulation/Distribution Line adl.pine
VWMA Volume Weighted MA vwma.pine
KVO Klinger Volume Oscillator kvo.pine

Filters (signal processing)

These are the heavy artillery. Kalman filters, Butterworth filters, wavelets. If you do not know what a transfer function is, start with the moving averages above and come back when you are ready. No judgment. (Some judgment.)

Indicator What It Does Pine Script
KALMAN Kalman Filter kalman.pine
SGF Savitzky-Golay Filter sgf.pine
SSF2 Ehlers Super Smoother (2-pole) ssf2.pine
SSF3 Ehlers Super Smoother (3-pole) ssf3.pine
GAUSS Gaussian Filter gauss.pine
BUTTER2 Butterworth (2nd order) butter2.pine
BUTTER3 Butterworth (3rd order) butter3.pine
HPF High-Pass Filter hpf.pine
LAGUERRE Laguerre Filter laguerre.pine
ROOFING Ehlers Roofing Filter roofing.pine
VOSS Voss Predictor voss.pine

Not every indicator is listed here. Browse the full catalog of 393 indicators for the complete collection, including cycles, statistics, error metrics, reversals, numerics, and forecasts.

Anatomy of a QuanTAlib Pine Script

Every script follows the same structure. Understanding this structure is optional but occasionally useful when things do not look right on your chart.

// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Exponential Moving Average (EMA)", "EMA", overlay=true)

// ---- The function definition ----
// This is where the math lives. You do not need to touch this.
ema(series float source, simple int period=0, simple float alpha=0) =>
    // ... math happens here ...
    result

// ---- Inputs ----
// These create the settings panel on TradingView
i_period = input.int(10, "Period", minval=1)
i_source = input.source(close, "Source")

// ---- Calculation ----
ema_value = ema(i_source, period=i_period)

// ---- Plot ----
plot(ema_value, "EMA", color=color.yellow, linewidth=2)

The parts that matter to you

  • Inputs section: Change 10 to whatever period you want as the default. Or just use the TradingView settings panel after adding the indicator.
  • Plot section: Change color.yellow to whatever color you prefer. Options include color.red, color.green, color.blue, color.white, color.orange, color.purple.
  • The function: Do not touch this unless you know what you are doing. It was validated against the C# reference implementation. Your modifications will not be validated against anything.

Common Questions

"The indicator shows values from bar 1 — shouldn't there be a warmup gap?"

That is correct behavior. QuanTAlib generates output from the very first bar. A 14-period RSI with only 5 bars uses the best estimate possible given available data. The values become fully converged once enough bars have accumulated, but you never see NaN or blank bars. Other libraries leave gaps. QuanTAlib fills them with mathematically defensible approximations that improve as data accumulates.

"Can I use this in a strategy?"

Yes. Replace indicator(...) with strategy(...) and add your entry/exit logic. The indicator function itself does not change.

"The values differ from TradingView's built-in version"

Possible reasons, in order of likelihood:

  1. Different default parameters. Check the period, source, and any multipliers.
  2. Different warmup handling. QuanTAlib uses exponential compensation from bar 1. TradingView's built-in indicators sometimes use different warmup methods.
  3. You are looking at the wrong indicator. DEMA is not "double EMA period." It is a specific algorithm by Patrick Mulloy.

"I modified the math and now it is broken"

Put the original code back. The math was correct before you edited it.

"Which moving average should I use?"

If you are asking this question, use EMA. It is the Honda Civic of moving averages: reliable, understood by everyone, and good enough for most situations. Come back and explore JMA, KAMA, or T3 when you have a specific problem that EMA does not solve.

"Can I combine multiple indicators?"

Yes. Add multiple scripts to your chart or combine functions within a single script:

//@version=6
indicator("EMA + RSI Combo", overlay=false)

// Calculate both
ema_val = ta.ema(close, 20)     // TradingView built-in
rsi_val = ta.rsi(close, 14)     // TradingView built-in

// Or use QuanTAlib versions by pasting the function definitions
// from their respective .pine files and calling them

plot(rsi_val, "RSI", color=color.purple)
hline(70, "Overbought", color=color.red)
hline(30, "Oversold", color=color.green)

Going Deeper (If You Dare)

Each indicator has a .md documentation file next to its .pine file. These contain:

  • Mathematical formulas (yes, with actual math notation)
  • Historical context (who invented it and why)
  • Performance characteristics
  • Validation against other libraries
  • Common pitfalls

For example: EMA documentation explains the exponential warmup compensator, why it matters, and why most other implementations get the first few values wrong.

You do not have to read any of this. But if you ever wonder why your backtest results differ from someone else's, the answer is probably in there.

The Full Catalog

All 393 indicators with descriptions →

Every indicator in that list has a .pine file. Every .pine file works on TradingView. Every implementation matches the C# reference engine.

Copy responsibly.