Advertisement
xmd79

Mad_Fibonaccibox

Dec 9th, 2023
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.51 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © djmad
  3. // special thanks for you help and time Bjorgum
  4.  
  5. //@version=5
  6.  
  7. // @description TODO: add library description here
  8. library("Mad_Fibonaccibox", overlay = true)//, max_labels_count = 500, max_boxes_count = 500, max_lines_count = 500, max_bars_back = 1000)
  9.  
  10. import djmad/Mad_Standardparts/10 as STDP
  11.  
  12. // _______ __ __ _____ ______ _____
  13. // |__ __| \ \ / / | __ \ | ____| / ____|
  14. // | | \ \_/ / | |__) | | |__ | (___
  15. // | | \ / | ___/ | __| \___ \
  16. // | | | | | | | |____ ____) |
  17. // |_| |_| |_| |______| |_____/
  18.  
  19. ///// Fibonacci Box Types{
  20.  
  21. // @types for defining a lines and texts of a fibonacci box
  22. export type type_level
  23. float level = na
  24. float price = na
  25. bool drawline = true
  26. int linewidth = 1
  27. string linetype = line.style_solid
  28. color fiblinecolor = color.gray
  29. string drawlabel = "all" //"price", "name", "none"
  30. string labeltext = ''
  31. int textshift = 0
  32. color fibtextcolor = color.gray
  33. string fibtextsize = size.small
  34. int transp = 0
  35.  
  36. // @types for defining the fills of a fibonaccibox
  37. export type type_fill
  38. int partner_A = na
  39. int partner_B = na
  40. color fill_color = na
  41. int transp = 0
  42.  
  43. // @types for defining a fibonacci box
  44. export type type_Fibonacci_box
  45. float bottom_price = na
  46. float top_price = na
  47. int StartBar = na
  48. int StopBar = na
  49. array<type_level> levels = na
  50. array<type_fill> fills = na
  51. bool ChartisLog = false
  52. bool fibreverse = false
  53. bool fibdrawreverse = false
  54. int decimals_price = 2
  55. int decimals_percent = 2
  56. bool drawlines = true
  57. bool drawlabels = true
  58. bool drawfills = true
  59. bool draw_biginfo = true
  60. int biginfo_textshift = 0
  61. int rangeinfo_location = -1
  62. color rangeinfo_color = color.gray
  63. string rangeinfo_textsize = size.small
  64. array<line> line_array = na
  65. array<linefill> linefill_array = na
  66. array<label> label_array = na
  67.  
  68. // }
  69.  
  70. // ______ _____ ____ ____ ____ __ __
  71. //| ____| |_ _| | _ \ | _ \ / __ \ \ \ / /
  72. //| |__ | | | |_) | | |_) | | | | | \ V /
  73. //| __| | | | _ < | _ < | | | | > <
  74. //| | _| |_ | |_) | | |_) | | |__| | / . \
  75. //|_| |_____| |____/ |____/ \____/ /_/ \_\
  76.  
  77. // FIBBOX Functions {
  78.  
  79. // @function fibonacci calc.
  80. // @description This function block uses the levels and paramters set into the type_fibonacci_box(levels) and fills the corresponding array of prices.
  81. // @returns returns a type_Fibonacci_box with the filled data
  82. export f_fib_calc(type_Fibonacci_box [] _Fibonacci_box, int _itemnumber = 0) =>
  83. if _Fibonacci_box.size() > 0 and _Fibonacci_box.size()-1 >= _itemnumber
  84. type_Fibonacci_box _get_RW = _Fibonacci_box.get(_itemnumber)
  85. // type_Fibonacci_box _get_RW = _Fibonacci_box
  86. // if not na(_Fibonacci_box)
  87. float Diff = _get_RW.top_price-_get_RW.bottom_price
  88.  
  89. if not _get_RW.fibreverse
  90. for i = 0 to _get_RW.levels.size() -1
  91. levelValue = _get_RW.levels.get(i)
  92. if _get_RW.ChartisLog
  93. levelValue.price := STDP.fromLog(STDP.toLog(_get_RW.bottom_price) + (STDP.toLog(_get_RW.top_price)-STDP.toLog(_get_RW.bottom_price)) * levelValue.level)
  94. else
  95. levelValue.price := _get_RW.bottom_price + Diff * levelValue.level
  96. _get_RW
  97. else if _get_RW.fibreverse
  98. for i =-_get_RW.levels.size() +1 to 0
  99. levelValue = _get_RW.levels.get(-i)
  100. if _get_RW.ChartisLog
  101. levelValue.price := STDP.fromLog(STDP.toLog(_get_RW.bottom_price) + (STDP.toLog(_get_RW.top_price)-STDP.toLog(_get_RW.bottom_price)) * levelValue.level )
  102. else
  103. levelValue.price := _get_RW.top_price - Diff * levelValue.level
  104. _get_RW
  105. _Fibonacci_box.set(_itemnumber,_get_RW)
  106. _Fibonacci_box
  107.  
  108.  
  109. // @function fibonacci draw.
  110. // @description This function block uses the levels, prices and paramters set into the type_fibonacci_box(levels) and draws the fib on the chart
  111. // @returns returns lines labels and fills on the chart, no data returns
  112. export f_fib_draw(type_Fibonacci_box [] _Fibonacci_box, int _itemnumber = 0) =>
  113. string _text_output = na
  114. string _text_name = na
  115.  
  116. _Fibonacci_box.get(_itemnumber).line_array.removeDeleteAll()
  117. _Fibonacci_box.get(_itemnumber).label_array.removeDeleteAll()
  118. _Fibonacci_box.get(_itemnumber).linefill_array.removeDeleteAll()
  119.  
  120. if _Fibonacci_box.size() > 0 and _Fibonacci_box.size()-1 >= _itemnumber
  121. type_Fibonacci_box _get_box_RW = na
  122. _get_box_RW := _Fibonacci_box.get(_itemnumber)
  123.  
  124. /////// L I N E S
  125. for i = 0 to array.size(_get_box_RW.levels)-1
  126. type_level _get_RW = na
  127. _get_RW := _get_box_RW.levels.get(i)
  128. _get_box_RW.line_array.push(
  129. line.new(
  130. x1= _get_box_RW.StartBar,
  131. y1= _get_RW.price,
  132. x2 = _get_box_RW.StopBar,
  133. y2 = _get_RW.price,
  134. xloc=xloc.bar_index,
  135. style= _get_RW.linetype,
  136. extend=extend.none,
  137. color=_get_box_RW.drawlines?_get_RW.fiblinecolor:na,
  138. width=_get_RW.linewidth)
  139. )
  140.  
  141. /////// L A B L E S
  142. _text_price = ' ' + str.tostring(STDP.round_to_Str(_get_RW.price,_get_box_RW.decimals_price)) +
  143. ' (' +
  144. str.tostring(_get_box_RW.fibdrawreverse?(1-_get_RW.level):_get_RW.level) +
  145. ') ' +
  146. str.tostring(STDP.round_to_Str(_get_RW.price/close*100-100,_get_box_RW.decimals_percent)) +
  147. '% = ' +
  148. str.tostring(STDP.round_to_Str(_get_RW.price-close,_get_box_RW.decimals_price))
  149.  
  150. _text_name := _get_RW.labeltext
  151.  
  152. _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))
  153.  
  154.  
  155. if _get_RW.drawlabel == "none"
  156. _text_output := ''
  157. else if _get_RW.drawlabel == "all"
  158. _text_output := _text_price + ' ' + _text_name
  159. else if _get_RW.drawlabel == "price"
  160. _text_output := _text_price
  161. else if _get_RW.drawlabel == "short"
  162. _text_output := _text_short
  163. else if _get_RW.drawlabel == "name"
  164. _text_output := _text_name
  165.  
  166. if _get_RW.drawlabel != "none" // "all", "price", "name", "none"
  167. _get_box_RW.label_array.push(label.new(
  168. x = _get_box_RW.StopBar,
  169. y = _get_RW.price,
  170. text = STDP.shifting(_get_RW.textshift) + _text_output,
  171. xloc = xloc.bar_index,
  172. yloc = yloc.price,
  173. color = na,
  174. style = label.style_none,
  175. textcolor = _get_RW.fibtextcolor,
  176. size = _get_RW.fibtextsize,
  177. textalign = text.align_left)
  178. )
  179.  
  180. /////// I N F O B O X
  181. if i == _get_box_RW.rangeinfo_location and _get_box_RW.drawlabels
  182. rangemax = math.max(_get_box_RW.top_price,_get_box_RW.bottom_price)
  183. rangemin = math.min(_get_box_RW.top_price,_get_box_RW.bottom_price)
  184.  
  185. _get_box_RW.label_array.push(label.new(
  186. x = _get_box_RW.StopBar,
  187. y = _get_RW.price,
  188. text = STDP.shifting(_get_box_RW.biginfo_textshift) + 'Range-Up = ' +
  189. str.tostring(STDP.round_to_Str((rangemax/rangemin)*100-100,_get_box_RW.decimals_percent)) +
  190. '% / ' +
  191. str.tostring(STDP.round_to_Str(math.abs(rangemin-rangemax),_get_box_RW.decimals_price)) +
  192. '\nRange-Down = ' + str.tostring(STDP.round_to_Str((rangemin/rangemax)*100-100,_get_box_RW.decimals_percent)) +
  193. '%\n',
  194. xloc=xloc.bar_index,
  195. yloc=yloc.price,
  196. color=na,
  197. style=label.style_none,
  198. textcolor=_get_box_RW.rangeinfo_color,
  199. size=_get_box_RW.rangeinfo_textsize,
  200. textalign=text.align_left))
  201.  
  202. if _get_box_RW.drawfills
  203. for i_F = 0 to _get_box_RW.fills.size() - 1
  204. var type_fill _get_RW = na
  205. _get_RW := _get_box_RW.fills.get(i_F)
  206. for i_LA = 0 to (_get_box_RW.levels.size() - 1)
  207. if _get_RW.partner_A == i_LA and _get_RW.partner_B <= _get_box_RW.levels.size() and _get_RW.partner_B >= 0
  208. _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) ))
  209. _Fibonacci_box
  210. ///// Fibonacci Box }
  211.  
  212.  
  213.  
  214.  
  215. // _____ ______ __ __ ____
  216. // | __ \| ____| \/ |/ __ \
  217. // | | | | |__ | \ / | | | |
  218. // | | | | __| | |\/| | | | |
  219. // | |__| | |____| | | | |__| |
  220. // |_____/|______|_| |_|\____/
  221.  
  222. // _____ _ _ _____ _ _ _______ _____
  223. // |_ _| | \ | | | __ \ | | | | |__ __| / ____|
  224. // | | | \| | | |__) | | | | | | | | (___
  225. // | | | . ` | | ___/ | | | | | | \___ \
  226. // _| |_ | |\ | | | | |__| | | | ____) |
  227. // |_____| |_| \_| |_| \____/ |_| |_____/
  228.  
  229. //setting manual inputs for the first element of Fibonacci_box {
  230.  
  231. var type_Fibonacci_box [] Fibonacci_box = array.new<type_Fibonacci_box>(1,type_Fibonacci_box.new(na))
  232. var type_Fibonacci_box [] Fibonacci_box_live = array.new<type_Fibonacci_box>(1,type_Fibonacci_box.new(na))
  233. _get_box_RW = Fibonacci_box.get(0)
  234. //
  235. _get_box_RW.bottom_price := ta.lowest(100)
  236. _get_box_RW.top_price := ta.highest(100)
  237. _get_box_RW.StartBar := bar_index -95
  238. _get_box_RW.StopBar := bar_index
  239.  
  240. _get_box_RW.fibreverse := input.bool(false,'Reverse Fib',group='Global', inline='11a')
  241. _get_box_RW.ChartisLog := input.bool(false,'Logarithmic',group='Global',inline='11a')
  242. _get_box_RW.rangeinfo_location := input.int(-1, 'Infobox Location')
  243. _get_box_RW.decimals_price := input.int(2,'Price decimals',group='decimals trimming',inline='1')
  244. _get_box_RW.decimals_percent := input.int(2,'Percent decimals',group='decimals trimming',inline='1')
  245.  
  246. Fibonacci_box_fibtextcolor = input.color(color.rgb(128, 128, 128),'Text color',group='Global',inline='1a')
  247. Fibonacci_box_fibtextsize = input.string(size.small,'size', options = [size.tiny,size.small,size.normal,size.large,size.huge],group='Global',inline='1a')
  248. Fibonacci_box_fiblinecolor = input.color(color.rgb(126, 126, 126),'Linecolor',inline='1a')
  249. Fibonacci_box_line_width = input.int(3,'Linewidth',group='Linecolor')
  250.  
  251. Fibonacci_box_fill_transp = input.int(60,'Transparency fillings',group='Fillings')
  252.  
  253. _get_box_RW.drawlines := true
  254. _get_box_RW.drawfills := true
  255. _get_box_RW.drawlabels := true
  256. _get_box_RW.fibdrawreverse := false
  257. _get_box_RW.fibreverse := false
  258.  
  259. _get_box_RW.levels := array.new<type_level>(0, type_level.new(na))
  260. _get_box_RW.fills := array.new<type_fill>(0, type_fill.new(na))
  261. _get_box_RW.line_array := array.new_line(0, line.new(x1 = na, y1 = na, x2 = na, y2 = na))
  262. _get_box_RW.linefill_array := array.new_linefill(0, linefill.new(line1 = na, line2 = na, color = na))
  263. _get_box_RW.label_array := array.new_label(0, label.new(x = na, y = na))
  264.  
  265. _get_box_RW.levels.push(
  266. type_level.new(
  267. level = input.float(1.0 ,'Level 0',group='Fiblevels 01',step=0.001),
  268. fiblinecolor = Fibonacci_box_fiblinecolor,
  269. fibtextsize = Fibonacci_box_fibtextsize,
  270. fibtextcolor = Fibonacci_box_fibtextcolor,
  271. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = 'Top'
  272. ))
  273. _get_box_RW.levels.push(
  274. type_level.new(
  275. level = input.float(0.786 ,'Level 1',group='Fiblevels 01',step=0.001,inline='1a'),
  276. fiblinecolor = Fibonacci_box_fiblinecolor,
  277. fibtextsize = Fibonacci_box_fibtextsize,
  278. fibtextcolor = Fibonacci_box_fibtextcolor,
  279. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
  280. ))
  281. _get_box_RW.levels.push(
  282. type_level.new(
  283. level = input.float(0.65 ,'Level 2',group='Fiblevels 23',step=0.001),
  284. fiblinecolor = Fibonacci_box_fiblinecolor,
  285. fibtextsize = Fibonacci_box_fibtextsize,
  286. fibtextcolor = Fibonacci_box_fibtextcolor,
  287. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
  288. ))
  289. _get_box_RW.levels.push(
  290. type_level.new(
  291. level = input.float(0.618 ,'Level 3',group='Fiblevels 23',step=0.001,inline='1b'),
  292. fiblinecolor = Fibonacci_box_fiblinecolor,
  293. fibtextsize = Fibonacci_box_fibtextsize,
  294. fibtextcolor = Fibonacci_box_fibtextcolor,
  295. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
  296. ))
  297. _get_box_RW.levels.push(
  298. type_level.new(
  299. level = input.float(0.5 ,'Level 4',group='Fiblevels 4',step=0.001),
  300. fiblinecolor = Fibonacci_box_fiblinecolor,
  301. fibtextsize = Fibonacci_box_fibtextsize,
  302. fibtextcolor = Fibonacci_box_fibtextcolor,
  303. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
  304. ))
  305. _get_box_RW.levels.push(
  306. type_level.new(
  307. level = input.float(0.382 ,'Level 5',group='Fiblevels 56',step=0.001 ),
  308. fiblinecolor = Fibonacci_box_fiblinecolor,
  309. fibtextsize = Fibonacci_box_fibtextsize,
  310. fibtextcolor = Fibonacci_box_fibtextcolor,
  311. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
  312. ))
  313. _get_box_RW.levels.push(
  314. type_level.new(
  315. level = input.float(0.35 ,'Level 6',group='Fiblevels 56',step=0.001,inline='1c'),
  316. fiblinecolor = Fibonacci_box_fiblinecolor,
  317. fibtextsize = Fibonacci_box_fibtextsize,
  318. fibtextcolor = Fibonacci_box_fibtextcolor,
  319. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
  320. ))
  321. _get_box_RW.levels.push(
  322. type_level.new(
  323. level = input.float(0.236 ,'Level 7',group='Fiblevels 78',step=0.001 ),
  324. fiblinecolor = Fibonacci_box_fiblinecolor,
  325. fibtextsize = Fibonacci_box_fibtextsize,
  326. fibtextcolor = Fibonacci_box_fibtextcolor,
  327. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = ''
  328. ))
  329. _get_box_RW.levels.push(
  330. type_level.new(
  331. level = input.float(0.0 ,'Level 8',group='Fiblevels 78',step=0.001,inline='1d'),
  332. fiblinecolor = Fibonacci_box_fiblinecolor,
  333. fibtextsize = Fibonacci_box_fibtextsize,
  334. fibtextcolor = Fibonacci_box_fibtextcolor,
  335. price = na, drawline = true, linewidth = Fibonacci_box_line_width, linetype = line.style_solid, drawlabel = "all", transp = 0, labeltext = 'Bottom'
  336. ))
  337.  
  338. //
  339. _1A = input(0, "partner1_A", group="Fill 1")
  340. _1B = input(1, "partner1_B", group="Fill 1")
  341. _1C = input.color(color.rgb(175, 76, 76, 0),'Fill-Color 0-1',group="Fill 1",inline='1a')
  342. _1D = input.int(50, "transparency", group='Fill 1')
  343. _2A = input(2, "partner2_A", group="Fill 2")
  344. _2B = input(3, "partner2_B", group="Fill 2")
  345. _2C = input.color(color.rgb(175, 172, 76, 0),'Fill-Color 2-3',group="Fill 2",inline='1b')
  346. _2D = input.int(50, "transparency", group='Fill 2')
  347. _3A = input(5, "partner3_A", group="Fill 3")
  348. _3B = input(6, "partner3_B", group="Fill 3")
  349. _3C = input.color(color.rgb(175, 172, 76, 0),'Fill-Color 5-6',group="Fill 3",inline='1c')
  350. _3D = input.int(50, "transparency", group='Fill 3')
  351. _4A = input(7, "partner4_A", group="Fill 4")
  352. _4B = input(8, "partner4_B", group="Fill 4")
  353. _4C = input.color(color.rgb(76, 175, 79, 0),'Fill-Color 7-8',group="Fill 4",inline='1d')
  354. _4D = input.int(50, "transparency", group='Fill 4')
  355. //
  356. _get_box_RW.fills.push(type_fill.new(partner_A = _1A, partner_B = _1B, fill_color = _1C, transp = _1D))
  357. _get_box_RW.fills.push(type_fill.new(partner_A = _2A, partner_B = _2B, fill_color = _2C, transp = _2D))
  358. _get_box_RW.fills.push(type_fill.new(partner_A = _3A, partner_B = _3B, fill_color = _3C, transp = _3D))
  359. _get_box_RW.fills.push(type_fill.new(partner_A = _4A, partner_B = _4B, fill_color = _4C, transp = _4D))
  360.  
  361. Fibonacci_box.set(0, _get_box_RW.copy())
  362.  
  363. if barstate.isfirst
  364. Fibonacci_box_live.set(0, _get_box_RW.copy())
  365. // }
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374. // ______ __ __ ______ _____
  375. // | ____| \ \ / / | ____| / ____|
  376. // | |__ \ V / | |__ | |
  377. // | __| > < | __| | |
  378. // | |____ / . \ | |____ | |____
  379. // |______| /_/ \_\ |______| \_____|
  380.  
  381. // {
  382. Fibonacci_box := f_fib_calc(_Fibonacci_box = Fibonacci_box, _itemnumber = 0)
  383.  
  384. /// HISTORY
  385.  
  386. if bar_index % 100 == 0 and barstate.isconfirmed
  387. Fibonacci_box.unshift(Fibonacci_box.get(0).copy())
  388.  
  389. while Fibonacci_box.size() > 5
  390. Fibonacci_box.pop()
  391.  
  392. var int remember_index = na
  393.  
  394. if bar_index % 100 == 0
  395. remember_index := bar_index + 2
  396.  
  397. _get_RW_live = Fibonacci_box_live.get(0)
  398. _get_RW_live.bottom_price := ta.lowest(math.max(1,(bar_index-remember_index)))
  399. _get_RW_live.StartBar := remember_index
  400. _get_RW_live.StopBar := bar_index
  401.  
  402. ////// }
  403.  
  404. // _____ _____ __ __
  405. // | __ \ | __ \ /\ \ \ / /
  406. // | | | | | |__) | / \ \ \ /\ / /
  407. // | | | | | _ / / /\ \ \ \/ \/ /
  408. // | |__| | | | \ \ / ____ \ \ /\ /
  409. // |_____/ |_| \_\ /_/ \_\ \/ \/
  410.  
  411. // ///// draw the fibboxes{
  412.  
  413. if barstate.islast or barstate.islastconfirmedhistory
  414. if Fibonacci_box.size() > 0
  415. for i = -(Fibonacci_box.size()-1) to -1
  416. f_fib_draw(_Fibonacci_box = Fibonacci_box, _itemnumber = -i)
  417.  
  418. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement