本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) K'u Hero
//@version=6
// Currency Strength Meter - Closed Bar v1.0
//
// Strength of the 8 major currencies (EUR GBP AUD NZD USD CAD CHF JPY) built from 28 pairs.
// Formula (fully disclosed):
// pairChange = (close[1] - close[1 + N]) / close[1 + N] * 100 (percent change over N CLOSED bars)
// strength(C) = average over the pairs that contain C of
// +pairChange when C is the base currency, -pairChange when C is the quote currency
// Pairs with no data are skipped (the table shows how many of the 28 pairs were used).
// Only closed bars are used (request.security with [1] and lookahead_on), so values on past bars do not change.
// Display only. This is not a buy/sell signal.
indicator("Currency Strength Meter - Closed Bar", shorttitle = "CSM Closed Bar", overlay = false, precision = 3, max_labels_count = 20)
// ---------- Inputs ----------
grpD = "Data"
srcPrefix = input.string("OANDA", "Data source (exchange prefix)", options = ["OANDA", "FX", "FX_IDC", "FOREXCOM", "SAXO", "PEPPERSTONE"], group = grpD,
tooltip = "Exchange used for the 28 pairs. OANDA has all 28 pairs. Missing pairs are skipped.")
lenN = input.int(20, "Lookback (closed bars)", minval = 1, maxval = 500, group = grpD)
grpV = "Display"
showNames = input.bool(true, "Currency names at the right end of the lines", group = grpV, display = display.none)
showTable = input.bool(true, "Ranking table", group = grpV, display = display.none)
tablePos = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpV, display = display.none)
lineW = input.int(2, "Line width", minval = 1, maxval = 4, group = grpV, display = display.none)
grpC = "Colors"
cEUR = input.color(#2962FF, "EUR", group = grpC, inline = "c1", display = display.none)
cGBP = input.color(#E91E63, "GBP", group = grpC, inline = "c1", display = display.none)
cAUD = input.color(#FF9800, "AUD", group = grpC, inline = "c1", display = display.none)
cNZD = input.color(#00BCD4, "NZD", group = grpC, inline = "c1", display = display.none)
cUSD = input.color(#4CAF50, "USD", group = grpC, inline = "c2", display = display.none)
cCAD = input.color(#F44336, "CAD", group = grpC, inline = "c2", display = display.none)
cCHF = input.color(#9C27B0, "CHF", group = grpC, inline = "c2", display = display.none)
cJPY = input.color(#795548, "JPY", group = grpC, inline = "c2", display = display.none)
// ---------- Data ----------
// Percent change over N bars, shifted by one bar so that only closed bars are used.
f_chg(int n) =>
float c1 = close[1]
float cn = close[1 + n]
na(c1) or na(cn) or cn <= 0 ? na : (c1 - cn) / cn * 100.0
f_pair(string pair) =>
request.security(srcPrefix + ":" + pair, timeframe.period, f_chg(lenN), lookahead = barmerge.lookahead_on, ignore_invalid_symbol = true)
// 28 calls (the request.* limit is 40)
p_EURUSD = f_pair("EURUSD")
p_GBPUSD = f_pair("GBPUSD")
p_AUDUSD = f_pair("AUDUSD")
p_NZDUSD = f_pair("NZDUSD")
p_USDCAD = f_pair("USDCAD")
p_USDCHF = f_pair("USDCHF")
p_USDJPY = f_pair("USDJPY")
p_EURGBP = f_pair("EURGBP")
p_EURAUD = f_pair("EURAUD")
p_EURNZD = f_pair("EURNZD")
p_EURCAD = f_pair("EURCAD")
p_EURCHF = f_pair("EURCHF")
p_EURJPY = f_pair("EURJPY")
p_GBPAUD = f_pair("GBPAUD")
p_GBPNZD = f_pair("GBPNZD")
p_GBPCAD = f_pair("GBPCAD")
p_GBPCHF = f_pair("GBPCHF")
p_GBPJPY = f_pair("GBPJPY")
p_AUDNZD = f_pair("AUDNZD")
p_AUDCAD = f_pair("AUDCAD")
p_AUDCHF = f_pair("AUDCHF")
p_AUDJPY = f_pair("AUDJPY")
p_NZDCAD = f_pair("NZDCAD")
p_NZDCHF = f_pair("NZDCHF")
p_NZDJPY = f_pair("NZDJPY")
p_CADCHF = f_pair("CADCHF")
p_CADJPY = f_pair("CADJPY")
p_CHFJPY = f_pair("CHFJPY")
// ---------- Strength ----------
// index: 0 EUR, 1 GBP, 2 AUD, 3 NZD, 4 USD, 5 CAD, 6 CHF, 7 JPY
var string[] NAMES = array.from("EUR", "GBP", "AUD", "NZD", "USD", "CAD", "CHF", "JPY")
float[] sums = array.new_float(8, 0.0)
int[] cnts = array.new_int(8, 0)
var int used = 0
used := 0
f_add(int b, int q, float v) =>
int ok = 0
if not na(v)
array.set(sums, b, array.get(sums, b) + v)
array.set(cnts, b, array.get(cnts, b) + 1)
array.set(sums, q, array.get(sums, q) - v)
array.set(cnts, q, array.get(cnts, q) + 1)
ok := 1
ok
used += f_add(0, 4, p_EURUSD)
used += f_add(1, 4, p_GBPUSD)
used += f_add(2, 4, p_AUDUSD)
used += f_add(3, 4, p_NZDUSD)
used += f_add(4, 5, p_USDCAD)
used += f_add(4, 6, p_USDCHF)
used += f_add(4, 7, p_USDJPY)
used += f_add(0, 1, p_EURGBP)
used += f_add(0, 2, p_EURAUD)
used += f_add(0, 3, p_EURNZD)
used += f_add(0, 5, p_EURCAD)
used += f_add(0, 6, p_EURCHF)
used += f_add(0, 7, p_EURJPY)
used += f_add(1, 2, p_GBPAUD)
used += f_add(1, 3, p_GBPNZD)
used += f_add(1, 5, p_GBPCAD)
used += f_add(1, 6, p_GBPCHF)
used += f_add(1, 7, p_GBPJPY)
used += f_add(2, 3, p_AUDNZD)
used += f_add(2, 5, p_AUDCAD)
used += f_add(2, 6, p_AUDCHF)
used += f_add(2, 7, p_AUDJPY)
used += f_add(3, 5, p_NZDCAD)
used += f_add(3, 6, p_NZDCHF)
used += f_add(3, 7, p_NZDJPY)
used += f_add(5, 6, p_CADCHF)
used += f_add(5, 7, p_CADJPY)
used += f_add(6, 7, p_CHFJPY)
f_str(int i) =>
int c = array.get(cnts, i)
c > 0 ? array.get(sums, i) / c : na
sEUR = f_str(0)
sGBP = f_str(1)
sAUD = f_str(2)
sNZD = f_str(3)
sUSD = f_str(4)
sCAD = f_str(5)
sCHF = f_str(6)
sJPY = f_str(7)
hline(0, "Zero", color = color.new(color.gray, 50), linestyle = hline.style_dashed)
plot(sEUR, "EUR", cEUR, lineW)
plot(sGBP, "GBP", cGBP, lineW)
plot(sAUD, "AUD", cAUD, lineW)
plot(sNZD, "NZD", cNZD, lineW)
plot(sUSD, "USD", cUSD, lineW)
plot(sCAD, "CAD", cCAD, lineW)
plot(sCHF, "CHF", cCHF, lineW)
plot(sJPY, "JPY", cJPY, lineW)
// ---------- Names at the line end ----------
float[] vals = array.from(sEUR, sGBP, sAUD, sNZD, sUSD, sCAD, sCHF, sJPY)
color[] cols = array.from(cEUR, cGBP, cAUD, cNZD, cUSD, cCAD, cCHF, cJPY)
var label[] nameLbl = array.new<label>()
if barstate.islast
for l in nameLbl
label.delete(l)
array.clear(nameLbl)
if showNames
for i = 0 to 7
float v = array.get(vals, i)
if not na(v)
array.push(nameLbl, label.new(bar_index, v, array.get(NAMES, i), style = label.style_label_left, color = color.new(color.black, 100),
textcolor = array.get(cols, i), size = size.small))
// ---------- Ranking table ----------
f_pos(string s) =>
s == "Top left" ? position.top_left : s == "Bottom right" ? position.bottom_right : s == "Bottom left" ? position.bottom_left : position.top_right
var table tbl = table.new(f_pos(tablePos), 3, 11, bgcolor = color.new(#131722, 15), border_width = 1, border_color = color.new(color.gray, 70))
if barstate.islast and showTable
color tx = color.white
table.cell(tbl, 0, 0, "#", text_color = tx, text_size = size.small)
table.cell(tbl, 1, 0, "Currency", text_color = tx, text_size = size.small)
table.cell(tbl, 2, 0, "Strength %", text_color = tx, text_size = size.small)
float[] key = array.new_float(8, 0.0)
for i = 0 to 7
float v = array.get(vals, i)
array.set(key, i, na(v) ? -1e9 : v)
int[] idx = array.sort_indices(key, order.descending)
for r = 0 to 7
int i = array.get(idx, r)
float v = array.get(vals, i)
table.cell(tbl, 0, r + 1, str.tostring(r + 1), text_color = tx, text_size = size.small)
table.cell(tbl, 1, r + 1, array.get(NAMES, i), text_color = array.get(cols, i), text_size = size.small)
table.cell(tbl, 2, r + 1, na(v) ? "n/a" : str.tostring(v, "0.000"), text_color = tx, text_size = size.small, text_halign = text.align_right)
table.cell(tbl, 0, 9, "Pairs used " + str.tostring(used) + "/28", text_color = used < 28 ? color.orange : color.gray, text_size = size.tiny)
table.merge_cells(tbl, 0, 9, 2, 9)
table.cell(tbl, 0, 10, "N=" + str.tostring(lenN) + " closed bars, " + srcPrefix, text_color = color.gray, text_size = size.tiny)
table.merge_cells(tbl, 0, 10, 2, 10)