Advertisement
xmd79

Dynamic Time Period Candles

Nov 11th, 2024
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.85 KB | None | 0 0
  1. //@version=5
  2. indicator("Dynamic Time Period Candles", overlay=true)
  3.  
  4. // Global Settings
  5. chartTimeframe = input.int(title="Chart Timeframe (minutes)", defval=3, minval=1, maxval=60, group="Global Settings")
  6. labelPosition = input.float(title="Label Position", defval=1.0, minval=-2.0, maxval=2.0, group="Global Settings")
  7.  
  8. // Candle 1 Settings (30min)
  9. enableCandle1 = input.bool(title="Enable Candle 1", defval=true, group="Candle 1 Settings")
  10. lookbackMinutes1 = input.int(title="Candle 1 Time Period (minutes)", defval=30, minval=1, maxval=1440, group="Candle 1 Settings")
  11. offsetBars1 = input.int(title="Offset (bars) for Candle 1", defval=25, minval=1, maxval=500, group="Candle 1 Settings")
  12. showLookbackLine1 = input.bool(title="Show Lookback Line (Candle 1)", defval=true, group="Candle 1 Settings")
  13. lookbackLineWidth1 = input.int(title="Lookback Line Width (Candle 1)", defval=2, minval=1, maxval=10, group="Candle 1 Settings")
  14. lookbackLineColor1 = input.color(title="Lookback Line Color (Candle 1)", defval=color.rgb(0, 188, 212), group="Candle 1 Settings")
  15. lookbackLineStyle1 = input.string(title="Lookback Line Style (Candle 1)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 1 Settings")
  16. showHighLow1 = input.bool(title="Show Lookback High & Low (Candle 1)", defval=true, group="Candle 1 Settings")
  17. highLowColor1 = input.color(title="Lookback H/L Lines Color (Candle 1)", defval=color.rgb(0, 188, 212), group="Candle 1 Settings")
  18. highLowStyle1 = input.string(title="Lookback H/L Lines Style (Candle 1)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 1 Settings")
  19. showLabel1 = input.bool(title="Show Label (Candle 1)", defval=true, group="Candle 1 Settings")
  20. labelColor1 = input.color(title="Label Color (Candle 1)", defval=color.rgb(0, 188, 212), group="Candle 1 Settings")
  21.  
  22. // Candle 2 Settings (1hr)
  23. enableCandle2 = input.bool(title="Enable Candle 2", defval=true, group="Candle 2 Settings")
  24. lookbackMinutes2 = input.int(title="Candle 2 Time Period (minutes)", defval=60, minval=1, maxval=1440, group="Candle 2 Settings")
  25. offsetBars2 = input.int(title="Offset (bars) for Candle 2", defval=50, minval=1, maxval=500, group="Candle 2 Settings")
  26. showLookbackLine2 = input.bool(title="Show Lookback Line (Candle 2)", defval=true, group="Candle 2 Settings")
  27. lookbackLineWidth2 = input.int(title="Lookback Line Width (Candle 2)", defval=2, minval=1, maxval=10, group="Candle 2 Settings")
  28. lookbackLineColor2 = input.color(title="Lookback Line Color (Candle 2)", defval=color.rgb(233, 30, 99), group="Candle 2 Settings")
  29. lookbackLineStyle2 = input.string(title="Lookback Line Style (Candle 2)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 2 Settings")
  30. showHighLow2 = input.bool(title="Show Lookback High & Low (Candle 2)", defval=true, group="Candle 2 Settings")
  31. highLowColor2 = input.color(title="Lookback H/L Lines Color (Candle 2)", defval=color.rgb(233, 30, 99), group="Candle 2 Settings")
  32. highLowStyle2 = input.string(title="Lookback H/L Lines Style (Candle 2)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 2 Settings")
  33. showLabel2 = input.bool(title="Show Label (Candle 2)", defval=true, group="Candle 2 Settings")
  34. labelColor2 = input.color(title="Label Color (Candle 2)", defval=color.rgb(233, 30, 99), group="Candle 2 Settings")
  35.  
  36. // Candle 3 Settings (2hr)
  37. enableCandle3 = input.bool(title="Enable Candle 3", defval=true, group="Candle 3 Settings")
  38. lookbackMinutes3 = input.int(title="Candle 3 Time Period (minutes)", defval=120, minval=1, maxval=1440, group="Candle 3 Settings")
  39. offsetBars3 = input.int(title="Offset (bars) for Candle 3", defval=75, minval=1, maxval=500, group="Candle 3 Settings")
  40. showLookbackLine3 = input.bool(title="Show Lookback Line (Candle 3)", defval=true, group="Candle 3 Settings")
  41. lookbackLineWidth3 = input.int(title="Lookback Line Width (Candle 3)", defval=2, minval=1, maxval=10, group="Candle 3 Settings")
  42. lookbackLineColor3 = input.color(title="Lookback Line Color (Candle 3)", defval=color.rgb(255, 99, 71), group="Candle 3 Settings")
  43. lookbackLineStyle3 = input.string(title="Lookback Line Style (Candle 3)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 3 Settings")
  44. showHighLow3 = input.bool(title="Show Lookback High & Low (Candle 3)", defval=true, group="Candle 3 Settings")
  45. highLowColor3 = input.color(title="Lookback H/L Lines Color (Candle 3)", defval=color.rgb(255, 99, 71), group="Candle 3 Settings")
  46. highLowStyle3 = input.string(title="Lookback H/L Lines Style (Candle 3)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 3 Settings")
  47. showLabel3 = input.bool(title="Show Label (Candle 3)", defval=true, group="Candle 3 Settings")
  48. labelColor3 = input.color(title="Label Color (Candle 3)", defval=color.rgb(255, 99, 71), group="Candle 3 Settings")
  49.  
  50. // Candle 4 Settings (4hr)
  51. enableCandle4 = input.bool(title="Enable Candle 4", defval=true, group="Candle 4 Settings")
  52. lookbackMinutes4 = input.int(title="Candle 4 Time Period (minutes)", defval=240, minval=1, maxval=1440, group="Candle 4 Settings")
  53. offsetBars4 = input.int(title="Offset (bars) for Candle 4", defval=100, minval=1, maxval=500, group="Candle 4 Settings")
  54. showLookbackLine4 = input.bool(title="Show Lookback Line (Candle 4)", defval=true, group="Candle 4 Settings")
  55. lookbackLineWidth4 = input.int(title="Lookback Line Width (Candle 4)", defval=2, minval=1, maxval=10, group="Candle 4 Settings")
  56. lookbackLineColor4 = input.color(title="Lookback Line Color (Candle 4)", defval=color.rgb(255, 152, 0), group="Candle 4 Settings")
  57. lookbackLineStyle4 = input.string(title="Lookback Line Style (Candle 4)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 4 Settings")
  58. showHighLow4 = input.bool(title="Show Lookback High & Low (Candle 4)", defval=true, group="Candle 4 Settings")
  59. highLowColor4 = input.color(title="Lookback H/L Lines Color (Candle 4)", defval=color.rgb(255, 152, 0), group="Candle 4 Settings")
  60. highLowStyle4 = input.string(title="Lookback H/L Lines Style (Candle 4)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 4 Settings")
  61. showLabel4 = input.bool(title="Show Label (Candle 4)", defval=true, group="Candle 4 Settings")
  62. labelColor4 = input.color(title="Label Color (Candle 4)", defval=color.rgb(255, 152, 0), group="Candle 4 Settings")
  63.  
  64. // Candle 5 Settings (12hr)
  65. enableCandle5 = input.bool(title="Enable Candle 5", defval=true, group="Candle 5 Settings")
  66. lookbackMinutes5 = input.int(title="Candle 5 Time Period (minutes)", defval=720, minval=1, maxval=1440, group="Candle 5 Settings")
  67. offsetBars5 = input.int(title="Offset (bars) for Candle 5", defval=125, minval=1, maxval=500, group="Candle 5 Settings")
  68. showLookbackLine5 = input.bool(title="Show Lookback Line (Candle 5)", defval=true, group="Candle 5 Settings")
  69. lookbackLineWidth5 = input.int(title="Lookback Line Width (Candle 5)", defval=2, minval=1, maxval=10, group="Candle 5 Settings")
  70. lookbackLineColor5 = input.color(title="Lookback Line Color (Candle 5)", defval=color.rgb(255, 87, 34), group="Candle 5 Settings")
  71. lookbackLineStyle5 = input.string(title="Lookback Line Style (Candle 5)", defval="dotted", options=["solid", "dotted", "dashed"], group="Candle 5 Settings")
  72. showHighLow5 = input.bool(title="Show Lookback High & Low (Candle 5)", defval=true, group="Candle 5 Settings")
  73. highLowColor5 = input.color(title="Lookback H/L Lines Color (Candle 5)", defval=color.rgb(255, 87, 34), group="Candle 5 Settings")
  74. highLowStyle5 = input.string(title="Lookback H/L Lines Style (Candle 5)", defval="dashed", options=["solid", "dotted", "dashed"], group="Candle 5 Settings")
  75. showLabel5 = input.bool(title="Show Label (Candle 5)", defval=true, group="Candle 5 Settings")
  76. labelColor5 = input.color(title="Label Color (Candle 5)", defval=color.rgb(255, 87, 34), group="Candle 5 Settings")
  77.  
  78. // Function to format time period for label
  79. formatTimePeriod(minutes) =>
  80. if minutes >= 60
  81. hours = math.floor(minutes / 60)
  82. str.tostring(hours) + " hrs"
  83. else
  84. str.tostring(minutes) + " mins"
  85.  
  86. // Function to calculate the candle's elements
  87. f_getCandleData(_lookbackMinutes) =>
  88. periodsForLookback = _lookbackMinutes / chartTimeframe
  89. periodHigh = ta.highest(high, int(periodsForLookback))
  90. periodLow = ta.lowest(low, int(periodsForLookback))
  91. firstPrice = close[int(periodsForLookback)]
  92. lastPrice = close
  93. colorCandle = (lastPrice > firstPrice) ? color.green : color.red
  94. [periodHigh, periodLow, firstPrice, lastPrice, colorCandle]
  95.  
  96. // Get data for each candle
  97. [periodHigh1, periodLow1, firstPrice1, lastPrice1, colorCandle1] = f_getCandleData(lookbackMinutes1)
  98. [periodHigh2, periodLow2, firstPrice2, lastPrice2, colorCandle2] = f_getCandleData(lookbackMinutes2)
  99. [periodHigh3, periodLow3, firstPrice3, lastPrice3, colorCandle3] = f_getCandleData(lookbackMinutes3)
  100. [periodHigh4, periodLow4, firstPrice4, lastPrice4, colorCandle4] = f_getCandleData(lookbackMinutes4)
  101. [periodHigh5, periodLow5, firstPrice5, lastPrice5, colorCandle5] = f_getCandleData(lookbackMinutes5)
  102.  
  103. // Calculate the highest point among all lookback periods
  104. maxHigh = math.max(periodHigh1, math.max(periodHigh2, math.max(periodHigh3, math.max(periodHigh4, periodHigh5))))
  105. minLow = math.min(periodLow1, math.min(periodLow2, math.min(periodLow3, math.min(periodLow4, periodLow5))))
  106. labelHeight = maxHigh + (maxHigh - minLow) * labelPosition
  107.  
  108. // Define variables with types and initialize with `na`
  109. var line bodyLine1 = na
  110. var line highWick1 = na
  111. var line lookbackLine1 = na
  112. var line highLine1 = na
  113. var line lowLine1 = na
  114. var label lookbackLabel1 = na
  115.  
  116. var line bodyLine2 = na
  117. var line highWick2 = na
  118. var line lookbackLine2 = na
  119. var line highLine2 = na
  120. var line lowLine2 = na
  121. var label lookbackLabel2 = na
  122.  
  123. var line bodyLine3 = na
  124. var line highWick3 = na
  125. var line lookbackLine3 = na
  126. var line highLine3 = na
  127. var line lowLine3 = na
  128. var label lookbackLabel3 = na
  129.  
  130. var line bodyLine4 = na
  131. var line highWick4 = na
  132. var line lookbackLine4 = na
  133. var line highLine4 = na
  134. var line lowLine4 = na
  135. var label lookbackLabel4 = na
  136.  
  137. var line bodyLine5 = na
  138. var line highWick5 = na
  139. var line lookbackLine5 = na
  140. var line highLine5 = na
  141. var line lowLine5 = na
  142. var label lookbackLabel5 = na
  143.  
  144. // Delete previous lines and labels if they exist
  145. if not na(bodyLine1)
  146. line.delete(bodyLine1)
  147. if not na(highWick1)
  148. line.delete(highWick1)
  149. if not na(lookbackLine1)
  150. line.delete(lookbackLine1)
  151. if not na(highLine1)
  152. line.delete(highLine1)
  153. if not na(lowLine1)
  154. line.delete(lowLine1)
  155. if not na(lookbackLabel1)
  156. label.delete(lookbackLabel1)
  157.  
  158. if not na(bodyLine2)
  159. line.delete(bodyLine2)
  160. if not na(highWick2)
  161. line.delete(highWick2)
  162. if not na(lookbackLine2)
  163. line.delete(lookbackLine2)
  164. if not na(highLine2)
  165. line.delete(highLine2)
  166. if not na(lowLine2)
  167. line.delete(lowLine2)
  168. if not na(lookbackLabel2)
  169. label.delete(lookbackLabel2)
  170.  
  171. if not na(bodyLine3)
  172. line.delete(bodyLine3)
  173. if not na(highWick3)
  174. line.delete(highWick3)
  175. if not na(lookbackLine3)
  176. line.delete(lookbackLine3)
  177. if not na(highLine3)
  178. line.delete(highLine3)
  179. if not na(lowLine3)
  180. line.delete(lowLine3)
  181. if not na(lookbackLabel3)
  182. label.delete(lookbackLabel3)
  183.  
  184. if not na(bodyLine4)
  185. line.delete(bodyLine4)
  186. if not na(highWick4)
  187. line.delete(highWick4)
  188. if not na(lookbackLine4)
  189. line.delete(lookbackLine4)
  190. if not na(highLine4)
  191. line.delete(highLine4)
  192. if not na(lowLine4)
  193. line.delete(lowLine4)
  194. if not na(lookbackLabel4)
  195. label.delete(lookbackLabel4)
  196.  
  197. if not na(bodyLine5)
  198. line.delete(bodyLine5)
  199. if not na(highWick5)
  200. line.delete(highWick5)
  201. if not na(lookbackLine5)
  202. line.delete(lookbackLine5)
  203. if not na(highLine5)
  204. line.delete(highLine5)
  205. if not na(lowLine5)
  206. line.delete(lowLine5)
  207. if not na(lookbackLabel5)
  208. label.delete(lookbackLabel5)
  209.  
  210. // Calculate the starting bar index for the lookback period for each candle
  211. lookbackBars1 = int(lookbackMinutes1 / chartTimeframe)
  212. lookbackBars2 = int(lookbackMinutes2 / chartTimeframe)
  213. lookbackBars3 = int(lookbackMinutes3 / chartTimeframe)
  214. lookbackBars4 = int(lookbackMinutes4 / chartTimeframe)
  215. lookbackBars5 = int(lookbackMinutes5 / chartTimeframe)
  216.  
  217. // Draw the candle for each period
  218. x1 = bar_index + offsetBars1
  219. x2 = bar_index + offsetBars2
  220. x3 = bar_index + offsetBars3
  221. x4 = bar_index + offsetBars4
  222. x5 = bar_index + offsetBars5
  223. // Original code remains the same until the line drawing sections
  224. // Only showing the modified sections for clarity
  225.  
  226. // [Previous code remains exactly the same until the drawing sections]
  227. // Only showing the modified sections where we reverse the drawing order
  228.  
  229. // For Candle 5 (Draw first - will be at bottom)
  230. if enableCandle5
  231. bodyLine5 := line.new(x5, firstPrice5, x5, lastPrice5, color=colorCandle5, width=24)
  232. highWick5 := line.new(x5, periodHigh5, x5, periodLow5, color=colorCandle5, width=2)
  233. if showLookbackLine5
  234. lookbackLine5 := line.new(bar_index - lookbackBars5, periodLow5, bar_index - lookbackBars5, periodHigh5, color=lookbackLineColor5, width=lookbackLineWidth5, style=lookbackLineStyle5 == "solid" ? line.style_solid : lookbackLineStyle5 == "dotted" ? line.style_dotted : line.style_dashed)
  235. if showHighLow5
  236. highLine5 := line.new(bar_index - lookbackBars5, periodHigh5, bar_index, periodHigh5, color=highLowColor5, width=lookbackLineWidth5, style=highLowStyle5 == "solid" ? line.style_solid : highLowStyle5 == "dotted" ? line.style_dotted : line.style_dashed)
  237. lowLine5 := line.new(bar_index - lookbackBars5, periodLow5, bar_index, periodLow5, color=highLowColor5, width=lookbackLineWidth5, style=highLowStyle5 == "solid" ? line.style_solid : highLowStyle5 == "dotted" ? line.style_dotted : line.style_dashed)
  238. if showLabel5
  239. lookbackLabel5 := label.new(x5, labelHeight, text=formatTimePeriod(lookbackMinutes5), color=labelColor5, textcolor=color.black, style=label.style_label_up, size=size.normal)
  240.  
  241. // For Candle 4
  242. if enableCandle4
  243. bodyLine4 := line.new(x4, firstPrice4, x4, lastPrice4, color=colorCandle4, width=24)
  244. highWick4 := line.new(x4, periodHigh4, x4, periodLow4, color=colorCandle4, width=2)
  245. if showLookbackLine4
  246. lookbackLine4 := line.new(bar_index - lookbackBars4, periodLow4, bar_index - lookbackBars4, periodHigh4, color=lookbackLineColor4, width=lookbackLineWidth4, style=lookbackLineStyle4 == "solid" ? line.style_solid : lookbackLineStyle4 == "dotted" ? line.style_dotted : line.style_dashed)
  247. if showHighLow4
  248. highLine4 := line.new(bar_index - lookbackBars4, periodHigh4, bar_index, periodHigh4, color=highLowColor4, width=lookbackLineWidth4, style=highLowStyle4 == "solid" ? line.style_solid : highLowStyle4 == "dotted" ? line.style_dotted : line.style_dashed)
  249. lowLine4 := line.new(bar_index - lookbackBars4, periodLow4, bar_index, periodLow4, color=highLowColor4, width=lookbackLineWidth4, style=highLowStyle4 == "solid" ? line.style_solid : highLowStyle4 == "dotted" ? line.style_dotted : line.style_dashed)
  250. if showLabel4
  251. lookbackLabel4 := label.new(x4, labelHeight, text=formatTimePeriod(lookbackMinutes4), color=labelColor4, textcolor=color.black, style=label.style_label_up, size=size.normal)
  252.  
  253. // For Candle 3
  254. if enableCandle3
  255. bodyLine3 := line.new(x3, firstPrice3, x3, lastPrice3, color=colorCandle3, width=24)
  256. highWick3 := line.new(x3, periodHigh3, x3, periodLow3, color=colorCandle3, width=2)
  257. if showLookbackLine3
  258. lookbackLine3 := line.new(bar_index - lookbackBars3, periodLow3, bar_index - lookbackBars3, periodHigh3, color=lookbackLineColor3, width=lookbackLineWidth3, style=lookbackLineStyle3 == "solid" ? line.style_solid : lookbackLineStyle3 == "dotted" ? line.style_dotted : line.style_dashed)
  259. if showHighLow3
  260. highLine3 := line.new(bar_index - lookbackBars3, periodHigh3, bar_index, periodHigh3, color=highLowColor3, width=lookbackLineWidth3, style=highLowStyle3 == "solid" ? line.style_solid : highLowStyle3 == "dotted" ? line.style_dotted : line.style_dashed)
  261. lowLine3 := line.new(bar_index - lookbackBars3, periodLow3, bar_index, periodLow3, color=highLowColor3, width=lookbackLineWidth3, style=highLowStyle3 == "solid" ? line.style_solid : highLowStyle3 == "dotted" ? line.style_dotted : line.style_dashed)
  262. if showLabel3
  263. lookbackLabel3 := label.new(x3, labelHeight, text=formatTimePeriod(lookbackMinutes3), color=labelColor3, textcolor=color.black, style=label.style_label_up, size=size.normal)
  264.  
  265. // For Candle 2
  266. if enableCandle2
  267. bodyLine2 := line.new(x2, firstPrice2, x2, lastPrice2, color=colorCandle2, width=24)
  268. highWick2 := line.new(x2, periodHigh2, x2, periodLow2, color=colorCandle2, width=2)
  269. if showLookbackLine2
  270. lookbackLine2 := line.new(bar_index - lookbackBars2, periodLow2, bar_index - lookbackBars2, periodHigh2, color=lookbackLineColor2, width=lookbackLineWidth2, style=lookbackLineStyle2 == "solid" ? line.style_solid : lookbackLineStyle2 == "dotted" ? line.style_dotted : line.style_dashed)
  271. if showHighLow2
  272. highLine2 := line.new(bar_index - lookbackBars2, periodHigh2, bar_index, periodHigh2, color=highLowColor2, width=lookbackLineWidth2, style=highLowStyle2 == "solid" ? line.style_solid : highLowStyle2 == "dotted" ? line.style_dotted : line.style_dashed)
  273. lowLine2 := line.new(bar_index - lookbackBars2, periodLow2, bar_index, periodLow2, color=highLowColor2, width=lookbackLineWidth2, style=highLowStyle2 == "solid" ? line.style_solid : highLowStyle2 == "dotted" ? line.style_dotted : line.style_dashed)
  274. if showLabel2
  275. lookbackLabel2 := label.new(x2, labelHeight, text=formatTimePeriod(lookbackMinutes2), color=labelColor2, textcolor=color.black, style=label.style_label_up, size=size.normal)
  276.  
  277. // For Candle 1 (Draw last - will be on top)
  278. if enableCandle1
  279. bodyLine1 := line.new(x1, firstPrice1, x1, lastPrice1, color=colorCandle1, width=24)
  280. highWick1 := line.new(x1, periodHigh1, x1, periodLow1, color=colorCandle1, width=2)
  281. if showLookbackLine1
  282. lookbackLine1 := line.new(bar_index - lookbackBars1, periodLow1, bar_index - lookbackBars1, periodHigh1, color=lookbackLineColor1, width=lookbackLineWidth1, style=lookbackLineStyle1 == "solid" ? line.style_solid : lookbackLineStyle1 == "dotted" ? line.style_dotted : line.style_dashed)
  283. if showHighLow1
  284. highLine1 := line.new(bar_index - lookbackBars1, periodHigh1, bar_index, periodHigh1, color=highLowColor1, width=lookbackLineWidth1, style=highLowStyle1 == "solid" ? line.style_solid : highLowStyle1 == "dotted" ? line.style_dotted : line.style_dashed)
  285. lowLine1 := line.new(bar_index - lookbackBars1, periodLow1, bar_index, periodLow1, color=highLowColor1, width=lookbackLineWidth1, style=highLowStyle1 == "solid" ? line.style_solid : highLowStyle1 == "dotted" ? line.style_dotted : line.style_dashed)
  286. if showLabel1
  287. lookbackLabel1 := label.new(x1, labelHeight, text=formatTimePeriod(lookbackMinutes1), color=labelColor1, textcolor=color.black, style=label.style_label_up, size=size.normal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement