Synthetic USD Strength Line

balamnnh99 · study · 75 行 · 点赞 2 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ MPL-2.0
//@version=6
indicator("Synthetic USD Strength Line", shorttitle = "USD Line", overlay = false)
 
// --- Inputs ---
string eurusdSymbol = input.symbol("EURUSD", "EURUSD", tooltip = "USD-quote pair; its move is inverted in the USD-strength basket.")
string gbpusdSymbol = input.symbol("GBPUSD", "GBPUSD", tooltip = "USD-quote pair; its move is inverted in the USD-strength basket.")
string audusdSymbol = input.symbol("AUDUSD", "AUDUSD", tooltip = "USD-quote pair; its move is inverted in the USD-strength basket.")
string nzdusdSymbol = input.symbol("NZDUSD", "NZDUSD", tooltip = "USD-quote pair; its move is inverted in the USD-strength basket.")
string usdjpySymbol = input.symbol("USDJPY", "USDJPY", tooltip = "USD-base pair; its move contributes directly to USD strength.")
string usdchfSymbol = input.symbol("USDCHF", "USDCHF", tooltip = "USD-base pair; its move contributes directly to USD strength.")
string usdcadSymbol = input.symbol("USDCAD", "USDCAD", tooltip = "USD-base pair; its move contributes directly to USD strength.")
string usdsekSymbol = input.symbol("USDSEK", "USDSEK", tooltip = "USD-base pair; its move contributes directly to USD strength.")
int lookbackInput = input.int(20, "Strength lookback bars", minval = 1, tooltip = "Bars used to classify the synthetic USD basket as strong, weak, or neutral.")
float neutralBandInput = input.float(0.05, "Neutral band (%)", minval = 0.0, step = 0.01, tooltip = "Changes inside this percentage band are classified as neutral.")
color bullColorInput = input.color(#089981, "USD strengthening candle", group = "Style", tooltip = "Candle color when synthetic USD strength closes above its open.")
color bearColorInput = input.color(#f23645, "USD weakening candle", group = "Style", tooltip = "Candle color when synthetic USD strength closes below its open.")
color neutralColorInput = input.color(#5b9cf6, "Reference / neutral", group = "Style", tooltip = "Color for the 100 reference and neutral strength readings.")
 
// --- FX basket OHLC ---
[eurOpen, eurHigh, eurLow, eurClose] = request.security(eurusdSymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
[gbpOpen, gbpHigh, gbpLow, gbpClose] = request.security(gbpusdSymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
[audOpen, audHigh, audLow, audClose] = request.security(audusdSymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
[nzdOpen, nzdHigh, nzdLow, nzdClose] = request.security(nzdusdSymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
[jpyOpen, jpyHigh, jpyLow, jpyClose] = request.security(usdjpySymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
[chfOpen, chfHigh, chfLow, chfClose] = request.security(usdchfSymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
[cadOpen, cadHigh, cadLow, cadClose] = request.security(usdcadSymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
[sekOpen, sekHigh, sekLow, sekClose] = request.security(usdsekSymbol, timeframe.period, [open, high, low, close], ignore_invalid_symbol = true)
 
bool basketReady = not na(eurClose) and not na(gbpClose) and not na(audClose) and not na(nzdClose) and not na(jpyClose) and not na(chfClose) and not na(cadClose) and not na(sekClose)
bool priorReady = not na(eurClose[1]) and not na(gbpClose[1]) and not na(audClose[1]) and not na(nzdClose[1]) and not na(jpyClose[1]) and not na(chfClose[1]) and not na(cadClose[1]) and not na(sekClose[1])
 
// --- Synthetic USD-strength candles ---
var float syntheticClose = na
float syntheticOpen = na
float syntheticHigh = na
float syntheticLow = na
float priorBasketClose = na(syntheticClose[1]) ? 100.0 : syntheticClose[1]
 
if basketReady and priorReady
    float openLogMove = math.log(jpyOpen / jpyClose[1]) + math.log(chfOpen / chfClose[1]) + math.log(cadOpen / cadClose[1]) + math.log(sekOpen / sekClose[1]) - math.log(eurOpen / eurClose[1]) - math.log(gbpOpen / gbpClose[1]) - math.log(audOpen / audClose[1]) - math.log(nzdOpen / nzdClose[1])
    float highLogMove = math.log(jpyHigh / jpyClose[1]) + math.log(chfHigh / chfClose[1]) + math.log(cadHigh / cadClose[1]) + math.log(sekHigh / sekClose[1]) - math.log(eurLow / eurClose[1]) - math.log(gbpLow / gbpClose[1]) - math.log(audLow / audClose[1]) - math.log(nzdLow / nzdClose[1])
    float lowLogMove = math.log(jpyLow / jpyClose[1]) + math.log(chfLow / chfClose[1]) + math.log(cadLow / cadClose[1]) + math.log(sekLow / sekClose[1]) - math.log(eurHigh / eurClose[1]) - math.log(gbpHigh / gbpClose[1]) - math.log(audHigh / audClose[1]) - math.log(nzdHigh / nzdClose[1])
    float closeLogMove = math.log(jpyClose / jpyClose[1]) + math.log(chfClose / chfClose[1]) + math.log(cadClose / cadClose[1]) + math.log(sekClose / sekClose[1]) - math.log(eurClose / eurClose[1]) - math.log(gbpClose / gbpClose[1]) - math.log(audClose / audClose[1]) - math.log(nzdClose / nzdClose[1])
    syntheticOpen := priorBasketClose * math.exp(openLogMove / 8.0)
    syntheticHigh := priorBasketClose * math.exp(highLogMove / 8.0)
    syntheticLow := priorBasketClose * math.exp(lowLogMove / 8.0)
    syntheticClose := priorBasketClose * math.exp(closeLogMove / 8.0)
else if basketReady
    syntheticOpen := 100.0
    syntheticHigh := 100.0
    syntheticLow := 100.0
    syntheticClose := 100.0
else
    syntheticClose := na
 
float changePct = not na(syntheticClose[lookbackInput]) and syntheticClose[lookbackInput] != 0.0 ? (syntheticClose / syntheticClose[lookbackInput] - 1.0) * 100.0 : na
bool isStrong = not na(changePct) and changePct > neutralBandInput
bool isWeak = not na(changePct) and changePct < -neutralBandInput
color stateColor = isStrong ? bullColorInput : isWeak ? bearColorInput : neutralColorInput
string stateText = na(changePct) ? "Waiting for basket data" : isStrong ? "USD STRONG" : isWeak ? "USD WEAK" : "USD NEUTRAL"
color candleColor = syntheticClose >= syntheticOpen ? bullColorInput : bearColorInput
 
// --- Visuals ---
plot(syntheticClose, title = "Synthetic USD Strength", color = stateColor, linewidth = 2)
hline(100.0, "Starting reference", color = color.new(neutralColorInput, 65), linestyle = hline.style_dotted)
 
var table statusTable = table.new(position.top_right, 2, 3, bgcolor = #161616, frame_color = #2E2E2E, frame_width = 1, border_color = #2E2E2E, border_width = 1)
if barstate.islast
    table.cell(statusTable, 0, 0, "USD BASKET", text_color = #808080, bgcolor = #161616)
    table.cell(statusTable, 1, 0, "8-PAIR LINE", text_color = #DBDBDB, bgcolor = #161616)
    table.cell(statusTable, 0, 1, "Strength", text_color = #DBDBDB, bgcolor = #161616)
    table.cell(statusTable, 1, 1, stateText, text_color = stateColor, bgcolor = #161616)
    table.cell(statusTable, 0, 2, "Change", text_color = #DBDBDB, bgcolor = #161616)
    table.cell(statusTable, 1, 2, na(changePct) ? "n/a" : (changePct > 0 ? "+" : changePct < 0 ? "-" : "") + str.tostring(math.abs(changePct), "#.##") + "%", text_color = stateColor, bgcolor = #161616)

← 返回列表