Pro PnF Ultimate Suite (Channels + Counts + RS)

The_Real_Hippopotamus · study · 238 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("Pro PnF Ultimate Suite (Channels + Counts + RS)", shorttitle="PnF Ultimate Suite", overlay=true, max_lines_count=100, max_labels_count=100, max_boxes_count=50)
 
// ==========================================
// 1. Input Settings
// ==========================================
grp_calc = "PnF Calculation Settings"
box_method = input.string("ATR", "Box Size Method", options=["ATR", "Traditional"], group=grp_calc)
atr_len    = input.int(20, "ATR Length", minval=1, group=grp_calc)
atr_mult   = input.float(0.5, "ATR Multiplier", step=0.1, minval=0.1, group=grp_calc)
trad_box   = input.float(1.0, "Traditional Box Size", step=0.05, minval=0.0001, group=grp_calc)
rev_amount = input.int(3, "Reversal Amount (Boxes)", minval=1, group=grp_calc)
 
grp_vis = "Visual & History Settings"
max_history    = input.int(5, "Max Historical Lines to Keep", minval=1, maxval=20, group=grp_vis)
show_pivots    = input.bool(true, "Show Pivot Markers", group=grp_vis)
show_targets   = input.bool(true, "Show Horizontal Count Targets", group=grp_vis)
bull_col       = input.color(color.new(#00E676, 0), "Bullish Line Color", group=grp_vis)
bear_col       = input.color(color.new(#FF5252, 0), "Bearish Line Color", group=grp_vis)
 
grp_rs = "Relative Strength (RS) Settings"
enable_rs      = input.bool(true, "Enable RS vs Benchmark", group=grp_rs)
benchmark_sym  = input.symbol("SPY", "Benchmark Symbol", group=grp_rs)
 
grp_alerts = "Alert Settings"
enable_alerts  = input.bool(true, "Enable Breakout Alerts", group=grp_alerts)
 
// ==========================================
// 2. Box Size Calculation
// ==========================================
float raw_atr = ta.atr(atr_len)
float box_size = box_method == "ATR" ? math.max(raw_atr * atr_mult, syminfo.mintick) : trad_box
 
// ==========================================
// 3. PnF State Machine Engine
// ==========================================
var int   col_dir       = 0
var float col_high      = na
var float col_low       = na
var int   col_start_bar = bar_index
 
var array<int>   arr_dir       = array.new_int()
var array<float> arr_high      = array.new_float()
var array<float> arr_low       = array.new_float()
var array<int>   arr_start_bar = array.new_int()
var array<int>   arr_end_bar   = array.new_int()
 
var array<line> bull_lines_arr = array.new_line()
var array<line> bear_lines_arr = array.new_line()
 
var float bull_anchor_px  = na
var int   bull_anchor_col = not na(bar_index) ? bar_index : 0
var float bear_anchor_px  = na
var int   bear_anchor_col = not na(bar_index) ? bar_index : 0
 
if na(col_high) and not na(box_size)
    col_high := math.floor(close / box_size) * box_size
    col_low  := col_high
 
bool new_column_created = false
 
if not na(col_high) and not na(box_size)
    float rev_dist = box_size * rev_amount
 
    if col_dir == 0
        if close >= col_high + box_size
            col_dir := 1
            col_high := math.floor(close / box_size) * box_size
        else if close <= col_low - box_size
            col_dir := -1
            col_low := math.ceil(close / box_size) * box_size
    else if col_dir == 1
        if close >= col_high + box_size
            int boxes_added = math.floor((close - col_high) / box_size)
            col_high += boxes_added * box_size
        else if close <= col_high - rev_dist
            array.push(arr_dir, col_dir)
            array.push(arr_high, col_high)
            array.push(arr_low, col_low)
            array.push(arr_start_bar, col_start_bar)
            array.push(arr_end_bar, bar_index)
            
            col_dir       := -1
            col_start_bar := bar_index
            float new_top  = col_high - box_size
            int boxes_down = math.floor((col_high - close) / box_size)
            col_high      := new_top
            col_low       := col_high - (boxes_down - 1) * box_size
            new_column_created := true
    else if col_dir == -1
        if close <= col_low - box_size
            int boxes_added = math.floor((col_low - close) / box_size)
            col_low -= boxes_added * box_size
        else if close >= col_low + rev_dist
            array.push(arr_dir, col_dir)
            array.push(arr_high, col_high)
            array.push(arr_low, col_low)
            array.push(arr_start_bar, col_start_bar)
            array.push(arr_end_bar, bar_index)
            
            col_dir       := 1
            col_start_bar := bar_index
            float new_bot  = col_low + box_size
            int boxes_up   = math.floor((close - col_low) / box_size)
            col_low       := new_bot
            col_high      := col_low + (boxes_up - 1) * box_size
            new_column_created := true
 
// ==========================================
// 4. Line Plotting & Horizontal Count Target
// ==========================================
int total_cols = array.size(arr_dir)
var line target_line = na
 
if new_column_created and total_cols >= 5
    int last_idx = total_cols - 1
    int pprev_idx = total_cols - 3
 
    // 1. Structural Support Detection (Bullish 45° Line) + Horizontal Count Target
    if array.get(arr_dir, last_idx) == -1 and array.get(arr_dir, pprev_idx) == -1
        float curr_o_low = array.get(arr_low, last_idx)
        float prev_o_low = array.get(arr_low, pprev_idx)
        
        if curr_o_low <= prev_o_low or na(bull_anchor_px)
            bull_anchor_px  := curr_o_low - box_size
            bull_anchor_col := last_idx
            int anchor_bar   = array.get(arr_end_bar, last_idx)
            
            line new_l = line.new(anchor_bar, bull_anchor_px, bar_index, bull_anchor_px + box_size, 
                                  color=bull_col, width=2, style=line.style_solid)
            array.push(bull_lines_arr, new_l)
            
            if array.size(bull_lines_arr) > max_history
                line.delete(array.shift(bull_lines_arr))
 
            if show_pivots
                label.new(anchor_bar, bull_anchor_px, "Bull 45°", style=label.style_label_up, 
                          color=color.new(bull_col, 80), textcolor=bull_col, size=size.tiny)
 
            // Horizontal Count Target based on base width
            if show_targets
                int base_width = math.min(total_cols, 20)
                float target_price = curr_o_low + (base_width * box_size * rev_amount)
                line.delete(target_line)
                target_line := line.new(anchor_bar, target_price, bar_index + 50, target_price, 
                                        color=color.yellow, width=2, style=line.style_dashed)
                label.new(bar_index + 10, target_price, "PnF Target: " + str.tostring(target_price, format.mintick), 
                          style=label.style_label_left, color=color.new(color.yellow, 30), textcolor=color.black, size=size.small)
 
    // 2. Structural Resistance Detection (Bearish 45° Line) - FIXED & RESTORED
    if array.get(arr_dir, last_idx) == 1 and array.get(arr_dir, pprev_idx) == 1
        float curr_x_high = array.get(arr_high, last_idx)
        float prev_x_high = array.get(arr_high, pprev_idx)
        
        if curr_x_high >= prev_x_high or na(bear_anchor_px)
            bear_anchor_px  := curr_x_high + box_size
            bear_anchor_col := last_idx
            int anchor_bar   = array.get(arr_end_bar, last_idx)
            
            line new_l = line.new(anchor_bar, bear_anchor_px, bar_index, bear_anchor_px - box_size, 
                                  color=bear_col, width=2, style=line.style_solid)
            array.push(bear_lines_arr, new_l)
            
            if array.size(bear_lines_arr) > max_history
                line.delete(array.shift(bear_lines_arr))
 
            if show_pivots
                label.new(anchor_bar, bear_anchor_px, "Bear 45°", style=label.style_label_down, 
                          color=color.new(bear_col, 80), textcolor=bear_col, size=size.tiny)
 
// ==========================================
// 5. Dynamic Active Lines Update
// ==========================================
int b_size = array.size(bull_lines_arr)
if b_size > 0
    for i = 0 to b_size - 1
        line l = array.get(bull_lines_arr, i)
        int cols_elapsed = total_cols - bull_anchor_col
        float current_level = bull_anchor_px + (cols_elapsed * box_size)
        line.set_xy2(l, bar_index, current_level)
 
int r_size = array.size(bear_lines_arr)
if r_size > 0
    for i = 0 to r_size - 1
        line l = array.get(bear_lines_arr, i)
        int cols_elapsed = total_cols - bear_anchor_col
        float current_level = bear_anchor_px - (cols_elapsed * box_size)
        line.set_xy2(l, bar_index, current_level)
 
// ==========================================
// 6. Relative Strength (RS) Calculation
// ==========================================
float bench_close = request.security(benchmark_sym, timeframe.period, close)
float asset_ratio = close / bench_close
float rs_roc      = ta.roc(asset_ratio, 14)
bool  is_outperforming = rs_roc > 0
 
// ==========================================
// 7. Alerts & Warning-Free Execution
// ==========================================
int cols_elapsed_bull = total_cols - bull_anchor_col
float active_bull_level = bull_anchor_px + (cols_elapsed_bull * box_size)
 
int cols_elapsed_bear = total_cols - bear_anchor_col
float active_bear_level = bear_anchor_px - (cols_elapsed_bear * box_size)
 
float safe_bull_level = na(active_bull_level) ? 0.0 : active_bull_level
float safe_bear_level = na(active_bear_level) ? 0.0 : active_bear_level
 
bool raw_cross_under = ta.crossunder(close, safe_bull_level)
bool raw_cross_over  = ta.crossover(close, safe_bear_level)
 
bool bull_broken = not na(bull_anchor_px) and raw_cross_under
bool bear_broken = not na(bear_anchor_px) and raw_cross_over
 
if enable_alerts and bull_broken
    alert("PnF Bullish Support Broken! Price crossed below 45° line.", alert.freq_once_per_bar)
 
if enable_alerts and bear_broken
    alert("PnF Bearish Resistance Broken! Price crossed above 45° line.", alert.freq_once_per_bar)
 
// ==========================================
// 8. Institutional Dashboard Table
// ==========================================
var table dashboard = table.new(position.top_right, 2, 4, bgcolor=color.new(color.black, 65), border_width=1)
if barstate.islast
    table.cell(dashboard, 0, 0, "PnF State", text_color=color.white, text_size=size.small)
    table.cell(dashboard, 1, 0, col_dir == 1 ? "X (Bullish)" : "O (Bearish)", text_color=col_dir == 1 ? bull_col : bear_col, text_size=size.small)
    
    table.cell(dashboard, 0, 1, "Box Size", text_color=color.white, text_size=size.small)
    table.cell(dashboard, 1, 1, str.tostring(box_size, format.mintick), text_color=color.yellow, text_size=size.small)
    
    table.cell(dashboard, 0, 2, "Active Columns", text_color=color.white, text_size=size.small)
    table.cell(dashboard, 1, 2, str.tostring(total_cols), text_color=color.silver, text_size=size.small)
 
    if enable_rs
        table.cell(dashboard, 0, 3, "RS vs " + benchmark_sym, text_color=color.white, text_size=size.small)
        table.cell(dashboard, 1, 3, is_outperforming ? "Outperforming 🚀" : "Underperforming 🔻", text_color=is_outperforming ? color.green : color.red, text_size=size.small)

← 返回列表