The EURUSD London Sweep (Dynamic ATR)

Mendhaz · strategy · 100 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
strategy("The EURUSD London Sweep", overlay = true, initial_capital = 3000, default_qty_type = strategy.cash, default_qty_value = 200)
 
// --- HTF Inputs ---
string i_htf1 = input.timeframe("60", "HTF 1 (1 Hour)", group = "Trend Bias")
string i_htf2 = input.timeframe("240", "HTF 2 (4 Hour)", group = "Trend Bias")
int i_emaLen  = input.int(200, "HTF EMA Length", group = "Trend Bias") // 200 EMA standard for EURUSD
 
// --- Oscillator Inputs ---
int i_wprLen  = input.int(14, "WPR Length", group = "Momentum")
int i_wprOb   = input.int(-20, "WPR Overbought", group = "Momentum") // Adjusted for deeper pullbacks
int i_wprOs   = input.int(-80, "WPR Oversold", group = "Momentum")
 
int i_rsiLen  = input.int(14, "RSI Length", group = "Momentum")
int i_rsiBull = input.int(50, "RSI Bullish Threshold", group = "Momentum")
int i_rsiBear = input.int(50, "RSI Bearish Threshold", group = "Momentum")
 
// --- Risk & Filters ---
float i_atrMult  = input.float(1.5, "ATR Stop Multiplier", group = "Risk") // Tighter stop for FX
float i_rr       = input.float(2.0, "Risk/Reward Ratio", group = "Risk")
bool  i_useVol   = input.bool(true, "Require Volume > SMA", group = "Filters")
 
// --- Session Inputs (UTC) ---
bool i_useLondon  = input.bool(true, "Trade London Session", group = "Sessions")
string i_lonSess  = input.session("0700-1100", "London Session Hours (UTC)", group = "Sessions")
 
bool i_useLonNY   = input.bool(true, "Trade London/NY Overlap", group = "Sessions")
string i_lonNySess = input.session("1200-1600", "Lon/NY Overlap Hours (UTC)", group = "Sessions")
 
// --- Session Logic ---
bool inLondon     = not na(time(timeframe.period, i_lonSess, "UTC"))
bool inLonNY      = not na(time(timeframe.period, i_lonNySess, "UTC"))
bool sessionValid = (i_useLondon and inLondon) or (i_useLonNY and inLonNY)
 
// --- HTF Bias ---
float baseEma    = ta.ema(close, i_emaLen)
// v6 request.security defaults to barmerge.lookahead_off, preventing historical repainting
float htf1_ema   = request.security(syminfo.tickerid, i_htf1, baseEma)
float htf1_close = request.security(syminfo.tickerid, i_htf1, close)
float htf2_ema   = request.security(syminfo.tickerid, i_htf2, baseEma)
float htf2_close = request.security(syminfo.tickerid, i_htf2, close)
 
//bool bullishBias = (htf1_close > htf1_ema) and (htf2_close > htf2_ema)
//bool bearishBias = (htf1_close < htf1_ema) and (htf2_close < htf2_ema)
bool bullishBias = (htf1_close > htf1_ema)
bool bearishBias = (htf1_close < htf1_ema)
 
// --- Momentum & Volume Triggers ---
float wpr = ta.wpr(i_wprLen)
float rsi = ta.rsi(close, i_rsiLen)
float atr = ta.atr(14)
 
float volSma = ta.sma(volume, 20)
bool volValid = i_useVol ? (volume > volSma) : true
 
// Momentum hooks for pullback continuation
bool wprLongHook  = ta.crossover(wpr, i_wprOs)
bool wprShortHook = ta.crossunder(wpr, i_wprOb)
 
bool longSignal  = sessionValid and bullishBias and wprLongHook and (rsi > i_rsiBull) and volValid
bool shortSignal = sessionValid and bearishBias and wprShortHook and (rsi < i_rsiBear) and volValid
 
// --- Execution ---
var float slLevel = na
var float tpLevel = na
 
if longSignal and strategy.position_size == 0
    slLevel := close - (atr * i_atrMult)
    tpLevel := close + (atr * i_atrMult * i_rr)
    strategy.entry("Long", strategy.long)
 
if shortSignal and strategy.position_size == 0
    slLevel := close + (atr * i_atrMult)
    tpLevel := close - (atr * i_atrMult * i_rr)
    strategy.entry("Short", strategy.short)
 
if strategy.position_size > 0
    strategy.exit("Exit Long", "Long", stop = slLevel, limit = tpLevel)
 
if strategy.position_size < 0
    strategy.exit("Exit Short", "Short", stop = slLevel, limit = tpLevel)
 
// --- Visuals & Dashboard ---
plot(htf1_ema, "1H EMA", color = color.blue)
plot(htf2_ema, "4H EMA", color = color.purple)
 
bgcolor(i_useLondon and inLondon ? color.new(color.blue, 90) : na, title = "London Session")
bgcolor(i_useLonNY and inLonNY ? color.new(color.purple, 90) : na, title = "Lon/NY Session")
 
// Statistics Dashboard
var table statsTable = table.new(position.top_right, 2, 2, border_width = 1)
if barstate.islastconfirmedhistory
    table.cell(statsTable, 0, 0, "Total Trades", bgcolor = color.gray, text_color = color.white)
    table.cell(statsTable, 1, 0, str.tostring(strategy.closedtrades), bgcolor = color.white, text_color = color.black)
    
    float grossProfit = strategy.grossprofit
    float grossLoss = strategy.grossloss
    float pf = grossLoss > 0 ? grossProfit / grossLoss : grossProfit
    table.cell(statsTable, 0, 1, "Profit Factor", bgcolor = color.gray, text_color = color.white)
    table.cell(statsTable, 1, 1, str.tostring(pf, "#.###"), bgcolor = color.white, text_color = color.black)

← 返回列表