Nasdaq Up Volume vs Down Volume

NoNobilityInPoverty · study · 138 行 · 点赞 4 · 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/
// © NoNobilityInPoverty
 
//@version=6
indicator("Nasdaq Up Volume vs Down Volume", "NQ UVol-DVol", precision = 2)
 
// --- Constants ---
color COLOR_U_MAX = #00ffd5
color COLOR_U_MIN = #004d40 
color COLOR_D_MAX = #ff1200
color COLOR_D_MIN = #4a0800 
color COLOR_BG    = #1e222d
 
// --- Inputs ---
string GP_DATA    = "Data Settings"
string GP_VISUALS = "Chart Visuals"
string GP_DASH    = "KPI Dashboard"
 
bool   use_extended       = input.bool(false, "Include Pre/Post Market Data", group = GP_DATA)
int    trend_length       = input.int(10, "Breadth Trend Length", minval = 1, group = GP_DATA)
 
string cumrel             = input.string("Cumulative", "Mode", options = ["Cumulative", "Relative"], group = GP_VISUALS)
string spl                = input.string("Split", "Layout", options = ["Split", "Stacked"], group = GP_VISUALS)
string color_style        = input.string("Static", "Color Style", options = ["Static", "Gradient"], group = GP_VISUALS)
bool   ninety             = input.bool(true, "Show 90% Threshold", group = GP_VISUALS)
 
bool   show_header        = input.bool(true, "Show KPI Table", group = GP_DASH)
bool   compact_table      = input.bool(false, "Use Compact Table", group = GP_DASH)
string i_textSize         = input.string("normal", "Text Size", options = ["auto", "tiny", "small", "normal", "large", "huge"], group = GP_DASH)
string i_tableYpos        = input.string("bottom", "Position", inline = "pos", options = ["top", "middle", "bottom"], group = GP_DASH)
string i_tableXpos        = input.string("right", "", inline = "pos", options = ["left", "center", "right"], group = GP_DASH)
 
// --- Calculations (Nasdaq only) ---
string session_state = use_extended ? session.extended : session.regular
string sym_up = ticker.modify("UPVOL.NQ", session = session_state)
string sym_dn = ticker.modify("DNVOL.NQ", session = session_state)
 
float upvol   = request.security(sym_up, timeframe.period, close, ignore_invalid_symbol = true)
float downvol = request.security(sym_dn, timeframe.period, close, ignore_invalid_symbol = true)
float totalVol = nz(upvol) + nz(downvol)
 
float uratio = totalVol > 0 ? upvol / totalVol : 0
float dratio = totalVol > 0 ? downvol / totalVol : 0
 
// Cycle Trend Calculation
float uratio_trend = ta.ema(uratio, trend_length)
 
bool is_u80 = uratio > 0.80 and uratio <= 0.90
bool is_d80 = dratio > 0.80 and dratio <= 0.90
bool is_u90 = uratio > 0.90
bool is_d90 = dratio > 0.90
 
// Color Logic
color up_col_grad = color.from_gradient(uratio, 0.50, 0.90, COLOR_U_MIN, COLOR_U_MAX)
color dn_col_grad = color.from_gradient(dratio, 0.50, 0.90, COLOR_D_MIN, COLOR_D_MAX)
 
int transp = spl == "Stacked" ? 0 : 25
color up_col_static = is_u90 ? color.new(COLOR_U_MAX, transp) : color.new(color.teal, transp)
color dn_col_static = is_d90 ? color.new(COLOR_D_MAX, transp) : color.new(color.red, transp)
 
color up_col = color_style == "Gradient" ? up_col_grad : up_col_static
color dn_col = color_style == "Gradient" ? dn_col_grad : dn_col_static
 
// Layout Math
float base_up = cumrel == "Cumulative" ? upvol : uratio
float base_dn = cumrel == "Cumulative" ? downvol : dratio
float base_total = cumrel == "Cumulative" ? totalVol : 1.0
 
float u90 = base_total * 0.9
float d90 = spl == "Split" ? -base_total * 0.9 : na
 
float plot_up   = base_up
float plot_down = spl == "Split" ? -base_dn : base_total
 
// --- Plotting ---
plot(plot_down, "Down Volume", dn_col, style = plot.style_columns)
plot(plot_up, "Up Volume", up_col, style = plot.style_columns)
 
plot(ninety ? u90 : na, "90% Up Guide", color.new(color.gray, 50), style = plot.style_line, linewidth = 2)
plot(ninety and spl == "Split" ? d90 : na, "90% Down Guide", color.new(color.gray, 50), style = plot.style_line, linewidth = 2)
 
plotshape(is_u80, "80% Up Day", shape.triangleup, location.bottom, color.teal, text = "8", textcolor = color.teal, size = size.tiny)
plotshape(is_d80, "80% Down Day", shape.triangledown, location.top, color.red, text = "8", textcolor = color.red, size = size.tiny)
plotshape(is_u90, "90% Up Day", shape.triangleup, location.bottom, COLOR_U_MAX, text = "9", textcolor = COLOR_U_MAX, size = size.tiny)
plotshape(is_d90, "90% Down Day", shape.triangledown, location.top, COLOR_D_MAX, text = "9", textcolor = COLOR_D_MAX, size = size.tiny)
 
plot(uratio * 100, "Up Vol %", display = display.data_window)
plot(dratio * 100, "Down Vol %", display = display.data_window)
 
// --- KPI Dashboard ---
var string table_pos = i_tableYpos + "_" + i_tableXpos
var table dtrDisplay = table.new(table_pos, 2, 4)
 
if barstate.islast and show_header
    table.clear(dtrDisplay, 0, 0, 1, 3)
    
    if compact_table
        color bg_up_comp = color.new(COLOR_U_MAX, 80)
        color bg_dn_comp = color.new(COLOR_D_MAX, 80)
        
        table.set_frame_width(dtrDisplay, 1)
        table.set_frame_color(dtrDisplay, color.black)
        table.set_border_width(dtrDisplay, 1)
        table.set_border_color(dtrDisplay, color.black)
        
        table.cell(dtrDisplay, 0, 0, "Up Volume", bgcolor = bg_up_comp, text_color = color.white, text_size = i_textSize)
        table.cell(dtrDisplay, 0, 1, str.tostring(math.round(uratio * 100, 1)) + "%", bgcolor = bg_up_comp, text_color = color.white, text_font_family = font.family_monospace, text_size = i_textSize)
        
        table.cell(dtrDisplay, 1, 0, "Down Volume", bgcolor = bg_dn_comp, text_color = color.white, text_size = i_textSize)
        table.cell(dtrDisplay, 1, 1, str.tostring(math.round(dratio * 100, 1)) + "%", bgcolor = bg_dn_comp, text_color = color.white, text_font_family = font.family_monospace, text_size = i_textSize)
    else
        table.set_frame_width(dtrDisplay, 2)
        table.set_frame_color(dtrDisplay, COLOR_BG)
        table.set_border_width(dtrDisplay, 1)
        table.set_border_color(dtrDisplay, COLOR_BG)
    
        color bg_up = color.new(COLOR_U_MAX, 80)
        color bg_dn = color.new(COLOR_D_MAX, 80)
        
        color bg_trend = uratio_trend > 0.55 ? color.new(COLOR_U_MAX, 80) : (uratio_trend < 0.45 ? color.new(COLOR_D_MAX, 80) : color.new(color.gray, 80))
        
        table.cell(dtrDisplay, 0, 0, "Metric", bgcolor = COLOR_BG, text_color = color.white, text_size = i_textSize)
        table.cell(dtrDisplay, 1, 0, "Nasdaq", bgcolor = COLOR_BG, text_color = color.white, text_size = i_textSize)
        
        table.cell(dtrDisplay, 0, 1, "Up Ratio", bgcolor = bg_up, text_color = color.white, text_size = i_textSize)
        table.cell(dtrDisplay, 1, 1, str.tostring(math.round(uratio * 100, 1)) + "%", bgcolor = bg_up, text_color = color.white, text_font_family = font.family_monospace, text_size = i_textSize)
        
        table.cell(dtrDisplay, 0, 2, "Down Ratio", bgcolor = bg_dn, text_color = color.white, text_size = i_textSize)
        table.cell(dtrDisplay, 1, 2, str.tostring(math.round(dratio * 100, 1)) + "%", bgcolor = bg_dn, text_color = color.white, text_font_family = font.family_monospace, text_size = i_textSize)
        
        table.cell(dtrDisplay, 0, 3, "Trend (" + str.tostring(trend_length) + "d)", bgcolor = bg_trend, text_color = color.white, text_size = i_textSize)
        table.cell(dtrDisplay, 1, 3, str.tostring(math.round(uratio_trend * 100, 1)) + "%", bgcolor = bg_trend, text_color = color.white, text_font_family = font.family_monospace, text_size = i_textSize)
 
// --- Alerts ---
alertcondition(is_u80, "80% Up Day", "Nasdaq: 80%+ Up-Volume Day")
alertcondition(is_d80, "80% Down Day", "Nasdaq: 80%+ Down-Volume Day")
alertcondition(is_u90, "90% Up Day", "Nasdaq: 90%+ Up-Volume Day")
alertcondition(is_d90, "90% Down Day", "Nasdaq: 90%+ Down-Volume Day")

← 返回列表