Professional Documents
Culture Documents
// |{-------------------------------------------------------------------------||
// || author: RicardoSantos
// ||-------------------------------------------------------------------------||
// || description:
// || � Zig Zag indicator plots points on the chart whenever prices reverse
// || by a percentage greater than a pre-chosen variable.
// || � Forecasts area based on zigzag statistics.
// || � Displays labels with detected patterns.
// || � Displays slope information rate of price, time diferentials and
angle(experimental)
// || � Tags: zigzag, range, average, forecast, pattern, time, levels
// ||---}---------------------------------------------------------------------||
// |{-------------------------------------------------------------------------||
study(
title="[RS]ZigZag Percent Reversal - Forecast - patterns - labels",
shorttitle="ZZ",
overlay=true
)
// ||---}---------------------------------------------------------------------||
// |--------------------------------------------------------------------------||
// | ZigZag: ||
// |--------------------------------------------------------------------------||
// |{
string percent_method = input(
defval="MANUAL",
title="Method to use for the zigzag reversal range:",
options=[
"MANUAL",
"ATR005 * X", "ATR010 * X", "ATR020 * X", "ATR050 * X", "ATR100 * X",
"ATR250 * X"
]
)
// ZigZag options:
bool show_real_pivots = input(defval=true, title="Show real zigzag pivots:",
type=input.bool)
bool show_zigzag_lines = input(defval=true, title="Show zigzag lines:",
type=input.bool)
// Label options:
bool show_pivot_price = input(defval=true, title="Show price labels on zigzag:",
type=input.bool)
bool show_triangle_rates = input(defval=true, title="Show rate labels on
triangles:", type=input.bool)
bool show_angles = input(defval=false, title="Show Angle Degree
information(EXPERIMENTAL):", type=input.bool)
bool show_patterns = input(defval=true, title="Show Patterns:", type=input.bool)
bool show_harmonic = input(defval=true, title="Show Harmonic Triangle Pattern:",
type=input.bool)
bool show_tap = input(defval=true, title="Show 2/3/4 Tap Patterns:",
type=input.bool)
bool show_abcd = input(defval=true, title="Show AB=CD Pattern:", type=input.bool)
bool show_bat = input(defval=true, title="Show Bat Pattern:", type=input.bool)
bool show_butterfly = input(defval=true, title="Show Butterfly Pattern:",
type=input.bool)
bool show_gartley = input(defval=true, title="Show Gartley Pattern:",
type=input.bool)
bool show_crab = input(defval=true, title="Show Crab Pattern:", type=input.bool)
bool show_shark = input(defval=true, title="Show Shark Pattern:", type=input.bool)
bool show_5o = input(defval=true, title="Show 5o Pattern:", type=input.bool)
bool show_wolfe = input(defval=true, title="Show wolfe Pattern:", type=input.bool)
bool show_contria = input(defval=true, title="Show Contracting Triangle Pattern:",
type=input.bool)
bool show_exptria = input(defval=true, title="Show Expanding Triangle Pattern:",
type=input.bool)
bool show_hns = input(defval=true, title="Show Head and Shoulders Pattern:",
type=input.bool)
// Forecast options:
bool show_projections = input(defval=true, title="Show projections forecast of
zigzag:", type=input.bool)
bool show_projections_square = input(defval=true, title="Show projections forecast
Bounds:", type=input.bool)
bool show_projections_square_rates = input(defval=false, title="Show projections
target rates:", type=input.bool)
// ||-------------------------------------------------------------------------||
// || zigzag function:
// ||-------------------------------------------------------------------------||
// |{
f_zz(_percent)=>
if na(_is_direction_up)
_is_direction_up := true
if _is_direction_up
_ltrack := na
_ltime := time
if na(_htrack)
if high > high[1]
_htrack := high
_htime := time
else
_htrack := high[1]
_htime := time[1]
else
if high > _htrack
_htrack := high
_htime := time
if not _is_direction_up
_htrack := na
_htime := na
if na(_ltrack)
if low < low[1]
_ltrack := low
_ltime := time
else
_ltrack := low[1]
_ltime := time[1]
else
if low < _ltrack
_ltrack := low
_ltime := time
// || |}---------------------------------------------------------------------<�
// ||-------------------------------------------------------------------------||
// || zigzag data:
// ||-------------------------------------------------------------------------||
// |{
[price_a, is_up, reverse, _rl_time] = f_zz(percent)
alt_time = show_real_pivots ? _rl_time : time
// | Get Pivots:
var int time_a = na
var int time_b = na, var float price_b = na
var int time_c = na, var float price_c = na
var int time_d = na, var float price_d = na
var int time_e = na, var float price_e = na
var int time_f = na, var float price_f = na
var int time_g = na, var float price_g = na
if is_new_zig
time_a := alt_time
time_b := time_a[1], price_b := price_a[1]
time_c := time_b[1], price_c := price_b[1]
time_d := time_c[1], price_d := price_c[1]
time_e := time_d[1], price_e := price_d[1]
time_f := time_e[1], price_f := price_e[1]
time_g := time_f[1], price_g := price_f[1]
// || |}---------------------------------------------------------------------<�
// ||-------------------------------------------------------------------------||
// || Draw zigzag:
// ||-------------------------------------------------------------------------||
// |{
f_draw_zigzag_lines()=>
var line _li = na
_li_color = price_a > price_b ? color.teal : color.orange
if is_new_zig
_li := line.new(
time_a, price_a,
time_b, price_b,
xloc.bar_time, extend=extend.none, color=_li_color, width=2
)
if show_zigzag_lines
f_draw_zigzag_lines()
// || |}---------------------------------------------------------------------<�
// || |}---------------------------------------------------------------------<�
// ||-------------------------------------------------------------------------||
// || Pivot Labels:
// ||-------------------------------------------------------------------------||
// |{
f_draw_zigzag_labels(_text)=>
var label _la = na
color _la_color = price_a > price_b ? color.orange : color.teal
string _la_style = price_a > price_b ? label.style_labeldown :
label.style_labelup
string _la_text = na
if show_pivot_price
_la_text := price_a > price_b ? tostring(price_a, "#.#####") + _text :
_text + tostring(price_a, "#.#####")
else
_la_text := _text
if is_new_zig
_la := label.new(
x=time_a, y=price_a,
text=_la_text,
xloc=xloc.bar_time, yloc=yloc.price,
style=_la_style,
color=_la_color, textcolor=color.black, size=size.small
)
// if show_zigzag_labels
// f_plot_zigzag_labels("")
// || |}---------------------------------------------------------------------<�
// ||-------------------------------------------------------------------------||
// || Function to process data, return range, avg, +/- dev, max to be ploted:
// ||-------------------------------------------------------------------------||
// |{
f_mode_process_stats(_weight, _data)=>
float _avg_range = _data, float _max_range = 0.0
if bar_index < 1
// on 1st bar, make it equal to _data
_avg_range := _data
else
if change(_data) != 0
_weight_data = _weight * _data
_weight_previous = (1 - _weight) * nz(_avg_range[1], _data[1])
_avg_range := _weight_data + _weight_previous
else
_avg_range := _avg_range[1]
// ||-------------------------------------------------------------------------||
// || Line projection:
// ||-------------------------------------------------------------------------||
// |{
f_cast_projections()=>
var line line_midline = na
var line line_price_dev = na
var line line_time_dev = na
var label _la0618 = na
var label _la1618 = na
// || Style abreviation:
xtend = extend.right
st_dash = line.style_dashed
st_arrow = line.style_arrow_both
line_midline := line.new(
time_a, price_a,
target_avg_time, target_avg_price,
xloc.bar_time, extend=xtend, color=color.orange, style=st_dash,
width=1
)
line_price_dev := line.new(
target_avg_time, target_price_lower_dev,
target_avg_time, target_price_upper_dev,
xloc.bar_time, color=#0066ff, style=st_arrow, width=1
)
line_time_dev := line.new(
target_time_lower_dev, target_avg_price,
target_time_upper_dev, target_avg_price,
xloc.bar_time, color=#0066ff, style=st_arrow, width=1
)
if show_projections_square_rates
_la0618 := label.new(
x=target_time_1618_dev, y=target_price_1618_dev,
text="Target 1.618 @ " + tostring(target_price_1618_dev,
"#.#####"),
xloc=xloc.bar_time, yloc=yloc.price,
style=is_up ? label.style_labeldown : label.style_labelup,
color=color.new(color.silver, 20), textcolor=color.black,
size=size.small
)
_la1618 := label.new(
x=target_time_0618_dev, y=target_price_0618_dev,
text="Target 0.618 @ " + tostring(target_price_0618_dev,
"#.#####"),
xloc=xloc.bar_time, yloc=yloc.price,
style=is_up ? label.style_labelup : label.style_labeldown,
color=color.new(color.silver, 20), textcolor=color.black,
size=size.small
)
// ||-------------------------------------------------------------------------||
// || Detect patterns:
// ||-------------------------------------------------------------------------||
// |{
// || Pattern Functions:
isHarmonicTriangle(_cba, _margin_of_error)=>
bool _return = false
// return true if its rate is near a harmonic rate:
// 0.146, 0.236, 0.382, 0.618, 1, 1.618, 2.618, 4.236, 6.854, 11.089,
17.942, 29.03
for _i = 1 to 12
if f_is_inrange(_cba, (-pow(1.618, -5+_i) - _margin_of_error), (-pow(1.618,
-5+_i) + _margin_of_error))
_return := true
_return
is2Tap(_cba, _margin_of_error)=>
_is_cba = f_is_inrange(_cba, -1.000 - _margin_of_error, -1.000 +
_margin_of_error)
_is_cba
// |}
// |{-------------------------------------------------------------------------||
// || Parameters:
// || _percent_of_error (float) : Margin of error in percentage.
f_Detect_Patterns(_percent_of_error)=>
// Placeholder for pattern label
string _pattern_label_placeholder = ""
// adjust margin of error into multiplier
float _margin_of_error = _percent_of_error / 100
// Placeholders for pivot rates:
var float price_gfe = na, var float time_gfe = na
var float price_gfc = na, var float time_gfc = na
var float price_gfa = na, var float time_gfa = na
var float price_gdc = na, var float time_gdc = na
var float price_gda = na, var float time_gda = na
var float price_gba = na, var float time_gba = na
// ||-------------------------------------------------------------------------||
// || Pattern check block:
// ||-------------------------------------------------------------------------||
// |{-------------------------------------------------------------------------||
if show_patterns
bool _isvalid_gfa = price_fed >= -1 and price_feb >= -1 and price_cba <= -1
and price_eda <= -1
bool _isvalid_gda = price_fed <= -1 and price_gfe >= -1 and price_cba <= -1
and price_dcb >= -1
bool _isvalid_gba = price_feb <= -1 and price_gfe >= -1 and price_cba <= -1
and price_dcb <= -1
bool _isvalid_eba = price_cba <= -1 and price_dcb <= -1
bool _isvalid_eda = price_cba <= -1 and price_dcb >= -1
bool _isvalid_fcb = price_fed >= -1 and price_edc <= -1
bool _isvalid_feb = price_edc >= -1 and price_dcb <= -1
_pattern_list = "\n"
// Check if its a harmonic triangle:
if show_harmonic
if isHarmonicTriangle(price_gfa, _margin_of_error) and _isvalid_gfa
_pattern_list := _pattern_list + "� Harmonic Triangle(GFA) �\n"
if isHarmonicTriangle(price_gda, _margin_of_error) and _isvalid_gda
_pattern_list := _pattern_list + "� Harmonic Triangle(GDA) �\n"
if isHarmonicTriangle(price_gba, _margin_of_error) and _isvalid_gba
_pattern_list := _pattern_list + "� Harmonic Triangle(GBA) �\n"
if isHarmonicTriangle(price_eba, _margin_of_error) and _isvalid_eba
_pattern_list := _pattern_list + "� Harmonic Triangle(EBA) �\n"
if isHarmonicTriangle(price_eda, _margin_of_error) and _isvalid_eda
_pattern_list := _pattern_list + "� Harmonic Triangle(EDA) �\n"
if isHarmonicTriangle(price_cba, _margin_of_error)
_pattern_list := _pattern_list + "� Harmonic Triangle(CBA) �\n"
// Check if its Double Tap
if show_tap
if is2Tap(price_cba, _margin_of_error)
_pattern_list := _pattern_list + "� Double Tap(CBA) �\n"
if is2Tap(price_eba, _margin_of_error) and _isvalid_eba
_pattern_list := _pattern_list + "� Double Tap(EBA) �\n"
if is2Tap(price_eda, _margin_of_error) and _isvalid_eda
_pattern_list := _pattern_list + "� Double Tap(EDA) �\n"
// Check if its Triple Tap
if show_tap
if is3Tap(price_edc, price_cba, _margin_of_error)
_pattern_list := _pattern_list + "� Triple Tap(EDC, CBA) �\n"
// Check if its Quadruple Tap
if show_tap
if is4Tap(price_gfe, price_edc, price_cba, _margin_of_error)
_pattern_list := _pattern_list + "� Quadruple Tap(GFE, EDC, CBA)
�\n"
f_draw_zigzag_labels(_pattern_list)
else
// Only shows price label:
if show_pivot_price
f_draw_zigzag_labels("")
// || |}---------------------------------------------------------------------<