本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=4
//
// THANKS :
// I want to thank the following pine coders that inspired me to create this tool with their code or their way of coding or their ideas.
// - dgtrd with https://fr.tradingview.com/script/kcqc3J2y-HTF-Candles-by-DGT/
// - KivancOzbilgic with https://fr.tradingview.com/script/f8PIBdha/
//
//
// LEGEND :
// - RED RECTANGLE : Area with VHF decreasing (ranging market)
// - GREEN RECTANGLE : Area with VHF increasing (trending market)
// - GREEN TRIANGLE : VHF reached a new bottom on the last 100 period (possible end of ranging market)
// - RED TRIANGLE : VHF reached a new top on the last 100 period (possible end of trending market)
// - RED CIRCLE : Candle with VHF decreasing (ranging market)
// - GREEN CIRCLE : Candle with VHF increasing (trending market)
//
//
// RECOMMENDED VALUES :
// - VHF LENGTH == 28 AND VHF SMOOTHING == (1 OR 9 OR 14)
// - VHF LENGTH == 18 AND VHF SMOOTHING == (1 OR 6 OR 9)
//
study("{Gunzo} Vertical Horizontal Filter (Trading ranges)", shorttitle="{Gunzo} Vertical Horizontal Filter", overlay=true, max_bars_back=100, max_boxes_count=500)
// #########################################################################################################
// VARIABLES AND CONSTANTS
// #########################################################################################################
// global input variables
vhf_length = input(18, title="VHF length", type=input.integer, minval=3, maxval=100)
vhf_source = input(close, title="VHF source", type=input.source)
smooth_length = input(6, title="VHF smoothing length", type=input.integer)
display_range_box = input(true, title="Display ranging market rectangles", type=input.bool)
display_trend_box = input(false, title="Display trending market rectangles", type=input.bool)
min_box_size = input(5, title="Minimum rectangle size", type=input.integer)
display_signal_line = input(false, title="Display signal line", type=input.bool)
display_top_bottoms = input(false, title="Display VHF tops and bottoms", type=input.bool)
// #########################################################################################################
// VHF CALCULATION
// #########################################################################################################
// vhf calculation
hcp = highest(vhf_source, vhf_length)
lcp = lowest(vhf_source, vhf_length)
mcp = sum(abs(change(vhf_source)), vhf_length)
vhf = (hcp - lcp) / mcp
// vhf smoothed calculation
vhf_smooth = ema(vhf, smooth_length)
// derivated calculations
vhf_trend_up = vhf_smooth > vhf_smooth[1]
vhf_trend_down = not vhf_trend_up
vhf_smooth_color = vhf_smooth > vhf_smooth[1] ? color.green : color.red
// top/bottoms calculations
vhf_high_100 = highest(vhf_smooth, 100)
vhf_low_100 = lowest(vhf_smooth, 100)
vhf_new_top = vhf_smooth > vhf_high_100[1]
vhf_new_bottom = vhf_smooth < vhf_low_100[1]
// #########################################################################################################
// RANGE BOX CALCULATIONS
// #########################################################################################################
// ranging market rectangle variables
range_box_begin_time = int(na)
range_box_begin_time := vhf_trend_up[1] and vhf_trend_down ? bar_index[1] : vhf_trend_down[1] and vhf_trend_down ? range_box_begin_time[1] : na
range_box_end_time = bar_index
range_box_drawing = not na(range_box_begin_time)
range_box_length = range_box_drawing ? range_box_end_time - range_box_begin_time : 1
range_box_begin_price = highest(high, range_box_length)
range_box_end_price = lowest(low, range_box_length)
range_box_overwrite = not na(range_box_begin_time) and not na(range_box_begin_time[1])
range_box_end_drawing = na(range_box_begin_time) and not na(range_box_begin_time[1])
range_box_too_small = range_box_end_drawing and range_box_length[1] < min_box_size
// trending market rectangle variables
trend_box_begin_time = int(na)
trend_box_begin_time := vhf_trend_down[1] and vhf_trend_up ? bar_index[1] : vhf_trend_up[1] and vhf_trend_up ? trend_box_begin_time[1] : na
trend_box_end_time = bar_index
trend_box_drawing = not na(trend_box_begin_time)
trend_box_length = trend_box_drawing ? trend_box_end_time - trend_box_begin_time : 1
trend_box_begin_price = highest(high, trend_box_length)
trend_box_end_price = lowest(low, trend_box_length)
trend_box_overwrite = not na(trend_box_begin_time) and not na(trend_box_begin_time[1])
trend_box_end_drawing = na(trend_box_begin_time) and not na(trend_box_begin_time[1])
trend_box_too_small = trend_box_end_drawing and trend_box_length[1] < min_box_size
// #########################################################################################################
// PLOTTING ON CHART
// #########################################################################################################
// display ranging market rectangle
var box range_box = na
if display_range_box
if range_box_overwrite or range_box_too_small
box.delete(range_box)
if range_box_drawing
range_box := box.new(range_box_begin_time, range_box_begin_price, range_box_end_time, range_box_end_price, color.red, 1, line.style_dotted, extend.none, xloc.bar_index, color.new(color.red, 85))
// display trending market rectangle
var box trend_box = na
if display_trend_box
if trend_box_overwrite or trend_box_too_small
box.delete(trend_box)
if trend_box_drawing
trend_box := box.new(trend_box_begin_time, trend_box_begin_price, trend_box_end_time, trend_box_end_price, color.green, 1, line.style_dotted, extend.none, xloc.bar_index, color.new(color.green, 85))
// #########################################################################################################
// PLOTTING SIGNALS
// #########################################################################################################
plotshape(display_signal_line and vhf_trend_down, title='Trend down', style=shape.circle, location=location.bottom, size=size.small, color=color.new(color.red,50))
plotshape(display_signal_line and vhf_trend_up, title='Trend up', style=shape.circle, location=location.bottom, size=size.small, color=color.new(color.green,50))
plotshape(display_top_bottoms and vhf_new_top, title='New top', style=shape.triangledown, location=location.abovebar, size=size.small, color=color.red)
plotshape(display_top_bottoms and vhf_new_bottom, title='New bottom', style=shape.triangleup, location=location.belowbar, size=size.small, color=color.green)