本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("TWP XAUUSD LONDON CPR SNIPER PRO + BIAS", shorttitle="TWP GOLD CPR BIAS", overlay=true, max_lines_count=500, max_labels_count=500)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 01 - LONDON SESSION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
gSession = "01 - London Session"
const string LONDON_TZ = "Europe/London"
openHour = input.int(8, "London Opening Hour", minval=0, maxval=23, group=gSession)
openMinute = input.int(0, "Opening Minute", minval=0, maxval=59, group=gSession)
extendHours = input.int(24, "Line Duration in Hours", minval=1, maxval=48, group=gSession)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 02 - TIMEFRAME SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
gTF = "02 - CPR Timeframes"
use5 = input.bool(true, "5M CPR", group=gTF)
use15 = input.bool(true, "15M CPR", group=gTF)
use30 = input.bool(false, "30M CPR", group=gTF)
use60 = input.bool(false, "60M CPR", group=gTF)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 03 - IMAGINARY MEASUREMENTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
gExt = "03 - Repeated CPR Measurements"
showBase = input.bool(true, "Original CPR", group=gExt)
showU1 = input.bool(true, "Upper 1", group=gExt)
showU2 = input.bool(true, "Upper 2", group=gExt)
showU3 = input.bool(true, "Upper 3", group=gExt)
showU4 = input.bool(true, "Upper 4", group=gExt)
showL1 = input.bool(true, "Lower 1", group=gExt)
showL2 = input.bool(true, "Lower 2", group=gExt)
showL3 = input.bool(true, "Lower 3", group=gExt)
showL4 = input.bool(true, "Lower 4", group=gExt)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 04 - DISPLAY SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
gDisplay = "04 - Display Settings"
showPivot = input.bool(true, "Show Central Pivot", group=gDisplay)
showLabels = input.bool(true, "Show Labels", group=gDisplay)
showHistory = input.bool(false, "Keep Previous Sessions", group=gDisplay)
keepSessions = input.int(2, "Sessions to Keep", minval=1, maxval=4, group=gDisplay)
lineWidth = input.int(2, "Line Width", minval=1, maxval=5, group=gDisplay)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 05 - TIMEFRAME COLORS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
gColors = "05 - CPR Colors"
c5 = input.color(color.aqua, "5M CPR", group=gColors)
c15 = input.color(color.orange, "15M CPR", group=gColors)
c30 = input.color(color.fuchsia, "30M CPR", group=gColors)
c60 = input.color(color.lime, "60M CPR", group=gColors)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 06 - LIVE BIAS SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
gBias = "06 - Live Market Bias"
showBiasBox = input.bool(true, "Show Bias Box", group=gBias)
biasTF = input.string("5M", "Bias Timeframe", options=["5M", "15M", "30M", "60M"], group=gBias)
biasPosition = input.string("Top Right", "Box Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=gBias)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 07 - CONFIRMED LONDON OPEN DATA
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// The opening candle is confirmed before
// its CPR values become available.
f_londonOpen() =>
bool wasOpen = hour(time[1], LONDON_TZ) == openHour and minute(time[1], LONDON_TZ) == openMinute
float h = ta.valuewhen(wasOpen, high[1], 0)
float l = ta.valuewhen(wasOpen, low[1], 0)
float c = ta.valuewhen(wasOpen, close[1], 0)
int t = ta.valuewhen(wasOpen, time[1], 0)
[h, l, c, t]
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 08 - MULTI TIMEFRAME REQUESTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[h5, l5, cl5, t5] = request.security(syminfo.tickerid, "5", f_londonOpen(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
[h15, l15, cl15, t15] = request.security(syminfo.tickerid, "15", f_londonOpen(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
[h30, l30, cl30, t30] = request.security(syminfo.tickerid, "30", f_londonOpen(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
[h60, l60, cl60, t60] = request.security(syminfo.tickerid, "60", f_londonOpen(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 09 - OBJECT STORAGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var array<line> allLines = array.new<line>()
var array<label> allLabels = array.new<label>()
var array<int> lineSessions = array.new<int>()
var array<int> labelSessions = array.new<int>()
var array<int> storedSessions = array.new<int>()
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 10 - CLEAR OBJECTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_clearAll() =>
while array.size(allLines) > 0
line.delete(array.pop(allLines))
while array.size(allLabels) > 0
label.delete(array.pop(allLabels))
array.clear(lineSessions)
array.clear(labelSessions)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 11 - DELETE ONE OLD SESSION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_deleteSession(int oldT) =>
int i = array.size(allLines) - 1
while i >= 0
if array.get(lineSessions, i) == oldT
line.delete(array.remove(allLines, i))
array.remove(lineSessions, i)
i -= 1
int j = array.size(allLabels) - 1
while j >= 0
if array.get(labelSessions, j) == oldT
label.delete(array.remove(allLabels, j))
array.remove(labelSessions, j)
j -= 1
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 12 - DRAW ONE CPR LEVEL
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_level(float price, int startT, int endT, string name, color clr, bool isPivot, int sessionT) =>
line newLine = line.new(startT, price, endT, price, xloc=xloc.bar_time, color=isPivot ? color.new(clr, 35) : clr, width=isPivot ? 1 : lineWidth, style=isPivot ? line.style_dashed : line.style_solid)
array.push(allLines, newLine)
array.push(lineSessions, sessionT)
if showLabels
label newLabel = label.new(endT, price, name, xloc=xloc.bar_time, style=label.style_label_left, color=clr, textcolor=color.black, size=size.tiny)
array.push(allLabels, newLabel)
array.push(labelSessions, sessionT)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 13 - CPR ZONE CALCULATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_zone(float h, float l, float c, int t, int shift, string tf, color clr, int mins) =>
float candleRange = h - l
float shiftedH = h + candleRange * shift
float shiftedL = l + candleRange * shift
float shiftedC = c + candleRange * shift
float pivot = (shiftedH + shiftedL + shiftedC) / 3.0
float rawBC = (shiftedH + shiftedL) / 2.0
float rawTC = 2.0 * pivot - rawBC
float tc = math.max(rawTC, rawBC)
float bc = math.min(rawTC, rawBC)
int startT = t + mins * 60 * 1000
int endT = t + extendHours * 60 * 60 * 1000
string zoneName = shift == 0 ? "BASE" : shift > 0 ? "U" + str.tostring(shift) : "L" + str.tostring(math.abs(shift))
string prefix = tf + " " + zoneName
f_level(tc, startT, endT, prefix + " TC", clr, false, t)
if showPivot
f_level(pivot, startT, endT, prefix + " P", clr, true, t)
f_level(bc, startT, endT, prefix + " BC", clr, false, t)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 14 - DRAW ALL 9 CPR MEASUREMENTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_drawTF(float h, float l, float c, int t, string tf, color clr, int mins) =>
if not na(h) and not na(l) and not na(c) and not na(t) and h > l
for shift = -4 to 4
bool enabled = switch shift
0 => showBase
1 => showU1
2 => showU2
3 => showU3
4 => showU4
-1 => showL1
-2 => showL2
-3 => showL3
-4 => showL4
=> false
if enabled
f_zone(h, l, c, t, shift, tf, clr, mins)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 15 - DETECT NEW CPR DATA
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
new5 = not na(t5) and (na(t5[1]) or t5 != t5[1])
new15 = not na(t15) and (na(t15[1]) or t15 != t15[1])
new30 = not na(t30) and (na(t30[1]) or t30 != t30[1])
new60 = not na(t60) and (na(t60[1]) or t60 != t60[1])
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 16 - SAME LONDON SESSION CHECK
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_sameDay(int t) =>
not na(t) and year(t, LONDON_TZ) == year(time, LONDON_TZ) and month(t, LONDON_TZ) == month(time, LONDON_TZ) and dayofmonth(t, LONDON_TZ) == dayofmonth(time, LONDON_TZ)
draw5 = use5 and new5 and f_sameDay(t5)
draw15 = use15 and new15 and f_sameDay(t15)
draw30 = use30 and new30 and f_sameDay(t30)
draw60 = use60 and new60 and f_sameDay(t60)
bool anyNew = draw5 or draw15 or draw30 or draw60
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 17 - SESSION MANAGEMENT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
int currentSessionT = na
if draw5
currentSessionT := t5
else if draw15
currentSessionT := t15
else if draw30
currentSessionT := t30
else if draw60
currentSessionT := t60
if anyNew and not na(currentSessionT)
bool sessionExists = array.includes(storedSessions, currentSessionT)
if not sessionExists
if not showHistory
f_clearAll()
array.clear(storedSessions)
array.push(storedSessions, currentSessionT)
int maxSessions = showHistory ? keepSessions : 1
while array.size(storedSessions) > maxSessions
int oldestSession = array.shift(storedSessions)
f_deleteSession(oldestSession)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 18 - DRAW EACH TIMEFRAME ONCE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if draw5
f_drawTF(h5, l5, cl5, t5, "5M", c5, 5)
if draw15
f_drawTF(h15, l15, cl15, t15, "15M", c15, 15)
if draw30
f_drawTF(h30, l30, cl30, t30, "30M", c30, 30)
if draw60
f_drawTF(h60, l60, cl60, t60, "60M", c60, 60)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 19 - SELECT BIAS TIMEFRAME
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
float biasH = biasTF == "5M" ? h5 : biasTF == "15M" ? h15 : biasTF == "30M" ? h30 : h60
float biasL = biasTF == "5M" ? l5 : biasTF == "15M" ? l15 : biasTF == "30M" ? l30 : l60
float biasC = biasTF == "5M" ? cl5 : biasTF == "15M" ? cl15 : biasTF == "30M" ? cl30 : cl60
int biasT = biasTF == "5M" ? t5 : biasTF == "15M" ? t15 : biasTF == "30M" ? t30 : t60
int biasMins = biasTF == "5M" ? 5 : biasTF == "15M" ? 15 : biasTF == "30M" ? 30 : 60
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 20 - CALCULATE BIAS CPR
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
float biasPivot = (biasH + biasL + biasC) / 3.0
float biasRawBC = (biasH + biasL) / 2.0
float biasRawTC = 2.0 * biasPivot - biasRawBC
float biasTC = math.max(biasRawTC, biasRawBC)
float biasBC = math.min(biasRawTC, biasRawBC)
// Valid for the selected 24-hour session
bool biasReady = not na(biasT) and not na(biasTC) and not na(biasBC) and biasH > biasL and time >= biasT + biasMins * 60 * 1000 and time < biasT + 24 * 60 * 60 * 1000
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 21 - LIVE MARKET BIAS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
string marketBias = "WAITING"
string priceLocation = "NOT READY"
color marketColor = color.gray
if biasReady
if close > biasTC
marketBias := "BULLISH"
priceLocation := "ABOVE TC"
marketColor := color.rgb(0, 155, 80)
else if close < biasBC
marketBias := "BEARISH"
priceLocation := "BELOW BC"
marketColor := color.rgb(210, 45, 55)
else
marketBias := "SIDEWAYS"
priceLocation := "INSIDE CPR"
marketColor := color.rgb(210, 150, 25)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 22 - STATUS BOX POSITION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
boxPosition = switch biasPosition
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 23 - LIVE STATUS TABLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var table biasTable = table.new(boxPosition, 2, 7, border_width=1, border_color=color.gray)
color darkBG = color.rgb(22, 24, 29)
color lightBG = color.rgb(42, 45, 51)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 24 - UPDATE TABLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if barstate.islast
if showBiasBox
table.cell(biasTable, 0, 0, "TWP GOLD", bgcolor=color.black, text_color=color.white, text_size=size.small)
table.cell(biasTable, 1, 0, "CPR SNIPER", bgcolor=color.black, text_color=color.aqua, text_size=size.small)
table.cell(biasTable, 0, 1, "SESSION", bgcolor=lightBG, text_color=color.white)
table.cell(biasTable, 1, 1, "LONDON 08:00", bgcolor=lightBG, text_color=color.white)
table.cell(biasTable, 0, 2, "BIAS TF", bgcolor=darkBG, text_color=color.white)
table.cell(biasTable, 1, 2, biasTF, bgcolor=darkBG, text_color=color.aqua)
table.cell(biasTable, 0, 3, "MARKET BIAS", bgcolor=lightBG, text_color=color.white)
table.cell(biasTable, 1, 3, marketBias, bgcolor=marketColor, text_color=color.white, text_size=size.normal)
table.cell(biasTable, 0, 4, "LOCATION", bgcolor=darkBG, text_color=color.white)
table.cell(biasTable, 1, 4, priceLocation, bgcolor=darkBG, text_color=color.white)
table.cell(biasTable, 0, 5, "XAUUSD PRICE", bgcolor=lightBG, text_color=color.white)
table.cell(biasTable, 1, 5, str.tostring(close, format.mintick), bgcolor=lightBG, text_color=color.aqua)
string cprText = biasReady ? str.tostring(biasTC, format.mintick) + " / " + str.tostring(biasBC, format.mintick) : "WAIT"
table.cell(biasTable, 0, 6, "TC / BC", bgcolor=darkBG, text_color=color.white)
table.cell(biasTable, 1, 6, cprText, bgcolor=darkBG, text_color=color.white)
else
table.clear(biasTable, 0, 0, 1, 6)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 25 - LIVE BIAS ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bool crossAboveTC = ta.crossover(close, biasTC)
bool crossBelowBC = ta.crossunder(close, biasBC)
bool bullishAlert = biasReady and crossAboveTC
bool bearishAlert = biasReady and crossBelowBC
alertcondition(bullishAlert, "TWP GOLD BULLISH", "XAUUSD crossed above London CPR TC")
alertcondition(bearishAlert, "TWP GOLD BEARISH", "XAUUSD crossed below London CPR BC")