Hyper Squeeze Sniper (Dual Side: Long + Short)

Gustav_Rex · study · 153 行 · 点赞 1,140 · TradingView 原页

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

Pine Script

//@version=6
indicator("Hyper Squeeze Sniper (Dual Side: Long + Short)", shorttitle="Hyper Sniper", overlay=true)
 
// ==========================================
// 1. SETTINGS
// ==========================================
group_sqz = "1. Squeeze Settings (หาจุดระเบิด)"
bb_len    = input.int(20, "BB Length", group=group_sqz)
bb_mult   = input.float(2.0, "BB MultFactor", group=group_sqz)
kc_mult   = input.float(1.5, "KC MultFactor", group=group_sqz)
 
group_vol = "2. Volume Filter (กรองหลอก)"
vol_len   = input.int(20, "Vol Length", group=group_vol)
vol_mult  = input.float(1.2, "Vol Spike Factor", group=group_vol)
 
group_exit = "3. Safety Exit (กันติดดอย)"
atr_period = input.int(14, "ATR Period", group=group_exit)
atr_mult   = input.float(3.0, "Trailing Stop Multiplier", group=group_exit)
 
// ==========================================
// 2. CALCULATIONS
// ==========================================
 
// --- A. Squeeze Detection (ภาวะบีบอัด) ---
[basis, upperBB, lowerBB] = ta.bb(close, bb_len, bb_mult)
[kc_mid, upperKC, lowerKC] = ta.kc(close, bb_len, kc_mult)
 
// Squeeze = BB แคบกว่า KC
is_squeeze_on = (lowerBB > lowerKC) and (upperBB < upperKC)
 
// --- B. Momentum & Trend ---
// Linear Regression
src_mom = close - math.avg(math.avg(ta.highest(high, bb_len), ta.lowest(low, bb_len)), ta.sma(close, bb_len))
val = ta.linreg(src_mom, bb_len, 0)
 
is_bullish_mom = val > 0 // โมเมนตัมขาขึ้น
is_bearish_mom = val < 0 // โมเมนตัมขาลง
 
// --- C. Volume Spike & Candle Color ---
vol_avg = ta.sma(volume, vol_len)
is_vol_spike = volume > (vol_avg * vol_mult)
is_green = close > open
is_red   = close < open
 
// --- D. Trailing Stop Logic (Dual Side) ---
atr = ta.atr(atr_period)
 
// 1. Long Stop Calculation (สำหรับขาขึ้น)
long_stop_calc = ta.highest(close, atr_period) - (atr * atr_mult)
var float long_trail = na
// ถ้าอยู่ในสถานะ Long เส้น Stop จะขยับขึ้นเท่านั้น (Max)
long_trail := na(long_trail[1]) ? long_stop_calc : math.max(long_stop_calc, long_trail[1])
 
// 2. Short Stop Calculation (สำหรับขาลง)
short_stop_calc = ta.lowest(close, atr_period) + (atr * atr_mult)
var float short_trail = na
// ถ้าอยู่ในสถานะ Short เส้น Stop จะขยับลงเท่านั้น (Min)
short_trail := na(short_trail[1]) ? short_stop_calc : math.min(short_stop_calc, short_trail[1])
 
 
// ==========================================
// 3. SIGNAL GENERATION (STATE MACHINE)
// ==========================================
 
// ตัวแปรเก็บสถานะ: 0 = ว่าง, 1 = ถือ Long, -1 = ถือ Short
var int trade_state = 0 
 
// เงื่อนไข Breakout
// Long: ทะลุกรอบบน + โมเมนตัมบวก + แท่งเขียว + วอลุ่ม
bull_breakout = ta.crossover(close, upperBB) and is_bullish_mom and is_vol_spike and is_green
// Short: ทะลุกรอบล่าง + โมเมนตัมลบ + แท่งแดง + วอลุ่ม
bear_breakout = ta.crossunder(close, lowerBB) and is_bearish_mom and is_vol_spike and is_red
 
// Logic การเปลี่ยนสถานะ
bool open_long  = false
bool close_long = false
bool open_short = false
bool close_short = false
 
