thinkScript functions in Pine Script
Each row is what the converter writes for it.
| thinkScript | Pine Script v6 |
|---|---|
def x = Average(close, 20); | x = ta.sma(close, 20) |
def x = ExpAverage(close, 20); | x = ta.ema(close, 20) |
def x = WildersAverage(close, 14); | x = ta.rma(close, 14) |
def x = MovingAverage(AverageType.HULL, close, 20); | x = ta.hma(close, 20) |
def x = Highest(high, 20); | x = ta.highest(high, 20) |
def x = Sum(volume, 10); | x = math.sum(volume, 10) |
def x = StDev(close, 20); | x = ta.stdev(close, 20) |
def x = TrueRange(high, close, low); | x = ta.tr(true) |
def x = ATR(14); | x = ta.atr(14) |
def x = RSI(14); | x = ta.rsi(close, 14) |
def x = MACD().Diff; | [_macdValue1, _macdAvg1, _macdDiff1] = ta.macd(close, 12, 26, 9)
x = _macdDiff1 |
def x = BollingerBands().UpperBand; | x = ta.sma(close, 20) + 2.0 * ta.stdev(close, 20) |
def x = close crosses above Average(close, 50); | x = ta.crossover(close, ta.sma(close, 50)) |
def x = if close > open then 1 else -1; | x = close > open ? 1 : -1 |
def x = IsNaN(close[1]); | x = na(close[1]) |
def x = AbsValue(close - open); | x = math.abs(close - open) |
def x = BarNumber(); | x = bar_index + 1 |
def x = GetValue(close, 5); | x = close[5] |
Other timeframes and symbols
thinkScript reads them inside any expression; Pine asks request.security().
| thinkScript | Pine Script v6 |
|---|---|
def x = close(period = AggregationPeriod.DAY); | x = request.security(syminfo.tickerid, "D", close) |
def x = high(period = AggregationPeriod.DAY)[1]; | x = request.security(syminfo.tickerid, "D", high[1], lookahead = barmerge.lookahead_on) |
def x = Average(close(period = AggregationPeriod.WEEK), 20); | x = request.security(syminfo.tickerid, "W", ta.sma(close, 20)) |
def x = close(symbol = "SPX"); | x = request.security("SP:SPX", timeframe.period, close) |
Why yesterday's high uses lookahead
On an intraday chart, high(period = AggregationPeriod.DAY) is today's high so far, and it changes until the day closes: Pine's request.security() without lookahead behaves the same way. Offset by a day first ([1]) and the value is finished, so asking for it with lookahead_on is safe and doesn't repaint: every bar sees yesterday's high as it was. A whole expression on another timeframe, such as a 20-week average, goes into one request, so the average is taken over weeks, as thinkorswim takes it.
Plots, bubbles, colours and orders
A plot's styles, set on later lines, fold into one plot() call.
| thinkScript | Pine Script v6 |
|---|---|
plot Avg = Average(close, 20);
Avg.SetDefaultColor(Color.CYAN);
Avg.SetLineWeight(2); | Avg = ta.sma(close, 20)
plot(Avg, "Avg", color = #00FFFF, linewidth = 2) |
plot Hist = close - open;
Hist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM); | Hist = close - open
plot(Hist, "Hist", style = plot.style_histogram) |
plot Up = close crosses above open[1];
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); | Up = ta.crossover(close, open[1])
plotshape(Up, "Up", shape.arrowup, location.belowbar, size = size.small) |
plot Line = close;
Line.AssignValueColor(if close > open then Color.GREEN else Color.RED); | Line = close
plot(Line, "Line", color = close > open ? #00FF00 : #FF0000) |
AddChartBubble(close > high[1], high, "Break", Color.GREEN, yes); | if close > high[1]
label.new(bar_index, high, "Break", color = #00FF00, textcolor = color.black, style = label.style_label_down, size = size.small) |
AssignPriceColor(if close > open then Color.GREEN else Color.CURRENT); | barcolor(close > open ? #00FF00 : na) |
def go = close crosses above Average(close, 20);
AddOrder(OrderType.BUY_AUTO, go); | go = ta.crossover(close, ta.sma(close, 20))
if go
strategy.entry("Long", strategy.long) |
What doesn't convert
Marked TODO in the code and listed with its line; the rest still compiles.
Future bars, thinkorswim data, thinkorswim-only studies
- A negative offset such as close[-1] reads a bar that hasn't happened yet. Pine can't, so the value becomes na and the line is marked.
- imp_volatility and open_interest are thinkorswim data a Pine script isn't given.
- TTM_Squeeze, ZigZagHighLow and other thinkorswim-only studies have no Pine source to call: rebuild them from their formula.
- HighestAll() and LowestAll() read the whole chart, later bars too; ta.max() and ta.min() give the highest and lowest so far.
- thinkScript's vwap price is each bar's own volume-weighted price, which TradingView doesn't give a script: hlc3 stands in. reference VWAP() converts to the session VWAP.
- SetHiding() and the alert sound have no Pine setting; the sound is picked in TradingView's alert dialog.
For the lines left to finish, Pine Fixer gives your AI the Pine v6 rules and the compiler's own error texts, and the Pine Script checker reads the result for repaint risks.