Advertisement
xmd79

Auto Fibonacci Of USA's Market Hour

Jan 17th, 2024
354
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.18 KB | None | 0 0
  1. // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2.  
  3. //@version=5
  4. indicator("Auto Fibonacci Of USA's Market Hour", overlay=true)
  5. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  6. //# * ══════════════════════════════════════════════════════════════════════════════════════════════
  7. //# *
  8. //# * Study : Auto Fibonacci Of USA's Market Hour
  9. //# *
  10. //# * Revision History
  11. //# * Release : Jan 018, 2024
  12. //# * Update : ver.001
  13. //# *
  14. //# * ══════════════════════════════════════════════════════════════════════════════════════════════
  15. // ══════════════════════════════════════════════════════════════════════════════════════════════════ //
  16.  
  17.  
  18. ////////////////////////////
  19. //// Setting Variables ////
  20. ////////////////////////////
  21.  
  22. // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  23. // Plot Setting Variables--------------------------------------------------------------------------------------------------------------------------------------------------- //
  24. isFib = input.bool (defval = true, title = "Set Fibonacci     ", group = "Plot Setting", inline = "Plot Setting")
  25. isFill = input.bool (defval = false, title = "Fill Fibonacci     ", group = "Plot Setting", inline = "Plot Setting")
  26. showHistory = input.bool (defval = false, title = "Show History", group = "Plot Setting", inline = "Plot Setting")
  27.  
  28. timezone = input.string (defval = "Asia/Seoul", title = "Timezone", group = "Indicator settings1", options = ["Etc/UTC", "Asia/Seoul", "Europe/London", "America/New_York"])
  29. labelSize = input.string (defval = "Normal", title = "Label Size", group = "Indicator settings1", options = ["Small", "Normal"])
  30. lineWidth = input.int (defval = 1, minval = 1, maxval = 10, title = "Line Width", group = "Indicator settings1")
  31. lineTransparency = input.int (defval = 0, minval = 0, maxval = 100, title = "Line Transparency", group = "Indicator settings2")
  32. backgroundTransparency = input.int (defval = 85, minval = 0, maxval = 100, title = "Background Transparency", group = "Indicator settings2")
  33. // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  34.  
  35. ////////////////////////////
  36. //// Element Variables ////
  37. ////////////////////////////
  38.  
  39. // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  40. // Plot Elements------------------------------------------------------------------------------------------------------------------------------------------------------------ //
  41. show_0 = input(true, "", inline = "Level0")
  42. value_0 = input(0, "", inline = "Level0")
  43. color_0 = input(#787b86, "", inline = "Level0")
  44. //width_0 = input(1, "", inline = "Level0")
  45.  
  46. show_1 = input(true, "", inline = "Level0")
  47. value_1 = input(1, "", inline = "Level0")
  48. color_1 = input(#f44336, "", inline = "Level0")
  49. //width_1 = input(1, "", inline = "Level0")
  50.  
  51. show_2 = input(true, "", inline = "Level1")
  52. value_2 = input(1.5, "", inline = "Level1")
  53. color_2 = input(#81c784, "", inline = "Level1")
  54. //width_2 = input(1, "", inline = "Level1")
  55.  
  56. show_3 = input(true, "", inline = "Level1")
  57. value_3 = input(-0.5, "", inline = "Level1")
  58. color_3 = input(#4caf50, "", inline = "Level1")
  59. //width_3 = input(1, "", inline = "Level1")
  60.  
  61. show_4 = input(true, "", inline = "Level2")
  62. value_4 = input(0.5, "", inline = "Level2")
  63. color_4 = input(#009688, "", inline = "Level2")
  64. //width_4 = input(1, "", inline = "Level2")
  65.  
  66. show_5 = input(false, "", inline = "Level2")
  67. value_5 = input(-0.118, "", inline = "Level2")
  68. color_5 = input(#009688, "", inline = "Level2")
  69. //width_5 = input(1, "", inline = "Level2")
  70.  
  71. show_6 = input(true, "", inline = "Level3")
  72. value_6 = input(0.264, "", inline = "Level3")
  73. color_6 = input(#64b5f6, "", inline = "Level3")
  74. //width_6 = input(1, "", inline = "Level3")
  75.  
  76. show_7 = input(true, "", inline = "Level3")
  77. value_7 = input(0.882, "", inline = "Level3")
  78. color_7 = input(#787b86, "", inline = "Level3")
  79. //width_7 = input(1, "", inline = "Level3")
  80.  
  81. show_8 = input(false, "", inline = "Level4")
  82. value_8 = input(1.118, "", inline = "Level4")
  83. color_8 = input(#81c784, "", inline = "Level4")
  84. //width_8 = input(1, "", inline = "Level4")
  85.  
  86. show_9 = input(false, "", inline = "Level4")
  87. value_9 = input(0.736, "", inline = "Level4")
  88. color_9 = input(#f44336, "", inline = "Level4")
  89. //width_9 = input(1, "", inline = "Level5")
  90.  
  91. show_10 = input(true, "", inline = "Level5")
  92. value_10 = input(1.286, "", inline = "Level5")
  93. color_10 = input(#2962ff, "", inline = "Level5")
  94. //width_10 = input(1, "", inline = "Level5")
  95.  
  96. show_11 = input(false, "", inline = "Level5")
  97. value_11 = input(1.772, "", inline = "Level5")
  98. color_11 = input(#2962ff, "", inline = "Level5")
  99. //width_11 = input(1, "", inline = "Level5")
  100.  
  101. show_12 = input(true, "", inline = "Level6")
  102. value_12 = input(-0.772, "", inline = "Level6")
  103. color_12 = input(#f44336, "", inline = "Level6")
  104. //width_12 = input(1, "", inline = "Level6")
  105.  
  106. show_13 = input(false, "", inline = "Level6")
  107. value_13 = input(2.118, "", inline = "Level6")
  108. color_13 = input(#f44336, "", inline = "Level6")
  109. //width_13 = input(1, "", inline = "Level6")
  110.  
  111. show_14 = input(true, "", inline = "Level7")
  112. value_14 = input(2.5, "", inline = "Level7")
  113. color_14 = input(#9c27b0, "", inline = "Level7")
  114. //width_14 = input(1, "", inline = "Level7")
  115.  
  116. show_15 = input(false, "", inline = "Level7")
  117. value_15 = input(3.118, "", inline = "Level7")
  118. color_15 = input(#9c27b0, "", inline = "Level7")
  119. //width_15 = input(1, "", inline = "Level7")
  120.  
  121. show_16 = input(true, "", inline = "Level8")
  122. value_16 = input(-1.118, "", inline = "Level8")
  123. color_16 = input(#e91e63, "", inline = "Level8")
  124. //width_16 = input(1, "", inline = "Level8")
  125.  
  126. show_17 = input(false, "", inline = "Level8")
  127. value_17 = input(-2.118, "", inline = "Level8")
  128. color_17 = input(#81c784, "", inline = "Level8")
  129. //width_17 = input(1, "", inline = "Level8")
  130.  
  131. show_18 = input(false, "", inline = "Level9")
  132. value_18 = input(-1.5, "", inline = "Level9")
  133. color_18 = input(#f44336, "", inline = "Level9")
  134. //width_18 = input(1, "", inline = "Level9")
  135.  
  136. show_19 = input(false, "", inline = "Level9")
  137. value_19 = input(-1, "", inline = "Level9")
  138. color_19 = input(#81c784, "", inline = "Level9")
  139. //width_19 = input(1, "", inline = "Level9")
  140.  
  141. show_20 = input(false, "", inline = "Level10")
  142. value_20 = input(0.5, "", inline = "Level10")
  143. color_20 = input(#009688, "", inline = "Level10")
  144. //width_20 = input(1, "", inline = "Level10")
  145.  
  146. show_21 = input(false, "", inline = "Level10")
  147. value_21 = input(0.5, "", inline = "Level10")
  148. color_21 = input(#009688, "", inline = "Level10")
  149. //width_21 = input(1, "", inline = "Level10")
  150. // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //
  151.  
  152.  
  153. ////////////////////////////
  154. //// Global Variables ////
  155. ////////////////////////////
  156.  
  157. //Input Data
  158. //Constance
  159. //Index Timestamp
  160.  
  161. //Timeframe Variables(string -> int)
  162. int currentPeriod = switch timeframe.period
  163. "1" => 1
  164. "3" => 3
  165. "5" => 5
  166. "15" => 15
  167. "30" => 30
  168. "60" => 60
  169. "240" => 240
  170. "D" => 3600
  171. "W" => 10080
  172.  
  173. //Unit Bar Timestamp
  174. int unit_bar_timestamp = time[0]-time[1]
  175.  
  176. //USA_Market_Time
  177. int USA_Market_Open_Hour = 09
  178. int USA_Market_Open_Minute = 30
  179. int USA_Market_Close_Hour = 16
  180. int USA_Market_Close_Minute = 00
  181.  
  182. //Today's Session Timestamp
  183. Session_start_timestamp = timestamp ("America/New_York" , year, month, dayofmonth-1, USA_Market_Open_Hour, USA_Market_Open_Minute, 00)
  184. Session_end_timestamp = timestamp ("America/New_York" , year, month, dayofmonth-1, USA_Market_Close_Hour, USA_Market_Close_Minute, 00)
  185.  
  186. //Session Diff Timestamp
  187. Session_Diff_Index = (Session_end_timestamp - Session_start_timestamp)/unit_bar_timestamp
  188.  
  189. //Variable
  190. //Current Timestamp
  191. current_timestamp = time
  192.  
  193.  
  194. ////////////////////////////
  195. //// Method ////
  196. ////////////////////////////
  197.  
  198. //Method to draw line
  199. f_drawLineX(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width) =>
  200. var id = line.new(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width)
  201.  
  202. if _y1 > 0 and _y2 > 0
  203. line.set_xy1(id, _x1, _y1)
  204. line.set_xy2(id, _x2, _y2)
  205. line.set_color(id, color=color.new(_color,lineTransparency))
  206. else
  207. line.set_xy1(id, _x1, close)
  208. line.set_xy2(id, _x2, close)
  209. line.set_color(id, color=color.new(#00000000,lineTransparency))
  210. id
  211.  
  212. //Method to draw label
  213. f_drawLabelX(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip) =>
  214. var id = label.new(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip)
  215. label.set_text(id, _text)
  216. label.set_tooltip(id, _tooltip)
  217.  
  218. if _y > 0
  219. label.set_xy(id, _x, _y)
  220. label.set_textcolor(id, _textcolor)
  221. else
  222. label.set_xy(id, _x, close)
  223. label.set_textcolor(id, #00000000)
  224.  
  225. //Method to find the highest price in session time
  226. findDayH(_session_start_timestamp) =>
  227.  
  228. Session_Start_Index = current_timestamp - _session_start_timestamp
  229.  
  230. dayHigh = high[Session_Start_Index/unit_bar_timestamp]
  231.  
  232. for i = 0 to Session_Diff_Index
  233. index = Session_Start_Index - i * unit_bar_timestamp
  234.  
  235. if dayHigh < high[index/unit_bar_timestamp]
  236. dayHigh := high[index/unit_bar_timestamp]
  237. dayHigh
  238.  
  239. //Method to find the lowest price in session time
  240. findDayL(_session_start_timestamp) =>
  241.  
  242. Session_Start_Index = current_timestamp - _session_start_timestamp
  243.  
  244. dayLow = low[Session_Start_Index/unit_bar_timestamp]
  245.  
  246. for i = 0 to Session_Diff_Index
  247. index = Session_Start_Index - i * unit_bar_timestamp
  248.  
  249. if dayLow > low[index/unit_bar_timestamp]
  250. dayLow := low[index/unit_bar_timestamp]
  251. dayLow
  252.  
  253. //Plot Method
  254. f_processLevelX(_session_start_timestamp, _show, _value, _color, _width, _lineIdOther) =>
  255. if isFib
  256.  
  257. Session_Start_Timestamp = _session_start_timestamp
  258. Bar_Start_Timestamp = _session_start_timestamp
  259. Bar_End_Timestamp = int(Bar_Start_Timestamp + (16 - 9.5 + 24) * 60 * 60 * 1000) // 09:30 - 16:00(Tomorrow) Difference Between Two Timestamp
  260.  
  261.  
  262. Session_Start_Index = current_timestamp - Session_Start_Timestamp
  263.  
  264. dayH = findDayH(Session_Start_Timestamp)
  265. dayL = findDayL(Session_Start_Timestamp)
  266. dayDiff = math.abs(dayH - dayL)
  267.  
  268. price = dayDiff * _value + dayL
  269.  
  270. if _show
  271. bar = bar_index[Session_Start_Index/unit_bar_timestamp]
  272. style = label.style_label_right
  273. size = labelSize == 'Small' ? size.small : size.normal
  274.  
  275. lineId = f_drawLineX(Bar_Start_Timestamp, price, Bar_End_Timestamp, price, xloc.bar_time, extend.none, _color, line.style_solid, _width)
  276.  
  277. if not showHistory
  278. f_drawLabelX(bar, price, str.tostring(_value) + "(" + str.tostring(price, format.mintick) + ")", xloc.bar_index, yloc.price, #00000000, style, _color, size, text.align_right, str.tostring(price, format.mintick))
  279.  
  280. if isFill
  281. linefill.new(lineId, _lineIdOther, color = color.new(_color, backgroundTransparency))
  282. lineId
  283. else
  284. _lineIdOther
  285.  
  286.  
  287. ////////////////////////////
  288. //// Plot ////
  289. ////////////////////////////
  290.  
  291. //Plot
  292. plotFibonacci(_session_start_timestamp) =>
  293. if currentPeriod <= 30
  294. lineId_0 = f_processLevelX(_session_start_timestamp, show_0, value_0, color_0, lineWidth, line(na))
  295. lineId_1 = f_processLevelX(_session_start_timestamp, show_1, value_1, color_1, lineWidth, lineId_0)
  296. lineId_2 = f_processLevelX(_session_start_timestamp, show_2, value_2, color_2, lineWidth, lineId_1)
  297. lineId_3 = f_processLevelX(_session_start_timestamp, show_3, value_3, color_3, lineWidth, lineId_2)
  298. lineId_4 = f_processLevelX(_session_start_timestamp, show_4, value_4, color_4, lineWidth, lineId_3)
  299. lineId_5 = f_processLevelX(_session_start_timestamp, show_5, value_5, color_5, lineWidth, lineId_4)
  300. lineId_6 = f_processLevelX(_session_start_timestamp, show_6, value_6, color_6, lineWidth, lineId_5)
  301. lineId_7 = f_processLevelX(_session_start_timestamp, show_7, value_7, color_7, lineWidth, lineId_6)
  302. lineId_8 = f_processLevelX(_session_start_timestamp, show_8, value_8, color_8, lineWidth, lineId_7)
  303. lineId_9 = f_processLevelX(_session_start_timestamp, show_9, value_9, color_9, lineWidth, lineId_8)
  304. lineId_10 = f_processLevelX(_session_start_timestamp, show_10, value_10, color_10, lineWidth, lineId_9)
  305. lineId_11 = f_processLevelX(_session_start_timestamp, show_11, value_11, color_11, lineWidth, lineId_10)
  306. lineId_12 = f_processLevelX(_session_start_timestamp, show_12, value_12, color_12, lineWidth, lineId_11)
  307. lineId_13 = f_processLevelX(_session_start_timestamp, show_13, value_13, color_13, lineWidth, lineId_12)
  308. lineId_14 = f_processLevelX(_session_start_timestamp, show_14, value_14, color_14, lineWidth, lineId_13)
  309. lineId_15 = f_processLevelX(_session_start_timestamp, show_15, value_15, color_15, lineWidth, lineId_14)
  310. lineId_16 = f_processLevelX(_session_start_timestamp, show_16, value_16, color_16, lineWidth, lineId_15)
  311. lineId_17 = f_processLevelX(_session_start_timestamp, show_17, value_17, color_17, lineWidth, lineId_16)
  312. lineId_18 = f_processLevelX(_session_start_timestamp, show_18, value_18, color_18, lineWidth, lineId_17)
  313. lineId_19 = f_processLevelX(_session_start_timestamp, show_19, value_19, color_19, lineWidth, lineId_18)
  314. lineId_20 = f_processLevelX(_session_start_timestamp, show_20, value_20, color_20, lineWidth, lineId_19)
  315. lineId_21 = f_processLevelX(_session_start_timestamp, show_21, value_21, color_21, lineWidth, lineId_20)
  316.  
  317. plotFibonacci(Session_start_timestamp)
  318.  
  319.  
  320. if showHistory
  321. int unit_day_timestamp = 60 * 60 * 24 * 1000
  322.  
  323. Session_start_timestamp -= unit_day_timestamp
  324. plotFibonacci(Session_start_timestamp)
  325. Session_start_timestamp -= unit_day_timestamp
  326. plotFibonacci(Session_start_timestamp)
  327. Session_start_timestamp -= unit_day_timestamp
  328. plotFibonacci(Session_start_timestamp)
  329. Session_start_timestamp -= unit_day_timestamp
  330. plotFibonacci(Session_start_timestamp)
  331.  
Advertisement
Comments
  • earnmoney9022
    1 year
    # text 0.12 KB | 0 1
    1. download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement