SOFR 1M Futures Implied Rates

mityagusev · study · 128 行 · 点赞 0 · TradingView 原页

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

Pine Script

//@version=6
indicator("SOFR 1M Futures – Implied Rates", shorttitle = "SR1 Implied", overlay = true)
 
// ═══════════════════════════════ Inputs ═══════════════════════════════
// display = display.none keeps the input values out of the chart's status line.
string EXCH     = input.string("CME", "Exchange", group = "Contracts", display = display.none)
string ROOT     = input.string("SR1", "Root", group = "Contracts", display = display.none,
     tooltip = "CME One-Month SOFR = SR1. Tickers are built as EXCHANGE:ROOT + month code + year, e.g. CME:SR1Z2026.")
int    N_MONTHS = input.int(36, "Months to scan", minval = 1, maxval = 36, group = "Contracts", display = display.none,
     tooltip = "Scans forward from the current month. Months with no listed contract are skipped automatically. Each month uses one request (Pine allows 40 per script).")
int    SKIP     = input.int(0, "Skip first N months", minval = 0, maxval = 12, group = "Contracts", display = display.none)
string PRICE_TF = input.timeframe("D", "Price timeframe", group = "Contracts", display = display.none)
 
bool   SHOW_REF = input.bool(true, "Compare to spot", inline = "ref", group = "Reference", display = display.none)
string REF_SYM  = input.symbol("FRED:SOFR", "", inline = "ref", group = "Reference", display = display.none)
 
string TBL_POS  = input.string(position.top_right, "Table position", group = "Display", display = display.none,
     options = [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right])
string TXT_SIZE = input.string(size.small, "Text size", options = [size.tiny, size.small, size.normal, size.large], group = "Display", display = display.none)
 
