CRT + Turtle Soup Indicator

securarte · study · 334 行 · 点赞 281 · TradingView 原页

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

Pine Script

//@version=5
indicator("CRT + Turtle Soup Indicator", shorttitle="CRT+TS", overlay=true, max_lines_count=500, max_labels_count=500)
 
// ============================================================================
// INPUTS - CONFIGURACIÓN
// ============================================================================
 
// --- Configuración CRT ---
htf_timeframe = input.timeframe("240", "HTF para CRT", group="CRT Settings")
lookback_candles = input.int(3, "Lookback Candles", minval=1, maxval=10, group="CRT Settings")
liquidity_buffer = input.float(0.1, "Liquidity Buffer %", minval=0.01, maxval=1.0, step=0.01, group="CRT Settings")
 
// --- Configuración Turtle Soup ---
sr_length = input.int(20, "Support/Resistance Length", minval=5, maxval=100, group="Turtle Soup Settings")
breakout_threshold = input.float(0.05, "False Breakout Threshold %", minval=0.01, maxval=0.5, step=0.01, group="Turtle Soup Settings")
reversal_bars = input.int(2, "Reversal Confirmation Bars", minval=1, maxval=5, group="Turtle Soup Settings")
 
// --- Gestión de Riesgo ---
risk_reward = input.float(2.0, "Risk/Reward Ratio", minval=0.5, maxval=5.0, step=0.1, group="Risk Management")
use_atr_stops = input.bool(true, "Use ATR for Stops", group="Risk Management")
atr_period = input.int(14, "ATR Period", minval=5, maxval=50, group="Risk Management")
atr_multiplier = input.float(1.5, "ATR Multiplier", minval=0.5, maxval=5.0, step=0.1, group="Risk Management")
 
// --- Filtros de Sesión ---
enable_session = input.bool(true, "Enable Session Filter", group="Session Filters")
london_session = input.bool(true, "London Session (08:00-17:00 GMT)", group="Session Filters")
newyork_session = input.bool(true, "New York Session (13:00-22:00 GMT)", group="Session Filters")
asian_session = input.bool(false, "Asian Session (00:00-09:00 GMT)", group="Session Filters")
 
// --- Visualización ---
show_crt_range = input.bool(true, "Show CRT Range", group="Display")
show_sr_levels = input.bool(true, "Show S/R Levels", group="Display")
show_liquidity_raids = input.bool(true, "Show Liquidity Raids", group="Display")
show_signals = input.bool(true, "Show Trade Signals", group="Display")
 
// ============================================================================
// FUNCIONES AUXILIARES
// ============================================================================
 
// Verificar si estamos en sesión activa
is_session_active() =>
    if not enable_session
        true
    else
        current_hour = hour(time, "GMT")
        in_london = london_session and (current_hour >= 8 and current_hour < 17)
        in_newyork = newyork_session and (current_hour >= 13 and current_hour < 22)
        in_asian = asian_session and (current_hour >= 0 and current_hour < 9)
        in_london or in_newyork or in_asian
 
// ============================================================================
// MÓDULO CRT (CANDLE RANGE THEORY)
// ============================================================================
 
// Obtener datos del HTF
[htf_high, htf_low, htf_close] = request.security(syminfo.tickerid, htf_timeframe, [high[1], low[1], close[1]], lookahead=barmerge.lookahead_off)
 
// Variables para almacenar rangos CRT
var float crt_high = na
var float crt_low = na
var bool crt_range_active = false
 
// Actualizar rangos CRT cuando hay nueva vela HTF
htf_changed = ta.change(time(htf_timeframe)) != 0
if htf_changed
    crt_high := htf_high
    crt_low := htf_low
    crt_range_active := true
 
// Calcular buffer de liquidez
liq_buffer = (crt_high - crt_low) * liquidity_buffer / 100
 
// Detectar raids de liquidez
raid_high = crt_range_active and high > (crt_high + liq_buffer) and close < crt_high
raid_low = crt_range_active and low < (crt_low - liq_buffer) and close > crt_low
 
// Variables para confirmación CRT
var bool crt_bullish_setup = false
var bool crt_bearish_setup = false
var int bars_since_raid = 0
 
// Lógica de confirmación CRT
if raid_low
    crt_bullish_setup := true
    crt_bearish_setup := false
    bars_since_raid := 0
else if raid_high
    crt_bearish_setup := true
    crt_bullish_setup := false
    bars_since_raid := 0
 
if crt_bullish_setup or crt_bearish_setup
    bars_since_raid += 1
 
// Confirmar CRT alcista: cierre por encima del CRT-Low después del raid
crt_bullish_confirmed = crt_bullish_setup and close > crt_low and bars_since_raid <= lookback_candles
 
// Confirmar CRT bajista: cierre por debajo del CRT-High después del raid
crt_bearish_confirmed = crt_bearish_setup and close < crt_high and bars_since_raid <= lookback_candles
 
// Resetear setups si pasa demasiado tiempo
if bars_since_raid > lookback_candles
    crt_bullish_setup := false
    crt_bearish_setup := false
 
// ============================================================================
// MÓDULO TURTLE SOUP (FALSE BREAKOUTS)
// ============================================================================
 
// Calcular niveles de soporte y resistencia
resistance = ta.highest(high, sr_length)
support = ta.lowest(low, sr_length)
 
// Variables para tracking de breakouts
var bool watching_resistance_break = false
var bool watching_support_break = false
var int bars_since_breakout = 0
var float breakout_level = na
 
// Detectar breakouts
breakout_threshold_price = (resistance - support) * breakout_threshold / 100
 
resistance_broken = high > resistance[1] and high > (resistance[1] + breakout_threshold_price)
support_broken = low < support[1] and low < (support[1] - breakout_threshold_price)
 
// Iniciar tracking de breakout
if resistance_broken and not watching_resistance_break
    watching_resistance_break := true
    watching_support_break := false
    bars_since_breakout := 0
    breakout_level := resistance[1]
 
if support_broken and not watching_support_break
    watching_support_break := true
    watching_resistance_break := false
    bars_since_breakout := 0
    breakout_level := support[1]
 
// Incrementar contador
if watching_resistance_break or watching_support_break
    bars_since_breakout += 1
 
// Detectar false breakouts (reversión)
false_breakout_bearish = watching_resistance_break and close < breakout_level and bars_since_breakout <= reversal_bars
false_breakout_bullish = watching_support_break and close > breakout_level and bars_since_breakout <= reversal_bars
 
// Resetear tracking después de confirmación o timeout
if false_breakout_bearish or false_breakout_bullish or bars_since_breakout > reversal_bars
    watching_resistance_break := false
    watching_support_break := false
    bars_since_breakout := 0
 
// ============================================================================
// MÓDULO MARKET STRUCTURE SHIFT (MSS)
// ============================================================================
 
// Detectar swing highs y lows
swing_length = 5
swing_high = ta.pivothigh(high, swing_length, swing_length)
swing_low = ta.pivotlow(low, swing_length, swing_length)
 
// Variables para estructura de mercado
var float last_swing_high = na
var float last_swing_low = na
 
if not na(swing_high)
    last_swing_high := swing_high
 
if not na(swing_low)
    last_swing_low := swing_low
 
// Market Structure Shift alcista: precio rompe último swing high después de hacer lower low
mss_bullish = not na(last_swing_high) and close > last_swing_high and low[swing_length] < last_swing_low
 
// Market Structure Shift bajista: precio rompe último swing low después de hacer higher high
mss_bearish = not na(last_swing_low) and close < last_swing_low and high[swing_length] > last_swing_high
 
// ============================================================================
// CÁLCULO ATR
// ============================================================================
 
atr_value = ta.atr(atr_period)
 
// ============================================================================
// SEÑALES DE TRADING
// ============================================================================
 
// Verificar sesión
session_ok = is_session_active()
 
// Señal LONG completa
signal_long = crt_bullish_confirmed and mss_bullish and session_ok
signal_long_with_ts = signal_long and false_breakout_bullish
 
// Señal SHORT completa
signal_short = crt_bearish_confirmed and mss_bearish and session_ok
signal_short_with_ts = signal_short and false_breakout_bearish
 
// Calcular niveles de entrada, SL y TP
var float entry_price = na
var float stop_loss = na
var float take_profit = na
 
if signal_long or signal_long_with_ts
    entry_price := close
    if use_atr_stops
        stop_loss := entry_price - (atr_value * atr_multiplier)
    else
        stop_loss := crt_low - liq_buffer
    stop_distance = entry_price - stop_loss
    take_profit := entry_price + (stop_distance * risk_reward)
 
if signal_short or signal_short_with_ts
    entry_price := close
    if use_atr_stops
        stop_loss := entry_price + (atr_value * atr_multiplier)
    else
        stop_loss := crt_high + liq_buffer
    stop_distance = stop_loss - entry_price
    take_profit := entry_price - (stop_distance * risk_reward)
 
// ============================================================================
// VISUALIZACIÓN
// ============================================================================
 
// Colores
color_crt_range = color.new(color.blue, 85)
color_crt_line = color.new(color.blue, 30)
color_resistance = color.new(color.red, 30)
color_support = color.new(color.green, 30)
color_long = color.new(color.lime, 0)
color_short = color.new(color.red, 0)
 
// Dibujar rangos CRT
if show_crt_range and not na(crt_high) and not na(crt_low)
    var line crt_high_line = na
    var line crt_low_line = na
    var box crt_box = na
    
    // Limpiar líneas anteriores
    if not na(crt_high_line)
        line.delete(crt_high_line)
    if not na(crt_low_line)
        line.delete(crt_low_line)
    if not na(crt_box)
        box.delete(crt_box)
    
    // Dibujar nuevas líneas
    crt_high_line := line.new(bar_index - 50, crt_high, bar_index + 10, crt_high, color=color_crt_line, width=1, style=line.style_dashed)
    crt_low_line := line.new(bar_index - 50, crt_low, bar_index + 10, crt_low, color=color_crt_line, width=1, style=line.style_dashed)
    crt_box := box.new(bar_index - 50, crt_high, bar_index + 10, crt_low, bgcolor=color_crt_range, border_color=color_crt_line, border_width=1)
 
// Dibujar niveles S/R
plot(show_sr_levels ? resistance : na, "Resistance", color=color_resistance, linewidth=2, style=plot.style_line)
plot(show_sr_levels ? support : na, "Support", color=color_support, linewidth=2, style=plot.style_line)
 
// Marcar raids de liquidez
if show_liquidity_raids
    if raid_low
        label.new(bar_index, low, "💧 Liq Raid", style=label.style_label_up, color=color.new(color.green, 20), textcolor=color.white, size=size.small)
    if raid_high
        label.new(bar_index, high, "💧 Liq Raid", style=label.style_label_down, color=color.new(color.red, 20), textcolor=color.white, size=size.small)
 
// Marcar false breakouts
if show_liquidity_raids
    if false_breakout_bullish
        label.new(bar_index, low, "🐢 TS", style=label.style_label_up, color=color.new(color.aqua, 20), textcolor=color.white, size=size.tiny)
    if false_breakout_bearish
        label.new(bar_index, high, "🐢 TS", style=label.style_label_down, color=color.new(color.orange, 20), textcolor=color.white, size=size.tiny)
 
// Dibujar señales de trading
if show_signals
    if signal_long_with_ts
        label.new(bar_index, low, "🚀 LONG\nCRT+TS", style=label.style_label_up, color=color_long, textcolor=color.white, size=size.normal)
    else if signal_long
        label.new(bar_index, low, "▲ LONG\nCRT", style=label.style_label_up, color=color.new(color.lime, 30), textcolor=color.white, size=size.small)
    
    if signal_short_with_ts
        label.new(bar_index, high, "🔻 SHORT\nCRT+TS", style=label.style_label_down, color=color_short, textcolor=color.white, size=size.normal)
    else if signal_short
        label.new(bar_index, high, "▼ SHORT\nCRT", style=label.style_label_down, color=color.new(color.red, 30), textcolor=color.white, size=size.small)
 
// ============================================================================
// TABLA DE INFORMACIÓN
// ============================================================================
 
var table info_table = table.new(position.top_right, 2, 6, bgcolor=color.new(color.black, 80), border_width=1, border_color=color.gray)
 
if barstate.islast
    // Headers
    table.cell(info_table, 0, 0, "Parámetro", bgcolor=color.new(color.gray, 70), text_color=color.white, text_size=size.small)
    table.cell(info_table, 1, 0, "Valor", bgcolor=color.new(color.gray, 70), text_color=color.white, text_size=size.small)
    
    // CRT Range
    table.cell(info_table, 0, 1, "CRT High", text_color=color.white, text_size=size.small)
    table.cell(info_table, 1, 1, str.tostring(crt_high, format.mintick), text_color=color.blue, text_size=size.small)
    
    table.cell(info_table, 0, 2, "CRT Low", text_color=color.white, text_size=size.small)
    table.cell(info_table, 1, 2, str.tostring(crt_low, format.mintick), text_color=color.blue, text_size=size.small)
    
    // Session
    session_text = session_ok ? "✓ Activa" : "✗ Inactiva"
    session_color = session_ok ? color.lime : color.red
    table.cell(info_table, 0, 3, "Sesión", text_color=color.white, text_size=size.small)
    table.cell(info_table, 1, 3, session_text, text_color=session_color, text_size=size.small)
    
    // ATR
    table.cell(info_table, 0, 4, "ATR", text_color=color.white, text_size=size.small)
    table.cell(info_table, 1, 4, str.tostring(atr_value, format.mintick), text_color=color.yellow, text_size=size.small)
    
    // Setup Status
    setup_status = crt_bullish_setup ? "🟢 Bullish Setup" : crt_bearish_setup ? "🔴 Bearish Setup" : "⚪ Buscando..."
    table.cell(info_table, 0, 5, "Estado", text_color=color.white, text_size=size.small)
    table.cell(info_table, 1, 5, setup_status, text_color=color.white, text_size=size.small)
 
// ============================================================================
// ALERTAS
// ============================================================================
 
alertcondition(raid_low, title="CRT Bullish Raid", message="CRT: Raid de liquidez alcista detectado")
alertcondition(raid_high, title="CRT Bearish Raid", message="CRT: Raid de liquidez bajista detectado")
alertcondition(false_breakout_bullish, title="Turtle Soup Bullish", message="Turtle Soup: False breakout alcista confirmado")
alertcondition(false_breakout_bearish, title="Turtle Soup Bearish", message="Turtle Soup: False breakout bajista confirmado")
alertcondition(signal_long_with_ts, title="LONG Signal (CRT+TS)", message="🚀 SEÑAL LONG COMPLETA - CRT + Turtle Soup confirmados")
alertcondition(signal_short_with_ts, title="SHORT Signal (CRT+TS)", message="🔻 SEÑAL SHORT COMPLETA - CRT + Turtle Soup confirmados")
alertcondition(signal_long, title="LONG Signal (CRT)", message="▲ SEÑAL LONG - CRT confirmado")
alertcondition(signal_short, title="SHORT Signal (CRT)", message="▼ SEÑAL SHORT - CRT confirmado")
 
// ============================================================================
// PLOTS PARA BACKTESTING
// ============================================================================
 
plot(signal_long or signal_long_with_ts ? 1 : 0, "Long Signal", display=display.none)
plot(signal_short or signal_short_with_ts ? 1 : 0, "Short Signal", display=display.none)

← 返回列表