OAK MT-RF Unified Engine Pro

OAKTLive · study · 252 行 · 点赞 50 · TradingView 原页

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

Pine Script

//@version=6
indicator('🌳 OAK + MT-RF Unified Engine Pro', overlay = true, max_boxes_count = 500, max_labels_count = 500, max_lines_count = 500)
// Develop by Ummer Aftab
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
oak_swingLen = 2
oak_atrLen = 14
oak_zoneATR = 0.22
hmaLen = 55
fastLen = 9
slowLen = 21
trendLen = 20
 
show_boxes = input.bool(true, 'Show Profit/Risk Zones', group = 'Visualization')
profit_color = input.color(color.new(color.yellow, 85), 'Profit Zone Color', group = 'Visualization')
risk_color = input.color(color.new(color.maroon, 97), 'Risk Zone Color', group = 'Visualization')
entry_style = input.string('Dotted', 'Entry Line Style', options = ['Solid', 'Dashed', 'Dotted'], group = 'Visualization')
entry_width = input.int(3, 'Entry Line Width', minval = 1, maxval = 5, group = 'Visualization')
 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ENGINE LOGIC
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
oak_atr = ta.atr(oak_atrLen)
oak_zoneSize = math.max(oak_atr * oak_zoneATR, syminfo.mintick * 10)
htf_high = request.security(syminfo.tickerid, '180', ta.highest(high, oak_swingLen), lookahead = barmerge.lookahead_off)
htf_low = request.security(syminfo.tickerid, '180', ta.lowest(low, oak_swingLen), lookahead = barmerge.lookahead_off)
 
var int oak_dir = 0
if ta.crossover(close, htf_high[1])
    oak_dir := 1
    oak_dir
if ta.crossunder(close, htf_low[1])
    oak_dir := -1
    oak_dir
 
