Sector Rotation & Market Breadth by Stocks above MA [AnyMinute]

AnyMinute · study · 143 行 · 点赞 3 · TradingView 原页

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

Pine Script

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © AnyMinute
//@version=6
indicator("Sector Rotation by Percentage of Stocks above Moving Averages [AnyMinute]", shorttitle = "Sector Rotation", overlay = false, precision = 1, max_labels_count = 15)
 
// Calculation
i_MAPeriod_int = input.int(20, "Moving Average Period", options = [5, 10, 20, 50, 100, 150, 200], group = "Calculation", tooltip = "Select the period for which you want to see participation data. All periods use daily breadth data, but the 10-day values are estimated.")
i_AutoPeriod_bl = input.bool(false, "Auto switch period", group = "Calculation", tooltip = "With this checked, you can use the chart's ticker name for period selection.\n\nIf you have tickers 5, 10, 20, 50, 100, 150 and 200 in a watchlist, you can easily change periods by clicking the ticker name. \n\nOther tickers will not be recognized, clicking those will make the script use the Moving Average period. ")
 
// Display
i_AboveHalfColor_cl = input.color(color.green, "Above 50% bar color", group = "Display")
i_BelowHalfColor_cl = input.color(color.red, "Below 50% bar color", group = "Display")
i_TextColor_cl = input.color(color.gray, "Text color", group = "Display")
i_BaselineColor_cl = input.color(color.new(color.gray, 50), "50% baseline color", group = "Display")
 
 
f_PeriodSuffix(Period_int) =>
    switch Period_int
        5 => "FD"
        20 => "TW"
        50 => "FI"
        100 => "OH"
        150 => "OF"
        200 => "TH"
        => ""
 
