Tuesday, August 20, 2024

Time Relative Volume Oscillator | Flux Charts | Tradingview.com

Pine script

 

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fluxchart
//@version=5
indicator("Time Relative Volume Oscillator | Flux Charts",shorttitle = "TRVO | Flux Charts")
relativeLen = input.int(6, minval=1, maxval=10, title = "Relative calculation backwards length", group="General Calculation Settings")
mode = input.string("Classic", "Display Mode", options=["Classic", "Oscillator", "Delta"], group="General Calculation Settings")
volumeMode = input.string("Total Volume", "Volume Display Mode", options=["Total Volume", "Buy/Sell Volume"], group="General Calculation Settings")
showTotalVolume = input.bool(false, "Total Volume", group="General Calculation Settings")
smoothingLine = input.bool(true, "Smoothing Line", group="General Calculation Settings")
oscillatorShortlen = input.int(21, minval=1, title = "Oscillator Short EMA Length", group="Oscillator Settings")
oscillatorLonglen = input.int(36, minval=1, title = "Oscillator Long EMA Length", group="Oscillator Settings")
zeroLineWidth = input.int(1, minval=1, title="Zero line thickness", group="General Calculation Settings", inline="Zero line settings")
zeroLineColor = input.color(color.white, title="Zero line color", group="General Calculation Settings", inline="Zero line settings")
directionChangeLabels = input.bool(true, title = "Direction Change Labels", group="Delta Mode Settings")
deltaModeSmoothing = input.int(9, minval=1, title = "Smoothing factor", group="Delta Mode Settings")
var barsInSession = 0
barsTillSessionClose = math.round((time_close("D") - time_close) / timeframe.in_seconds() / 1000) + 1
if barsTillSessionClose > barsTillSessionClose[1]
barsInSession := barsTillSessionClose
tfMultiplier = barsInSession
if timeframe.in_seconds() >= 86400
tfMultiplier := 1
total_volume = 0.0
for i = 1 to relativeLen
total_volume := total_volume + volume[tfMultiplier * i]
relativeVolume = (volume) / (total_volume / relativeLen)
buyRelativeVolume = ( (high==low) ? 0 : relativeVolume*(close-low)/(high-low))
sellRelativeVolume = ( (high==low) ? 0 : relativeVolume*(high-close)/(high-low))
buyVolumePercent = buyRelativeVolume / (buyRelativeVolume + sellRelativeVolume)
sellVolumePercent = sellRelativeVolume / (buyRelativeVolume + sellRelativeVolume)
volume200 = ta.sma(volume, 200)
calculateOscilatorMode() =>
buyVsSell = buyVolumePercent - sellVolumePercent
long = ta.ema((buyVsSell), oscillatorShortlen)
short = ta.ema((buyVsSell), oscillatorLonglen)
diff = ta.ema(long - short, 9)
barColor = diff>diff[1] ? color.teal : color.red
boundSma = ta.sma(math.abs(diff), 200) * 2
normalizedDiff = 2.4/(1+math.pow(math.e, ((-1/boundSma)*diff))) - 1.2
[normalizedDiff, barColor]
calculateClassicMode() =>
osc = 4/(1+math.pow(math.e, (-2*relativeVolume + 3))) - 0.18
relativePlot = osc * volume200
[relativePlot]
calculateDeltaMode() =>
osc = 4/(1+math.pow(math.e, (-2*relativeVolume + 3))) - 0.18
relativePlot = osc * volume200
// buyVsSell = buyVolumePercent / sellVolumePercent
buyVsSell = buyVolumePercent - sellVolumePercent
[relativePlot, buyVsSell]
// classic mode ----------------------------------------------------------------------------------------------------------------------------------
[relativePlot] = calculateClassicMode()
plot(mode == "Classic" and showTotalVolume ? volume : na, style=plot.style_columns, color=color.new(color.gray, 50), title="Volume")
plot(mode == "Classic" and volumeMode == "Total Volume" ? relativePlot : na, style=plot.style_columns, color=close > open ? color.teal : color.red, title="Relative Volume")
plot(mode == "Classic" and volumeMode == "Buy/Sell Volume" ? relativePlot : na, style=plot.style_columns, color=color.teal, title="Relative BUY Volume")
plot(mode == "Classic" and volumeMode == "Buy/Sell Volume" ? relativePlot * sellVolumePercent : na, style=plot.style_columns, color=color.red, title="Relative SELL Volume")
plot(mode == "Classic" and smoothingLine ? ta.ema(relativePlot, 9) : na, color= color.white, title="Relative volume smooth line", linewidth = 4)
plot(mode == "Classic" and smoothingLine ? ta.ema(relativePlot, 9) : na, color= close > open ? color.teal : color.red, title="Relative volume smooth line", linewidth = 3)
// classic mode ----------------------------------------------------------------------------------------------------------------------------------
//oscillator mode -------------------------------------------------------------------------------------------------------------------------------
[oscillator, barColor] = calculateOscilatorMode()
plot(mode == "Oscillator" and volumeMode == "Total Volume" ? oscillator : na, style=plot.style_columns, color=barColor, title="Oscillator")
plot(mode == "Oscillator" and volumeMode == "Buy/Sell Volume" ? oscillator : na, style=plot.style_columns, color=color.teal, title="Oscillator BUY Volume")
plot(mode == "Oscillator" and volumeMode == "Buy/Sell Volume" ? oscillator * sellVolumePercent : na, style=plot.style_columns, color=color.red, title="Oscillator SELL Volume")
oscillatorSmoothLine = ta.ema(oscillator, 9)
plot(mode == "Oscillator" and smoothingLine ? oscillatorSmoothLine : na, color=oscillatorSmoothLine > 0 ? color.lime : color.red, title="Oscillator smooth line", linewidth=2)
plotshape(mode == "Oscillator" and oscillator > 0 and oscillator[1] < 0 and directionChangeLabels ? -0.05 : na, style=shape.triangleup, location=location.absolute, color=color.lime)
plotshape(mode == "Oscillator" and oscillator < 0 and oscillator[1] > 0 and directionChangeLabels ? 0.05 : na, style=shape.triangledown, location=location.absolute, color=color.red)
//oscillator mode -------------------------------------------------------------------------------------------------------------------------------
// delta mode ----------------------------------------------------------------------------------------------------------------------------------
[deltaRelativePlot, buyVsSell] = calculateDeltaMode()
buyVsSellShortEma = ta.ema((buyVsSell), 21)*volume200*20
buyVsSellLongEma = ta.ema((buyVsSell), 36)*volume200*20
buyVsSellDiff = ta.ema(buyVsSellShortEma - buyVsSellLongEma, deltaModeSmoothing)
plot(mode == "Delta" and showTotalVolume ? volume * buyVolumePercent : na, style=plot.style_columns, color=color.new(color.gray, 60), title="Volume")
plot(mode == "Delta" and showTotalVolume ? -volume * sellVolumePercent : na, style=plot.style_columns, color=color.new(color.gray, 60), title="Volume")
plot(mode == "Delta" ? deltaRelativePlot * buyVolumePercent : na, style=plot.style_columns, color=color.new(color.teal, 40), title="Relative BUY Volume")
plot(mode == "Delta" ? -deltaRelativePlot * sellVolumePercent : na, style=plot.style_columns, color=color.new(color.red, 40), title="Relative SELL Volume")
plot(mode == "Delta" and smoothingLine ? buyVsSellDiff : na, color=buyVsSellDiff > 0 ? color.lime : color.red, title="Relative volume smooth line", linewidth=2)
plotshape(mode == "Delta" and directionChangeLabels and buyVsSellDiff > 0 and buyVsSellDiff[1] < 0 ? (-deltaRelativePlot * sellVolumePercent - volume200) : na, style=shape.triangleup, location=location.absolute, color=color.lime)
plotshape(mode == "Delta" and directionChangeLabels and buyVsSellDiff < 0 and buyVsSellDiff[1] > 0 ? (deltaRelativePlot * buyVolumePercent + volume200) : na, style=shape.triangledown, location=location.absolute, color=color.red)
// delta mode ----------------------------------------------------------------------------------------------------------------------------------
plot(0, color=zeroLineColor, title="Zero line", linewidth = zeroLineWidth)

No comments:

Post a Comment