本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("5 Currency Strength (USDJPY Focus)", shorttitle="5CS", overlay=false, max_labels_count=50)
// =====================================================
// 設定 [Settings]
// =====================================================
grpC = "計算 [Calculation]"
modeStr = input.string("直近N本 [Last N bars]", "基準点 [Base point]", options=["直近N本 [Last N bars]", "セッション開始 [Session start]"], group=grpC)
period = input.int(14, "強弱計算期間 [Lookback period]", minval=1, group=grpC)
smLen = input.int(3, "滑らかさ [Smoothing]", minval=1, group=grpC)
useChartSrc = input.bool(true, "チャートと同じ提供元を使う [Use chart's data source]", tooltip="オンのとき、チャートの提供元(例:OANDA)で10ペアを取得します。FXのチャート以外では、オフにして下の欄に入力してください [When on, uses the chart's source. Turn off on non-FX charts]", group=grpC)
prefixIn = input.string("OANDA", "データ提供元(手入力) [Data source, manual]", tooltip="上のチェックがオフのときに使います。例 / e.g. FX, OANDA, FOREXCOM [Used when the box above is off]", group=grpC)
useNorm = input.bool(false, "正規化 [Normalize]", tooltip="各ペアの変化を、そのペアの普段の値動きの大きさで割ります。時間足が変わっても値の大きさがそろいます [Divides each pair's change by its typical volatility]", group=grpC)
normLen = input.int(100, "正規化の期間 [Normalize length]", minval=10, group=grpC)
grpS = "セッション [Session]"
sess = input.session("0900-1500", "セッション時間 [Session time]", tooltip="基準点が「セッション開始」のとき、この開始時刻を0にします。日中の足(日足未満)でのみ使えます [Resets to 0 at session start. Intraday charts only]。例 / e.g. 東京 0900-1500 Asia/Tokyo、ロンドン 0800-1600 Europe/London、NY 0800-1700 America/New_York", group=grpS)
tz = input.string("Asia/Tokyo", "タイムゾーン [Time zone]", options=["Asia/Tokyo", "Europe/London", "America/New_York"], group=grpS)
grpD = "表示 [Display]"
showDiff = input.bool(true, "USD-JPY 強弱差を表示 [Show USD-JPY spread]", group=grpD)
labelShift = input.int(10, "ラベルの右オフセット [Label offset]", minval=1, maxval=400, group=grpD)
labelSpace = input.float(8.0, "ラベルの間隔(小さいほど広い) [Label spacing, smaller = wider]", minval=2.0, maxval=30.0, step=1.0, group=grpD)
modeN = modeStr == "直近N本 [Last N bars]"
prefix = useChartSrc ? syminfo.prefix : prefixIn
// 通貨ごとの色 [Colors]
cUSD = color.red
cEUR = color.blue
cGBP = color.orange
cAUD = color.green
cJPY = color.gray
cDifUp = color.new(color.red, 70)
cDifDn = color.new(color.gray, 60)
// =====================================================
// 価格の取得(平均足でも通常の価格) [Standard prices]
// =====================================================
px(string pair) =>
request.security(ticker.standard(prefix + ":" + pair), timeframe.period, close)
pEURUSD = px("EURUSD")
pGBPUSD = px("GBPUSD")
pAUDUSD = px("AUDUSD")
pUSDJPY = px("USDJPY")
pEURGBP = px("EURGBP")
pEURAUD = px("EURAUD")
pEURJPY = px("EURJPY")
pGBPAUD = px("GBPAUD")
pGBPJPY = px("GBPJPY")
pAUDJPY = px("AUDJPY")
// =====================================================
// セッション開始の判定と経過本数 [Session start / bars since]
// =====================================================
inSess = not na(time(timeframe.period, sess, tz))
isStart = inSess and not inSess[1]
var int nb = 0
if isStart
nb := 0
else
nb += 1
// =====================================================
// ペアごとの変化率(対数、%)と正規化 [Log change % / normalize]
// =====================================================
chg(float p) =>
var float b = na
if isStart
b := p
float r1 = 100 * math.log(p / p[1])
float sd = ta.stdev(r1, normLen)
float r = modeN ? 100 * math.log(p / p[period]) : 100 * math.log(p / b)
float k = modeN ? period : math.max(nb, 1)
float out = r
if useNorm
out := sd > 0 ? r / (sd * math.sqrt(k)) : na
out
rEURUSD = chg(pEURUSD)
rGBPUSD = chg(pGBPUSD)
rAUDUSD = chg(pAUDUSD)
rUSDJPY = chg(pUSDJPY)
rEURGBP = chg(pEURGBP)
rEURAUD = chg(pEURAUD)
rEURJPY = chg(pEURJPY)
rGBPAUD = chg(pGBPAUD)
rGBPJPY = chg(pGBPJPY)
rAUDJPY = chg(pAUDJPY)
// =====================================================
// 通貨ごとの強弱(10ペアの平均) [Strength from 10 pairs]
// =====================================================
rawUSD = (-rEURUSD - rGBPUSD - rAUDUSD + rUSDJPY) / 4
rawEUR = ( rEURUSD + rEURGBP + rEURAUD + rEURJPY) / 4
rawGBP = ( rGBPUSD - rEURGBP + rGBPAUD + rGBPJPY) / 4
rawAUD = ( rAUDUSD - rEURAUD - rGBPAUD + rAUDJPY) / 4
rawJPY = (-rUSDJPY - rEURJPY - rGBPJPY - rAUDJPY) / 4
// セッション開始では滑らかさもリセット(開始の足で0から) [Smoothing resets at session start]
effLen = modeN ? smLen : math.min(smLen, nb + 1)
sUSD = ta.sma(rawUSD, effLen)
sEUR = ta.sma(rawEUR, effLen)
sGBP = ta.sma(rawGBP, effLen)
sAUD = ta.sma(rawAUD, effLen)
sJPY = ta.sma(rawJPY, effLen)
sDIF = sUSD - sJPY
// =====================================================
// プロット [Plots]
// =====================================================
hline(0, "基準線 [Baseline]", color=#787B86, linestyle=hline.style_dotted, linewidth=1)
plot(showDiff ? sDIF : na, "USD-JPY", color=sDIF >= 0 ? cDifUp : cDifDn, style=plot.style_columns)
plot(sUSD, "USD", color=cUSD, linewidth=2)
plot(sEUR, "EUR", color=cEUR, linewidth=2)
plot(sGBP, "GBP", color=cGBP, linewidth=2)
plot(sAUD, "AUD", color=cAUD, linewidth=2)
plot(sJPY, "JPY", color=cJPY, linewidth=3)
// =====================================================
// 日足以上で「セッション開始」を選んだときの案内 [Intraday-only notice]
// =====================================================
var table note = table.new(position.bottom_right, 1, 2)
if barstate.islast
table.cell(note, 0, 0, "提供元 [Source]: " + prefix, text_color=color.gray, bgcolor=color.new(color.gray, 90), text_size=size.small)
if not modeN and timeframe.isdwm
table.cell(note, 0, 1, "セッション開始は日中の足でのみ使えます [Session start: intraday only]", text_color=color.white, bgcolor=color.new(color.gray, 20), text_size=size.small)
else
table.clear(note, 0, 1)
// =====================================================
// ラベル(強い順・重なり防止) [Labels]
// =====================================================
maxV = math.max(math.max(math.max(sUSD, sEUR), math.max(sGBP, sAUD)), sJPY)
minV = math.min(math.min(math.min(sUSD, sEUR), math.min(sGBP, sAUD)), sJPY)
topR = ta.highest(maxV, 100)
botR = ta.lowest(minV, 100)
minGap = (topR - botR) / labelSpace
var label[] lbls = array.new_label(5)
if barstate.islast
for l in lbls
label.delete(l)
allOk = not na(sUSD) and not na(sEUR) and not na(sGBP) and not na(sAUD) and not na(sJPY) and not na(minGap)
if allOk
float[] vArr = array.from(sUSD, sEUR, sGBP, sAUD, sJPY)
string[] nArr = array.from("USD", "EUR", "GBP", "AUD", "JPY")
color[] cArr = array.from(cUSD, cEUR, cGBP, cAUD, cJPY)
int[] ids = array.sort_indices(vArr, order.descending)
float lastY = 1e10
for i = 0 to 4
int idx = array.get(ids, i)
float y = array.get(vArr, idx)
if lastY - y < minGap
y := lastY - minGap
array.set(lbls, i, label.new(bar_index + labelShift, y, array.get(nArr, idx), color=array.get(cArr, idx), textcolor=color.white, style=label.style_label_left, size=size.small))
lastY := y