Dem Sup

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

indicator('ACC PUMP', shorttitle='ACC PUMP', overlay=true, max_bars_back=4999,

max_lines_count=500, max_labels_count=10)
import PineCoders/Time/2 as timeLib
import PineCoders/lower_tf/3 as PCltf
import TradingView/ta/4 as TVta

// ———————————————————— Constants and Inputs {

// ————— Constants

int MS_IN_MIN = 60 * 1000


int MS_IN_HOUR = MS_IN_MIN * 60
int MS_IN_DAY = MS_IN_HOUR * 24

// Default colors
color GRAY = #808080ff
color LIME = #00FF00ff
color MAROON = #800000ff
color ORANGE = #FF8000ff
color PINK = #FF0080ff
color TEAL = #008080ff
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"

// Volume Delta calculation mode


string VD01 = "Volume Delta"
string VD02 = "Volume Delta Percent"

// Realtime calculation modes


string RT1 = "Intrabars (same as on historical bars)"
string RT2 = "Chart updates"

// Intrabar precisions
string LTF1 = "Covering most chart bars (least precise)"
string LTF2 = "Covering some chart bars (less precise)"
string LTF3 = "Covering less chart bars (more precise)"
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 factor.
For Aroon, the first value is the lookback length."
string TT_TOTVOL = "Total volume can only be displayed when '" + VD01 +"' is
selected as the Volume Delta Calculation mode.\n\n
The 'Bodies' value is the transparency of the total volume candle bodies. Zero is
opaque, 100 is transparent."
string TT_LINE = "This plots a line at the `close` values of the CVD candles.
You can use it instead of the CVD candles."
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 four choices the quantity of intrabars is determined from the type
of chart bar coverage you want.
The last four choices allow you to select approximately how many intrabars you
want analyzed per chart bar."
string TT_MA = "This plots the running average of CVD from its last reset.
The 'Length' period only applies when CVD does not reset, i.e., the reset is
'None'."

// ————— Inputs

