Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © djmad
- // special thanks for you help and time Bjorgum
- //@version=5
- // @description TODO: add library description here
- library("Mad_Fibonaccibox", overlay = true)//, max_labels_count = 500, max_boxes_count = 500, max_lines_count = 500, max_bars_back = 1000)
- import djmad/Mad_Standardparts/10 as STDP
- // _______ __ __ _____ ______ _____
- // |__ __| \ \ / / | __ \ | ____| / ____|
- // | | \ \_/ / | |__) | | |__ | (___
- // | | \ / | ___/ | __| \___ \
- // | | | | | | | |____ ____) |
- // |_| |_| |_| |______| |_____/
- ///// Fibonacci Box Types{
- // @types for defining a lines and texts of a fibonacci box
- export type type_level
- float level = na
- float price = na
- bool drawline = true
- int linewidth = 1
- string linetype = line.style_solid
- color fiblinecolor = color.gray
- string drawlabel = "all" //"price", "name", "none"
- string labeltext = ''
- int textshift = 0
- color fibtextcolor = color.gray
- string fibtextsize = size.small
- int transp = 0
- // @types for defining the fills of a fibonaccibox
- export type type_fill
- int partner_A = na
- int partner_B = na
- color fill_color = na
- int transp = 0
- // @types for defining a fibonacci box
- export type type_Fibonacci_box
- float bottom_price = na
- float top_price = na
- int StartBar = na
- int StopBar = na
- array<type_level> levels = na
- array<type_fill> fills = na
- bool ChartisLog = false
- bool fibreverse = false
- bool fibdrawreverse = false
- int decimals_price = 2
- int decimals_percent = 2
- bool drawlines = true
- bool drawlabels = true
- bool drawfills = true
- bool draw_biginfo = true
- int biginfo_textshift = 0
- int rangeinfo_location = -1
- color rangeinfo_color = color.gray
- string rangeinfo_textsize = size.small
- array<line> line_array = na
- array<linefill> linefill_array = na
- array<label> label_array = na
- // }
- // ______ _____ ____ ____ ____ __ __
- //| ____| |_ _| | _ \ | _ \ / __ \ \ \ / /
- //| |__ | | | |_) | | |_) | | | | | \ V /
- //| __| | | | _ < | _ < | | | | > <
- //| | _| |_ | |_) | | |_) | | |__| | / . \
- //|_| |_____| |____/ |____/ \____/ /_/ \_\
- // FIBBOX Functions {
- // @function fibonacci calc.
- // @description This function block uses the levels and paramters set into the type_fibonacci_box(levels) and fills the corresponding array of prices.
- // @returns returns a type_Fibonacci_box with the filled data
- export f_fib_calc(type_Fibonacci_box [] _Fibonacci_box, int _itemnumber = 0) =>
- if _Fibonacci_box.size() > 0 and _Fibonacci_box.size()-1 >= _itemnumber
- type_Fibonacci_box _get_RW = _Fibonacci_box.get(_itemnumber)
- // type_Fibonacci_box _get_RW = _Fibonacci_box
- // if not na(_Fibonacci_box)
- float Diff = _get_RW.top_price-_get_RW.bottom_price
- if not _get_RW.fibreverse
- for i = 0 to _get_RW.levels.size() -1
- levelValue = _get_RW.levels.get(i)
- if _get_RW.ChartisLog
- levelValue.price := STDP.fromLog(STDP.toLog(_get_RW.bottom_price) + (STDP.toLog(_get_RW.top_price)-STDP.toLog(_get_RW.bottom_price)) * levelValue.level)
- else
- levelValue.price := _get_RW.bottom_price + Diff * levelValue.level
- _get_RW
- else if _get_RW.fibreverse
- for i =-_get_RW.levels.size() +1 to 0
- levelValue = _get_RW.levels.get(-i)
- if _get_RW.ChartisLog
- levelValue.price := STDP.fromLog(STDP.toLog(_get_RW.bottom_price) + (STDP.toLog(_get_RW.top_price)-STDP.toLog(_get_RW.bottom_price)) * levelValue.level )
- else
- levelValue.price := _get_RW.top_price - Diff * levelValue.level
- _get_RW
- _Fibonacci_box.set(_itemnumber,_get_RW)
- _Fibonacci_box
- // @function fibonacci draw.
- // @description This function block uses the levels, prices and paramters set into the type_fibonacci_box(levels) and draws the fib on the chart
- // @returns returns lines labels and fills on the chart, no data returns
- export f_fib_draw(type_Fibonacci_box [] _Fibonacci_box, int _itemnumber = 0) =>
- string _text_output = na
- string _text_name = na
- _Fibonacci_box.get(_itemnumber).line_array.removeDeleteAll()
- _Fibonacci_box.get(_itemnumber).label_array.removeDeleteAll()
- _Fibonacci_box.get(_itemnumber).linefill_array.removeDeleteAll()
- if _Fibonacci_box.size() > 0 and _Fibonacci_box.size()-1 >= _itemnumber
- type_Fibonacci_box _get_box_RW = na
- _get_box_RW := _Fibonacci_box.get(_itemnumber)
- /////// L I N E S
- for i = 0 to array.size(_get_box_RW.levels)-1
- type_level _get_RW = na
- _get_RW := _get_box_RW.levels.get(i)
- _get_box_RW.line_array.push(
- line.new(
- x1= _get_box_RW.StartBar,
- y1= _get_RW.price,
- x2 = _get_box_RW.StopBar,
- y2 = _get_RW.price,
- xloc=xloc.bar_index,
- style= _get_RW.linetype,
- extend=extend.none,
- color=_get_box_RW.drawlines?_get_RW.fiblinecolor:na,
- width=_get_RW.linewidth)
- )
- /////// L A B L E S
- _text_price = ' ' + str.tostring(STDP.round_to_Str(_get_RW.price,_get_box_RW.decimals_price)) +
- ' (' +
- str.tostring(_get_box_RW.fibdrawreverse?(1-_get_RW.level):_get_RW.level) +
- ') ' +
- str.tostring(STDP.round_to_Str(_get_RW.price/close*100-100,_get_box_RW.decimals_percent)) +
- '% = ' +
- str.tostring(STDP.round_to_Str(_get_RW.price-close,_get_box_RW.decimals_price))
- _text_name := _get_RW.labeltext
- _text_short = _get_RW.labeltext + '\n' + STDP.shifting(_get_RW.textshift) + str.tostring(STDP.round_to_Str(_get_RW.price,_get_box_RW.decimals_price))
- if _get_RW.drawlabel == "none"
- _text_output := ''
- else if _get_RW.drawlabel == "all"
- _text_output := _text_price + ' ' + _text_name
- else if _get_RW.drawlabel == "price"
- _text_output := _text_price
- else if _get_RW.drawlabel == "short"
- _text_output := _text_short
- else if _get_RW.drawlabel == "name"
- _text_output := _text_name
- if _get_RW.drawlabel != "none" // "all", "price", "name", "none"
- _get_box_RW.label_array.push(label.new(
- x = _get_box_RW.StopBar,
- y = _get_RW.price,
- text = STDP.shifting(_get_RW.textshift) + _text_output,
- xloc = xloc.bar_index,
- yloc = yloc.price,
- color = na,
- style = label.style_none,
- textcolor = _get_RW.fibtextcolor,
- size = _get_RW.fibtextsize,
- textalign = text.align_left)
- )
- /////// I N F O B O X
- if i == _get_box_RW.rangeinfo_location and _get_box_RW.drawlabels
- rangemax = math.max(_get_box_RW.top_price,_get_box_RW.bottom_price)
- rangemin = math.min(_get_box_RW.top_price,_get_box_RW.bottom_price)
- _get_box_RW.label_array.push(label.new(
- x = _get_box_RW.StopBar,
- y = _get_RW.price,
- text = STDP.shifting(_get_box_RW.biginfo_textshift) + 'Range-Up = ' +
- str.tostring(STDP.round_to_Str((rangemax/rangemin)*100-100,_get_box_RW.decimals_percent)) +
- '% / ' +
- str.tostring(STDP.round_to_Str(math.abs(rangemin-rangemax),_get_box_RW.decimals_price)) +
- '\nRange-Down = ' + str.tostring(STDP.round_to_Str((rangemin/rangemax)*100-100,_get_box_RW.decimals_percent)) +
- '%\n',
- xloc=xloc.bar_index,
- yloc=yloc.price,
- color=na,
- style=label.style_none,
- textcolor=_get_box_RW.rangeinfo_color,
- size=_get_box_RW.rangeinfo_textsize,
- textalign=text.align_left))
- if _get_box_RW.drawfills
- for i_F = 0 to _get_box_RW.fills.size() - 1
- var type_fill _get_RW = na
- _get_RW := _get_box_RW.fills.get(i_F)
- for i_LA = 0 to (_get_box_RW.levels.size() - 1)
- if _get_RW.partner_A == i_LA and _get_RW.partner_B <= _get_box_RW.levels.size() and _get_RW.partner_B >= 0
- _get_box_RW.linefill_array.push(linefill.new(line1 = _get_box_RW.line_array.get(i_LA), line2 = _get_box_RW.line_array.get(_get_RW.partner_B), color = color.new(_get_RW.fill_color, _get_RW.transp) ))
- _Fibonacci_box
- ///// Fibonacci Box }
- // _____ ______ __ __ ____
- // | __ \| ____| \/ |/ __ \
- // | | | | |__ | \ / | | | |
- // | | | | __| | |\/| | | | |
- // | |__| | |____| | | | |__| |
- // |_____/|______|_| |_|\____/
- // _____ _ _ _____ _ _ _______ _____
- // |_ _| | \ | | | __ \ | | | | |__ __| / ____|
- // | | | \| | | |__) | | | | | | | | (___
- // | | | . ` | | ___/ | | | | | | \___ \
- // _| |_ | |\ | | | | |__| | | | ____) |
- // |_____| |_| \_| |_| \____/ |_| |_____/
- //setting manual inputs for the first element of Fibonacci_box {
- var type_Fibonacci_box [] Fibonacci_box = array.new<type_Fibonacci_box>(1,type_Fibonacci_box.new(na))
- var type_Fibonacci_box [] Fibonacci_box_live = array.new<type_Fibonacci_box>(1,type_Fibonacci_box.new(na))
- _get_box_RW = Fibonacci_box.get(0)
- //
- _get_box_RW.bottom_price := ta.lowest(100)
- _get_box_RW.top_price := ta.highest(100)
- _get_box_RW.StartBar := bar_index -95
- _get_box_RW.StopBar := bar_index
- _get_box_RW.fibreverse := input.bool(false,'Reverse Fib',group='Global', inline='11a')
- _get_box_RW.ChartisLog := input.bool(false,'Logarithmic',group='Global',inline='11a')
- _get_box_RW.rangeinfo_location := input.int(-1, 'Infobox Location')
- _get_box_RW.decimals_price := input.int(2,'Price decimals',group='decimals trimming',inline='1')
- _get_box_RW.decimals_percent := input.int(2,'Percent decimals',group='decimals trimming',inline='1')
- Fibonacci_box_fibtextcolor = input.color(color.rgb(128, 128, 128),'Text color',group='Global',inline='1a')
- Fibonacci_box_fibtextsize = input.string(size.small,'size', options = [size.tiny,size.small,size.normal,size.large,size.huge],group='Global',inline='1a')
- Fibonacci_box_fiblinecolor = input.color(color.rgb(126, 126, 126),'Linecolor',inline='1a')
- Fibonacci_box_line_width = input.int(3,'Linewidth',group='Linecolor')
- Fibonacci_box_fill_transp = input.int(60,'Transparency fillings',group='Fillings')
- _get_box_RW.drawlines := true
- _get_box_RW.drawfills := true
- _get_box_RW.drawlabels := true
- _get_box_RW.fibdrawreverse := false
- _get_box_RW.fibreverse := false
- _get_box_RW.levels := array.new<type_level>(0, type_level.new(na))
- _get_box_RW.fills := array.new<type_fill>(0, type_fill.new(na))
- _get_box_RW.line_array := array.new_line(0, line.new(x1 = na, y1 = na, x2 = na, y2 = na))
- _get_box_RW.linefill_array := array.new_linefill(0, linefill.new(line1 = na, line2 = na, color = na))
- _get_box_RW.label_array := array.new_label(0, label.new(x = na, y = na))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(1.0 ,'Level 0',group='Fiblevels 01',step=0.001),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = 'Top'
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.786 ,'Level 1',group='Fiblevels 01',step=0.001,inline='1a'),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.65 ,'Level 2',group='Fiblevels 23',step=0.001),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.618 ,'Level 3',group='Fiblevels 23',step=0.001,inline='1b'),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.5 ,'Level 4',group='Fiblevels 4',step=0.001),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.382 ,'Level 5',group='Fiblevels 56',step=0.001 ),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.35 ,'Level 6',group='Fiblevels 56',step=0.001,inline='1c'),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.236 ,'Level 7',group='Fiblevels 78',step=0.001 ),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
- ))
- _get_box_RW.levels.push(
- type_level.new(
- level = input.float(0.0 ,'Level 8',group='Fiblevels 78',step=0.001,inline='1d'),
- fiblinecolor = Fibonacci_box_fiblinecolor,
- fibtextsize = Fibonacci_box_fibtextsize,
- fibtextcolor = Fibonacci_box_fibtextcolor,
- price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = 'Bottom'
- ))
- //
- _1A = input(0, "partner1_A", group="Fill 1")
- _1B = input(1, "partner1_B", group="Fill 1")
- _1C = input.color(color.rgb(175, 76, 76, 0),'Fill-Color 0-1',group="Fill 1",inline='1a')
- _1D = input.int(50, "transparency", group='Fill 1')
- _2A = input(2, "partner2_A", group="Fill 2")
- _2B = input(3, "partner2_B", group="Fill 2")
- _2C = input.color(color.rgb(175, 172, 76, 0),'Fill-Color 2-3',group="Fill 2",inline='1b')
- _2D = input.int(50, "transparency", group='Fill 2')
- _3A = input(5, "partner3_A", group="Fill 3")
- _3B = input(6, "partner3_B", group="Fill 3")
- _3C = input.color(color.rgb(175, 172, 76, 0),'Fill-Color 5-6',group="Fill 3",inline='1c')
- _3D = input.int(50, "transparency", group='Fill 3')
- _4A = input(7, "partner4_A", group="Fill 4")
- _4B = input(8, "partner4_B", group="Fill 4")
- _4C = input.color(color.rgb(76, 175, 79, 0),'Fill-Color 7-8',group="Fill 4",inline='1d')
- _4D = input.int(50, "transparency", group='Fill 4')
- //
- _get_box_RW.fills.push(type_fill.new(partner_A = _1A, partner_B = _1B, fill_color = _1C, transp = _1D))
- _get_box_RW.fills.push(type_fill.new(partner_A = _2A, partner_B = _2B, fill_color = _2C, transp = _2D))
- _get_box_RW.fills.push(type_fill.new(partner_A = _3A, partner_B = _3B, fill_color = _3C, transp = _3D))
- _get_box_RW.fills.push(type_fill.new(partner_A = _4A, partner_B = _4B, fill_color = _4C, transp = _4D))
- Fibonacci_box.set(0, _get_box_RW.copy())
- if barstate.isfirst
- Fibonacci_box_live.set(0, _get_box_RW.copy())
- // }
- // ______ __ __ ______ _____
- // | ____| \ \ / / | ____| / ____|
- // | |__ \ V / | |__ | |
- // | __| > < | __| | |
- // | |____ / . \ | |____ | |____
- // |______| /_/ \_\ |______| \_____|
- // {
- Fibonacci_box := f_fib_calc(_Fibonacci_box = Fibonacci_box, _itemnumber = 0)
- /// HISTORY
- if bar_index % 100 == 0 and barstate.isconfirmed
- Fibonacci_box.unshift(Fibonacci_box.get(0).copy())
- while Fibonacci_box.size() > 5
- Fibonacci_box.pop()
- var int remember_index = na
- if bar_index % 100 == 0
- remember_index := bar_index + 2
- _get_RW_live = Fibonacci_box_live.get(0)
- _get_RW_live.bottom_price := ta.lowest(math.max(1,(bar_index-remember_index)))
- _get_RW_live.StartBar := remember_index
- _get_RW_live.StopBar := bar_index
- ////// }
- // _____ _____ __ __
- // | __ \ | __ \ /\ \ \ / /
- // | | | | | |__) | / \ \ \ /\ / /
- // | | | | | _ / / /\ \ \ \/ \/ /
- // | |__| | | | \ \ / ____ \ \ /\ /
- // |_____/ |_| \_\ /_/ \_\ \/ \/
- // ///// draw the fibboxes{
- if barstate.islast or barstate.islastconfirmedhistory
- if Fibonacci_box.size() > 0
- for i = -(Fibonacci_box.size()-1) to -1
- f_fib_draw(_Fibonacci_box = Fibonacci_box, _itemnumber = -i)
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement