Advertisement
xmd79

Multi Timeframe Supply & Demand zones

Oct 19th, 2023
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.19 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. // © Lenny_Kiruthu
  3.  
  4. //@version=5
  5. indicator(title = "Multi Timeframe Supply & Demand zones"
  6. , shorttitle = "MTF SnD"
  7. , overlay = true
  8. , max_bars_back = 500
  9. , max_lines_count = 500
  10. , max_boxes_count = 500)
  11.  
  12. // Constants
  13. Transparent_Color = color.new(color.white, 100)
  14.  
  15. // Groups
  16. General_Settings_group = "-------General Settings-------"
  17. Timeframe_1_Group = "-------Timeframe 1 Settings--------"
  18. Timeframe_2_Group = "-------Timeframe 2 Settings--------"
  19. Timeframe_3_Group = "-------Timeframe 3 Settings--------"
  20.  
  21. // Tooltips
  22. Timeframe_Tooltip = "If set to chart is true no need to alter these two inputs."
  23. Set_To_Chart_Tooltip = "If set to chart is set to true, there is no need to alter the Timeframe inputs, it will automatically configure itself to the charts timeframe."
  24. Lower_Timeframe_Tooltip = "If set to true and chart timeframe is higher than the choosen timeframe, supply and demand will not display. Note plotting ltf structure on a htf will provide inaccurate plots."
  25. Mitigation_Tooltip = "If set to true supply zones with a high above them or a demand zone with a low below them will be removed since they are considered mitigated if false the close value will be used for both supply and demand."
  26. Alert_Tooltip = "After setting the alert to true or false head over to the alert dialog box and activate the any alert function under the indicator."
  27. Hide_Demand = "Hide or show the demand zones of the specific timeframe."
  28. Hide_Supply = "Hide or show the supply zones of the specific timeframe."
  29. Hide_Timeframe = "Hide or show the entire timeframe."
  30.  
  31. // General Settings
  32. Hide_All_Demand = input.bool(defval = false, title = "Hide all demand zones", group = General_Settings_group)
  33. Hide_All_Supply = input.bool(defval = false, title = "Hide all supply zones", group = General_Settings_group)
  34. Show_Only_On_Lower_Timeframes = input.bool(defval = true, title = "Show Supply and Demand only on Lower Timeframes", group = General_Settings_group, tooltip = Lower_Timeframe_Tooltip)
  35.  
  36. // User Inputs
  37. // Timeframe 1 Settings
  38. TF_1_Chart_Feature = input.bool(defval = false, title = "Set Timeframe to Chart", group = Timeframe_1_Group, tooltip = Set_To_Chart_Tooltip)
  39. TF_1_Use_High_Low = input.bool(defval = false, title = "Use High/Low to mitigates zones", group = Timeframe_1_Group, tooltip = Mitigation_Tooltip)
  40. TF_1_Show_Demand = input.bool(defval = true, title = "Show Demand", group = Timeframe_1_Group, tooltip = Hide_Demand)
  41. TF_1_Show_Supply = input.bool(defval = true, title = "Show Supply", group = Timeframe_1_Group, tooltip = Hide_Supply)
  42. TF_1_Demand_Alert = input.bool(defval = true, title = "Use TF 1 Demand Alert", group = Timeframe_1_Group, tooltip = Alert_Tooltip)
  43. TF_1_Supply_Alert = input.bool(defval = true, title = "Use TF 1 Supply Alert", group = Timeframe_1_Group, tooltip = Alert_Tooltip)
  44. TF_1_Multip = input.int(defval=2, minval=1, maxval=1440, title="Timeframe 1", group=Timeframe_1_Group, inline="T1")
  45. TF_1_Period = input.string(defval="Hour", title="", options=["Minute", "Hour", "Day", "Week", "Month"], group=Timeframe_1_Group, inline="T1", tooltip=Timeframe_Tooltip)
  46. TF_1_Swing_Length = input.int(defval = 7, title = "Swing Length", minval = 1, group = Timeframe_1_Group)
  47. TF_1_Line_Type = input.string(defval = "Solid", title = "Border Type", options = ["Solid", "Dashed", "Dotted"], group = Timeframe_1_Group)
  48. TF_1_Text_Size = input.string(defval = "Small", title = "Text Size", options = ["Normal", "Tiny", "Small", "Large", "Huge", "Auto"], group = Timeframe_1_Group)
  49. TF_1_Line_Width = input.int(defval = 1, title = "Border Width", group = Timeframe_1_Group)
  50. TF_1_Demand_Show_Last = input.int(defval = 2, title = "Show last (Demand)", group = Timeframe_1_Group)
  51. TF_1_Supply_Show_Last = input.int(defval = 2, title = "Show last (Supply)", group = Timeframe_1_Group)
  52. TF_1_Demand_Color = input.color(defval = #c6f89560, title = "Demand Color", group = Timeframe_1_Group, inline = "TF 1 Color")
  53. TF_1_Supply_Color = input.color(defval = #fb726a60, title = "Supply Color", group = Timeframe_1_Group, inline = "TF 1 Color")
  54. TF_1_Text_Color = input.color(defval = color.white, title = "Text Color", group = Timeframe_1_Group)
  55.  
  56. // Timeframe 2 Settings
  57. TF_2_Chart_Feature = input.bool(defval = false, title = "Set Timeframe to Chart", group = Timeframe_2_Group, tooltip = Set_To_Chart_Tooltip)
  58. TF_2_Use_High_Low = input.bool(defval = false, title = "Use High/Low to mitigates zones", group = Timeframe_2_Group, tooltip = Mitigation_Tooltip)
  59. TF_2_Show_Demand = input.bool(defval = true, title = "Show Demand", group = Timeframe_2_Group, tooltip = Hide_Demand)
  60. TF_2_Show_Supply = input.bool(defval = true, title = "Show Supply", group = Timeframe_2_Group, tooltip = Hide_Supply)
  61. TF_2_Demand_Alert = input.bool(defval = true, title = "Use TF 2 Demand Alert", group = Timeframe_2_Group, tooltip = Alert_Tooltip)
  62. TF_2_Supply_Alert = input.bool(defval = true, title = "Use TF 2 Supply Alert", group = Timeframe_2_Group, tooltip = Alert_Tooltip)
  63. TF_2_Multip = input.int(defval=30, minval=1, maxval=1440, title="Timeframe 2", group=Timeframe_2_Group, inline="T2")
  64. TF_2_Period = input.string(defval="Minute", title="", options=["Minute", "Hour", "Day", "Week", "Month"], group=Timeframe_2_Group, inline="T2", tooltip=Timeframe_Tooltip)
  65. TF_2_Swing_Length = input.int(defval = 7, title = "Swing Length", minval = 1, group = Timeframe_2_Group)
  66. TF_2_Line_Type = input.string(defval = "Solid", title = "Border Type", options = ["Solid", "Dashed", "Dotted"], group = Timeframe_2_Group)
  67. TF_2_Text_Size = input.string(defval = "Small", title = "Text Size", options = ["Normal", "Tiny", "Small", "Large", "Huge", "Auto"], group = Timeframe_2_Group)
  68. TF_2_Line_Width = input.int(defval = 1, title = "Border Width", group = Timeframe_2_Group)
  69. TF_2_Demand_Show_Last = input.int(defval = 2, title = "Show last (Demand)", group = Timeframe_2_Group)
  70. TF_2_Supply_Show_Last = input.int(defval = 2, title = "Show last (Supply)", group = Timeframe_2_Group)
  71. TF_2_Demand_Color = input.color(defval = #5794f860, title = "Demand Color", group = Timeframe_2_Group, inline = "TF 2 Color")
  72. TF_2_Supply_Color = input.color(defval = #f9c9fe60, title = "Supply Color", group = Timeframe_2_Group, inline = "TF 2 Color")
  73. TF_2_Text_Color = input.color(defval = color.white, title = "Text Color", group = Timeframe_2_Group)
  74.  
  75. // General functions
  76. // Getting the line type from the user.
  77. Line_Type_Control(Type) =>
  78. Line_Functionality = switch Type
  79. "Solid" => line.style_solid
  80. "Dashed" => line.style_dashed
  81. "Dotted" => line.style_dotted
  82.  
  83. Line_Functionality
  84.  
  85. // Text size from the user
  86. Text_Size_Switch(Text_Size) =>
  87. Text_Type = switch Text_Size
  88. "Normal" => size.normal
  89. "Tiny" => size.tiny
  90. "Small" => size.small
  91. "Large" => size.large
  92. "Huge" => size.huge
  93. "Auto" => size.auto
  94. Text_Type
  95.  
  96. // Timeframe functionality
  97. // Timeframe for security functions
  98. TF(TF_Period, TF_Multip) =>
  99. switch TF_Period
  100. "Minute" => str.tostring(TF_Multip)
  101. "Hour" => str.tostring(TF_Multip*60)
  102. "Day" => str.tostring(TF_Multip) + "D"
  103. "Week" => str.tostring(TF_Multip) + "W"
  104. "Month" => str.tostring(TF_Multip) + "M"
  105. => timeframe.period
  106.  
  107. // Timeframe shortcut form
  108. TF_Display(Chart_as_Timeframe, TF_Period, TF_Multip) =>
  109. if Chart_as_Timeframe == false
  110. switch TF_Period
  111. "Minute" => str.tostring(TF_Multip) + "Min"
  112. "Hour" => str.tostring(TF_Multip) + "H"
  113. "Day" => str.tostring(TF_Multip) + "D"
  114. "Week" => str.tostring(TF_Multip) + "W"
  115. "Month" => str.tostring(TF_Multip) + "M"
  116.  
  117. else if Chart_as_Timeframe == true
  118. switch
  119. timeframe.isminutes and timeframe.multiplier % 60 != 0
  120. => str.tostring(timeframe.multiplier) + "Min"
  121.  
  122. timeframe.isminutes and timeframe.multiplier % 60 == 0
  123. => str.tostring(timeframe.multiplier/60) + "H"
  124.  
  125. timeframe.isdaily => str.tostring(timeframe.multiplier) + "D"
  126. timeframe.isweekly => str.tostring(timeframe.multiplier) + "W"
  127. timeframe.ismonthly => str.tostring(timeframe.multiplier) + "M"
  128.  
  129. MTF_MS_Display(Chart_as_Timeframe, TF_Period, TF_Multip, Swing_Length) =>
  130. if Chart_as_Timeframe == true
  131. Swing_Length
  132. else
  133. switch
  134. TF_Period == "Month" and timeframe.isminutes and timeframe.multiplier % 60 == 0 and
  135. (24*5*5/((TF_Multip * 1440*5*5)/timeframe.multiplier)) * 60 == timeframe.multiplier => ((TF_Multip * 1440)*5*5/ timeframe.multiplier)*Swing_Length
  136.  
  137. TF_Period == "Month" and timeframe.isweekly and
  138. (5/((TF_Multip * 1440 * 5)/timeframe.multiplier)) * 1440 == timeframe.multiplier => ((TF_Multip * 1440)*5/ 1440)*Swing_Length
  139.  
  140. TF_Period == "Month" and timeframe.isdaily and
  141. (5*5/((TF_Multip * 1440 * 5*5)/timeframe.multiplier)) * 1440 == timeframe.multiplier => ((TF_Multip * 1440)*5*5/ 1440)*Swing_Length
  142.  
  143. timeframe.ismonthly and timeframe.multiplier == TF_Multip and
  144. TF_Period == "Month" => Swing_Length
  145.  
  146. TF_Period == "Week" and timeframe.isminutes and timeframe.multiplier % 60 == 0 and
  147. (24*5/((TF_Multip * 1440*5)/timeframe.multiplier)) * 60 == timeframe.multiplier => ((TF_Multip * 1440)*5/ timeframe.multiplier)*Swing_Length
  148.  
  149. TF_Period == "Week" and timeframe.isdaily and
  150. (5/((TF_Multip * 1440 * 5)/timeframe.multiplier)) * 1440 == timeframe.multiplier => ((TF_Multip * 1440)*5/ 1440)*Swing_Length
  151.  
  152. timeframe.isweekly and timeframe.multiplier == TF_Multip and
  153. TF_Period == "Week" => Swing_Length
  154.  
  155. TF_Period == "Day" and timeframe.isminutes and timeframe.multiplier % 60 == 0 and
  156. (24/((TF_Multip * 1440)/timeframe.multiplier)) * 60 == timeframe.multiplier => (TF_Multip * 1440/ timeframe.multiplier)*Swing_Length
  157.  
  158. timeframe.isdaily and timeframe.multiplier == TF_Multip and
  159. TF_Period == "Day" => Swing_Length
  160.  
  161. timeframe.isminutes and timeframe.multiplier % 60 != 0 and
  162. TF_Period == "Minute" and TF_Multip == timeframe.multiplier => Swing_Length
  163.  
  164. timeframe.isminutes and timeframe.multiplier % 60 != 0 and
  165. TF_Period == "Minute" and TF_Multip != timeframe.multiplier => ((TF_Multip/60) * 60/timeframe.multiplier)*Swing_Length
  166.  
  167. timeframe.isminutes and timeframe.multiplier % 60 != 0 and
  168. TF_Period == "Hour" and TF_Multip != timeframe.multiplier => ((TF_Multip * 60 /60) * 60/timeframe.multiplier)*Swing_Length
  169.  
  170. timeframe.isminutes and timeframe.multiplier % 60 != 0 and
  171. TF_Period == "Hour" and TF_Multip == timeframe.multiplier and timeframe.multiplier * 60 == 60 => ((TF_Multip * 60 /60) * 60/timeframe.multiplier)*Swing_Length
  172.  
  173. timeframe.isminutes and timeframe.multiplier % 60 != 0 and
  174. TF_Period == "Day" and TF_Multip != timeframe.multiplier => ((TF_Multip * 1440 /60) * 60/timeframe.multiplier)*Swing_Length
  175.  
  176. timeframe.isminutes and timeframe.multiplier % 60 == 0 and
  177. TF_Period == "Hour" and TF_Multip * 60 == timeframe.multiplier => Swing_Length
  178.  
  179. timeframe.isminutes and timeframe.multiplier % 60 == 0 and
  180. TF_Period == "Hour" and TF_Multip * 60 != timeframe.multiplier => (TF_Multip * 60/timeframe.multiplier)*Swing_Length
  181.  
  182. HTF_Structure_Control(Chart_as_Timeframe, TF_Period, TF_Multip) =>
  183. if Chart_as_Timeframe == true
  184. true
  185. else if Show_Only_On_Lower_Timeframes == false
  186. true
  187. else
  188. switch
  189. TF_Period == "Minute" and TF_Multip < timeframe.multiplier and timeframe.isminutes
  190. => false
  191. TF_Period == "Minute" and TF_Multip >= timeframe.multiplier and timeframe.isminutes
  192. => true
  193. TF_Period == "Minute" and timeframe.isdaily
  194. => false
  195. TF_Period == "Minute" and timeframe.isweekly
  196. => false
  197. TF_Period == "Minute" and timeframe.ismonthly
  198. => false
  199. TF_Period == "Hour" and TF_Multip * 60 < timeframe.multiplier and timeframe.isminutes
  200. => false
  201. TF_Period == "Hour" and TF_Multip * 60 >= timeframe.multiplier and timeframe.isminutes
  202. => true
  203. TF_Period == "Hour" and timeframe.isdaily
  204. => false
  205. TF_Period == "Hour" and timeframe.isweekly
  206. => false
  207. TF_Period == "Hour" and timeframe.ismonthly
  208. => false
  209. TF_Period == "Day" and timeframe.isdaily or timeframe.isminutes
  210. => true
  211. TF_Period == "Week" and timeframe.isweekly or timeframe.isdaily or timeframe.isminutes
  212. => true
  213. TF_Period == "Month" and timeframe.ismonthly or timeframe.isweekly or timeframe.isdaily or timeframe.isminutes
  214. => true
  215.  
  216.  
  217. // Arrays
  218. var Bullish_SnD_Top_TF_1 = array.new_float(0)
  219. var Bullish_SnD_Btm_TF_1 = array.new_float(0)
  220. var Bullish_SnD_Left_TF_1 = array.new_int(0)
  221. var Bullish_SnD_Type_TF_1 = array.new_int(0)
  222. var Bearish_SnD_Top_TF_1 = array.new_float(0)
  223. var Bearish_SnD_Btm_TF_1 = array.new_float(0)
  224. var Bearish_SnD_Left_TF_1 = array.new_int(0)
  225. var Bearish_SnD_Type_TF_1 = array.new_int(0)
  226.  
  227. var Bullish_SnD_Top_TF_2 = array.new_float(0)
  228. var Bullish_SnD_Btm_TF_2 = array.new_float(0)
  229. var Bullish_SnD_Left_TF_2 = array.new_int(0)
  230. var Bullish_SnD_Type_TF_2 = array.new_int(0)
  231. var Bearish_SnD_Top_TF_2 = array.new_float(0)
  232. var Bearish_SnD_Btm_TF_2 = array.new_float(0)
  233. var Bearish_SnD_Left_TF_2 = array.new_int(0)
  234. var Bearish_SnD_Type_TF_2 = array.new_int(0)
  235.  
  236. // TF Pivot values
  237. // TF_1_Calc_High = ta.pivothigh(high, TF_1_Swing_Length, TF_1_Swing_Length)
  238.  
  239. // Getting the high and low values
  240. [TF_1_SH, TF_1_SL, TF_1_SH_Low, TF_1_SL_High, TF_1_Atr] = request.security(symbol = syminfo.tickerid, timeframe = (TF_1_Chart_Feature ? timeframe.period : TF(TF_1_Period, TF_1_Multip))
  241. , expression = [ta.pivothigh(high, TF_1_Swing_Length, TF_1_Swing_Length)
  242. , ta.pivotlow(low, TF_1_Swing_Length, TF_1_Swing_Length)
  243. , not na(ta.pivothigh(high, TF_1_Swing_Length, TF_1_Swing_Length)) ? low[TF_1_Swing_Length] : 0
  244. , not na(ta.pivotlow(low, TF_1_Swing_Length, TF_1_Swing_Length)) ? high[TF_1_Swing_Length] : 0
  245. , ta.atr(200)]
  246. , gaps = barmerge.gaps_on)
  247.  
  248. [TF_2_SH, TF_2_SL, TF_2_SH_Low, TF_2_SL_High, TF_2_Atr] = request.security(symbol = syminfo.tickerid, timeframe = (TF_2_Chart_Feature ? timeframe.period : TF(TF_2_Period, TF_2_Multip))
  249. , expression = [ta.pivothigh(high, TF_2_Swing_Length, TF_2_Swing_Length)
  250. , ta.pivotlow(low, TF_2_Swing_Length, TF_2_Swing_Length)
  251. , not na(ta.pivothigh(high, TF_2_Swing_Length, TF_2_Swing_Length)) ? low[TF_2_Swing_Length] : 0
  252. , not na(ta.pivotlow(low, TF_2_Swing_Length, TF_2_Swing_Length)) ? high[TF_2_Swing_Length] : 0
  253. , ta.atr(200)]
  254. , gaps = barmerge.gaps_on)
  255. // [TF_3_SH, TF_3_SL] = request.security(symbol = syminfo.ticker, timeframe = (TF_3_Chart_Feature ? timeframe.period : TF(TF_3_Period, TF_3_Multip))
  256. // , expression = [ta.pivothigh(high, TF_3_Swing_Length, TF_3_Swing_Length), ta.pivotlow(low, TF_3_Swing_Length, TF_3_Swing_Length)], gaps = barmerge.gaps_on)
  257.  
  258. // Functions //
  259. // The function below is designed to loop through the arrays holding the box plot values for supply and demand box plots
  260. // and remove the mitigated (unnecessary plots) on the chart.
  261.  
  262. Supply_and_Demand_Mitigation(SnD_Top, SnD_Btm, SnD_Left, SnD_Type, SnD_Dir, TF_Use_High_Low) =>
  263. if SnD_Dir == "Bearish"
  264. for i in SnD_Type
  265. index = array.indexof(SnD_Type, i)
  266.  
  267. if (TF_Use_High_Low ? high : close) > array.get(SnD_Top, index)
  268. array.remove(SnD_Top, index)
  269. array.remove(SnD_Btm, index)
  270. array.remove(SnD_Left, index)
  271. array.remove(SnD_Type, index)
  272.  
  273. // array.set()
  274.  
  275. else if SnD_Dir == "Bullish"
  276. for i in SnD_Type
  277. index = array.indexof(SnD_Type, i)
  278.  
  279. if (TF_Use_High_Low ? low : close) < array.get(SnD_Btm, index)
  280. array.remove(SnD_Top, index)
  281. array.remove(SnD_Btm, index)
  282. array.remove(SnD_Left, index)
  283. array.remove(SnD_Type, index)
  284.  
  285. // The function below is designed to find the necessary swing points in our chart that fit the description
  286. // of demand and supply zones
  287.  
  288. Supply_and_Demand_Functionality(TF_SH, TF_SL, TF_SH_Low, TF_SL_High, TF_Atr
  289. , Swing_Length, Chart_as_Timeframe, TF_Period, TF_Multip
  290. , Bullish_SnD_Top, Bullish_SnD_Btm, Bullish_SnD_Left, Bullish_SnD_Type
  291. , Bearish_SnD_Top, Bearish_SnD_Btm, Bearish_SnD_Left, Bearish_SnD_Type
  292. , Use_Demand_Alert, Use_Supply_Alert) =>
  293.  
  294. // Variables to identify HH, HL, LH, LL
  295. var float TF_Prev_High = na
  296. var float TF_Prev_Low = na
  297. TF_Prev_High_Time = 0
  298. TF_Prev_Low_Time = 0
  299.  
  300. //Tracking whether previous levels have been broken
  301. var bool TF_High_Present = false
  302. var bool TF_Low_Present = false
  303.  
  304. // Variables for creating supply and demand boxes
  305. bool HH = false
  306. bool LH = false
  307. bool HL = false
  308. bool LL = false
  309.  
  310.  
  311. // Identify new pivot highs and lows
  312. if not na(TF_SH)
  313. if TF_SH >= TF_Prev_High
  314. HH := true
  315. else
  316. LH := true
  317. TF_Prev_High := TF_SH
  318. TF_Prev_High_Time := TF_Prev_High != TF_Prev_High[1] ? time[MTF_MS_Display(Chart_as_Timeframe, TF_Period, TF_Multip, Swing_Length)] : TF_Prev_High_Time[1]
  319. TF_High_Present := true
  320.  
  321. if not na(TF_SL)
  322. if TF_SL >= TF_Prev_Low
  323. HL := true
  324. else
  325. LL := true
  326. TF_Prev_Low := TF_SL
  327. TF_Prev_Low_Time := TF_Prev_Low != TF_Prev_Low[1] ? time[MTF_MS_Display(Chart_as_Timeframe, TF_Period, TF_Multip, Swing_Length)] : TF_Prev_Low_Time[1]
  328. TF_Low_Present := true
  329.  
  330.  
  331. // Displaying Swing Level
  332. // Demand zones
  333. if LL and (math.abs(TF_SL_High - TF_Prev_Low) < TF_Atr * 2)
  334. array.unshift(Bullish_SnD_Top, TF_SL_High)
  335. array.unshift(Bullish_SnD_Btm, TF_Prev_Low)
  336. array.unshift(Bullish_SnD_Left, TF_Prev_Low_Time)
  337. array.unshift(Bullish_SnD_Type, 1)
  338. if Use_Demand_Alert
  339. alert(message = "New demand zone formed on " + + TF_Display(Chart_as_Timeframe, TF_Period, TF_Multip), freq = alert.freq_once_per_bar_close)
  340.  
  341. if HL and (math.abs(TF_SL_High - TF_Prev_Low) < TF_Atr * 2)
  342. array.unshift(Bullish_SnD_Top, TF_SL_High)
  343. array.unshift(Bullish_SnD_Btm, TF_Prev_Low)
  344. array.unshift(Bullish_SnD_Left, TF_Prev_Low_Time)
  345. array.unshift(Bullish_SnD_Type, 1)
  346. if Use_Demand_Alert
  347. alert(message = "New demand zone formed on " + + TF_Display(Chart_as_Timeframe, TF_Period, TF_Multip), freq = alert.freq_once_per_bar_close)
  348.  
  349. // Supply zones
  350. if HH and (math.abs(TF_Prev_High - TF_SH_Low) < TF_Atr * 2)
  351. array.unshift(Bearish_SnD_Top, TF_Prev_High)
  352. array.unshift(Bearish_SnD_Btm, TF_SH_Low)
  353. array.unshift(Bearish_SnD_Left, TF_Prev_High_Time)
  354. array.unshift(Bearish_SnD_Type, -1)
  355. if Use_Supply_Alert
  356. alert(message = "New supply zone formed on " + + TF_Display(Chart_as_Timeframe, TF_Period, TF_Multip), freq = alert.freq_once_per_bar_close)
  357.  
  358. if LH and (math.abs(TF_Prev_High - TF_SH_Low) < TF_Atr * 2)
  359. array.unshift(Bearish_SnD_Top, TF_Prev_High)
  360. array.unshift(Bearish_SnD_Btm, TF_SH_Low)
  361. array.unshift(Bearish_SnD_Left, TF_Prev_High_Time)
  362. array.unshift(Bearish_SnD_Type, -1)
  363. if Use_Supply_Alert
  364. alert(message = "New supply zone formed on " + + TF_Display(Chart_as_Timeframe, TF_Period, TF_Multip), freq = alert.freq_once_per_bar_close)
  365.  
  366.  
  367. // The function below is designed to sift through our boxes, plot them on the chart and apply the necessary formatting.
  368.  
  369. Display_SnD_Zones(Box_PLot_Type, Box_Top, Box_Btm, Box_Left, Box_Type, Show_Last, Box_Arr_size
  370. , TF_Demand_Color, TF_Supply_Color, TF_Text_Color, TF_Text_Size, TF_Border_style, TF_Border_width
  371. , Chart_as_Timeframe, TF_Period, TF_Multip)=>
  372. for i = 0 to math.min(Show_Last-1, Box_Arr_size-1)
  373. get_box = array.get(Box_PLot_Type, i)
  374.  
  375. if HTF_Structure_Control(Chart_as_Timeframe, TF_Period, TF_Multip)
  376. box.set_top(get_box, array.get(Box_Top, i))
  377. box.set_bottom(get_box, array.get(Box_Btm, i))
  378. box.set_left(get_box, array.get(Box_Left,i))
  379. box.set_right(get_box, time_close("W"))
  380.  
  381. if Box_Type == "Bullish"
  382. box.set_bgcolor(get_box, TF_Demand_Color)
  383. box.set_border_color(get_box, TF_Demand_Color)
  384. box.set_text(get_box, TF_Display(Chart_as_Timeframe, TF_Period, TF_Multip))
  385. box.set_text_color(get_box, TF_Text_Color)
  386. box.set_text_font_family(get_box, font.family_default)
  387. box.set_text_halign(get_box, text.align_right)
  388. box.set_text_valign(get_box, text.align_top)
  389. box.set_text_size(get_box, TF_Text_Size)
  390. box.set_border_style(get_box, TF_Border_style)
  391. box.set_border_width(get_box, TF_Border_width)
  392. else if Box_Type == "Bearish"
  393. box.set_bgcolor(get_box, TF_Supply_Color)
  394. box.set_border_color(get_box, TF_Supply_Color)
  395. box.set_text(get_box, TF_Display(Chart_as_Timeframe, TF_Period, TF_Multip))
  396. box.set_text_color(get_box, TF_Text_Color)
  397. box.set_text_font_family(get_box, font.family_default)
  398. box.set_text_halign(get_box, text.align_right)
  399. box.set_text_valign(get_box, text.align_bottom)
  400. box.set_text_size(get_box, TF_Text_Size)
  401. box.set_border_style(get_box, TF_Border_style)
  402. box.set_border_width(get_box, TF_Border_width)
  403.  
  404. // Calling functions
  405. Supply_and_Demand_Functionality(TF_1_SH, TF_1_SL, TF_1_SH_Low, TF_1_SL_High, TF_1_Atr
  406. , TF_1_Swing_Length, TF_1_Chart_Feature, TF_1_Period, TF_1_Multip
  407. , Bullish_SnD_Top_TF_1, Bullish_SnD_Btm_TF_1, Bullish_SnD_Left_TF_1 ,Bullish_SnD_Type_TF_1
  408. , Bearish_SnD_Top_TF_1, Bearish_SnD_Btm_TF_1, Bearish_SnD_Left_TF_1, Bearish_SnD_Type_TF_1
  409. , TF_1_Demand_Alert, TF_1_Supply_Alert)
  410.  
  411. Supply_and_Demand_Functionality(TF_2_SH, TF_2_SL, TF_2_SH_Low, TF_2_SL_High, TF_2_Atr
  412. , TF_2_Swing_Length, TF_2_Chart_Feature, TF_2_Period, TF_2_Multip
  413. , Bullish_SnD_Top_TF_2, Bullish_SnD_Btm_TF_2, Bullish_SnD_Left_TF_2 ,Bullish_SnD_Type_TF_2
  414. , Bearish_SnD_Top_TF_2, Bearish_SnD_Btm_TF_2, Bearish_SnD_Left_TF_2, Bearish_SnD_Type_TF_2
  415. , TF_2_Demand_Alert, TF_2_Supply_Alert)
  416.  
  417. var TF_1_Bullish_Box_PLots = array.new_box(0)
  418. var TF_1_Bearish_Box_PLots = array.new_box(0)
  419. var TF_2_Bullish_Box_PLots = array.new_box(0)
  420. var TF_2_Bearish_Box_PLots = array.new_box(0)
  421.  
  422. TF_1_Bullish_Size = array.size(Bullish_SnD_Top_TF_1)
  423. TF_1_Bearish_Size = array.size(Bearish_SnD_Top_TF_1)
  424. TF_2_Bullish_Size = array.size(Bullish_SnD_Top_TF_2)
  425. TF_2_Bearish_Size = array.size(Bearish_SnD_Top_TF_2)
  426.  
  427. Supply_and_Demand_Mitigation(Bullish_SnD_Top_TF_1, Bullish_SnD_Btm_TF_1, Bullish_SnD_Left_TF_1, Bullish_SnD_Type_TF_1, "Bullish", TF_1_Use_High_Low)
  428. Supply_and_Demand_Mitigation(Bearish_SnD_Top_TF_1, Bearish_SnD_Btm_TF_1, Bearish_SnD_Left_TF_1, Bearish_SnD_Type_TF_1, "Bearish", TF_1_Use_High_Low)
  429.  
  430. Supply_and_Demand_Mitigation(Bullish_SnD_Top_TF_2, Bullish_SnD_Btm_TF_2, Bullish_SnD_Left_TF_2, Bullish_SnD_Type_TF_2, "Bullish", TF_2_Use_High_Low)
  431. Supply_and_Demand_Mitigation(Bearish_SnD_Top_TF_2, Bearish_SnD_Btm_TF_2, Bearish_SnD_Left_TF_2, Bearish_SnD_Type_TF_2, "Bearish", TF_2_Use_High_Low)
  432.  
  433. if barstate.isfirst
  434. for i = 0 to 125
  435. array.push(TF_1_Bullish_Box_PLots, box.new(na,na,na,na, xloc = xloc.bar_time))
  436. array.push(TF_1_Bearish_Box_PLots, box.new(na,na,na,na, xloc = xloc.bar_time))
  437. array.push(TF_2_Bullish_Box_PLots, box.new(na,na,na,na, xloc = xloc.bar_time))
  438. array.push(TF_2_Bearish_Box_PLots, box.new(na,na,na,na, xloc = xloc.bar_time))
  439.  
  440. if TF_1_Bullish_Size > 0 and TF_1_Show_Demand and not Hide_All_Demand
  441. if barstate.islast
  442. Display_SnD_Zones(TF_1_Bullish_Box_PLots, Bullish_SnD_Top_TF_1, Bullish_SnD_Btm_TF_1, Bullish_SnD_Left_TF_1, "Bullish", TF_1_Demand_Show_Last, TF_1_Bullish_Size
  443. , TF_1_Demand_Color, TF_1_Supply_Color, TF_1_Text_Color, Text_Size_Switch(TF_1_Text_Size), Line_Type_Control(TF_1_Line_Type), TF_1_Line_Width
  444. , TF_1_Chart_Feature, TF_1_Period, TF_1_Multip)
  445.  
  446. if TF_1_Bearish_Size > 0 and TF_1_Show_Supply and not Hide_All_Supply
  447. if barstate.islast
  448. Display_SnD_Zones(TF_1_Bearish_Box_PLots, Bearish_SnD_Top_TF_1, Bearish_SnD_Btm_TF_1, Bearish_SnD_Left_TF_1, "Bearish", TF_1_Supply_Show_Last, TF_1_Bearish_Size
  449. , TF_1_Demand_Color, TF_1_Supply_Color, TF_1_Text_Color, Text_Size_Switch(TF_1_Text_Size), Line_Type_Control(TF_1_Line_Type), TF_1_Line_Width
  450. , TF_1_Chart_Feature, TF_1_Period, TF_1_Multip)
  451.  
  452. if TF_2_Bullish_Size > 0 and TF_2_Show_Demand and not Hide_All_Demand
  453. if barstate.islast
  454. Display_SnD_Zones(TF_2_Bullish_Box_PLots, Bullish_SnD_Top_TF_2, Bullish_SnD_Btm_TF_2, Bullish_SnD_Left_TF_2, "Bullish", TF_2_Demand_Show_Last, TF_2_Bullish_Size
  455. , TF_2_Demand_Color, TF_2_Supply_Color, TF_2_Text_Color, Text_Size_Switch(TF_2_Text_Size), Line_Type_Control(TF_2_Line_Type), TF_2_Line_Width
  456. , TF_2_Chart_Feature, TF_2_Period, TF_2_Multip)
  457.  
  458. if TF_2_Bearish_Size > 0 and TF_2_Show_Supply and not Hide_All_Supply
  459. if barstate.islast
  460. Display_SnD_Zones(TF_2_Bearish_Box_PLots, Bearish_SnD_Top_TF_2, Bearish_SnD_Btm_TF_2, Bearish_SnD_Left_TF_2, "Bearish", TF_2_Supply_Show_Last, TF_2_Bearish_Size
  461. , TF_2_Demand_Color, TF_2_Supply_Color, TF_2_Text_Color, Text_Size_Switch(TF_2_Text_Size), Line_Type_Control(TF_2_Line_Type), TF_2_Line_Width
  462. , TF_2_Chart_Feature, TF_2_Period, TF_2_Multip)
  463.  
  464.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement