TWP 9:15 HYPERSCOPE PRO V5 | Four Pattern Forecasts

TWP_Vino · study · 217 行 · 点赞 10 · TradingView 原页

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

Pine Script

//@version=6
indicator("TWP 9:15 HYPERSCOPE PRO V5 | Four Pattern Forecasts", shorttitle="TWP 4 PATTERN %", overlay=true, dynamic_requests=true)
 
// Four opening-session forecast scores, frozen after 09:20 IST.
// Scores are relative model weights, NOT validated event probabilities.
// No chart lines, labels, entries, or alerts.
 
string TZ = "Asia/Kolkata"
string G1 = "01 - Higher Timeframes"
bool useD = input.bool(true, "Daily", group=G1)
bool use4H = input.bool(true, "4 Hour", group=G1)
bool use1H = input.bool(true, "1 Hour", group=G1)
bool use30 = input.bool(true, "30 Minutes", group=G1)
bool use15 = input.bool(false, "15 Minutes (Optional)", group=G1)
 
string G2 = "02 - Weights"
float wD = input.float(30, "Daily weight", minval=0, group=G2)
float w4H = input.float(25, "4H weight", minval=0, group=G2)
float w1H = input.float(20, "1H weight", minval=0, group=G2)
float w30 = input.float(15, "30m weight", minval=0, group=G2)
float w15 = input.float(10, "15m weight", minval=0, group=G2)
 
string G3 = "03 - Forecast Model"
int fastLen = input.int(9, "HTF fast EMA", minval=2, group=G3)
int slowLen = input.int(21, "HTF slow EMA", minval=5, group=G3)
float reversalSens = input.float(1.0, "Reversal sensitivity", minval=0.4, maxval=2.5, step=0.05, group=G3)
float scorePower = input.float(2.0, "Score separation", minval=1, maxval=5, step=0.25, group=G3)
float strongGap = input.float(10.0, "Strong lead margin (points)", minval=1, maxval=35, group=G3)
 
string G4 = "04 - Dashboard"
string where = input.string("Top Right", "Box position", options=["Top Right", "Top Left", "Bottom Right"], group=G4)
string fontChoice = input.string("Normal", "Text size", options=["Small", "Normal"], group=G4)
 
if not timeframe.isminutes and not timeframe.isdaily
    runtime.error("Use any chart timeframe from 1m through 1D.")
if timeframe.in_seconds() < 60 or timeframe.in_seconds() > 86400
    runtime.error("Use any chart timeframe from 1m through 1D.")
 
float totalWeight = (useD ? wD : 0.0) + (use4H ? w4H : 0.0) + (use1H ? w1H : 0.0) + (use30 ? w30 : 0.0) + (use15 ? w15 : 0.0)
if totalWeight <= 0
    runtime.error("Switch on at least one timeframe with a positive weight.")
 
f_cap(float x) =>
    math.max(0.0, math.min(1.0, x))
 
f_bias(float c, float fast, float slow, float rsi, float atr) =>
    float safeAtr = math.max(atr, syminfo.mintick * 10)
    float loc = f_cap(0.5 + (c - slow) / (2.0 * safeAtr))
    float slope = f_cap(0.5 + (fast - slow) / (1.25 * safeAtr))
    0.45 * loc + 0.35 * slope + 0.20 * f_cap(rsi / 100.0)
 
f_biasName(float b) =>
    na(b) ? "OFF" : b >= 0.56 ? "BULLISH" : b <= 0.44 ? "BEARISH" : "NEUTRAL"
 
// Called in 1m context. HTF values always refer to completed candles.
f_predict() =>
    [dC,dH,dL,dF,dS,dR,dA] = request.security(syminfo.tickerid, "D", [close[1], high[1], low[1], ta.ema(close,fastLen)[1], ta.ema(close,slowLen)[1], ta.rsi(close,14)[1], ta.atr(14)[1]], lookahead=barmerge.lookahead_on)
    [h4C,h4F,h4S,h4R,h4A] = request.security(syminfo.tickerid, "240", [close[1], ta.ema(close,fastLen)[1], ta.ema(close,slowLen)[1], ta.rsi(close,14)[1], ta.atr(14)[1]], lookahead=barmerge.lookahead_on)
    [h1C,h1F,h1S,h1R,h1A] = request.security(syminfo.tickerid, "60", [close[1], ta.ema(close,fastLen)[1], ta.ema(close,slowLen)[1], ta.rsi(close,14)[1], ta.atr(14)[1]], lookahead=barmerge.lookahead_on)
    [m30C,m30F,m30S,m30R,m30A] = request.security(syminfo.tickerid, "30", [close[1], ta.ema(close,fastLen)[1], ta.ema(close,slowLen)[1], ta.rsi(close,14)[1], ta.atr(14)[1]], lookahead=barmerge.lookahead_on)
    [m15C,m15F,m15S,m15R,m15A] = request.security(syminfo.tickerid, "15", [close[1], ta.ema(close,fastLen)[1], ta.ema(close,slowLen)[1], ta.rsi(close,14)[1], ta.atr(14)[1]], lookahead=barmerge.lookahead_on)
 
    var int savedDay = na
    var float firstOpen = na
    var float firstHigh = na
    var float firstLow = na
    var float firstClose = na
    var float upPct = na
    var float downPct = na
    var float highRevPct = na
    var float lowRevPct = na
    var float bD = na
    var float b4H = na
    var float b1H = na
    var float b30 = na
    var float b15 = na
    var float consensus = na
    var float leadMargin = na
    var int forecastAt = na
 
    int today = year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
    if na(savedDay) or savedDay != today
        savedDay := today
        firstOpen := na
        firstHigh := na
        firstLow := na
        firstClose := na
        upPct := na
        downPct := na
        highRevPct := na
        lowRevPct := na
        bD := na
        b4H := na
        b1H := na
        b30 := na
        b15 := na
        consensus := na
        leadMargin := na
        forecastAt := na
 
    bool session = not na(time("1", "0915-1530:23456", TZ))
    int minuteIST = hour(time, TZ) * 60 + minute(time, TZ)
    bool opening = session and minuteIST >= 555 and minuteIST < 560
    if opening
        firstOpen := na(firstOpen) ? open : firstOpen
        firstHigh := na(firstHigh) ? high : math.max(firstHigh, high)
        firstLow := na(firstLow) ? low : math.min(firstLow, low)
        firstClose := close
 
    bool dataReady = not na(dC) and not na(dH) and not na(dL) and not na(dA) and dA > 0 and (not useD or (not na(dF) and not na(dS) and not na(dR))) and (not use4H or (not na(h4C) and not na(h4F) and not na(h4S) and not na(h4R) and not na(h4A))) and (not use1H or (not na(h1C) and not na(h1F) and not na(h1S) and not na(h1R) and not na(h1A))) and (not use30 or (not na(m30C) and not na(m30F) and not na(m30S) and not na(m30R) and not na(m30A))) and (not use15 or (not na(m15C) and not na(m15F) and not na(m15S) and not na(m15R) and not na(m15A)))
 
    if na(forecastAt) and session and minuteIST >= 560 and not na(firstHigh) and not na(firstLow) and firstHigh > firstLow and dataReady
        float db = f_bias(dC,dF,dS,dR,dA)
        float fb = f_bias(h4C,h4F,h4S,h4R,h4A)
        float hb = f_bias(h1C,h1F,h1S,h1R,h1A)
        float mb = f_bias(m30C,m30F,m30S,m30R,m30A)
        float qb = f_bias(m15C,m15F,m15S,m15R,m15A)
        bD := useD ? db : na
        b4H := use4H ? fb : na
        b1H := use1H ? hb : na
        b30 := use30 ? mb : na
        b15 := use15 ? qb : na
        consensus := ((useD ? wD * db : 0.0) + (use4H ? w4H * fb : 0.0) + (use1H ? w1H * hb : 0.0) + (use30 ? w30 * mb : 0.0) + (use15 ? w15 * qb : 0.0)) / totalWeight
 
        float candleRange = math.max(firstHigh - firstLow, syminfo.mintick)
        float dayAtr = math.max(dA, syminfo.mintick * 10)
        float firstBull = f_cap(0.50 + 0.50 * (firstClose - firstOpen) / candleRange)
        float gapBull = f_cap(0.50 + (firstOpen - dC) / (1.5 * dayAtr))
        float upperWick = f_cap((firstHigh - math.max(firstOpen, firstClose)) / candleRange)
        float lowerWick = f_cap((math.min(firstOpen, firstClose) - firstLow) / candleRange)
        float prevHighNear = f_cap(1.0 - math.abs(firstHigh - dH) / (0.65 * dayAtr))
        float prevLowNear = f_cap(1.0 - math.abs(firstLow - dL) / (0.65 * dayAtr))
        float stretchedUp = f_cap((consensus - 0.55) / 0.35)
        float stretchedDown = f_cap((0.45 - consensus) / 0.35)
 
        float upRaw = 0.10 + 0.65 * consensus + 0.20 * firstBull + 0.05 * gapBull
        float downRaw = 0.10 + 0.65 * (1 - consensus) + 0.20 * (1 - firstBull) + 0.05 * (1 - gapBull)
        float highRaw = reversalSens * (0.08 + 0.30 * consensus * prevHighNear + 0.21 * upperWick + 0.21 * stretchedUp + 0.12 * gapBull + 0.08 * consensus * (1 - firstBull))
        float lowRaw = reversalSens * (0.08 + 0.30 * (1 - consensus) * prevLowNear + 0.21 * lowerWick + 0.21 * stretchedDown + 0.12 * (1 - gapBull) + 0.08 * (1 - consensus) * firstBull)
 
        float u = math.pow(upRaw, scorePower)
        float d = math.pow(downRaw, scorePower)
        float h = math.pow(highRaw, scorePower)
        float l = math.pow(lowRaw, scorePower)
        float sumScores = math.max(u + d + h + l, 0.000001)
        upPct := 100 * u / sumScores
        downPct := 100 * d / sumScores
        highRevPct := 100 * h / sumScores
        lowRevPct := 100 * l / sumScores
 
        float highest = math.max(math.max(upPct, downPct), math.max(highRevPct, lowRevPct))
        float runner = highest == upPct ? math.max(downPct, math.max(highRevPct, lowRevPct)) : highest == downPct ? math.max(upPct, math.max(highRevPct, lowRevPct)) : highest == highRevPct ? math.max(upPct, math.max(downPct, lowRevPct)) : math.max(upPct, math.max(downPct, highRevPct))
        leadMargin := highest - runner
        forecastAt := time
 
    [upPct,downPct,highRevPct,lowRevPct,bD,b4H,b1H,b30,b15,consensus,leadMargin,forecastAt]
 