if trade_state == 0
    // ถ้าว่างอยู่ ให้รอสัญญาณเข้า
    if bull_breakout
        trade_state := 1
        open_long := true
        long_trail := long_stop_calc // Reset เส้น Stop ใหม่
    else if bear_breakout
        trade_state := -1
        open_short := true
        short_trail := short_stop_calc // Reset เส้น Stop ใหม่
 
else if trade_state == 1
    // ถ้าถือ Long อยู่ ให้รอราคาหลุดเส้น Trailing Stop
    if ta.crossunder(close, long_trail)
        trade_state := 0
        close_long := true
        
else if trade_state == -1
    // ถ้าถือ Short อยู่ ให้รอราคาทะลุขึ้นเส้น Trailing Stop
    if ta.crossover(close, short_trail)
        trade_state := 0
        close_short := true
 
// ==========================================
// 4. VISUALIZATION
// ==========================================
 
// 4.1 Plot Trailing Stop
// เส้น Long (สีเขียวสว่าง)
plot(trade_state == 1 ? long_trail : na, "Long Safety Line", color=color.new(color.lime, 0), style=plot.style_linebr, linewidth=2)
// เส้น Short (สีส้ม)
plot(trade_state == -1 ? short_trail : na, "Short Safety Line", color=color.new(color.orange, 0), style=plot.style_linebr, linewidth=2)
 
// 4.2 Squeeze Zone Background
bgcolor(is_squeeze_on ? color.new(color.gray, 90) : na, title="Squeeze Zone")
 
// 4.3 Paint Bar
barcolor(trade_state == 1 ? color.lime : trade_state == -1 ? color.red : na)
 
// 4.4 Signals Plotting
// Buy Arrow
plotshape(open_long, title="LONG ENTRY", location=location.belowbar, color=color.lime, style=shape.triangleup, size=size.small, text="LONG")
// Sell (Short) Arrow
plotshape(open_short, title="SHORT ENTRY", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small, text="SHORT")
 
// Exit Long (Cut/Take Profit)
plotshape(close_long, title="EXIT LONG", location=location.abovebar, color=color.white, style=shape.xcross, size=size.tiny)
// Exit Short (Cover)
plotshape(close_short, title="EXIT SHORT", location=location.belowbar, color=color.white, style=shape.xcross, size=size.tiny)
 
// ==========================================
// 5. DASHBOARD
// ==========================================
var table statusTable = table.new(position.top_right, 2, 4, frame_width=1, frame_color=color.gray, border_width=1)
if barstate.islast
    table.cell(statusTable, 0, 0, "HYPER SNIPER", bgcolor=color.navy, text_color=color.white, width=12)
    table.cell(statusTable, 1, 0, syminfo.ticker, bgcolor=color.navy, text_color=color.white)
 
    // Market Phase
    phase_txt = is_squeeze_on ? "SQUEEZE" : (val > 0 ? "BULLISH" : "BEARISH")
    phase_col = is_squeeze_on ? color.gray : (val > 0 ? color.green : color.red)
    table.cell(statusTable, 0, 1, "Trend", bgcolor=color.black, text_color=color.white)
    table.cell(statusTable, 1, 1, phase_txt, bgcolor=phase_col, text_color=color.white)
 
    // Position Status
    pos_txt = trade_state == 1 ? "LONG" : (trade_state == -1 ? "SHORT" : "WAIT")
    pos_col = trade_state == 1 ? color.lime : (trade_state == -1 ? color.red : color.gray)
    table.cell(statusTable, 0, 2, "Status", bgcolor=color.black, text_color=color.white)
    table.cell(statusTable, 1, 2, pos_txt, bgcolor=pos_col, text_color=color.black)
 
    // Stop Loss Price (Show only if in position)
    sl_price = trade_state == 1 ? str.tostring(long_trail, "#.##") : (trade_state == -1 ? str.tostring(short_trail, "#.##") : "-")
    table.cell(statusTable, 0, 3, "Stop Loss", bgcolor=color.white, text_color=color.black)
    table.cell(statusTable, 1, 3, sl_price, bgcolor=pos_col, text_color=color.black)

← 返回列表