TTSCoupon
Back to blog
TutorialsMay 8, 202612 min read

TS Script Tutorial: Build Your First Custom TrendSpider Indicator

TS Script is TrendSpider's scripting language for custom indicators, alerts, and strategies. This 30-minute tutorial gets you from zero to deploying your first working indicator.

Verified TrendSpider Coupon
Save 25% on every annual plan
Applies to All annual plans (Essential, Elite, Advanced). Auto-applies at checkout.
Apply

Why learn TS Script

The AI Strategy Lab handles 80% of what most traders need — describe a strategy in English and TrendSpider builds it. But for the remaining 20% — custom indicators, complex multi-condition alerts, advanced strategy logic — TS Script is the tool. It's TrendSpider's equivalent of TradingView's Pine Script.

This tutorial walks you from a blank script to a working custom indicator in 30 minutes.

TS Script vs Pine Script (for TradingView migrants)

If you're coming from Pine Script, the mental model is similar but the syntax is different:

ConceptPine ScriptTS Script
Variable declarationx = closex := close
Functionf(x) =>function f(x) {}
Series accessclose[1]close.shift(1)
Plotplot(x)plot(x)

TS Script is closer to JavaScript syntactically; Pine is closer to Python. Most Pine users adapt within a week.

Where to write TS Script in TrendSpider

Open any chart → right toolbar → Custom Indicators → New Script. The TS Script editor opens with syntax highlighting, autocomplete, and built-in documentation.

Tutorial: Building a "Volatility Squeeze Breakout" indicator

We'll build an indicator that flags when price breaks out of a low-volatility consolidation — a classic momentum setup. Three components:

Step 1: Define the squeeze

A "squeeze" exists when Bollinger Bands are inside the Keltner Channels. Code:

```

// 20-period Bollinger Bands

bbBasis := sma(close, 20)

bbDev := 2 * stdev(close, 20)

bbUpper := bbBasis + bbDev

bbLower := bbBasis - bbDev

// 20-period Keltner Channels

kcBasis := ema(close, 20)

kcRange := atr(20) * 1.5

kcUpper := kcBasis + kcRange

kcLower := kcBasis - kcRange

// Squeeze condition

squeeze := bbUpper < kcUpper && bbLower > kcLower

```

Step 2: Detect the breakout

The breakout fires when the squeeze ends AND price closes above the 20-period high (bullish) or below the 20-period low (bearish):

```

period := 20

priorHigh := highest(high, period).shift(1)

priorLow := lowest(low, period).shift(1)

// Was in a squeeze, now breaking

wasSqueezed := squeeze.shift(1)

exitedSqueeze := wasSqueezed && !squeeze

bullishBreakout := exitedSqueeze && close > priorHigh

bearishBreakdown := exitedSqueeze && close < priorLow

```

Step 3: Plot the signal

```

// Color the bars

plotBar(bullishBreakout ? "lime" : bearishBreakdown ? "red" : null)

// Plot a marker on the breakout bar

plotShape(bullishBreakout, "triangleUp", "below", "lime", "Squeeze Long")

plotShape(bearishBreakdown, "triangleDown", "above", "red", "Squeeze Short")

// Background highlight during squeeze

bgcolor(squeeze ? "yellow:0.1" : null)

```

Save the script. Apply it to any chart. You'll see yellow shaded zones during squeezes and triangle markers when price breaks out.

Step 4: Convert to an alert

In the script editor, click "Use as Alert Condition." TrendSpider auto-generates an alert template:

```

Alert when: bullishBreakout = true on watchlist [your watchlist]

Notify via: SMS, Email, Webhook

```

Save. TrendSpider now monitors every name on your watchlist and fires whenever a squeeze breakout triggers in real time.

Step 5: Convert to a backtest

Same script, but wrap entry/exit logic for the Strategy Tester:

```

strategy.entry("long", bullishBreakout, size=0.1) // 10% of equity

strategy.exit("longExit", "long", profit=0.08, stop=0.04) // +8% / -4%

strategy.entry("short", bearishBreakdown, size=0.1)

strategy.exit("shortExit", "short", profit=0.08, stop=0.04)

```

Run the Strategy Tester. You get a tearsheet with win rate, profit factor, drawdown, etc.

Common TS Script gotchas

1. Look-ahead bias

Always use close.shift(1) for prior-bar values, not close. Otherwise you're using future data in your signal.

2. Repainting

If your script uses live (intra-bar) values like close, alerts may fire then "un-fire" as the bar updates. Use close.shift(1) for confirmed-bar logic, OR set the alert to fire only on bar close.

3. Time zone

TrendSpider runs on UTC internally. Use time.dayOfWeek and time.hour carefully if you want to skip US market open or pre-market.

4. Complex strings break the editor

Keep variable names short and avoid emoji or special characters in plot labels. Older versions of the editor have parsing issues.

Where to go next

Once you're comfortable with the basics:

1. Combine TS Script with the AI Strategy Lab — generate a base strategy with AI, refine the edge cases manually

2. Build a personal indicator library — save common setups (ORB, VWAP reclaim, squeeze breakout) as reusable scripts

3. Backtest aggressively — every script should be backtested on at least 5 years of data on a liquid universe before live trading

4. Read the official TS Script documentation — TrendSpider's docs have a complete language reference

TL;DR

TS Script unlocks the long tail of what TrendSpider can do. Most traders never need it because the AI Strategy Lab covers the common cases. But if you have a specific edge that doesn't translate cleanly into English prompts, TS Script is your tool.

Start the 7-day free trial and apply SET25 at upgrade. The TS Script editor is included on every plan.

Keep reading