var box last_box = na
if oak_dir != oak_dir[1]
    if not na(last_box)
        box.set_right(last_box, bar_index)
    float top = oak_dir == 1 ? htf_low + oak_zoneSize : htf_high + oak_zoneSize
    float bot = oak_dir == 1 ? htf_low - oak_zoneSize : htf_high - oak_zoneSize
    last_box := box.new(left = bar_index, top = top, right = bar_index + 1, bottom = bot, bgcolor = oak_dir == 1 ? #00800026 : #ff000026, border_color = oak_dir == 1 ? #008000cc : #ff0000cc, border_width = 2)
    last_box
else if not na(last_box)
    box.set_right(last_box, bar_index + 1)
 
hullTrend = ta.hma(close, hmaLen)
mtrf_dir = close > hullTrend ? 1 : -1
isExpanding = high - low > ta.sma(high - low, 20)
isVolHigh = volume > ta.sma(volume, 20)
 
dema(src, len) =>
    e1 = ta.ema(src, len)
    e2 = ta.ema(e1, len)
    2 * e1 - e2
fastDEMA = dema(close, fastLen)
slowDEMA = dema(close, slowLen)
trendEMA = ta.ema(close, trendLen)
raw_cloud_dir = fastDEMA > slowDEMA and close > trendEMA ? 1 : fastDEMA < slowDEMA and close < trendEMA ? -1 : 0
 
var int cloud_dir = 0
cloud_dir := raw_cloud_dir == raw_cloud_dir[1] ? raw_cloud_dir : cloud_dir
 
int bullCount = (oak_dir == 1 ? 1 : 0) + (mtrf_dir == 1 ? 1 : 0) + (cloud_dir == 1 ? 1 : 0)
int bearCount = (oak_dir == -1 ? 1 : 0) + (mtrf_dir == -1 ? 1 : 0) + (cloud_dir == -1 ? 1 : 0)
int currentAlignment = bullCount >= 2 ? 1 : bearCount >= 2 ? -1 : 0
 
var int last_confirmed_trend = 0
if currentAlignment != 0
    last_confirmed_trend := currentAlignment
    last_confirmed_trend
 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// TRADINGVIEW STYLE LONG/SHORT VISUALIZER
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var box profitBox = na
var box riskBox = na
var line entryLine = na
var float activeTP = na
var float activeSL = na
var float entry = na
var int signal_state = 0
 
bool isNewTransition = currentAlignment != 0 and currentAlignment != signal_state
 
if isNewTransition and show_boxes
    signal_state := currentAlignment
    entry := close
    activeSL := currentAlignment == 1 ? low - oak_atr * 1.0 : high + oak_atr * 1.0
 
    float volFactor = volume / ta.sma(volume, 20)
    activeTP := currentAlignment == 1 ? entry + math.abs(entry - activeSL) * volFactor : entry - math.abs(entry - activeSL) * volFactor
 
    // Boxes are set to start exactly at entry
    profitBox := box.new(bar_index, entry, bar_index, activeTP, bgcolor = profit_color, border_width = 0)
    riskBox := box.new(bar_index, entry, bar_index, activeSL, bgcolor = risk_color, border_width = 0)
 
    line_style = entry_style == 'Solid' ? line.style_solid : entry_style == 'Dashed' ? line.style_dashed : line.style_dotted
    line_color = currentAlignment == 1 ? color.green : color.red
    entryLine := line.new(bar_index, entry, bar_index + 1, entry, color = line_color, style = line_style, width = entry_width)
    entryLine
 
if show_boxes and not na(profitBox)
    bool oppositeSignal = signal_state == 1 and currentAlignment == -1 or signal_state == -1 and currentAlignment == 1
    bool slHit = signal_state == 1 and low <= activeSL or signal_state == -1 and high >= activeSL
 
    if oppositeSignal or slHit
        profitBox := na
        riskBox := na
        line.delete(entryLine)
    else
        box.set_right(profitBox, bar_index)
        box.set_right(riskBox, bar_index)
        line.set_x2(entryLine, bar_index)
 
        // Fixed TP Logic: Always starts at entry, only expands
        if signal_state == 1
            float currentMax = math.max(high, box.get_top(profitBox))
            box.set_top(profitBox, currentMax)
            box.set_bottom(profitBox, entry)
        else
            float currentMin = math.min(low, box.get_bottom(profitBox))
            box.set_bottom(profitBox, currentMin)
            box.set_top(profitBox, entry)
 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PLOTS & CLOUD
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plot(oak_dir == 1 ? htf_high : na, 'OAK Bull', color = #00ff00, style = plot.style_linebr, linewidth = 2)
plot(oak_dir == -1 ? htf_low : na, 'OAK Bear', color = #ff0000, style = plot.style_linebr, linewidth = 2)
plot(hullTrend, 'MT-RF HMA', color = mtrf_dir == 1 ? color.lime : color.red, linewidth = 2)
p1 = plot(fastDEMA, color = color.new(color.gray, 100), title = 'Fast DEMA')
p2 = plot(slowDEMA, color = color.new(color.gray, 100), title = 'Slow DEMA')
fill(p1, p2, color = cloud_dir == 1 ? color.new(color.green, 75) : cloud_dir == -1 ? color.new(color.red, 75) : color.new(color.gray, 90), title = 'Momentum Cloud')
 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DASHBOARD
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var table dash = table.new(position.top_right, 2, 6, border_width = 1, border_color = color.gray)
if barstate.islast
    color t_trendCol = last_confirmed_trend == 1 ? color.green : last_confirmed_trend == -1 ? color.red : color.gray
    color t_oakCol = oak_dir == 1 ? color.green : color.red
    color t_mtrfCol = mtrf_dir == 1 ? color.green : color.red
    color t_cloudCol = cloud_dir == 1 ? color.green : cloud_dir == -1 ? color.red : color.gray
 
    table.cell(dash, 0, 0, 'Direction', bgcolor = color.black, text_color = color.white)
    table.cell(dash, 1, 0, last_confirmed_trend == 1 ? 'BULL' : last_confirmed_trend == -1 ? 'BEAR' : 'WAIT', bgcolor = t_trendCol, text_color = color.white)
    table.cell(dash, 0, 1, 'Volatility', bgcolor = color.black, text_color = color.white)
    table.cell(dash, 1, 1, isExpanding ? 'EXPANDING' : 'CONTRACTING', bgcolor = isExpanding ? t_trendCol : color.gray, text_color = color.white)
    table.cell(dash, 0, 2, 'Volume', bgcolor = color.black, text_color = color.white)
    table.cell(dash, 1, 2, isVolHigh ? 'HIGH' : 'LOW', bgcolor = isVolHigh ? t_trendCol : color.gray, text_color = color.white)
    table.cell(dash, 0, 3, 'Cloud', bgcolor = color.black, text_color = color.white)
    table.cell(dash, 1, 3, cloud_dir == 1 ? 'BULL' : cloud_dir == -1 ? 'BEAR' : 'NEUTRAL', bgcolor = t_cloudCol, text_color = color.white)
    table.cell(dash, 0, 4, 'MT-RF', bgcolor = color.black, text_color = color.white)
    table.cell(dash, 1, 4, mtrf_dir == 1 ? 'BULL' : 'BEAR', bgcolor = t_mtrfCol, text_color = color.white)
    table.cell(dash, 0, 5, 'OAK', bgcolor = color.black, text_color = color.white)
    table.cell(dash, 1, 5, oak_dir == 1 ? 'BULL' : 'BEAR', bgcolor = t_oakCol, text_color = color.white)
 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(currentAlignment == 1 and last_confirmed_trend[1] == -1, title = 'Direction Change: BULL', message = 'Direction switched to BULL')
alertcondition(currentAlignment == -1 and last_confirmed_trend[1] == 1, title = 'Direction Change: BEAR', message = 'Direction switched to BEAR')
 
//=========================================
//====== OAK Fib Retracement Part =========
//=========================================
 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CORE LOGIC 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
x_swing = 2
show_history = input.bool(false, 'Show Historical Lines')
x_h = request.security(syminfo.tickerid, '180', ta.highest(high, x_swing), lookahead = barmerge.lookahead_off)
x_l = request.security(syminfo.tickerid, '180', ta.lowest(low, x_swing), lookahead = barmerge.lookahead_off)
 
var int x_dir = 0
var float x_fix = na
var float x_mov = na
var int x_bar = bar_index
var line x_line = line.new(na, na, na, na, width = 2)
 
if ta.crossover(close, x_h[1])
    x_dir := 1
    x_fix := x_l
    x_mov := x_h
    x_bar := bar_index
    x_bar
else if ta.crossunder(close, x_l[1])
    x_dir := -1
    x_fix := x_h
    x_mov := x_l
    x_bar := bar_index
    x_bar
 
if not na(x_fix)
    line.set_xy1(x_line, x_bar, x_fix)
    line.set_xy2(x_line, bar_index, x_fix)
    line.set_color(x_line, x_dir == 1 ? color.green : color.red)
 
if x_dir == 1 and ta.change(x_h) != 0
    x_mov := x_h
    x_mov
else if x_dir == -1 and ta.change(x_l) != 0
    x_mov := x_l
    x_mov
 
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DYNAMIC FIBONACCI CALCULATION
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var array<line> x_active_fibs = array.new_line(4, na)
var array<line> x_history_fibs = array.new_line(0)
var array<float> x_lvls = array.from(0.25, 0.33, 0.66, 0.75)
 
// Detect Pivot Change
if ta.change(x_mov) != 0 or ta.change(x_fix) != 0
    if not na(x_fix)
        // 1. Move current active lines to history
        for i = 0 to 3 by 1
            line l = array.get(x_active_fibs, i)
            if not na(l)
                line.set_style(l, line.style_dashed)
                line.set_x2(l, bar_index - 1)
                array.push(x_history_fibs, l)
 
        // 2. Clear tracking array
        array.fill(x_active_fibs, na)
 
        // 3. Create NEW active lines
        float x_diff = x_mov - x_fix
        for i = 0 to 3 by 1
            float p = x_fix + x_diff * array.get(x_lvls, i)
            line l = line.new(x_bar, p, bar_index, p, color = color.gray, style = line.style_solid)
            array.set(x_active_fibs, i, l)
 
// 4. Update Visibility for History
for l in x_history_fibs
    line.set_color(l, show_history ? color.gray : color.new(color.gray, 100))
 
// 5. Update active lines
if not na(x_fix)
    float x_diff = x_mov - x_fix
    color x_col = close > x_fix + x_diff * 0.33 ? color.green : close < x_fix + x_diff * 0.66 ? color.red : color.gray
    for i = 0 to 3 by 1
        line l = array.get(x_active_fibs, i)
        if not na(l)
            line.set_x2(l, bar_index)
            line.set_color(l, x_col)
//=========================================
//=========== END of SCRIPT ===============
//=========================================

← 返回列表