Indicator · Pine Script v5 · On the chart

TWD Triple-Layer Driver Detector

by stanwu

Normalises USD/TWD, TSMC, the 0050 ETF, the dollar index and the SOXX semiconductor ETF against their own moving averages and plots them together.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code80
Settings8inputs
LicenceMIT
Last update11 months ago14 Nov 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

80 lines of Pine Script v5 from twd_driver_detector_v1.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

twd_driver_detector_v1.pine
//@version=5
indicator("TWD Triple-Layer + Driver Detector (Emoji HUD)", overlay=true)

// === 使用者輸入(請依你實際代碼調整) ===
twdSym     = input.symbol("FX_IDC:USDTWD", "TWD FX (USD/TWD)")
tsmcSym    = input.symbol("TWSE:2330", "TSMC 2330")
etf0050Sym = input.symbol("TWSE:0050", "0050 ETF")
dxySym     = input.symbol("TVC:DXY",     "DXY Index")
soxSym     = input.symbol("NASDAQ:SOXX", "SOXX (SOX proxy)")

corrLen   = input.int(60,  "Correlation length (bars)", minval=10)
normLen   = input.int(100, "Normalization length",       minval=20)
corrThres = input.float(0.4, "主因判定門檻 (|corr| >=)", minval=0.1, maxval=1.0, step=0.05)

// === 取得各商品收盤價 ===
twd   = request.security(twdSym,     timeframe.period, close)
tsmc  = request.security(tsmcSym,    timeframe.period, close)
etf50 = request.security(etf0050Sym, timeframe.period, close)
dxy   = request.security(dxySym,     timeframe.period, close)
sox   = request.security(soxSym,     timeframe.period, close)

// === 簡單標準化:價格 / 最近 normLen 的平均 ===
f_norm(src) =>
    basis = ta.sma(src, normLen)
    basis == 0.0 ? na : src / basis

twdN   = f_norm(twd)
tsmcN  = f_norm(tsmc)
etf50N = f_norm(etf50)
dxyN   = f_norm(dxy)
soxN   = f_norm(sox)

// === 主圖:三層結構 ===
plot(twdN,   title="TWD (USD/TWD, normalized)",    color=color.red,    linewidth=3)
plot(tsmcN,  title="TSMC 2330 (normalized)",       color=color.blue,   linewidth=2)
plot(etf50N, title="0050 ETF (normalized)",        color=color.green,  linewidth=2)
plot(dxyN,   title="DXY (normalized)",             color=color.orange, linewidth=1)
plot(soxN,   title="SOXX (SOX proxy, normalized)", color=color.purple, linewidth=1)

// === 相關係數計算 ===
corr_twd_dxy  = ta.correlation(twd, dxy,  corrLen)
corr_twd_sox  = ta.correlation(twd, sox,  corrLen)
corr_twd_tsmc = ta.correlation(twd, tsmc, corrLen)

absD = math.abs(corr_twd_dxy)
absS = math.abs(corr_twd_sox)
absE = math.abs(corr_twd_tsmc)
maxAbs = math.max(absD, math.max(absS, absE))

driverUSD = absD == maxAbs and absD >= corrThres
driverSOX = absS == maxAbs and absS >= corrThres
driverEQ  = absE == maxAbs and absE >= corrThres

driver = driverUSD ? 1 : driverSOX ? 2 : driverEQ ? 3 : 0

// 主因名稱與 emoji
driverName  = driver == 1 ? "美元 DXY" : driver == 2 ? "全球科技 SOXX" : driver == 3 ? "本地股市 2330" : "混合 / 無明顯主導"
driverEmoji = driver == 1 ? "🟧"       : driver == 2 ? "🟪"            : driver == 3 ? "🟦"            : "⬜️"

// 背景顏色狀態燈
bgColor =
     driver == 1 ? color.new(color.orange, 85) :
     driver == 2 ? color.new(color.purple, 85) :
     driver == 3 ? color.new(color.blue,   85) :
                   color.new(color.gray,   90)

bgcolor(bgColor)

// === 右上角固定資訊 label(單行,百分比顯示) ===
var label infoLabel = na

if barstate.islast
    label.delete(infoLabel)
    percD = absD * 100.0
    percS = absS * 100.0
    percE = absE * 100.0
    txt = "主因 " + driverEmoji + " " + driverName + " | DXY=" + str.tostring(percD, "#.0") + "% SOXX=" + str.tostring(percS, "#.0") + "% 2330=" + str.tostring(percE, "#.0") + "%"
    topVal = math.max(math.max(twdN, tsmcN), math.max(etf50N, math.max(dxyN, soxN)))
    yPos = topVal + 0.15
    infoLabel := label.new(bar_index, yPos, txt, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_right, textcolor=color.white, color=color.new(color.black, 40))

Overview

TWD Triple-Layer Driver Detector then measures rolling correlations of USD/TWD with the dollar index, SOXX and TSMC, and labels whichever has the largest absolute value above a threshold as the main driver. Background colour and a last-bar label show the result. Inputs are symbols, correlation length, normalisation length and threshold.

Details

Published by

stanwu on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Screeners & dashboards

Markets

Forex, Stocks, Indices

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
TWD FX (USD/TWD)symbolFX_IDC:USDTWDtwdSym
TSMC 2330symbolTWSE:2330tsmcSym
0050 ETFsymbolTWSE:0050etf0050Sym
DXY IndexsymbolTVC:DXYdxySym
SOXX (SOX proxy)symbolNASDAQ:SOXXsoxSym
Correlation length (bars)int60corrLen
Normalization lengthint100normLen
主因判定門檻 (|corr| >=)float0.4corrThres
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Symbol Info Helper

by everget
948 stars

Symbol Info Helper is an informational utility script that displays metadata about the currently selected instrument directly on the main price chart. It gathers built-in symbol properties including ticker identifier, description, instrument type, point value,

Indicator v4 Screeners & dashboards
Updated 11 days ago · GPL-3.0 GitHub repository of Symbol Info Helper
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

Pricing Table

by namuan
318 stars

This indicator calculates theoretical options contract values and in-the-money probabilities using historical realized volatility across multiple standard deviations. It displays these metrics in a table overlay directly on the price chart. Users can adjust th

Indicator v4 Volatility & bandsScreeners & dashboards
Updated 2 months ago · MIT GitHub repository of Pricing Table

3938 Tracker

by Allen1212
303 stars

3938 Tracker visualizes historical buying and selling activity from ARK Invest funds directly on the main price chart. It plots labels at specific timestamps corresponding to trade dates, indicating buy or sell actions along with the count of participating ETF

Indicator v4 Screeners & dashboards
Updated 2 years ago · None stated GitHub repository of 3938 Tracker

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers