本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("EP001 - 15m Opening Range Audit (Fade vs Breakout)", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.04, slippage=1)
// ==========================================
// 1. Strategy Mode Switch & Inputs
// ==========================================
strat_mode = input.string("Flipped: Momentum Breakout", "Strategy Logic Mode", options=["Original: Mean-Reversion Fade", "Flipped: Momentum Breakout"], tooltip="Switch between the original viral counter-trend rules and our flipped trend-following model.")
atr_mult = input.float(0.25, "Minimum OR Range (Daily ATR %)")
sl_offset_tic = input.int(2, "Stop-Loss Offset (Ticks)")
rr_ratio = input.float(2.0, "Risk:Reward Ratio (Breakout Mode Only)")
daily_atr = request.security(syminfo.tickerid, "D", ta.atr(14)[1])
ny_tz = "America/New_York"
or_session = "0930-0945:1234567"
trade_window = strat_mode == "Original: Mean-Reversion Fade" ? "0930-1100:1234567" : "0930-1130:1234567"
// ==========================================
// 2. Opening Range (OR) Calculation
// ==========================================
var float or_h = na
var float or_l = na
var float or_open = na
var float or_close = na
var bool is_valid_atr = false
var bool done_today = false
is_new_day = ta.change(time("D", ny_tz)) != 0
in_or = not na(time(timeframe.period, or_session, ny_tz))
in_window = not na(time(timeframe.period, trade_window, ny_tz))
if is_new_day
or_h := na
or_l := na
or_open := na
or_close := na
is_valid_atr := false
done_today := false
if in_or
if na(or_open)
or_open := open
or_h := na(or_h) ? high : math.max(or_h, high)
or_l := na(or_l) ? low : math.min(or_l, low)
or_close := close
if not in_or and in_or[1]
is_valid_atr := (or_h - or_l) >= (daily_atr * atr_mult)
// ==========================================
// 3. Candlestick Pattern Definitions
// ==========================================
body = math.abs(open - close)
u_wick = high - math.max(open, close)
l_wick = math.min(open, close) - low
is_red = close < open
is_green = close > open
is_red_inv_ham = is_red and u_wick > body * 2 and l_wick < (u_wick / 3)
is_red_eng_green = is_red and close[1] > open[1] and close < open[1] and open > close[1]
is_green_ham = is_green and l_wick > body * 2 and u_wick < (l_wick / 3)
is_green_eng_red = is_green and close[1] < open[1] and close > open[1] and open < close[1]
// ==========================================
// 4. Execution Logic (Original vs Flipped)
// ==========================================
can_trade = not in_or and in_window and is_valid_atr and not done_today
or_bullish = or_close > or_open
or_bearish = or_close < or_open
if strat_mode == "Original: Mean-Reversion Fade"
if can_trade and or_bullish and high > or_h and (is_red_inv_ham or is_red_eng_green)
float sl = is_red_inv_ham ? high : high + (syminfo.mintick * sl_offset_tic)
float entry_price = is_red_inv_ham ? close : close[1]
strategy.entry("Fade Short", strategy.short, limit=entry_price, comment="Fade S")
strategy.exit("ExS", "Fade Short", limit=or_l, stop=sl)
done_today := true
if can_trade and or_bearish and low < or_l and (is_green_ham or is_green_eng_red)
float sl = is_green_ham ? low : low - (syminfo.mintick * sl_offset_tic)
float entry_price = is_green_ham ? close : close[1]
strategy.entry("Fade Long", strategy.long, limit=entry_price, comment="Fade L")
strategy.exit("ExL", "Fade Long", limit=or_h, stop=sl)
done_today := true
else
if can_trade and close > or_h and (is_green_ham or is_green_eng_red)
float sl_price = is_green_ham ? low : low - (syminfo.mintick * sl_offset_tic)
if sl_price >= close
sl_price := low - syminfo.mintick * 5
float risk = close - sl_price
strategy.entry("Trend Long", strategy.long, comment="Breakout L")
strategy.exit("Exit L", "Trend Long", stop=sl_price, limit=close + (risk * rr_ratio))
done_today := true
if can_trade and close < or_l and (is_red_inv_ham or is_red_eng_green)
float sl_price = is_red_inv_ham ? high : high + (syminfo.mintick * sl_offset_tic)
if sl_price <= close
sl_price := high + syminfo.mintick * 5
float risk = sl_price - close
strategy.entry("Trend Short", strategy.short, comment="Breakout S")
strategy.exit("Exit S", "Trend Short", stop=sl_price, limit=close - (risk * rr_ratio))
done_today := true
plot(or_h, "OR High", color=is_valid_atr ? color.green : color.gray, style=plot.style_linebr, linewidth=2)
plot(or_l, "OR Low", color=is_valid_atr ? color.red : color.gray, style=plot.style_linebr, linewidth=2)
bgcolor(in_window ? color.new(color.blue, 95) : na)