f_ReadBreadth(Prefix_str, PrimarySuffix_str, Estimate_bl) =>
    [PrimaryBreadth_fl_rq, PrimaryTime_int_rq] = request.security("INDEX:" + Prefix_str + PrimarySuffix_str, "1D", [close, time], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    float Breadth_fl = PrimaryBreadth_fl_rq
 
    if Estimate_bl
        [TwentyDayBreadth_fl_rq, TwentyDayTime_int_rq] = request.security("INDEX:" + Prefix_str + "TW", "1D", [close, time], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
        // Equal weighting provides a transparent proxy; mismatched source days must not be blended.
        Breadth_fl := PrimaryTime_int_rq == TwentyDayTime_int_rq ? (PrimaryBreadth_fl_rq + TwentyDayBreadth_fl_rq) / 2.0 : na
 
    Breadth_fl
 
 
if not timeframe.isdaily or timeframe.multiplier != 1
    runtime.error("This indicator requires a 1-day (1D) chart. Intraday and higher timeframes are not supported.")
 
AutoPeriod_int = switch syminfo.ticker
    "5" => 5
    "10" => 10
    "20" => 20
    "50" => 50
    "100" => 100
    "150" => 150
    "200" => 200
    => i_MAPeriod_int
 
SelectedPeriod_int = i_AutoPeriod_bl ? AutoPeriod_int : i_MAPeriod_int
Estimate_bl = SelectedPeriod_int == 10
PrimarySuffix_str = f_PeriodSuffix(Estimate_bl ? 5 : SelectedPeriod_int)
 
CommunicationBreadth_fl = f_ReadBreadth("SL", PrimarySuffix_str, Estimate_bl)
ConsumerDiscretionaryBreadth_fl = f_ReadBreadth("SY", PrimarySuffix_str, Estimate_bl)
ConsumerStaplesBreadth_fl = f_ReadBreadth("SP", PrimarySuffix_str, Estimate_bl)
EnergyBreadth_fl = f_ReadBreadth("SE", PrimarySuffix_str, Estimate_bl)
FinancialsBreadth_fl = f_ReadBreadth("SF", PrimarySuffix_str, Estimate_bl)
HealthCareBreadth_fl = f_ReadBreadth("SV", PrimarySuffix_str, Estimate_bl)
IndustrialsBreadth_fl = f_ReadBreadth("SI", PrimarySuffix_str, Estimate_bl)
MaterialsBreadth_fl = f_ReadBreadth("SB", PrimarySuffix_str, Estimate_bl)
TechnologyBreadth_fl = f_ReadBreadth("SK", PrimarySuffix_str, Estimate_bl)
RealEstateBreadth_fl = f_ReadBreadth("SS", PrimarySuffix_str, Estimate_bl)
UtilitiesBreadth_fl = f_ReadBreadth("SU", PrimarySuffix_str, Estimate_bl)
SPXCoreBreadth_fl = f_ReadBreadth("S5", PrimarySuffix_str, Estimate_bl)
SPXValueBreadth_fl = f_ReadBreadth("SW", PrimarySuffix_str, Estimate_bl)
SPXGrowthBreadth_fl = f_ReadBreadth("SZ", PrimarySuffix_str, Estimate_bl)
 
MethodCaption_str = Estimate_bl ? " (estimated)" : ""
TitleText_str = "% of Stocks Above the " + str.tostring(SelectedPeriod_int) + "-Day Average" + MethodCaption_str
 
var RowNames_str_ar = array.from("Communication", "Cons. Discr.", "Cons. Staples", "Energy", "Financials", "Health Care", "Industrials", "Materials", "Technology", "Real Estate", "Utilities", "S&P 500", "S&P 500 value", "S&P 500 growth")
var RowBaselines_fl_ar = array.from(50.0, 150.0, 250.0, 350.0, 450.0, 550.0, 650.0, 750.0, 850.0, 950.0, 1050.0, 1250.0, 1350.0, 1450.0)
var RowLabels_ar = array.new<label>()
var label TitleLabel = label.new(na, na, "", style = label.style_label_left, color = color.new(color.gray, 100), textalign = text.align_center, size = size.huge)
BreadthValues_fl_ar = array.from(CommunicationBreadth_fl, ConsumerDiscretionaryBreadth_fl, ConsumerStaplesBreadth_fl, EnergyBreadth_fl, FinancialsBreadth_fl, HealthCareBreadth_fl, IndustrialsBreadth_fl, MaterialsBreadth_fl, TechnologyBreadth_fl, RealEstateBreadth_fl, UtilitiesBreadth_fl, SPXCoreBreadth_fl, SPXValueBreadth_fl, SPXGrowthBreadth_fl)
 
 
// Visuals
plot(CommunicationBreadth_fl + 0.0, title = "Communication bars", color = CommunicationBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 50.0, display = display.pane)
plot(ConsumerDiscretionaryBreadth_fl + 100.0, title = "Cons. Discr. bars", color = ConsumerDiscretionaryBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 150.0, display = display.pane)
plot(ConsumerStaplesBreadth_fl + 200.0, title = "Cons. Staples bars", color = ConsumerStaplesBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 250.0, display = display.pane)
plot(EnergyBreadth_fl + 300.0, title = "Energy bars", color = EnergyBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 350.0, display = display.pane)
plot(FinancialsBreadth_fl + 400.0, title = "Financials bars", color = FinancialsBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 450.0, display = display.pane)
plot(HealthCareBreadth_fl + 500.0, title = "Health Care bars", color = HealthCareBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 550.0, display = display.pane)
plot(IndustrialsBreadth_fl + 600.0, title = "Industrials bars", color = IndustrialsBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 650.0, display = display.pane)
plot(MaterialsBreadth_fl + 700.0, title = "Materials bars", color = MaterialsBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 750.0, display = display.pane)
plot(TechnologyBreadth_fl + 800.0, title = "Technology bars", color = TechnologyBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 850.0, display = display.pane)
plot(RealEstateBreadth_fl + 900.0, title = "Real Estate bars", color = RealEstateBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 950.0, display = display.pane)
plot(UtilitiesBreadth_fl + 1000.0, title = "Utilities bars", color = UtilitiesBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 1050.0, display = display.pane)
plot(SPXCoreBreadth_fl + 1200.0, title = "S&P 500 bars", color = SPXCoreBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 1250.0, display = display.pane)
plot(SPXValueBreadth_fl + 1300.0, title = "S&P 500 value bars", color = SPXValueBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 1350.0, display = display.pane)
plot(SPXGrowthBreadth_fl + 1400.0, title = "S&P 500 growth bars", color = SPXGrowthBreadth_fl >= 50.0 ? i_AboveHalfColor_cl : i_BelowHalfColor_cl, style = plot.style_columns, histbase = 1450.0, display = display.pane)
 
// Unshifted percentages remain available for historical inspection (without showing artificial row offsets)
plot(CommunicationBreadth_fl, title = "Communication breadth %", display = display.data_window, editable = false)
plot(ConsumerDiscretionaryBreadth_fl, title = "Cons. Discr. breadth %", display = display.data_window, editable = false)
plot(ConsumerStaplesBreadth_fl, title = "Cons. Staples breadth %", display = display.data_window, editable = false)
plot(EnergyBreadth_fl, title = "Energy breadth %", display = display.data_window, editable = false)
plot(FinancialsBreadth_fl, title = "Financials breadth %", display = display.data_window, editable = false)
plot(HealthCareBreadth_fl, title = "Health Care breadth %", display = display.data_window, editable = false)
plot(IndustrialsBreadth_fl, title = "Industrials breadth %", display = display.data_window, editable = false)
plot(MaterialsBreadth_fl, title = "Materials breadth %", display = display.data_window, editable = false)
plot(TechnologyBreadth_fl, title = "Technology breadth %", display = display.data_window, editable = false)
plot(RealEstateBreadth_fl, title = "Real Estate breadth %", display = display.data_window, editable = false)
plot(UtilitiesBreadth_fl, title = "Utilities breadth %", display = display.data_window, editable = false)
plot(SPXCoreBreadth_fl, title = "S&P 500 breadth %", display = display.data_window, editable = false)
plot(SPXValueBreadth_fl, title = "S&P 500 value breadth %", display = display.data_window, editable = false)
plot(SPXGrowthBreadth_fl, title = "S&P 500 growth breadth %", display = display.data_window, editable = false)
 
hline(50.0, title = "Communication 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(150.0, title = "Cons. Discr. 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(250.0, title = "Cons. Staples 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(350.0, title = "Energy 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(450.0, title = "Financials 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(550.0, title = "Health Care 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(650.0, title = "Industrials 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(750.0, title = "Materials 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(850.0, title = "Technology 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(950.0, title = "Real Estate 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(1050.0, title = "Utilities 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(1250.0, title = "S&P 500 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(1350.0, title = "S&P 500 value 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
hline(1450.0, title = "S&P 500 growth 50%", color = i_BaselineColor_cl, linestyle = hline.style_dotted)
 
if barstate.isfirst
    for RowIndex_int = 0 to array.size(RowNames_str_ar) - 1
        array.push(RowLabels_ar, label.new(na, na, "", style = label.style_label_left, color = color.new(color.gray, 100), textalign = text.align_center, size = size.large))
 
if barstate.islast
    for RowIndex_int = 0 to array.size(RowNames_str_ar) - 1
        RowLabel = array.get(RowLabels_ar, RowIndex_int)
        RowBreadth_fl = array.get(BreadthValues_fl_ar, RowIndex_int)
        RowValueText_str = na(RowBreadth_fl) ? "N/A" : str.tostring(RowBreadth_fl, "#.0") + "%"
        label.set_xy(RowLabel, bar_index, array.get(RowBaselines_fl_ar, RowIndex_int))
        label.set_text(RowLabel, array.get(RowNames_str_ar, RowIndex_int) + "\n" + RowValueText_str)
        label.set_textcolor(RowLabel, i_TextColor_cl)
 
    label.set_xy(TitleLabel, bar_index - 60, 1150)
    label.set_text(TitleLabel, TitleText_str)
    label.set_textcolor(TitleLabel, i_TextColor_cl)

← 返回列表