Buy/Sell Delta Candles

BullBearSR · study · 60 行 · 点赞 68 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Buy/Sell Delta Candles", "Buy/Sell Delta", overlay = true, format = format.volume)
 
import TradingView/ta/12
 
// Under each candle, on the price chart: a copy of the same candle (same body and wicks). Its body is split into green
// (buy volume share, on top) and red (sell volume share, below), so the color that fills more of the body is the side
// that won the candle. Buy and sell volume are approximated from lower timeframe data, like TradingView's Volume Delta.
 
dataAggregationTooltip = "Defines how lower timeframe data is aggregated. \"1 tick\" uses tick data and is available only on expert plans."
dataAggregationInput = input.string("Time", "Data aggregation", options = ["Time", "1 tick"], tooltip = dataAggregationTooltip)
useTicks = dataAggregationInput == "1 tick"
 
lowerTimeframeTooltip = "The indicator scans lower timeframe data to approximate buy and sell volume. By default, the timeframe is chosen automatically."
 + " \n\nHigher timeframes provide more historical data, but the data will be less precise."
useCustomTimeframeInput = input.bool(false, "Use custom timeframe", tooltip = lowerTimeframeTooltip, active = not useTicks)
lowerTimeframeInput = input.timeframe("1", "Timeframe", active = useCustomTimeframeInput and not useTicks)
buyCol = input.color(color.new(color.teal, 0), "Buy volume")
sellCol = input.color(color.new(color.red, 0), "Sell volume")
wickCol = input.color(color.new(color.gray, 0), "Wicks")
offset = input.float(0.3, "Distance below the candle (ATR)", minval = 0, step = 0.1)
 
var lowerTimeframe = switch
    useTicks                => "1T"
    useCustomTimeframeInput => lowerTimeframeInput
    timeframe.isseconds     => "1S"
    timeframe.isintraday    => "1"
    timeframe.isdaily       => "5"
    => "60"
 
[upVolume, downVolume, lastVolume] = ta.requestUpAndDownVolume(lowerTimeframe)
buyVol = math.abs(nz(upVolume))
sellVol = math.abs(nz(downVolume))
totVol = buyVol + sellVol
buyShare = totVol > 0 ? buyVol / totVol : na
 
// the copy sits just below the real candle: its high is offset ATR under the real low
atr = ta.atr(14)
shift = high - (low - offset * atr)
cHigh = high - shift
cLow = low - shift
bodyTop = math.max(open, close) - shift
bodyBot = math.min(open, close) - shift
split = bodyTop - (bodyTop - bodyBot) * buyShare
 
// wicks (the body of this plot is transparent), then the two parts of the body
plotcandle(bodyTop, cHigh, cLow, bodyBot, "Copy wicks", color = color.new(wickCol, 100), wickcolor = wickCol, bordercolor = color.new(wickCol, 100), display = display.pane)
plotcandle(bodyTop, bodyTop, split, split, "Buy part", color = buyCol, wickcolor = buyCol, bordercolor = buyCol, display = display.pane)
plotcandle(split, split, bodyBot, bodyBot, "Sell part", color = sellCol, wickcolor = sellCol, bordercolor = sellCol, display = display.pane)
 
plot(buyVol, "Buy volume", display = display.data_window)
plot(sellVol, "Sell volume", display = display.data_window)
plot(buyVol - sellVol, "Delta", display = display.data_window)
plot(100 * buyShare, "Buy share %", display = display.data_window)
 
var cumVol = 0.
cumVol += nz(volume)
 
if barstate.islast and cumVol == 0
    runtime.error("The data vendor doesn't provide volume data for this symbol.")

← 返回列表