// ═══════════════════════════════ Constants ═══════════════════════════════
const string TZ = "America/Chicago"
var array<string> MONTH_CODES = array.from("F", "G", "H", "J", "K", "M", "N", "Q", "U", "V", "X", "Z")
var array<string> MONTH_NAMES = array.from("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
color UP_COL    = #ef5350
color DN_COL    = #26a69a
color STALE_COL = color.orange
color DIM_COL   = color.new(chart.fg_color, 45)
 
// Anchor the scan to the month the script was loaded in. It is fixed for the whole run so the
// set of requested tickers is identical on historical and realtime bars (a Pine requirement).
var int anchorY = year(timenow, TZ)
var int anchorM = month(timenow, TZ) - 1   // 0 = Jan
 
// ═══════════════════════════════ Requests ═══════════════════════════════
array<string> rowMonth  = array.new<string>()
array<string> rowTicker = array.new<string>()
array<float>  rowPrice  = array.new<float>()
array<int>    rowAsOf   = array.new<int>()
 
for i = SKIP to SKIP + N_MONTHS - 1
    int    k      = anchorM + i
    int    mIdx   = k % 12
    int    yr     = anchorY + math.floor(k / 12)
    string ticker = ROOT + MONTH_CODES.get(mIdx) + str.tostring(yr)
    // Dynamic request (Pine v6): the ticker is built at runtime; unlisted contracts return na.
    [px, tday] = request.security(EXCH + ":" + ticker, PRICE_TF, [close, time_tradingday], ignore_invalid_symbol = true)
    if barstate.islast and not na(px)
        rowMonth.push(MONTH_NAMES.get(mIdx) + " " + str.tostring(yr))
        rowTicker.push(ticker)
        rowPrice.push(px)
        rowAsOf.push(tday)
 
[refRate, refDay] = request.security(REF_SYM, "D", [close, time_tradingday], ignore_invalid_symbol = true)
 
// ═══════════════════════════════ Table ═══════════════════════════════
var table tbl = table.new(TBL_POS, 7, N_MONTHS + 3, bgcolor = chart.bg_color,
     frame_color = color.new(chart.fg_color, 70), frame_width = 1,
     border_color = color.new(chart.fg_color, 85), border_width = 1)
 
f_cell(int col, int row, string txt, color txtCol = na, color bg = na, string halign = text.align_right) =>
    table.cell(tbl, col, row, txt, text_color = na(txtCol) ? chart.fg_color : txtCol,
         bgcolor = bg, text_size = TXT_SIZE, text_halign = halign)
 
f_bp(float d) =>
    float r = math.round(d, 2)
    r == 0 ? "0.00" : (r > 0 ? "+" : "") + str.tostring(r, "0.00")
 
f_bpColor(float d) =>
    float r = math.round(d, 2)
    r > 0 ? UP_COL : r < 0 ? DN_COL : DIM_COL
 
f_date(int t) =>
    na(t) ? "—" : str.format_time(t, "yyyy-MM-dd", "UTC")
 
if barstate.islast
    tbl.clear(0, 0, 6, N_MONTHS + 2)
    color hdrBg = color.new(chart.fg_color, 88)
    int   cDate = SHOW_REF ? 6 : 5
 
    f_cell(0, 0, "Month",       bg = hdrBg, halign = text.align_left)
    f_cell(1, 0, "Contract",    bg = hdrBg, halign = text.align_left)
    f_cell(2, 0, "Price",       bg = hdrBg)
    f_cell(3, 0, "Implied",     bg = hdrBg)
    f_cell(4, 0, "Δ prev (bp)", bg = hdrBg)
    if SHOW_REF
        f_cell(5, 0, "Δ spot (bp)", bg = hdrBg)
    f_cell(cDate, 0, "As of", bg = hdrBg)
 
    // Spot reference row sits directly under the header, ahead of the futures.
    int firstRow = 1
    if SHOW_REF
        f_cell(0, 1, "Spot", bg = hdrBg, halign = text.align_left)
        f_cell(1, 1, REF_SYM, txtCol = DIM_COL, bg = hdrBg, halign = text.align_left)
        f_cell(2, 1, "", bg = hdrBg)
        f_cell(3, 1, na(refRate) ? "n/a" : str.tostring(refRate, "0.00") + "%", bg = hdrBg)
        f_cell(4, 1, "", bg = hdrBg)
        f_cell(5, 1, "", bg = hdrBg)
        f_cell(6, 1, na(refRate) ? "" : f_date(refDay), txtCol = DIM_COL, bg = hdrBg)
        firstRow := 2
 
    int n = rowTicker.size()
    if n == 0
        f_cell(0, firstRow, "No " + EXCH + ":" + ROOT + " contracts found", txtCol = DIM_COL, halign = text.align_left)
    else
        int latestDay = rowAsOf.max()
        for j = 0 to n - 1
            int   r    = j + firstRow
            float p    = rowPrice.get(j)
            float rate = 100 - p
            f_cell(0, r, rowMonth.get(j), halign = text.align_left)
            f_cell(1, r, rowTicker.get(j), txtCol = DIM_COL, halign = text.align_left)
            f_cell(2, r, str.tostring(p, "0.0000"))
            f_cell(3, r, str.tostring(rate, "0.0000") + "%")
            if j > 0
                float dPrev = (rate - (100 - rowPrice.get(j - 1))) * 100
                f_cell(4, r, f_bp(dPrev), txtCol = f_bpColor(dPrev))
            else
                f_cell(4, r, "—", txtCol = DIM_COL)
            if SHOW_REF
                if na(refRate)
                    f_cell(5, r, "—", txtCol = DIM_COL)
                else
                    float dSpot = (rate - refRate) * 100
                    f_cell(5, r, f_bp(dSpot), txtCol = f_bpColor(dSpot))
            int t = rowAsOf.get(j)
            // Orange date = this contract's last price is older than the freshest contract's.
            f_cell(cDate, r, f_date(t), txtCol = t < latestDay ? STALE_COL : DIM_COL)

← 返回列表