// This Pine Script® code is subject to the terms of the Mozilla Public License 2.
0
at [Link]
// © ParsHawk-8282
//@version=5
indicator("Fractal Levels [BigBeluga]",
overlay = true,
max_labels_count = 75,
max_bars_back = 1000,
max_lines_count = 500,
max_boxes_count = 100)
// USER
INPUTS‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗{
// Inputs for fractal detection and filtering
int length = input(10, "Length") // Length for
detecting pivot highs and lows
int filter = input(2) // Filter to display
only significant volume levels
bool zone = [Link]("Wide", "Level Detects Zones", ["Wide", "Tight"]) ==
"Tight" // Detection zone type (Tight or Wide)
// Additional options for volume delta display and line styles
bool show_delta = [Link](false, "Delta Volume Fractals") // Show volume delta
for fractals
bool show_broke = [Link](false, "Broken Levels") // Display broken
levels
// Color inputs for bullish and bearish fractal lines
color up = #14b477 // Color for bullish
fractals (up)
color dn = [Link](204, 51, 71) // Color for bearish fractals
(down)
// }
//
CALCULATIONS‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗{
// Detecting pivot highs and lows based on the input length
series float ph = [Link](open, length, length) // Detect pivot
highs
series float pl = [Link] (close, length, length) // Detect pivot lows
// Initialize label variables to store fractal markers
label lbl1 = label(na) // Label for bullish
fractals
label lbl2 = label(na) // Label for bearish
fractals
// Arrays to store volume data and fractal levels
array<float> vol1 = [Link]<float>() // Array for
normalized volume values
array<float> vol2 = [Link]<float>() // Array for delta
volume values
var array<line> lines = [Link]<line>(50) // Array to store
fractal lines
array<box> boxes = [Link]<box>() // Array to store
fractal zones (boxes)
// Method to normalize values between 0 and 100
method normalize(float src)=>
int coef = 100
float value = ((src - [Link](src, 200)) / ([Link](src, 200) -
[Link](src, 200))) * coef
value
// Calculating normalized volume and volatility
series float volume_val = [Link]() // Normalize volume
between 0 and 100
series float volatility = [Link](high - low, 200) // Calculate
volatility using the simple moving average of range
series float delta_vol = close > open ? volume : -volume // Delta volume
calculation (positive for bullish candles, negative for bearish candles)
// }
// FRACTAL DETECTION AND VOLUME
ZONES‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
‗‗‗‗{
if last_bar_index - bar_index < 1000 // Limiting the calculation within the
lookback period
// Detect Pivot High (Bearish Fractals)
if not na(ph)
lbl1 := [Link](na, na, color = color(na), style =
label.style_label_down)
// Calculate average volume and delta volume for the detected fractal
for i = 0 to length * 2
[Link](volume_val[i])
[Link](delta_vol[i])
series float volume_avg1 = [Link]([Link]()) // Average
normalized volume
series float volume_sum2 = [Link]() // Sum of delta
volume
// Creating labels and drawing lines and boxes for bearish fractals
string txt = [Link](volume_avg1) + "\n🢃"
int x1 = bar_index[length]
int x2 = bar_index[length]
float y1 = high[length] + volatility * (zone ? 1.5 : 2.5)
float y2 = high[length] - volatility * (zone ? 1.5 : 2.5)
float y = high[length]
color color = color.from_gradient(volume_avg1, 0, 20, [Link](dn, 70),
dn)
// Drawing lines and boxes if volume exceeds filter
if volume_avg1 > filter
[Link]([Link](x1, y, x2, y, color = color, width = 3))
[Link]([Link](x1, y1, x2, y2, border_width = 0, bgcolor =
color(na)))
// Optionally display delta volume for fractals
if show_delta
[Link](
bar_index[length*2], high[length], bar_index, high[length]
- volatility*2.5,
border_width = 0,
bgcolor = color.from_gradient([Link](), 0, 205,
[Link](chart.fg_color, 98),
[Link](chart.fg_color, 40)),
text = [Link](volume_sum2, [Link]),
text_size = [Link],
text_color = chart.fg_color,
text_halign = text.align_right,
text_valign = text.align_top
)
lbl1.set_xy(bar_index[length], high[length])
lbl1.set_text(txt)
lbl1.set_textcolor(dn)
// Detect Pivot Low (Bullish Fractals)
if not na(pl)
lbl2 := [Link](na, na, color = color(na), style = label.style_label_up)
// Calculate average volume and delta volume for the detected fractal
for i = 0 to length * 2
[Link](volume_val[i])
[Link](delta_vol[i])
series float volume_avg1 = [Link]([Link]()) // Average
normalized volume
series float volume_sum2 = [Link]() // Sum of delta
volume
// Creating labels and drawing lines and boxes for bullish fractals
string txt = "🢁\n" + [Link](volume_avg1)
int x1 = bar_index[length]
int x2 = bar_index[length]
float y1 = low[length] + volatility * (zone ? 1 : 1.5)
float y2 = low[length] - volatility * (zone ? 1 : 1.5)
float y = low[length]
color color = color.from_gradient(volume_avg1, 0, 20, [Link](up, 70),
up)
// Drawing lines and boxes if volume exceeds filter
if volume_avg1 > filter
[Link]([Link](x1, y, x2, y, color = color, width = 3))
[Link]([Link](x1, y1, x2, y2, border_width = 0, bgcolor =
color(na)))
// Optionally display delta volume for fractals
if show_delta
[Link](
bar_index[length*2], low[length], bar_index, low[length] +
volatility*2.5,
border_width = 0,
bgcolor = [Link](chart.fg_color, 90),
text = [Link](volume_sum2, [Link]),
text_size = [Link],
text_color = chart.fg_color,
text_halign = text.align_left,
text_valign = text.align_bottom
)
lbl2.set_xy(bar_index[length], low[length])
lbl2.set_text(txt)
lbl2.set_textcolor(up)
// }
// BROKEN LEVELS DETECTION
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
‗‗‗‗‗‗‗‗‗{
// Handle logic for broken levels (levels that price crosses)
for line_id in lines
float line_y = line_id.get_y1()
// If price crosses the fractal line, change its width or delete it
if hl2 > line_y and hl2[1] <= line_y or hl2 < line_y and hl2[1] >= line_y and
[Link]
if show_broke
line_id.set_width(3)
line_id.set_color([Link](chart.fg_color, 92))
if not show_broke
[Link](line_id)
// Extend the fractal lines as the bars move forward
if line_id.get_x1() < line_id.get_x2()
line_id.set_x2(bar_index+15)
// Update boxes with fractal lines
for box_id in boxes
float top = box_id.get_top()
float bottom = box_id.get_bottom()
for line_id in lines
float line_y = line_id.get_y2()
// Adjust box dimensions when lines are broken
if line_y > bottom and line_y < top
line_id.set_x2(bar_index[length])
// A dummy line to ensure proper execution of the script
[Link](na, na, na, na)
// Warning if no volume
if [Link](volume) <= 0 and [Link]
[Link](bar_index, hl2, "No Volume Data is provided\n Check other tickers",
style = label.style_label_left,
textcolor = chart.fg_color)
// }
//////
overlay=true ,max_bars_back = 5000 , max_labels_count = 500, max_lines_count = 500
ATR = [Link](55)
//
MADC //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////
[macdLine, signalLine, Hist_MACD] = [Link](close, 12, 26, 9)
//
AO ////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////
Hist_AO = [Link](hl2,5) - [Link](hl2,34)
//
AO ////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////
RSI_Adj = [Link](close, 14) - 50
// Define "n" as the number of periods and keep a minimum value of 2 for error
handling.
n = [Link](title="Fractal Periods", defval=4, minval=2 , group = 'Logic
Setting')
// switch Method
string Hist_Type = [Link]("MACD", "Divergence Detecte Method", options =
["MACD", "RSI" ,"AO"], group = 'Logic Setting')
float Hist = switch Hist_Type
"MACD" => Hist_MACD
"AO" => Hist_AO
"RSI" => RSI_Adj
Show_Table = [Link]('Yes' , 'Show Table' , options = ['Yes' , 'No'], group =
'display Setting')
Show_Label = [Link]('Yes' , 'Show Label' , options = ['Yes' , 'No'], group =
'display Setting')
//
Trend//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////
EMA = [Link](close, 50)
up_trend =close[n] > EMA
down_trend = close[n] < EMA
//
Fractal////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////
// UpFractal
UpPivot = [Link](n,n)
// downFractal
downPivot = [Link](n,n)
upFractal = UpPivot and up_trend
downFractal = downPivot and down_trend
// plotshape(downFractal and Show_Fractal =='Yes', style=[Link],
location=[Link], offset=-n, color= [Link](57, 160, 60), size =
[Link])
// plotshape(upFractal and Show_Fractal =='Yes', style=[Link],
location=[Link], offset=-n, color= [Link](236, 69, 69), size =
[Link])
//
Data///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////
// Bearish Divergennce Data
//price Data
High_Last_Price = [Link](upFractal,high[n],0)
High_Per_Price = [Link](upFractal,high[n],1)
//MACD hist Data
High_Last_Hist = [Link](upFractal, Hist[n],0)
High_Per_Hist = [Link](upFractal, Hist[n],1)
//Bar index Data
High_Last_Bar = [Link](upFractal, bar_index[n],0)
High_Per_Bar = [Link](upFractal, bar_index[n],1)
// Time Condition Bullish
Time_Condition_Bear = (High_Last_Bar + 30) > bar_index
//Bearish Divergenc Detector////////////////////////
Last_Bearish_Divergece = if High_Last_Hist > 0 and High_Per_Hist > 0 and
Time_Condition_Bear and (High_Last_Bar - High_Per_Bar) < 30
(High_Last_Price > High_Per_Price) and (High_Last_Hist < High_Per_Hist)
else
false
//count_Bear_Divergence
var int CBearDive = 0
if [Link](High_Last_Price) and Last_Bearish_Divergece == true
CBearDive := CBearDive + 1
else if Last_Bearish_Divergece == false
CBearDive := 0
//Detecte change phase in Bearish Divergence
var bool Bear_Phase = na
len_back_for_Bear_Phase_check = if not na(High_Last_Bar - High_Per_Bar)
High_Last_Bar - High_Per_Bar
else
1
if [Link](High_Last_Price) and Last_Bearish_Divergece == true
for i = (n+1) to (n+1) + len_back_for_Bear_Phase_check
if Hist[i] < 0
Bear_Phase := true
else if Last_Bearish_Divergece == false
Bear_Phase := false
// Bullish Divergennce Data
//price Data
Low_Last_Price = [Link](downFractal,low[n],0)
Low_Per_Price = [Link](downFractal,low[n],1)
//MACD hist Data
Low_Last_Hist = [Link](downFractal, Hist[n],0)
Low_Per_Hist = [Link](downFractal, Hist[n],1)
//Bar index Data
Low_Last_Bar = [Link](downFractal, bar_index[n],0)
Low_Per_Bar = [Link](downFractal, bar_index[n],1)
// Time Condition Bullish
Time_Condition_Bull = (Low_Last_Bar + 30) > bar_index
//Bullish Divergenc Detector////////////////////////
Last_Bullish_Divergece = if Low_Last_Hist < 0 and Low_Per_Hist < 0 and
Time_Condition_Bull and (Low_Last_Bar - Low_Per_Bar) < 30
(Low_Last_Price < Low_Per_Price) and (Low_Last_Hist > Low_Per_Hist)
else
false
//count_Bear_Divergence
var int CBullDive = 0
if [Link](Low_Last_Price) and Last_Bullish_Divergece == true
CBullDive := CBullDive + 1
else if Last_Bullish_Divergece == false
CBullDive := 0
//Detecte and count change phase in Bullish Divergence
//var int CBullPhase = 0
var bool Bull_Phase = na
len_back_for_Bull_Phase_check = if not na(Low_Last_Bar - Low_Per_Bar)
Low_Last_Bar - Low_Per_Bar
else
1
if [Link](Low_Last_Price) and Last_Bullish_Divergece == true
for i = (n+1) to (n+1) + len_back_for_Bull_Phase_check
if Hist[i] > 0
//CBullPhase := CBullPhase + 1
Bull_Phase := true
else if Last_Bullish_Divergece == false
Bull_Phase := false
//Score Divergence
text_power_bull = if CBullDive == 1
"Normal Bull Dive"
else if CBullDive == 2
"Good Bull Dive"
else if CBullDive >= 3
"Strong Bull Dive"
else
"-"
text_power_bear = if CBearDive == 1
"Normal Bear Dive"
else if CBearDive == 2
"Good Bear Dive"
else if CBearDive >= 3
"Strong Bear Dive"
else
"-"
//Table
table bear_div_table = na
bear_div_table := [Link](position = position.top_right,
columns = 3,
rows = 5,
bgcolor = #ebeaea,
frame_width = 1,
frame_color = [Link],
border_color = [Link],
border_width = 1)
a = false
if Show_Table == 'Yes'
[Link](table_id = bear_div_table, column = 0, row = 0, text = "Type
Divergence : " , text_size = [Link])
[Link](table_id = bear_div_table, column = 0, row = 1, text = "Exist : ",
text_size = [Link])
[Link](table_id = bear_div_table, column = 0, row = 2, text =
"Consecutive : " , text_size = [Link])
[Link](table_id = bear_div_table, column = 1, row = 0, text = "Bearish
Divergence", text_color = [Link](216, 66, 66), text_size = [Link])
[Link](table_id = bear_div_table, column = 1, row = 1, text =
Last_Bearish_Divergece ? "+" : "-", text_color = [Link](216, 66, 66), text_size
= [Link])
[Link](table_id = bear_div_table, column = 1, row = 2, text =
[Link](CBearDive), text_size = [Link])
[Link](table_id = bear_div_table, column = 2, row = 0, text = "Bullish
Divergence", text_color = [Link](40, 177, 52), text_size = [Link])
[Link](table_id = bear_div_table, column = 2, row = 1, text =
Last_Bullish_Divergece ? "+" : "-", text_color = [Link](40, 177, 52), text_size
= [Link])
[Link](table_id = bear_div_table, column = 2, row = 2, text =
[Link](CBullDive), text_size = [Link])
[Link](table_id = bear_div_table, column = 0, row = 3, text = "Divergence
Quality : " , text_size = [Link])
[Link](table_id = bear_div_table, column = 1, row = 3, text =
text_power_bear, text_color = [Link](216, 66, 66), text_size = [Link])
[Link](table_id = bear_div_table, column = 2, row = 3, text =
text_power_bull, text_color = [Link](40, 177, 52), text_size = [Link])
//Check Change Phase
[Link](table_id = bear_div_table, column = 0, row = 4, text = "Change Phase
Indicator : ", text_size = [Link])
[Link](table_id = bear_div_table, column = 1, row = 4, text = Bear_Phase ?
"+" : "-", text_color = [Link](216, 66, 66), text_size = [Link])
[Link](table_id = bear_div_table, column = 2, row = 4, text = Bull_Phase ?
"+" : "-", text_color = [Link](40, 177, 52), text_size = [Link])
// plot
//Divergence Line & Label
Drawing()=>
var line BeLine = na
var line BuLine = na
var label BeLabel = na
var label BuLabel = na
// "MACD", "RSI" ,"AO"
if Last_Bullish_Divergece
BuLine := [Link](Low_Per_Bar, Low_Per_Price,
Low_Last_Bar,Low_Last_Price , color = [Link](21, 151, 25) , width = 2)
if Show_Label == 'Yes'
BuLabel := [Link](Low_Last_Bar,Low_Last_Price , '+RD', color =
[Link], textcolor = [Link], size = [Link] , style =
label.style_label_up)
BuLine := [Link](Low_Per_Bar, Low_Per_Price , Low_Last_Bar,Low_Last_Price
, color = [Link](21, 151, 25), width = 2)
if Show_Label == 'Yes'
BuLabel := [Link](Low_Last_Bar,Low_Last_Price , '+RD', color =
[Link], textcolor = [Link], size = [Link], style =
label.style_label_up)
if Last_Bearish_Divergece
BeLine := [Link](High_Per_Bar, High_Per_Price,
High_Last_Bar,High_Last_Price , color = [Link](212, 44, 44), width = 2)
if Show_Label == 'Yes'
BeLabel := [Link](High_Last_Bar,High_Last_Price ,'-RD', color =
[Link], textcolor = [Link], size = [Link], style =
label.style_label_down)
BeLine := [Link](High_Per_Bar, High_Per_Price ,
High_Last_Bar,High_Last_Price , color = [Link](212, 44, 44), width = 2)
if Show_Label == 'Yes'
BeLabel := [Link](High_Last_Bar,High_Last_Price ,'-RD', color =
[Link], textcolor = [Link], size = [Link], style =
label.style_label_down)
Drawing()