[aU,aDn,aHR,aLR,aD,a4,a1,a30,a15,aBias,aGap,aTime] = request.security_lower_tf(syminfo.tickerid, "1", f_predict())
int n = array.size(aU)
float u = n > 0 ? array.get(aU,n-1) : na
float d = n > 0 ? array.get(aDn,n-1) : na
float hr = n > 0 ? array.get(aHR,n-1) : na
float lr = n > 0 ? array.get(aLR,n-1) : na
float dBias = n > 0 ? array.get(aD,n-1) : na
float fourBias = n > 0 ? array.get(a4,n-1) : na
float hourBias = n > 0 ? array.get(a1,n-1) : na
float halfBias = n > 0 ? array.get(a30,n-1) : na
float quarterBias = n > 0 ? array.get(a15,n-1) : na
float combinedBias = n > 0 ? array.get(aBias,n-1) : na
float gap = n > 0 ? array.get(aGap,n-1) : na
int timeIssued = n > 0 ? array.get(aTime,n-1) : na
bool ready = not na(timeIssued)
 
float best = ready ? math.max(math.max(u,d),math.max(hr,lr)) : na
string quality = not ready ? "WAIT 09:20 IST" : gap >= strongGap ? "CLEAR MODEL LEAD" : gap >= strongGap / 2 ? "MODERATE LEAD" : "MIXED / UNCERTAIN"
 
