本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Daily Fixed Volume Profile [DAVIES T]", "Daily FVP", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// ---------------- Inputs ----------------
string G1 = "Profile"
string dayMode = input.string("Exchange", "Day Start", options = ["Exchange", "NY 00:00", "NY 18:00"], group = G1, tooltip = "Exchange = chart's own daily candle (KuCoin = 00:00 UTC). NY 00:00 = ICT midnight day. NY 18:00 = futures/forex trading day. NY modes are intraday only.")
int daysBack = input.int(5, "Completed Days", minval = 0, maxval = 20, group = G1)
bool showCur = input.bool(true, "Show Developing Day", group = G1)
int rowsIn = input.int(30, "Rows", minval = 5, maxval = 100, group = G1, tooltip = "Auto-capped so all days fit inside the 500 box limit.")
float vaPct = input.float(70, "Value Area %", minval = 10, maxval = 95, group = G1) / 100
float widthPct = input.float(35, "Width % of Day", minval = 5, maxval = 100, group = G1) / 100
string side = input.string("Left", "Anchor", options = ["Left", "Right"], group = G1)
bool splitUD = input.bool(true, "Split Up/Down Volume", group = G1)
string G2 = "Levels"
bool showPOC = input.bool(true, "POC Line", group = G2)
int pocW = input.int(2, "POC Width", minval = 1, maxval = 5, group = G2)
bool showVA = input.bool(true, "VAH / VAL Lines", group = G2)
bool showLbl = input.bool(true, "Price Labels", group = G2)
bool showRng = input.bool(false, "Day Range Box", group = G2)
bool nakedPOC = input.bool(true, "Naked POC (extend until touched)", group = G2)
int maxNaked = input.int(10, "Max Naked POCs", minval = 1, maxval = 30, group = G2)
string G3 = "Colours"
color cUp = input.color(color.new(#26a69a, 55), "Up Volume", group = G3)
color cDn = input.color(color.new(#ef5350, 55), "Down Volume", group = G3)
color cVol = input.color(color.new(#787b86, 55), "Volume (no split)", group = G3)
color cPOC = input.color(#ffeb3b, "POC", group = G3)
color cVA = input.color(#42a5f5, "VAH / VAL", group = G3)
color cRng = input.color(#9598a1, "Range Box", group = G3)
int outFade = input.int(30, "Outside VA Extra Transparency", minval = 0, maxval = 100, group = G3)
string G4 = "Previous Day"
bool showPDO = input.bool(true, "Yesterday Open (PDO)", group = G4)
bool showPDH = input.bool(true, "Previous Day High (PDH)", group = G4)
bool showPDL = input.bool(true, "Previous Day Low (PDL)", group = G4)
color cPDO = input.color(#ff9800, "PDO", group = G4)
color cPDH = input.color(#00bcd4, "PDH", group = G4)
color cPDL = input.color(#e040fb, "PDL", group = G4)
string pdStyIn = input.string("Dashed", "Line Style", options = ["Solid", "Dashed", "Dotted"], group = G4)
int pdW = input.int(1, "Line Width", minval = 1, maxval = 4, group = G4)
int pdExt = input.int(10, "Extend Right (bars)", minval = 0, maxval = 200, group = G4)
bool pdLbl = input.bool(true, "PD Labels", group = G4)
string pdSty = pdStyIn == "Solid" ? line.style_solid : pdStyIn == "Dotted" ? line.style_dotted : line.style_dashed
// ---------------- Constants ----------------
string NY = "America/New_York"
int barMs = timeframe.in_seconds() * 1000
int DAY_MS = 86400000
int effRows = math.max(5, math.min(rowsIn, int(450 / ((daysBack + 1) * (splitUD ? 2 : 1)))))
// ---------------- Types / helpers ----------------
type Prof
array<box> bx
array<line> ln
array<label> lb
float poc = na
method wipe(Prof p) =>
for b in p.bx
b.delete()
for l in p.ln
l.delete()
for x in p.lb
x.delete()
fade(color c) =>
color.new(c, math.min(100, color.t(c) + outFade))
mkLbl(int x, float y, string t, color c) =>
label.new(x, y, t + " " + str.tostring(y, format.mintick), xloc = xloc.bar_time, style = label.style_label_left, color = color(na), textcolor = c, size = size.small)
// ---------------- Bar storage for the current day ----------------
var array<float> aH = array.new<float>()
var array<float> aL = array.new<float>()
var array<float> aV = array.new<float>()
var array<bool> aU = array.new<bool>()
buildProfile(int tStart, int tEnd) =>
Prof p = Prof.new(array.new<box>(), array.new<line>(), array.new<label>())
int n = aV.size()
if n > 0
float hi = aH.max()
float lo = aL.min()
float step = (hi - lo) / effRows
if step > 0
array<float> up = array.new<float>(effRows, 0.0)
array<float> dn = array.new<float>(effRows, 0.0)
// distribute each bar's volume across the rows its range covers
for i = 0 to n - 1
float h = aH.get(i)
float l = aL.get(i)
float v = aV.get(i)
bool isU = aU.get(i)
int r0 = math.max(0, math.min(effRows - 1, int(math.floor((l - lo) / step))))
int r1 = math.max(0, math.min(effRows - 1, int(math.floor((h - lo) / step))))
float rng = h - l
for r = r0 to r1
float rLo = lo + r * step
float rHi = rLo + step
float share = rng > 0 ? (math.min(h, rHi) - math.max(l, rLo)) / rng : 1.0 / (r1 - r0 + 1)
if share > 0
if isU
up.set(r, up.get(r) + v * share)
else
dn.set(r, dn.get(r) + v * share)
array<float> tot = array.new<float>(effRows, 0.0)
float sumV = 0.0
for r = 0 to effRows - 1
tot.set(r, up.get(r) + dn.get(r))
sumV += tot.get(r)
float maxV = tot.max()
if maxV > 0
// value area: expand out from POC row
int pocR = tot.indexof(maxV)
int vaLoR = pocR
int vaHiR = pocR
float vaV = tot.get(pocR)
while vaV < sumV * vaPct and (vaLoR > 0 or vaHiR < effRows - 1)
float nUp = vaHiR < effRows - 1 ? tot.get(vaHiR + 1) : -1.0
float nDn = vaLoR > 0 ? tot.get(vaLoR - 1) : -1.0
if nUp >= nDn
vaHiR += 1
vaV += nUp
else
vaLoR -= 1
vaV += nDn
int tE = math.max(tEnd, tStart + barMs)
int maxW = int(DAY_MS * widthPct)
float pocPx = lo + (pocR + 0.5) * step
float vah = lo + (vaHiR + 1) * step
float val = lo + vaLoR * step
for r = 0 to effRows - 1
float t = tot.get(r)
if t > 0
bool inVA = r >= vaLoR and r <= vaHiR
int w = int(maxW * t / maxV)
int wA = splitUD ? int(w * up.get(r) / t) : w
int x0 = side == "Left" ? tStart : tE - w
float yB = lo + r * step
float yT = yB + step
color cA = splitUD ? cUp : cVol
color cB = cDn
if not inVA
cA := fade(cA)
cB := fade(cB)
if wA > 0
p.bx.push(box.new(x0, yT, x0 + wA, yB, border_color = color(na), border_width = 0, xloc = xloc.bar_time, bgcolor = cA))
if splitUD and w - wA > 0
p.bx.push(box.new(x0 + wA, yT, x0 + w, yB, border_color = color(na), border_width = 0, xloc = xloc.bar_time, bgcolor = cB))
if showRng
p.bx.push(box.new(tStart, hi, tE, lo, border_color = cRng, border_width = 1, border_style = line.style_dotted, xloc = xloc.bar_time, bgcolor = color.new(cRng, 96)))
if showVA
p.ln.push(line.new(tStart, vah, tE, vah, xloc = xloc.bar_time, color = cVA, style = line.style_dashed, width = 1))
p.ln.push(line.new(tStart, val, tE, val, xloc = xloc.bar_time, color = cVA, style = line.style_dashed, width = 1))
if showPOC
p.ln.push(line.new(tStart, pocPx, tE, pocPx, xloc = xloc.bar_time, color = cPOC, width = pocW))
if showLbl
if showPOC
p.lb.push(mkLbl(tE, pocPx, "dPOC", cPOC))
if showVA
p.lb.push(mkLbl(tE, vah, "dVAH", cVA))
p.lb.push(mkLbl(tE, val, "dVAL", cVA))
p.poc := pocPx
p
// ---------------- Day detection ----------------
// NY modes: shift time so the chosen start hour becomes midnight, then watch the NY calendar day change
bool chgD = timeframe.change("D")
int shift = dayMode == "NY 18:00" ? 6 * 3600000 : 0
int dayKey = dayofmonth(time + shift, NY)
bool useNY = dayMode != "Exchange" and timeframe.isintraday
bool newDay = useNY ? dayKey != dayKey[1] : chgD
// ---------------- State ----------------
var array<Prof> hist = array.new<Prof>()
var Prof cur = na
var array<line> nkL = array.new<line>()
var array<float> nkP = array.new<float>()
var int dStart = na
var int dEnd = na
// previous / current day OHLC (uses the same Day Start as the profile)
var float cdO = na
var float cdH = na
var float cdL = na
var float pdO = na
var float pdH = na
var float pdL = na
var line lPDO = na
var line lPDH = na
var line lPDL = na
var label bPDO = na
var label bPDH = na
var label bPDL = na
var bool pdhTaken = false
var bool pdlTaken = false
pdLine(float y, color c) =>
line.new(time, y, time + barMs, y, xloc = xloc.bar_time, color = c, style = pdSty, width = pdW)
pdLabel(float y, string t, color c) =>
label.new(time, y, t + " " + str.tostring(y, format.mintick), xloc = xloc.bar_time, style = label.style_label_left, color = color(na), textcolor = c, size = size.small)
bool inWindow = time >= timenow - (daysBack + 2) * DAY_MS
if newDay
if not na(cur)
cur.wipe()
cur := na
if daysBack > 0 and aV.size() > 0 and not na(dStart) and inWindow
Prof done = buildProfile(dStart, dEnd)
hist.push(done)
if hist.size() > daysBack
Prof old = hist.shift()
old.wipe()
if nakedPOC and not na(done.poc)
nkL.push(line.new(dEnd, done.poc, dEnd + barMs, done.poc, xloc = xloc.bar_time, color = cPOC, style = line.style_dotted, width = 1))
nkP.push(done.poc)
if nkL.size() > maxNaked
nkL.shift().delete()
nkP.shift()
// roll today's OHLC into "previous day" and redraw PD levels
if not na(cdO)
pdO := cdO
pdH := cdH
pdL := cdL
pdhTaken := false
pdlTaken := false
line.delete(lPDO)
line.delete(lPDH)
line.delete(lPDL)
label.delete(bPDO)
label.delete(bPDH)
label.delete(bPDL)
lPDO := showPDO ? pdLine(pdO, cPDO) : na
lPDH := showPDH ? pdLine(pdH, cPDH) : na
lPDL := showPDL ? pdLine(pdL, cPDL) : na
bPDO := showPDO and pdLbl ? pdLabel(pdO, "PDO", cPDO) : na
bPDH := showPDH and pdLbl ? pdLabel(pdH, "PDH", cPDH) : na
bPDL := showPDL and pdLbl ? pdLabel(pdL, "PDL", cPDL) : na
cdO := open
cdH := high
cdL := low
aH.clear()
aL.clear()
aV.clear()
aU.clear()
dStart := time
aH.push(high)
aL.push(low)
aV.push(nz(volume))
aU.push(close >= open)
dEnd := time + barMs
// track today's high/low and keep PD lines running to the right
cdH := math.max(nz(cdH, high), high)
cdL := math.min(nz(cdL, low), low)
int pdX2 = time + barMs * (1 + pdExt)
line.set_x2(lPDO, pdX2)
line.set_x2(lPDH, pdX2)
line.set_x2(lPDL, pdX2)
label.set_x(bPDO, pdX2)
label.set_x(bPDH, pdX2)
label.set_x(bPDL, pdX2)
// first break of PDH / PDL each day
bool pdhBreak = not na(pdH) and not pdhTaken and high > pdH
bool pdlBreak = not na(pdL) and not pdlTaken and low < pdL
if pdhBreak
pdhTaken := true
if pdlBreak
pdlTaken := true
// ---------------- Naked POC tracking ----------------
bool nkHit = false
if nkL.size() > 0
for i = nkL.size() - 1 to 0
float px = nkP.get(i)
if high >= px and low <= px
nkL.get(i).delete()
nkL.remove(i)
nkP.remove(i)
nkHit := true
else
nkL.get(i).set_x2(time + barMs)
// ---------------- Developing day ----------------
if barstate.islast and showCur and not na(dStart) and aV.size() > 0
if not na(cur)
cur.wipe()
cur := buildProfile(dStart, dEnd)
alertcondition(nkHit, "Naked daily POC touched", "Daily FVP: price traded into a naked daily POC")
alertcondition(pdhBreak, "PDH taken", "Daily FVP: price traded above the previous day high")
alertcondition(pdlBreak, "PDL taken", "Daily FVP: price traded below the previous day low")