Institutional ES VWAP Projected to SPX (Fair-Value Spread Lock)

wyndmillwillie · study · 76 行 · 点赞 14 · 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/
// © wyndmillwillie
 
//@version=6
indicator("ES VWAP Projected to SPX v4", overlay=true)
 
// --- Settings UI ---
es_sym = input.symbol("CME_MINI:ES1!", "Futures Contract", group="Data Sources")
spx_sym = input.symbol("SP:SPX", "Cash Index", group="Data Sources")
note = input.bool(true, title="ℹ️ Read Spread Mechanics Note", tooltip="LIVE SPREAD: During cash hours (9:30 AM - 4:00 PM EST), the ES/SPX spread calculates tick-for-tick.\n\nLOCKED SPREAD: During the Globex overnight session, SPX cash data stops. The script locks the final closing spread and applies it to live ES VWAP to project a synthetic SPX line on 24-hour CFD/Futures charts.", group="Data Sources")
 
// HUD Settings
show_panel = input.bool(true, "Show Data HUD", group="HUD Panel Settings")
pos_in = input.string("Bottom Right", title="HUD Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Bottom Center", "Middle Right", "Middle Left"], group="HUD Panel Settings")
t_pos = pos_in == "Top Right" ? position.top_right : pos_in == "Top Left" ? position.top_left : pos_in == "Bottom Right" ? position.bottom_right : pos_in == "Bottom Left" ? position.bottom_left : pos_in == "Bottom Center" ? position.bottom_center : pos_in == "Middle Right" ? position.middle_right : position.middle_left
 
// Label Settings
show_lbl = input.bool(true, "Show Line Label", group="Line Label Settings")
lbl_size_in = input.string("Small", title="Label Text Size", options=["Tiny", "Small", "Normal", "Large"], group="Line Label Settings")
l_size = lbl_size_in == "Tiny" ? size.tiny : lbl_size_in == "Small" ? size.small : lbl_size_in == "Normal" ? size.normal : size.large
 
lbl_bg_color = input.color(color.new(color.black, 100), title="Label Background Color", group="Line Label Settings")
lbl_txt_color = input.color(color.aqua, title="Label Text Color", group="Line Label Settings")
lbl_style_in = input.string("Point Left (Right of Line)", title="Label Shape / Position", options=["Point Left (Right of Line)", "Point Right (Left of Line)", "Center", "Text Only (No Box)"], group="Line Label Settings")
 
l_style = lbl_style_in == "Point Left (Right of Line)" ? label.style_label_left : lbl_style_in == "Point Right (Left of Line)" ? label.style_label_right : lbl_style_in == "Center" ? label.style_label_center : label.style_none
 
// --- Chart Guard / Fail-Safe ---
t = syminfo.ticker
is_valid_chart = str.contains(t, "SPX") or str.contains(t, "ES") or str.contains(t, "US500") or str.contains(t, "SPY") or str.contains(t, "INX") or str.contains(t, "MES")
 
// --- Data Fetching ---
d_es = request.security(es_sym, "D", close[1], barmerge.gaps_off, barmerge.lookahead_on)
d_spx = request.security(spx_sym, "D", close[1], barmerge.gaps_off, barmerge.lookahead_on)
es_c = request.security(es_sym, timeframe.period, close)
spx_c = request.security(spx_sym, timeframe.period, close)
es_vwap = request.security(es_sym, timeframe.period, ta.vwap)
 
// Session Detection (9:30 AM - 4:00 PM EST)
is_rth = not na(time(timeframe.period, "0930-1600", "America/New_York"))
 
// --- Dynamic Spread Logic ---
var float locked_spread = na
live_spread = es_c - spx_c
fallback_spread = d_es - d_spx
 
if is_rth
    locked_spread := live_spread
 
final_spread = is_rth ? live_spread : (not na(locked_spread) ? locked_spread : fallback_spread)
spx_vwap = es_vwap - final_spread
 
if not is_valid_chart
    spx_vwap := na 
 
// --- Plotting ---
plot(spx_vwap, color=color.aqua, linewidth=2, title="Projected SPX VWAP")
 
// Draw the Floating Label dynamically so settings apply cleanly
var label v_lbl = label.new(na, na, "ES VWAP CONV")
if barstate.islast and show_lbl and is_valid_chart
    label.set_xy(v_lbl, bar_index, spx_vwap)
    label.set_size(v_lbl, l_size)
    label.set_color(v_lbl, lbl_bg_color)
    label.set_textcolor(v_lbl, lbl_txt_color)
    label.set_style(v_lbl, l_style)
else if barstate.islast and (not show_lbl or not is_valid_chart)
    label.set_xy(v_lbl, na, na) 
 
// --- HUD Table Logic ---
var table hud = table.new(t_pos, 2, 5, bgcolor=color.new(color.black, 15), border_color=color.new(color.gray, 50), border_width=1)
if show_panel and barstate.islast
    if is_valid_chart
        table.cell(hud, 0, 0, "Proj SPX VWAP", text_color=color.white, text_size=size.normal, text_halign=text.align_left, bgcolor=color.new(color.navy, 40)), table.cell(hud, 1, 0, str.tostring(spx_vwap, "#.##"), text_color=color.aqua, text_size=size.normal, text_halign=text.align_right, bgcolor=color.new(color.navy, 40)), table.cell(hud, 0, 1, "Raw ES VWAP", text_color=color.gray, text_size=size.small, text_halign=text.align_left), table.cell(hud, 1, 1, str.tostring(es_vwap, "#.##"), text_color=color.silver, text_size=size.small, text_halign=text.align_right), table.cell(hud, 0, 2, is_rth ? "Live Spread" : "Locked Spread", text_color=color.gray, text_size=size.small, text_halign=text.align_left), table.cell(hud, 1, 2, str.tostring(final_spread, "#.##"), text_color=color.silver, text_size=size.small, text_halign=text.align_right), table.cell(hud, 0, 3, " ", text_size=size.small), table.cell(hud, 1, 3, " ", text_size=size.small)
    else
        table.cell(hud, 0, 0, "⚠️ INVALID CHART", text_color=color.white, text_size=size.normal, text_halign=text.align_left, bgcolor=color.new(color.red, 40)), table.cell(hud, 1, 0, "HIDE LINE", text_color=color.white, text_size=size.normal, text_halign=text.align_right, bgcolor=color.new(color.red, 40)), table.cell(hud, 0, 1, "Must use SPX,", text_color=color.gray, text_size=size.small, text_halign=text.align_left), table.cell(hud, 1, 1, "ES, or US500", text_color=color.gray, text_size=size.small, text_halign=text.align_right)

← 返回列表