string resetInput = input.string(RST1, "CVD Resets",


inline = "00", options = [RST1, RST2, RST5, RST6, RST3, RST4, RST7], tooltip =
TT_RST)
string fixedTfInput = input.timeframe("D", "    Fixed higher
timeframe:", tooltip = TT_RST_HTF)
int hourInput = input.int(9, "    Fixed time:  Hour",
inline = "01", minval = 0, maxval = 23)
int minuteInput = input.int(30, "Minute",
inline = "01", minval = 0, maxval = 59, tooltip = TT_RST_TIME)
string ltfModeInput = input.string(LTF10, "Intrabar precision",
inline = "03", options = [LTF1, LTF2, LTF3, LTF4, LTF5, LTF6, LTF7, LTF8, LTF9,
LTF10], tooltip = TT_LTF)
int trendPeriodInput = 14
float trendValue2Input= 3
string trendInput = TR01

// ———————————————————— Functions {

// @function Determines if the volume for an intrabar is up or down.


// @returns ([float, float]) A tuple of two values, one of which contains the
bar's volume. `upVol` is the volume of up bars. `dnVol` is the volume of down bars.
// Note that when this function is called with
`request.security_lower_tf()` a tuple of float[] arrays will be returned by
`request.security_lower_tf()`.
upDnIntrabarVolumes() =>
float upVol = 0.0
float dnVol = 0.0
switch
// Bar polarity can be determined.
close > open => upVol += volume
close < open => dnVol -= volume
// If not, use price movement since last bar.
close > nz(close[1]) => upVol += volume
close < nz(close[1]) => dnVol -= volume
// If not, use previously known polarity.
nz(upVol[1]) > 0 => upVol += volume
nz(dnVol[1]) < 0 => dnVol -= volume
[upVol, dnVol]

// @function Selects a HTF from the chart's TF.


// @returns (simple string) A timeframe string.
htfStep() =>
int tfInMs = timeframe.in_seconds() * 1000
string result =
switch
tfInMs < MS_IN_MIN => "60"
tfInMs < MS_IN_HOUR * 3 => "D"
tfInMs <= MS_IN_HOUR * 12 => "W"
tfInMs < MS_IN_DAY * 7 => "M"
=> "12M"

// @function Determines when a bar opens at a given time.


// @param hours (series int) "Hour" part of the time we are looking for.
// @param minutes (series int) "Minute" part of the time we are looking for.
// @returns (series bool) `true` when the bar opens at `hours`:`minutes`,
false otherwise.
timeReset(int hours, int minutes) =>
int openTime = timestamp(year, month, dayofmonth, hours, minutes, 0)
bool timeInBar = time <= openTime and time_close > openTime
bool result = timeframe.isintraday and not timeInBar[1] and timeInBar
// }

// ———————————————————— Calculations {

// Lower timeframe (LTF) used to mine intrabars.


var string ltfString = PCltf.ltf(ltfModeInput, LTF1, LTF2, LTF3, LTF4, LTF5, LTF6,
LTF7, LTF8, LTF9, LTF10)

// Get two arrays, one each for up and dn volumes.


[upVolumes, dnVolumes] = request.security_lower_tf(syminfo.tickerid, ltfString,
upDnIntrabarVolumes())

// ————— Select between historical and realtime DV calcs. In rt, use user
selection.
float totalUpVolume = nz(array.sum(upVolumes))
float totalDnVolume = nz(array.sum(dnVolumes))
float totalVolume = totalUpVolume - totalDnVolume
float maxUpVolume = nz(array.max(upVolumes))
float maxDnVolume = nz(array.min(dnVolumes))
float delta = totalUpVolume + totalDnVolume
float deltaPct = delta / totalVolume

// Track cumulative volume.


var float cvd = 0.0
[reset, trendIsUp, resetDescription] =
switch resetInput
RST1 => [false, na, "No resets"]
RST2 => [timeframe.change(htfStep()), na, "Resets every " + htfStep()]
RST3 => [timeframe.change(fixedTfInput), na, "Resets every " + fixedTfInput]
RST4 => [timeReset(hourInput, minuteInput), na, str.format("Resets at
{0,number,00}:{1,number,00}", hourInput, minuteInput)]
RST5 => [session.isfirstbar_regular, na, "Resets at the beginning of the
session"]
RST6 => [time == chart.left_visible_bar_time, na, "Resets at the beginning of
visible bars"]
RST7 =>
switch trendInput
TR01 =>
[_, direction] = ta.supertrend(trendValue2Input, trendPeriodInput)
[ta.change(direction, 1) != 0, direction == -1, "Resets on
Supertrend changes"]
TR02 =>
[up, dn] = TVta.aroon(trendPeriodInput)
[ta.cross(up, dn), ta.crossover(up, dn), "Resets on Aroon changes"]
=> [na, na, na]

if reset
cvd := 0

// Build OHLC values for CVD candles.


float barDelta = delta
float cvdO = cvd
float cvdC = cvdO + barDelta
float cvdH = cvdO + maxUpVolume
float cvdL = cvdO + maxDnVolume
cvd += barDelta

plotcandle(cvdO,cvdH,cvdL,cvdC, color=cvdC>cvdO ? color.green : color.red,


wickcolor= cvdC>cvdO ? color.green : color.red, bordercolor=cvdC>cvdO ? color.green
: color.red)

//Smoothed MA Calculation

smma1 = 0.0
smma2 = 0.0
smakj1 = ta.sma(cvdC, i_kjminlen)
smakj2 = ta.sma(cvdC, i_kjmaxlen)
smma1 := na(smma1[1]) ? smakj1 : (smma1[1] * (20 - 1) + cvdC) / 20
smma2 := na(smma2[1]) ? smakj2 : (smma2[1] * (60 - 1) + cvdC) / 60

//Kijun-Sen Calculation
kjsmma = math.avg(ta.lowest(cvdC,26), ta.highest(cvdC,26))

//Conditions

hiddenbearkj = ta.crossunder(smma1, kjsmma) and kjsmma < smma2 and cvdC < smma1
hiddenbullkj = ta.crossover(smma1, kjsmma) and kjsmma > smma2 and cvdC > smma1

uif i_showsr and i_showkjhsr and hiddenbearkj


pperphzone2 := cvdH_
upperplzone2 := cvdC_ < cvdO_ ? cvdC_ : cvdO_
upperplzoneline2 := i_layout == 'Zone' ? line.new(bar_index_, upperplzone2,
bar_index, upperplzone2, width=i_linewidth) : na
upperphzoneline2 := nobool2 ? line.new(bar_index_, upperphzone2,
bar_index, upperphzone2, width=i_linewidth) : line.new(bar_index_, (upperphzone2 +
upperplzone2) / 2, bar_index, (upperphzone2 + upperplzone2) / 2, width=i_linewidth)
labelph2 := i_showsr ? label.new(bar_index_, nobool2 ?
upperphzone2 : (upperphzone2 + upperplzone2) / 2, text=str.tostring(bar_index -
bar_index_), textcolor=upperzonecolor2, style=label.style_none) : na
if array.size(upperphzonearr2) > numberofline2
line.delete(array.shift(upperphzonearr2))
line.delete(array.shift(upperplzonearr2))
array.shift(upperzonetestedarr2)
label.delete(array.shift(labelpharr2))
array.push(upperphzonearr2, upperphzoneline2)
array.push(upperplzonearr2, upperplzoneline2)
array.push(upperzonetestedarr2, i_extend ? true : false)
array.push(labelpharr2, labelph2)
if array.size(upperplzonearr2) > 0
for i = 0 to array.size(upperplzonearr2) - 1 by 1
line tempupperline2 = array.get(upperphzonearr2, i)
line templowerline2 = array.get(upperplzonearr2, i)
label linepricelabel2 = array.get(labelpharr2, i)
bool tested2 = array.get(upperzonetestedarr2, i)
line.set_style(tempupperline2, f_i_linestyle(i_linestyle))
line.set_style(templowerline2, f_i_linestyle(i_linestyle))
line.set_color(tempupperline2, color.from_gradient(i, 1,
numberofline2, fzonecolor(upperzonecolor2, 00), fzonecolor(upperzonecolor2, 00)))
line.set_color(templowerline2, color.from_gradient(i, 1,
numberofline2, fzonecolor(upperzonecolor2, 00), fzonecolor(upperzonecolor2, 00)))
label.set_textcolor(linepricelabel2, color.from_gradient(i, 1,
numberofline2, fzonecolor(upperzonecolor2, 00), upperzonecolor2))
label.set_text(linepricelabel2,
str.tostring(math.round_to_mintick(line.get_y1(tempupperline2))))
label.set_text(linepricelabel2,
'                                                                Hidden Kijun
Resistance - ' + str.tostring(math.round_to_mintick(line.get_y1(tempupperline2))))
label.set_x(linepricelabel2, bar_index)
crossed = cvdH > line.get_y1(tempupperline2)
if crossed and not tested2
array.set(upperzonetestedarr2, i, true)
label.delete(linepricelabel2)
else if i_extend ? tested2 : not tested2
line.set_x2(tempupperline2, bar_index)
array.set(upperphzonearr2, i, tempupperline2)
line.set_x2(templowerline2, bar_index)
array.set(upperplzonearr2, i, templowerline2)

You might also like