本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LonesomeTheBlue
//@version=4
study("Volume-Supported Linear Regression Trend", "VSLRT", overlay = false)
src = input(defval = close, title = "Source")
len1 = input(defval = 20, title = "Short Term Length", minval = 5)
len2 = input(defval = 50, title = "Long Term Length", minval = 5)
// get short/long-term regression slope
slope_price = linreg(src, len1, 0) - linreg(src, len1, 1)
slope_price_lt = linreg(src, len2, 0) - linreg(src, len2, 1)
// get size of top/bottom/body of the candle
tw = high - max(open, close)
bw = min(open, close) - low
body = abs(close - open)
//Calculate Buy/Sell Volume
_rate(cond) =>
ret = 0.5 * (tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body)
ret := nz(ret) == 0 ? 0.5 : ret
ret
// Calculate Regression Slope for Buy/Sell Volumes
_get_trend(len)=>
deltaup = volume * _rate(open <= close)
deltadown = volume * _rate(open > close)
slope_volume_up = linreg(deltaup, len, 0) - linreg(deltaup, len, 1)
slope_volume_down = linreg(deltadown, len, 0) - linreg(deltadown, len, 1)
[slope_volume_up, slope_volume_down]
// get buy/sell volume regression slopes for short term period
[slope_volume_up, slope_volume_down] = _get_trend(len1)
// coloring
col1 = slope_price > 0 ? slope_volume_up > 0 ? slope_volume_up > slope_volume_down ? #00FF00ff : #00BC00 : #00FF006f :
slope_price < 0 ? slope_volume_down > 0 ? slope_volume_up < slope_volume_down ? #FF0000ff : #BF0000 : #FF00006f :
color.gray
// short term
plot(slope_price * len1, color = col1, style = plot.style_columns)
// get buy/sell volume regression slopes for long term period
[slope_volume_up_lt, slope_volume_down_lt] = _get_trend(len2)
// coloring
col2 = slope_price_lt > 0 ? slope_volume_up_lt > 0 ? slope_volume_up_lt > slope_volume_down_lt ? #008EFF : #006ec5 : #024478 :
slope_price_lt < 0 ? slope_volume_down_lt > 0 ? slope_volume_up_lt < slope_volume_down_lt ? #fd9701 : #CE7A00 : #663d00 :
color.gray
// Long term
plot(slope_price_lt * len2, color = col2, linewidth = 3, transp = 0)