Compilação CVD + Order Book
Compilação CVD + Order Book
Compilação CVD + Order Book
// ————— Constants
// Default colors
color LIME = color.lime
color PINK = color.fuchsia
color WHITE = color.white
color ORANGE = color.orange
color GRAY = #808080ff
color LIME_LINE = color.new(LIME, 30)
color LIME_BG = color.new(LIME, 95)
color LIME_HI = color.new(LIME, 80)
color PINK_LINE = color.new(PINK, 30)
color PINK_BG = color.new(PINK, 95)
color PINK_LO = color.new(PINK, 80)
color BG_DIV = color.new(ORANGE, 90)
color BG_RESETS = color.new(GRAY, 90)
// Reset conditions
string RST1 = "None"
string RST2 = "On a stepped higher timeframe"
string RST3 = "On a fixed higher timeframe..."
string RST4 = "At a fixed time..."
string RST5 = "At the beginning of the regular session"
string RST6 = "At the first visible chart bar"
string RST7 = "On trend changes..."
// Trends
string TR01 = "Supertrend"
string TR02 = "Aroon"
string TR03 = "Parabolic SAR"
// Label Size
string LS01 = "tiny"
string LS02 = "small"
string LS03 = "normal"
string LS04 = "large"
string LS05 = "huge"
// LTF distinction
string LTF1 = "Least Precise (Most chart bars)"
string LTF2 = "Less Precise (Some chart bars)"
string LTF3 = "More Precise (Less chart bars)"
string LTF4 = "Very precise (2min intrabars)"
string LTF5 = "Most precise (1min intrabars)"
string LTF6 = "~12 intrabars per chart bar"
string LTF7 = "~24 intrabars per chart bar"
string LTF8 = "~50 intrabars per chart bar"
string LTF9 = "~100 intrabars per chart bar"
string LTF10 = "~250 intrabars per chart bar"
// Tooltips
string TT_RST = "This is where you specify how you want the
cumulative volume delta to reset.
If you select one of the last three choices, you must also specify the
relevant additional information below."
string TT_RST_HTF = "This value only matters when '" + RST3 +"' is
selected."
string TT_RST_TIME = "Hour: 0-23\nMinute: 0-59\nThese values only matter
when '" + RST4 +"' is selected.
A reset will occur when the time is greater or equal to the bar's open
time, and less than its close time."
string TT_RST_TREND = "These values only matter when '" + RST7 +"' is
selected.\n
For Supertrend, the first value is the length of ATR, the second is the
multiplier. For Aroon, the first value is the lookback length."
string TT_LINE = "Select the style and color of CVD lines to display."
string TT_LTF = "Your selection here controls how many intrabars will
be analyzed for each chart bar.
The more intrabars you analyze, the less chart bars will be covered by
the indicator's calculations
because a maximum of 100K intrabars can be analyzed.\n\n
With the first five choices, the quantity of intrabars is determined from
the type of chart bar coverage you want.
The last five choices allow you to select approximately how many
intrabars you want analyzed per chart bar."
string TT_CVD = "Shows a cumulative volume delta value summed from
the reset point. Disabling this will show the raw volume delta for each
bar."
string TT_YSPCE = "Scales the height of all oscillator zones using a
percentage of the largest zone's y-range."
// ————— Inputs
// Calculate the maximum volumes, total volume, and volume delta, and
assign the result to the `barDelta` variable.
float totalUpVolume = nz(array.sum(upVolumes))
float totalDnVolume = nz(array.sum(dnVolumes))
float maxUpVolume = nz(array.max(upVolumes))
float maxDnVolume = nz(array.min(dnVolumes))
float totalVolume = totalUpVolume - totalDnVolume
float delta = totalUpVolume + totalDnVolume
float deltaPct = delta / totalVolume
float barDelta = useVdPct ? deltaPct : delta
// Get the qty of intrabars per chart bar and the average.
int intrabars = array.size(upVolumes)
int chartBarsCovered = int(ta.cum(math.sign(intrabars)))
float avgIntrabars = ta.cum(intrabars) / chartBarsCovered
// Store values for the visible segment on the reset bar, or last bar.
if reset or isLastBar
if isVisible
array.push(segCvdValues, cumulativeInput ? cvd + barDelta :
barDelta)
array.push(resetBars, bar_index)
array.push(zeroLevels, math.avg(priceHi, priceLo) - (priceHi -
priceLo))
array.push(priceRanges, (priceHi - priceLo) / 2)
array.push(highCvd, array.max(segCvdValues))
array.push(lowCvd, array.min(segCvdValues))
array.push(barCenters, hl2)
array.concat(rawCvdValues, segCvdValues)
array.clear(segCvdValues)
priceHi := high
priceLo := low
cvd := reset ? 0 : cvd
// Cumulate CVD.
float cvdO = cvd
cvd += barDelta
// Display information box only once on the last historical bar, instead of
on all realtime updates, as when `barstate.islast` is used.
if showInfoBoxInput and barstate.islastconfirmedhistory
var table infoBox = table.new(infoBoxYPosInput + "_" +
infoBoxXPosInput, 1, 1)
color infoBoxBgColor = infoBoxColorInput
string txt = str.format(
"{0}\nUses intrabars at {1}\nAvg intrabars per chart bar:
{2,number,#.##}\nChart bars covered: {3} / {4} ({5,number,percent})",
resetDescription,
PCtime.formattedNoOfPeriods(timeframe.in_seconds(ltfString) * 1000),
avgIntrabars, chartBarsCovered, bar_index + 1, chartBarsCovered /
(bar_index + 1))
if avgIntrabars < 5
txt += "\nThis quantity of intrabars is dangerously small.\nResults
will not be as reliable with so few."
infoBoxBgColor := color.red
table.cell(infoBox, 0, 0, txt, text_color = infoBoxTxtColorInput,
text_size = infoBoxSizeInput, bgcolor = infoBoxBgColor)
//#endregion
//#region ———————————————————— Errors
//-------------------------------------------------------------------------
-----
//Settings
//-------------------------------------------------------------------------
----{
length = input.int(5, 'Volume Pivot Length'
, minval = 1)
//-------------------------------------------------------------------------
----}
//Functions
//-------------------------------------------------------------------------
----{
//Line Style function
get_line_style(style) =>
out = switch style
'⎯⎯⎯' => line.style_solid
'----' => line.style_dashed
'····' => line.style_dotted
float ob = na
array.unshift(ob_top, top)
array.unshift(ob_btm, btm)
array.unshift(ob_avg, avg)
array.unshift(ob_left, time[length])
ob := ob_val
array.remove(ob_top, idx)
array.remove(ob_btm, idx)
array.remove(ob_avg, idx)
array.remove(ob_left, idx)
mitigated
array.unshift(ob_lvl, line.new(na,na,na,na
, xloc = xloc.bar_time
, extend = extend.right
, color = lvl_css
, style = get_line_style(line_style)
, width = line_width))
//-------------------------------------------------------------------------
----}
//Global elements
//-------------------------------------------------------------------------
----{
var os = 0
var target_bull = 0.
var target_bear = 0.
n = bar_index
upper = ta.highest(length)
lower = ta.lowest(length)
if mitigation == 'Close'
target_bull := ta.lowest(close, length)
target_bear := ta.highest(close, length)
else
target_bull := lower
target_bear := upper
[bear_top
, bear_btm
, bear_avg
, bear_left
, bear_ob] = get_coordinates(phv and os == 0, high[length], hl2[length],
high[length])
//-------------------------------------------------------------------------
----}
//Remove mitigated order blocks
//-------------------------------------------------------------------------
----{
mitigated_bull = remove_mitigated(bull_top
, bull_btm
, bull_left
, bull_avg
, target_bull
, true)
mitigated_bear = remove_mitigated(bear_top
, bear_btm
, bear_left
, bear_avg
, target_bear
, false)
//-------------------------------------------------------------------------
----}
//Display order blocks
//-------------------------------------------------------------------------
----{
//Set bullish order blocks
set_order_blocks(bull_top
, bull_btm
, bull_left
, bull_avg
, bull_ext_last
, bg_bull_css
, bull_css
, bull_avg_css)
//-------------------------------------------------------------------------
----}
//Alerts
//-------------------------------------------------------------------------
----{
alertcondition(bull_ob, 'Bullish OB Formed', 'Bullish order block
detected')