本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// GOLD Londonsession V2 WD
// Marks the Asia, pre-London and London entry sessions on gold and draws entry, stop loss
// and take profit whenever a setup appears. Signals are only made on closed candles.
// Built and tested for XAUUSD on the 5-minute chart. Backtest results are not a guarantee.
// Educational tool, not financial advice.
indicator("GOLD Londonsession V2 WD", overlay = true, max_boxes_count = 500, max_lines_count = 50, max_labels_count = 500)
string RS_A = "London Pullback"
string RS_O = "Original Asia Sweep Reversal"
string ruleSet = input.string(RS_A, "Rule set", options = [RS_A, RS_O], tooltip = "London Pullback trades with the sweep and enters on a pullback. Original trades against the sweep, as taught in the source video.")
bool showSess = input.bool(true, "Show session boxes")
bool showTbl = input.bool(true, "Show info panel")
bool useA = ruleSet == RS_A
string TZ = "Europe/London"
float ATR_MULT = 0.3 // London Pullback stop distance, in 14-day ATR
float RR_A = 1.5 // London Pullback target
float RR_O = 2.0 // Original target
int RETEST = 12 // Pullback order stays valid for 12 five-minute candles (60 minutes)
float BUF = 0.10 // Original stop buffer beyond the swing, in dollars
float ER_LIM = 0.60 // Original: a session counts as trending at this net move / range
color C_ASIA = color.new(#3B82F6, 88)
color C_PRE = color.new(#14B8A6, 88)
color C_WIN = color.new(#F59E0B, 88)
color C_TP = color.new(#16A34A, 80)
color C_SL = color.new(#DC2626, 80)
bool tfOk = timeframe.period == "5"
bool goldOk = str.contains(syminfo.ticker, "XAU")
bool inAsia = not na(time(timeframe.period, "0100-0600", TZ))
bool inPre = not na(time(timeframe.period, "0600-0800", TZ))
bool inWin = not na(time(timeframe.period, "0800-0900", TZ))
bool asiaNew = inAsia and not inAsia[1]
bool preNew = inPre and not inPre[1]
bool winNew = inWin and not inWin[1]
// Prior completed day's 14-day average true range
float dAtr = request.security(syminfo.tickerid, "D", ta.sma(ta.tr(true), 14)[1], lookahead = barmerge.lookahead_on)
// ---- Session state, reset each day when Asia opens
var float aH = na
var float aL = na
var float aO = na
var float aC = na
var float pH = na
var float pL = na
var float pO = na
var float pC = na
var int hiIdx = -1
var int loIdx = -1
var bool dayDone = false
var box bxA = na
var box bxP = na
var box bxW = na
if asiaNew
aH := high
aL := low
aO := open
pH := na
pL := na
hiIdx := -1
loIdx := -1
dayDone := false
if showSess
bxA := box.new(bar_index, high, bar_index, low, border_color = color.new(#3B82F6, 40), bgcolor = C_ASIA, text = "Asia", text_color = color.new(#3B82F6, 0), text_size = size.small, text_halign = text.align_left, text_valign = text.align_top)
if inAsia
aH := math.max(aH, high)
aL := math.min(aL, low)
aC := close
if showSess and not na(bxA)
box.set_right(bxA, bar_index)
box.set_top(bxA, aH)
box.set_bottom(bxA, aL)
if inPre and not na(aH)
if preNew
pH := high
pL := low
pO := open
if showSess
bxP := box.new(bar_index, high, bar_index, low, border_color = color.new(#14B8A6, 40), bgcolor = C_PRE, text = "Pre-London", text_color = color.new(#14B8A6, 0), text_size = size.small, text_halign = text.align_left, text_valign = text.align_top)
pH := math.max(pH, high)
pL := math.min(pL, low)
pC := close
if high > aH
hiIdx := bar_index
if low < aL
loIdx := bar_index
if showSess and not na(bxP)
box.set_right(bxP, bar_index)
box.set_top(bxP, pH)
box.set_bottom(bxP, pL)
if inWin and showSess
if winNew
bxW := box.new(bar_index, high, bar_index, low, border_color = color.new(#F59E0B, 40), bgcolor = C_WIN, text = "Entry window", text_color = color.new(#F59E0B, 0), text_size = size.small, text_halign = text.align_left, text_valign = text.align_top)
else if not na(bxW)
box.set_right(bxW, bar_index)
box.set_top(bxW, math.max(box.get_top(bxW), high))
box.set_bottom(bxW, math.min(box.get_bottom(bxW), low))
int sweepDir = hiIdx < 0 and loIdx < 0 ? 0 : loIdx > hiIdx ? 1 : -1 // 1 = latest sweep took the low, -1 = took the high
int bias = useA ? -sweepDir : sweepDir
// ---- Trade state
var int state = 0 // 0 flat, 1 pullback order waiting, 2 market entry on next open, 3 in trade
var int dir = 0
var int trigBar = 0
var int fillBar = 0
var float ent = na
var float sl = na
var float tp = na
var float swingRef = na
var box tpBox = na
var box slBox = na
var label entLbl = na
var label tpLbl = na
var label slLbl = na
var string lastWhen = "None yet"
var string lastRes = "-"
bool drawNow = false
string tick = syminfo.ticker
fmt(float v) => str.tostring(v, format.mintick)
sideTxt(int d) => d == 1 ? "BUY" : "SELL"
// Market entry for the Original rule set happens at this candle's open
if state == 2
ent := open
sl := swingRef - dir * BUF
float risk0 = dir * (ent - sl)
if risk0 < math.max(1.0, 0.0004 * ent) or risk0 > 0.6 * dAtr
state := 0
lastRes := "Skipped, stop size out of range"
else
tp := ent + dir * RR_O * risk0
state := 3
fillBar := bar_index - 1 // target may be hit on the entry candle at market
drawNow := true
lastRes := "In trade"
alert("GOLD Londonsession V2 WD | " + tick + " | " + sideTxt(dir) + " at " + fmt(ent) + " | SL " + fmt(sl) + " | TP " + fmt(tp), alert.freq_once_per_bar)
// Pullback order: fill or expire
if state == 1
if bar_index > trigBar + RETEST
state := 0
lastRes := "Not filled, no trade"
label.new(bar_index, ent, "Not filled, no trade", style = label.style_label_left, color = color.new(color.gray, 20), textcolor = color.white, size = size.small)
else if (dir == 1 and low <= ent) or (dir == -1 and high >= ent)
state := 3
fillBar := bar_index
lastRes := "In trade"
alert("GOLD Londonsession V2 WD | " + tick + " | " + sideTxt(dir) + " order filled at " + fmt(ent) + " | SL " + fmt(sl) + " | TP " + fmt(tp), alert.freq_once_per_bar)
// Open trade: stop is checked first when both levels fall inside one candle
if state == 3
bool hitS = dir == 1 ? low <= sl : high >= sl
bool hitT = bar_index > fillBar and (dir == 1 ? high >= tp : low <= tp)
if hitS or hitT
string res = hitS ? "SL hit" : "TP hit"
lastRes := res
label.new(bar_index, hitS ? sl : tp, res, style = dir == 1 ? (hitS ? label.style_label_up : label.style_label_down) : (hitS ? label.style_label_down : label.style_label_up), color = hitS ? #DC2626 : #16A34A, textcolor = color.white, size = size.small)
alert("GOLD Londonsession V2 WD | " + tick + " | " + res + " at " + fmt(hitS ? sl : tp), alert.freq_once_per_bar)
box.set_right(tpBox, bar_index)
box.set_right(slBox, bar_index)
state := 0
// ---- Swings: confirmed 2 candles later, so the swing used below was already known
var float lastSH = na
var float lastSL = na
// ---- Break of structure in the first hour of London, on the closed candle
if tfOk and inWin and not dayDone and bias != 0 and not na(lastSH) and not na(lastSL) and barstate.isconfirmed
bool up = close > lastSH and close[1] <= lastSH
bool dn = close < lastSL and close[1] >= lastSL
if (up and bias == 1) or (dn and bias == -1)
dayDone := true
bool trending = not useA and math.abs(aC - aO) / math.max(aH - aL, 1e-9) >= ER_LIM and math.abs(pC - pO) / math.max(pH - pL, 1e-9) >= ER_LIM
if state == 0 and not trending
dir := bias
trigBar := bar_index
lastWhen := str.format_time(time, "dd MMM yyyy HH:mm", TZ) + " UK"
lastRes := useA ? "Limit order waiting" : "Entering at next open"
label.new(bar_index, dir == 1 ? low : high, dir == 1 ? "LONG setup" : "SHORT setup", style = dir == 1 ? label.style_label_up : label.style_label_down, color = dir == 1 ? #16A34A : #DC2626, textcolor = color.white, size = size.normal)
if useA
ent := dir == 1 ? lastSH : lastSL
float risk1 = ATR_MULT * dAtr
sl := ent - dir * risk1
tp := ent + dir * RR_A * risk1
if not na(dAtr) and risk1 >= math.max(1.0, 0.0004 * ent)
state := 1
drawNow := true
alert("GOLD Londonsession V2 WD | " + tick + " | " + sideTxt(dir) + " LIMIT at " + fmt(ent) + " | SL " + fmt(sl) + " | TP " + fmt(tp) + " | valid 60 minutes", alert.freq_once_per_bar_close)
else
swingRef := dir == 1 ? lastSL : lastSH
state := 2
// ---- Draw entry, stop and target
if drawNow
tpBox := box.new(bar_index, math.max(ent, tp), bar_index + 1, math.min(ent, tp), border_color = color.new(#16A34A, 50), bgcolor = C_TP)
slBox := box.new(bar_index, math.max(ent, sl), bar_index + 1, math.min(ent, sl), border_color = color.new(#DC2626, 50), bgcolor = C_SL)
string kind = state == 1 ? " LIMIT " : " "
entLbl := label.new(bar_index + 1, ent, "Entry: " + sideTxt(dir) + kind + fmt(ent), style = label.style_label_left, color = #1F2937, textcolor = color.white, size = size.normal)
tpLbl := label.new(bar_index + 1, tp, "Take profit " + fmt(tp), style = label.style_label_left, color = #16A34A, textcolor = color.white, size = size.normal)
slLbl := label.new(bar_index + 1, sl, "Stop loss " + fmt(sl), style = label.style_label_left, color = #DC2626, textcolor = color.white, size = size.normal)
if state == 1 or state == 3
box.set_right(tpBox, bar_index + 1)
box.set_right(slBox, bar_index + 1)
label.set_x(entLbl, bar_index + 1)
label.set_x(tpLbl, bar_index + 1)
label.set_x(slLbl, bar_index + 1)
// Update swings last, so the current candle cannot use a swing it confirms itself
if high[2] > math.max(high[3], high[4]) and high[2] >= math.max(high[1], high)
lastSH := high[2]
if low[2] < math.min(low[3], low[4]) and low[2] <= math.min(low[1], low)
lastSL := low[2]
// ---- Info panel
var table tb = table.new(position.top_right, 2, 12, bgcolor = color.new(#111827, 8), border_color = color.new(#6B7280, 60), border_width = 1)
row(int r, string a, string b, color c) =>
table.cell(tb, 0, r, a, text_color = color.new(#D1D5DB, 0), text_size = size.small, text_halign = text.align_left)
table.cell(tb, 1, r, b, text_color = c, text_size = size.small, text_halign = text.align_left)
if barstate.islast and showTbl
bool before = inAsia or inPre
string stTxt = state == 1 ? "Limit order waiting" : state == 2 ? "Entering at next open" : state == 3 ? "In trade" : dayDone ? "Today's setup is finished" : before ? "Waiting for London, 08:00 UK" : inWin ? (bias == 0 ? "No sweep, no trade today" : "Watching for a break") : bias == 0 ? "No sweep, no trade today" : "No break, no trade today"
string biasTxt = bias == 1 ? "Buys only" : bias == -1 ? "Sells only" : "No sweep yet"
bool has = not na(ent) and lastWhen != "None yet"
row(0, "GOLD Londonsession V2 WD", tfOk and goldOk ? ruleSet : not tfOk ? "SWITCH TO 5-MINUTE CHART" : "MADE FOR XAUUSD", tfOk and goldOk ? color.white : color.orange)
row(1, "Timeframe", "5-minute only", color.white)
row(2, "Today's direction", biasTxt, color.white)
row(3, "Today's status", stTxt, color.white)
row(4, "Last signal", lastWhen, color.white)
row(5, "Side", has ? (dir == 1 ? "LONG (BUY)" : "SHORT (SELL)") : "-", dir == 1 ? #4ADE80 : #F87171)
row(6, "Entry", has ? fmt(ent) : "-", color.white)
row(7, "Take profit", has ? fmt(tp) : "-", #4ADE80)
row(8, "Stop loss", has ? fmt(sl) : "-", #F87171)
row(9, "Result", lastRes, color.white)
row(10, "Backtest Nov 23 to Oct 26", useA ? "48% wins, PF 1.39" : "27% wins, PF 0.72", color.white)
row(11, "Backtest Feb 21 to Dec 23", useA ? "39% wins, PF 0.89" : "31% wins, PF 0.84", color.orange)