color bg = color.rgb(12,23,42)
color cell = color.rgb(21,39,65)
color header = color.rgb(20,79,157)
color dim = color.rgb(188,205,227)
color bull = color.rgb(33,212,170)
color bear = color.rgb(242,86,109)
color orange = color.rgb(249,161,98)
color blue = color.rgb(93,160,248)
s = fontChoice == "Small" ? size.small : size.normal
pos = where == "Top Left" ? position.top_left : where == "Bottom Right" ? position.bottom_right : position.top_right
f_pct(float v) =>
    na(v) ? "--" : str.tostring(v,"#.1") + "%"
 
var table t = table.new(pos, 2, 13, frame_color=color.rgb(75,117,177), frame_width=1)
if barstate.islast
    table.cell(t,0,0,"TWP 9:15 HYPERSCOPE", bgcolor=header, text_color=color.white, text_size=s)
    table.cell(t,1,0,"4 PATTERN FORECASTS", bgcolor=header, text_color=color.white, text_size=s)
    table.cell(t,0,1,"UPTREND", bgcolor=bg, text_color=bull, text_size=s)
    table.cell(t,1,1,f_pct(u), bgcolor=ready and u == best ? color.new(bull,78) : cell, text_color=bull, text_size=s)
    table.cell(t,0,2,"DOWNTREND", bgcolor=bg, text_color=bear, text_size=s)
    table.cell(t,1,2,f_pct(d), bgcolor=ready and d == best ? color.new(bear,78) : cell, text_color=bear, text_size=s)
    table.cell(t,0,3,"9:15 HIGH REVERSAL", bgcolor=bg, text_color=orange, text_size=s)
    table.cell(t,1,3,f_pct(hr), bgcolor=ready and hr == best ? color.new(orange,78) : cell, text_color=orange, text_size=s)
    table.cell(t,0,4,"9:15 LOW REVERSAL", bgcolor=bg, text_color=blue, text_size=s)
    table.cell(t,1,4,f_pct(lr), bgcolor=ready and lr == best ? color.new(blue,78) : cell, text_color=blue, text_size=s)
    table.cell(t,0,5,"DAILY BIAS", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,5,ready ? f_biasName(dBias) : "--", bgcolor=cell, text_color=color.white, text_size=s)
    table.cell(t,0,6,"4H BIAS", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,6,ready ? f_biasName(fourBias) : "--", bgcolor=cell, text_color=color.white, text_size=s)
    table.cell(t,0,7,"1H BIAS", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,7,ready ? f_biasName(hourBias) : "--", bgcolor=cell, text_color=color.white, text_size=s)
    table.cell(t,0,8,"30M BIAS", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,8,ready ? f_biasName(halfBias) : "--", bgcolor=cell, text_color=color.white, text_size=s)
    table.cell(t,0,9,"15M BIAS", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,9,not use15 ? "OFF (OPTIONAL)" : ready ? f_biasName(quarterBias) : "--", bgcolor=cell, text_color=color.white, text_size=s)
    table.cell(t,0,10,"COMBINED HTF BIAS", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,10,ready ? f_biasName(combinedBias) : "--", bgcolor=cell, text_color=color.white, text_size=s)
    table.cell(t,0,11,"FORECAST STATUS", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,11,quality, bgcolor=cell, text_color=ready and gap >= strongGap ? bull : orange, text_size=s)
    table.cell(t,0,12,"FORECAST TIME", bgcolor=bg, text_color=dim, text_size=s)
    table.cell(t,1,12,ready ? str.format_time(timeIssued,"dd MMM, HH:mm",TZ) + " IST" : "WAIT 09:20 IST", bgcolor=cell, text_color=dim, text_size=size.small)

← 返回列表