Advertisement
Guest User

no error about scopes

a guest
Oct 19th, 2022
784
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 192.00 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. // https://docs.google.com/drawings/d/16COpQpKyvTaH4LYwAmTfXl-ApHo5uuaMDYGcdng5WVk/edit
  3. // Β© AVAndronov
  4.  
  5. //@version=5
  6.  
  7. // INITIALISATION SETTINGS
  8. float INITIAL_CAPITAL = 1000000
  9. float DEFAULT_COMMISSION = 0.06
  10. int MAX_DRAWINGS = 500
  11. int PYRAMIDING = 0
  12. bool IS_OVERLAY = true
  13. int MAX_BARS_BACK = 5000
  14.  
  15. // // Stable
  16. // // Dev
  17. //strategy("Magnum Opus ", overlay = IS_OVERLAY, max_bars_back = MAX_BARS_BACK, pyramiding = PYRAMIDING, initial_capital = INITIAL_CAPITAL,
  18. // currency = currency.NONE, max_labels_count = MAX_DRAWINGS, max_boxes_count = MAX_DRAWINGS, max_lines_count = MAX_DRAWINGS,
  19. // default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent,
  20. // commission_value = DEFAULT_COMMISSION, close_entries_rule="ANY")
  21. indicator("Magnum Opus Dev", overlay = IS_OVERLAY, max_bars_back = MAX_BARS_BACK, max_labels_count = MAX_DRAWINGS, max_boxes_count = MAX_DRAWINGS,
  22. max_lines_count = MAX_DRAWINGS)
  23.  
  24. // Plot Bar Index so we can easilly find what bar to start / end on
  25. plotshape(bar_index, "bar_index", shape.flag, color = na)
  26.  
  27. // --------------------------------------------------------------
  28. // 01 SETTINGS
  29. // --------------------------------------------------------------
  30.  
  31. // --------------------------------------------------------------
  32. // 01.02 SETTINGS | CONSTS
  33. // --------------------------------------------------------------
  34.  
  35. // --------------------------------------------------------------
  36. // 01.02.01 SETTINGS | CONSTS | LIBRARY SETTINGS
  37. // --------------------------------------------------------------
  38.  
  39. bool SHOW_DEBUG_TEXT = true
  40. bool SHOW_DEBUG_LINES = true
  41. bool SHOW_DEBUG_BOXES = true
  42.  
  43. // --------------------------------------------------------------
  44. // 01.02.02 SETTINGS | CONSTS | BOS STYLES
  45. // --------------------------------------------------------------
  46.  
  47. // Used to need this to save plots, not anymore
  48.  
  49. // --------------------------------------------------------------
  50. // 01.02 SETTINGS | INPUTS
  51. // --------------------------------------------------------------
  52.  
  53. // DEBUG SETTINGS
  54. gd = "DEBUG"
  55. DBG_DebugEnabled = input.bool(true, "DEBUG ENABLED", group = gd, tooltip = "Master Switch.\n\nWithout it neither of the following options in this group will work")
  56. DBG_DisableTrim = input.bool(false, "Disable Array Trimming", group = gd)
  57. DBG_FirstLimitOn = input.bool(false, "First Bar", group = gd, inline="gdi1")
  58. DBG_FirstBarLimit = input.int(0, "", group = gd, inline="gdi1")
  59. DBG_LastLimitOn = input.bool(false, "Last Bar", group = gd, inline="gdi1")
  60. DBG_LastBarLimit = input.int(0, "", group = gd, inline="gdi1")
  61. DBG_FirstBar = (DBG_DebugEnabled and DBG_FirstLimitOn) ? DBG_FirstBarLimit : na
  62. DBG_LastBar = (DBG_DebugEnabled and DBG_LastLimitOn) ? DBG_LastBarLimit : na
  63. DBG_ShowPriceActionRecognitionEnabled = input.bool(false, "Show Price Leg Recognition:", group = gd, inline="gdi3")
  64. DBG_ShowPriceActionRecognitionCtf = input.bool(true, "CTF", group = gd, inline="gdi3")
  65. DBG_ShowPriceActionRecognitionHtf = input.bool(false, "HTF", group = gd, inline="gdi3")
  66. DBG_ShowPriceActionRecognitionSingleDirection = input.bool(true, " πŸž— πŸž— Only current direction", group = gd, inline="gdi5")
  67. DBG_ShowPriceActionRecognitionLines = input.bool(true, "High/Low", group = gd, inline="gdi5")
  68. DBG_ShowPriceActionRecognitionBoxes = input.bool(true, "Close/Reverse", group = gd, inline="gdi5")
  69. DBG_ShowPriceActionRecognitionBounds = input.bool(true, "Bounds", group = gd, inline="gdi5")
  70. DBG_VerboseBosCheckDoOutput = input.bool(false, "Show Price Leg Evaluation: ", group = gd, inline="gdi4")
  71. DBG_VerboseBosCheckOutputCtf = input.bool(true, "CTF", group = gd, inline="gdi4")
  72. DBG_VerboseBosCheckOutputHtf = input.bool(false, "HTF", group = gd, inline="gdi4")
  73. DBG_VerboseBosCheckOnlySuccess = input.bool(false, " πŸž— πŸž— Only successfull checks", group = gd, inline="gdi6")
  74. DBG_VerboseBosCheckDetailed = input.bool(true, "Detailed check results", group = gd, inline="gdi6")
  75. DBG_VerboseBosCheckCompact = input.bool(true, "Compact", group = gd, inline="gdi6")
  76. DBG_ShowHTFDebugPlots = input.bool(false, "Show Debug HTF Plots - Candle Values and Pivots", group = gd)
  77. DBG_ShowHLCDebugPlots = input.bool(false, "Show Debug CTF Highest/Lowest Close Plots", group = gd)
  78. DBG_DisableTrimEnabled = DBG_DisableTrim and DBG_DebugEnabled
  79.  
  80. g4 = "Display Settings"
  81. showPPEnabled = input.bool(false, "Show HH/LL Points:", group = g4, inline="g4i5")
  82. showPPCtf = input.bool(true, "CTF ", group = g4, inline="g4i5")
  83. showPPHtf = input.bool(true, "HTF ", group = g4, inline="g4i5")
  84. showHhLlLevels = input.bool(false, "Show HH/LL Levels πŸž— πŸž— πŸž— πŸž— CTF", group = g4, inline="g4i2")
  85. showHhLlLevelsOrigin = input.bool(true, "To Origin", group = g4, inline="g4i2")
  86. showBosEnabled = input.bool(true, "Show BOS Levels:", group = g4, tooltip = "Show BOS levels, as calcualted by algorithm. \n\n"+
  87. "If Core is set to [None], then To Origin won't display regardless")
  88. bosStyleOption0 = "[Same]"
  89. bosStyleOption1 = "[None]"
  90. bosStyleOption2 = "Line x1"
  91. bosStyleOption3 = "Fill"
  92. bosStyleOption4 = "Line x2"
  93. bosStyleOption5 = "Line x1 + Fill"
  94. bosStyleOption6 = "[Full] Line x2 + Fill"
  95. showBosCtfMainText = input.string(bosStyleOption6, "CTF: Core", group = g4, inline="g4i6",
  96. options = [bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  97. showBosCtfOriginText = input.string(bosStyleOption4, "to Origin", group = g4, inline="g4i6",
  98. options = [bosStyleOption0, bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  99. _11 = "1/1"
  100. _21 = "2/1"
  101. _32 = "3/2"
  102. _42 = "4/2"
  103. _43 = "4/3"
  104. _44 = "4/4"
  105. showBosCtfWidth = input.string(_21, "Width", group = g4, inline="g4i6",
  106. options = [_11,_21,_32,_42,_43,_44])
  107. showBosHtfMainText = input.string(bosStyleOption6, "HTF: Core", group = g4, inline="g4i7",
  108. options = [bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  109. showBosHtfOriginText = input.string(bosStyleOption4, "to Origin", group = g4, inline="g4i7",
  110. options = [bosStyleOption0, bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  111. showBosHtfWidth = input.string(_11, "Width", group = g4, inline="g4i7",
  112. options = [_11, _21,_32,_42,_43,_44])
  113. [showBosCtfMainLine, showBosCtfAuxLine, showBosCtfFill] = switch showBosCtfMainText
  114. bosStyleOption1 => [false,false,false]
  115. bosStyleOption2 => [true,false,false]
  116. bosStyleOption3 => [false,false,true]
  117. bosStyleOption4 => [true,true,false]
  118. bosStyleOption5 => [true,false,true]
  119. bosStyleOption6 => [true,true,true]
  120. [showBosHtfMainLine, showBosHtfAuxLine, showBosHtfFill] = switch showBosHtfMainText
  121. bosStyleOption1 => [false,false,false]
  122. bosStyleOption2 => [true,false,false]
  123. bosStyleOption3 => [false,false,true]
  124. bosStyleOption4 => [true,true,false]
  125. bosStyleOption5 => [true,false,true]
  126. bosStyleOption6 => [true,true,true]
  127. [showBosCtfOriginMainLine, showBosCtfOriginAuxLine, showBosCtfOriginFill] = switch showBosCtfOriginText
  128. bosStyleOption0 => [showBosCtfMainLine, showBosCtfAuxLine, showBosCtfFill]
  129. bosStyleOption1 => [false,false,false]
  130. bosStyleOption2 => [true,false,false]
  131. bosStyleOption3 => [false,false,true]
  132. bosStyleOption4 => [true,true,false]
  133. bosStyleOption5 => [true,false,true]
  134. bosStyleOption6 => [true,true,true]
  135. [showBosHtfOriginMainLine, showBosHtfOriginAuxLine, showBosHtfOriginFill] = switch showBosHtfOriginText
  136. bosStyleOption0 => [showBosHtfMainLine, showBosHtfAuxLine, showBosHtfFill]
  137. bosStyleOption1 => [false,false,false]
  138. bosStyleOption2 => [true,false,false]
  139. bosStyleOption3 => [false,false,true]
  140. bosStyleOption4 => [true,true,false]
  141. bosStyleOption5 => [true,false,true]
  142. bosStyleOption6 => [true,true,true]
  143. showBosCtfOriginMainLine := showBosCtfOriginMainLine and showBosCtfMainText != bosStyleOption1
  144. showBosCtfOriginAuxLine := showBosCtfOriginAuxLine and showBosCtfMainText != bosStyleOption1
  145. showBosCtfOriginFill := showBosCtfOriginFill and showBosCtfMainText != bosStyleOption1
  146. showBosHtfOriginMainLine := showBosHtfOriginMainLine and showBosHtfMainText != bosStyleOption1
  147. showBosHtfOriginAuxLine := showBosHtfOriginAuxLine and showBosHtfMainText != bosStyleOption1
  148. showBosHtfOriginFill := showBosHtfOriginFill and showBosHtfMainText != bosStyleOption1
  149. showBosCtfWidthMain = showBosCtfWidth == _44 or showBosCtfWidth == _43 or showBosCtfWidth == _42 ? 4 :
  150. showBosCtfWidth == _32 ? 3 : showBosCtfWidth == _21 ? 2 : 1
  151. showBosCtfWidthAux = showBosCtfWidth == _44 ? 4 : showBosCtfWidth == _43 ? 3 :
  152. showBosCtfWidth == _42 or showBosCtfWidth == _32 ? 2 : 1
  153. showBosHtfWidthMain = showBosHtfWidth == _44 or showBosHtfWidth == _43 or showBosHtfWidth == _42 ? 4 :
  154. showBosHtfWidth == _32 ? 3 : showBosHtfWidth == _21 ? 2 : 1
  155. showBosHtfWidthAux = showBosHtfWidth == _44 ? 4 : showBosHtfWidth == _43 ? 3 :
  156. showBosHtfWidth == _42 or showBosHtfWidth == _32 ? 2 : 1
  157.  
  158. showCBosEnabled = input.bool(true, " πŸž— πŸž— Show Classic BOS Levels:", group = g4,
  159. tooltip = "Show classic BOS levels, meaning, furthest end of correction before continuation.\n\n"+
  160. "If BOS levels are hidden or have Core set to [None], then Classic levels won't display.\n\n"+
  161. "If Classic BOS Core is set to [None], then To Origin won't display regardless.")
  162. showCBosCtfMainText = input.string(bosStyleOption6, "CTF: Core", group = g4, inline="g4i8",
  163. options = [bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  164. showCBosCtfOriginText = input.string(bosStyleOption4, "to Origin", group = g4, inline="g4i8",
  165. options = [bosStyleOption0, bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  166. showCBosCtfWidth = input.string(_21, "Width", group = g4, inline="g4i8",
  167. options = [_11,_21,_32,_42,_43,_44])
  168. showCBosHtfMainText = input.string(bosStyleOption6, "HTF: Core", group = g4, inline="g4i9",
  169. options = [bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  170. showCBosHtfOriginText = input.string(bosStyleOption4, "to Origin", group = g4, inline="g4i9",
  171. options = [bosStyleOption0, bosStyleOption1, bosStyleOption2, bosStyleOption3, bosStyleOption4, bosStyleOption5, bosStyleOption6])
  172. showCBosHtfWidth = input.string(_11, "Width", group = g4, inline="g4i9",
  173. options = [_11, _21,_32,_42,_43,_44])
  174. showCBosHideCollision = input.bool(true, " πŸž— πŸž— Hide CTF Classic BOS if colliding with HTF BOS", group = g4)
  175. [showCBosCtfMainLine, showCBosCtfAuxLine, showCBosCtfFill] = switch showCBosCtfMainText
  176. bosStyleOption1 => [false,false,false]
  177. bosStyleOption2 => [true,false,false]
  178. bosStyleOption3 => [false,false,true]
  179. bosStyleOption4 => [true,true,false]
  180. bosStyleOption5 => [true,false,true]
  181. bosStyleOption6 => [true,true,true]
  182. [showCBosHtfMainLine, showCBosHtfAuxLine, showCBosHtfFill] = switch showCBosHtfMainText
  183. bosStyleOption1 => [false,false,false]
  184. bosStyleOption2 => [true,false,false]
  185. bosStyleOption3 => [false,false,true]
  186. bosStyleOption4 => [true,true,false]
  187. bosStyleOption5 => [true,false,true]
  188. bosStyleOption6 => [true,true,true]
  189. [showCBosCtfOriginMainLine, showCBosCtfOriginAuxLine, showCBosCtfOriginFill] = switch showCBosCtfOriginText
  190. bosStyleOption0 => [showCBosCtfMainLine, showCBosCtfAuxLine, showCBosCtfFill]
  191. bosStyleOption1 => [false,false,false]
  192. bosStyleOption2 => [true,false,false]
  193. bosStyleOption3 => [false,false,true]
  194. bosStyleOption4 => [true,true,false]
  195. bosStyleOption5 => [true,false,true]
  196. bosStyleOption6 => [true,true,true]
  197. [showCBosHtfOriginMainLine, showCBosHtfOriginAuxLine, showCBosHtfOriginFill] = switch showCBosHtfOriginText
  198. bosStyleOption0 => [showCBosHtfMainLine, showCBosHtfAuxLine, showCBosHtfFill]
  199. bosStyleOption1 => [false,false,false]
  200. bosStyleOption2 => [true,false,false]
  201. bosStyleOption3 => [false,false,true]
  202. bosStyleOption4 => [true,true,false]
  203. bosStyleOption5 => [true,false,true]
  204. bosStyleOption6 => [true,true,true]
  205. showCBosCtfMainLine := showCBosCtfMainLine and showBosCtfMainText != bosStyleOption1
  206. showCBosCtfAuxLine := showCBosCtfAuxLine and showBosCtfMainText != bosStyleOption1
  207. showCBosCtfFill := showCBosCtfFill and showBosCtfMainText != bosStyleOption1
  208. showCBosHtfMainLine := showCBosHtfMainLine and showBosHtfMainText != bosStyleOption1
  209. showCBosHtfAuxLine := showCBosHtfAuxLine and showBosHtfMainText != bosStyleOption1
  210. showCBosHtfFill := showCBosHtfFill and showBosHtfMainText != bosStyleOption1
  211. showCBosCtfOriginMainLine := showCBosCtfOriginMainLine and showCBosCtfMainText != bosStyleOption1 and showBosCtfMainText != bosStyleOption1
  212. showCBosCtfOriginAuxLine := showCBosCtfOriginAuxLine and showCBosCtfMainText != bosStyleOption1 and showBosCtfMainText != bosStyleOption1
  213. showCBosCtfOriginFill := showCBosCtfOriginFill and showCBosCtfMainText != bosStyleOption1 and showBosCtfMainText != bosStyleOption1
  214. showCBosHtfOriginMainLine := showCBosHtfOriginMainLine and showCBosHtfMainText != bosStyleOption1 and showBosHtfMainText != bosStyleOption1
  215. showCBosHtfOriginAuxLine := showCBosHtfOriginAuxLine and showCBosHtfMainText != bosStyleOption1 and showBosHtfMainText != bosStyleOption1
  216. showCBosHtfOriginFill := showCBosHtfOriginFill and showCBosHtfMainText != bosStyleOption1 and showBosHtfMainText != bosStyleOption1
  217. showCBosCtfWidthMain = showCBosCtfWidth == _44 or showCBosCtfWidth == _43 or showCBosCtfWidth == _42 ? 4 :
  218. showCBosCtfWidth == _32 ? 3 : showCBosCtfWidth == _21 ? 2 : 1
  219. showCBosCtfWidthAux = showCBosCtfWidth == _44 ? 4 : showCBosCtfWidth == _43 ? 3 :
  220. showCBosCtfWidth == _42 or showCBosCtfWidth == _32 ? 2 : 1
  221. showCBosHtfWidthMain = showCBosHtfWidth == _44 or showCBosHtfWidth == _43 or showCBosHtfWidth == _42 ? 4 :
  222. showCBosHtfWidth == _32 ? 3 : showCBosHtfWidth == _21 ? 2 : 1
  223. showCBosHtfWidthAux = showCBosHtfWidth == _44 ? 4 : showCBosHtfWidth == _43 ? 3 :
  224. showCBosHtfWidth == _42 or showCBosHtfWidth == _32 ? 2 : 1
  225.  
  226. g11 = "Trend Display Settings"
  227. showTrendOptionNone = "None"
  228. showTrendOptionCtf = "Current"
  229. showTrendOptionHtf = "Higher"
  230. showTrendBGText = input.string(showTrendOptionNone, "Trend Display: Background", group = g11,
  231. options = [showTrendOptionNone, showTrendOptionCtf, showTrendOptionHtf])
  232. showTrendBG = showTrendBGText == showTrendOptionNone ? 0 : showTrendBGText == showTrendOptionCtf ? 1 : 2
  233. showTrendAboveText = input.string(showTrendOptionCtf, "Trend Display: Above", group = g11,
  234. options = [showTrendOptionNone, showTrendOptionCtf, showTrendOptionHtf])
  235. showTrendAbove = showTrendAboveText == showTrendOptionNone ? 0 : showTrendAboveText == showTrendOptionCtf ? 1 : 2
  236. showTrendBelowText = input.string(showTrendOptionHtf, "Trend Display: Below", group = g11,
  237. options = [showTrendOptionNone, showTrendOptionCtf, showTrendOptionHtf])
  238. showTrendBelow = showTrendBelowText == showTrendOptionNone ? 0 : showTrendBelowText == showTrendOptionCtf ? 1 : 2
  239. showTrendLabels = input.bool(false, "Show Trend Change Labels", group = g11, inline = "g11i1")
  240. showTrendLabelsCtf = input.bool(true, "CTF", group = g11, inline = "g11i1")
  241. showTrendLabelsHtf = input.bool(false, "HTF", group = g11, inline = "g11i1")
  242.  
  243. g13 = "Trend and BOS Algo Settings"
  244. maxInitialCandlesToDetermineTrend = input.int(5, "Max candles to determine the initial trend", group = g13,
  245. tooltip = "After receiving starting bar, wait this many candles before forcing determination of the initial trend.\n\n"+
  246. "Within this limit, the algorithm tries to be smart and detect the first price leg (high-low-higher high, or low-high-lower low), but once enough bars elapse, "+
  247. "a simple choice will be made based on whether the starting bar's high or low was breached first. \n\nIf price stayed within the high and low of the first bar, then "+
  248. "the choice will be made based on whether the first bar was bearish or bullish, with a bias towards bullish (in rare cases when open = close)")
  249. initialTrendOption1 = "None"
  250. initialTrendOption2 = "Long"
  251. initialTrendOption3 = "Short"
  252. initialCtfTrend = input.string(initialTrendOption1, "Initial Trend CTF", group = g13, inline = "g13i2",
  253. options = [initialTrendOption1, initialTrendOption2, initialTrendOption3])
  254. initialHtfTrend = input.string(initialTrendOption1, "HTF", group = g13, inline = "g13i2",
  255. options = [initialTrendOption1, initialTrendOption2, initialTrendOption3], tooltip = "Force initial trend or let it be determined by the algorithm")
  256. var initialCtfTrendDirection = initialCtfTrend == initialTrendOption1 ? 0 : initialCtfTrend == initialTrendOption2 ? 2 : -2
  257. var initialHtfTrendDirection = initialHtfTrend == initialTrendOption1 ? 0 : initialHtfTrend == initialTrendOption2 ? 2 : -2
  258. calculateHtfTrendEnabled = input.bool(true, "Calculate HTF Trend & BOS, with HTF determined", group = g13, inline = "g13i1")
  259. chooseHtfMethodOptionSequential = "Sequentially: 5m/15m/30m/1H/4H/1D/1W/1M"
  260. chooseHtfMethodOptionSkip = "Skipping: 1m/5m/30m/4H/1D/1W/1M"
  261. chooseHtfMethodText = input.string(chooseHtfMethodOptionSkip, "", group = g13, inline = "g13i1",
  262. options = [chooseHtfMethodOptionSequential, chooseHtfMethodOptionSkip], tooltip ="The way HTF is chosen for calculating HTF trend.\n\n"+
  263. chooseHtfMethodOptionSequential+"\nNext higher timeframe is chosen, in order, so for anything up to 5m, the 5m TF is chosen, for anything up to 30m, "+
  264. "the 30m TF is chosen etc.\n\n"+
  265. chooseHtfMethodOptionSkip+"\nHigher timeframe is chosen skipping intermediary timeframes. So, for anything up to and including 1m, the 5m TF is chosen, "+
  266. "then for anything up to and including 5m, the 30m TF is chosen, for for anything up to and including 30m, the 4H TF is chosen, etc")
  267. var chooseHtfSkip = chooseHtfMethodText == chooseHtfMethodOptionSkip
  268. // This is used in code to query HTF used for calculations
  269. var chosenHtf =
  270. // DUPLICATE CODE
  271. // BECAUSE OF PINESCRIPT SHENANIGANS
  272. not calculateHtfTrendEnabled ? "1M" : chooseHtfSkip ?
  273. (timeframe.in_seconds() <= timeframe.in_seconds("1") ? "5" :
  274. timeframe.in_seconds() <= timeframe.in_seconds("5") ? "30" :
  275. timeframe.in_seconds() <= timeframe.in_seconds("30") ? "240" :
  276. timeframe.in_seconds() <= timeframe.in_seconds("240") ? "1D" :
  277. timeframe.in_seconds() <= timeframe.in_seconds("1D") ? "1W" :
  278. timeframe.in_seconds() <= timeframe.in_seconds("1W") ? "1M" : "1M")
  279. :
  280. (timeframe.in_seconds() < timeframe.in_seconds("1") ? "1" :
  281. timeframe.in_seconds() < timeframe.in_seconds("5") ? "5" :
  282. timeframe.in_seconds() < timeframe.in_seconds("15") ? "15" :
  283. timeframe.in_seconds() < timeframe.in_seconds("30") ? "30" :
  284. timeframe.in_seconds() < timeframe.in_seconds("60") ? "60" :
  285. timeframe.in_seconds() < timeframe.in_seconds("240") ? "240" :
  286. timeframe.in_seconds() < timeframe.in_seconds("1D") ? "1D" :
  287. timeframe.in_seconds() < timeframe.in_seconds("1W") ? "1W" :
  288. timeframe.in_seconds() < timeframe.in_seconds("1M") ? "1M" : "1M")
  289. // This is used in code to check whether we are calculating htf trend or not
  290. calculateHtfTrend = calculateHtfTrendEnabled and timeframe.in_seconds() < timeframe.in_seconds(chosenHtf)
  291.  
  292. g3 = "HH LL Settings"
  293. ctfpivPLeft = input.int(2, "Pivot Point Period: Left", minval = 1, group = g3, inline="g3i1")
  294. ctfpivPer = input.int(0, "Right", minval = 0, group = g3, inline="g3i1")
  295. ctfcheckAdjacentPivotThreshold = input.float(1.0, "Pivot Point Adjacency Threshold (%)",minval = 0, step = 0.01, group = g3,
  296. tooltip = "If pivot period left is at least 2, and pivot point was not found because adjacent candle's wick is slightly off by this much percent, in price, "+
  297. "but open/close is within this much percent, and otherwise there is a clear pivot point there, consider it a pivot point.\n\n"+
  298. "0 = Only consider true fractal pivot points\n\n"+
  299. "Example: For a short pivot point with a left period of 2, low of this candle must be lower than 2 previous lows. In case \"1\" is set for this input, "+
  300. "a short pivot point will be accepted also if all of the following conditions are met: \n"+
  301. "a) this candle's low is lower than all lows except the last low\n"+
  302. "b) this candle's body low (close or open, whichever lowest) is lower or within 1% of the previous one\n"+
  303. "c) this candle's low is within 1% of the previous one")
  304.  
  305. g9 = "BOS & HL Break Settings"
  306. ctfHLBreakThreshold = input.float(0.25, "High/Low Break Threshold (%)",minval = 0, step = 0.1, group = g9,
  307. tooltip = "Prior High / Low is considered broken only when price exceeds it by this much percent")
  308. ctfHLBreakATREnabled = input.bool(true, " πŸž— πŸž— Use ATR instead, Len.", group = g9, inline = "g9i1")
  309. ctfHLBreakATRLength = input.int(14, "",minval = 1, group = g9, inline = "g9i1")
  310. ctfHLBreakATRThreshold = input.float(12, "Thr. (%)",minval = 0, step = 0.1, group = g9, inline = "g9i1",
  311. tooltip = "Prior High / Low is considered broken when price exceeds it by ATR value mutliplied by this much percent")
  312. newBosConfirmOptionWithin = "At least within old BOS"
  313. newBosConfirmOptionPartiallyOutside = "Partially outside old BOS"
  314. newBosConfirmOptionBeyond = "Completely beyond old BOS"
  315. ctfnewBosConfirmConditionString = input.string(newBosConfirmOptionPartiallyOutside, "BOS Break is finalized when new BOS is", group = g9,
  316. options = [newBosConfirmOptionWithin, newBosConfirmOptionPartiallyOutside, newBosConfirmOptionBeyond],
  317. tooltip = "Trend changes when BOS is breached. But after the BOS is breached, new trend's BOS cannot be located "+
  318. "between the old extreme (high for an uptrend, low for a downtrend) and the old BOS, "+
  319. "as it would make no sense.\n\nTherefore, new BOS must be located at least beyond the closest boundary of the old BOS (top for a short BOS, bottom for a long BOS). "+
  320. "This setting controls how far must new BOS be located for it to be accepted and for trend break to be finalized.\n\n"+
  321. "\"At least within old BOS\":\nOnce the new BOS is beyond the closest boundary of an old BOS it's accepted as a new BOS and trend break is finalized. "+
  322. "For example, for a trend to change from long to short, it means that the new long BOS must be below old short BOS's top bound. "+
  323. "It may be within old BOS, it just has to clear the area between the high and the beginning of the old BOS\n\n"+
  324. "\"Beyond old BOS\":\nOnly once the new BOS is beyond the furthest boundary of an old BOS it's accepted as a new BOS and trend break is finalized. "+
  325. "For example, for a trend to change from long to short, it means that the new long BOS must be below old short BOS's bottom bound. "+
  326. "It has to be completely below the old BOS.")
  327. ctfnewBosConfirmConditionLevel = ctfnewBosConfirmConditionString == newBosConfirmOptionPartiallyOutside ? 2 :
  328. ctfnewBosConfirmConditionString == newBosConfirmOptionBeyond ? 1 : 0
  329. ctfnewBosThreshold = input.float(0.4, "BOS Condition Check Threshold (%)",minval = 0, step = 0.1, group = g9,
  330. tooltip = "Conditions that check below/above/beyond BOS must be met with an additional offset of this much percent, in absoulte price movement.\n\n"+
  331. "For example, for candle being beyond BOS, it must not just differ by 1 tick, but by this much percent. Same way, for new BOS to be beyond old BOS, "+
  332. "1 tick is not enough, it must be this much percent")
  333.  
  334. g7 = "BOS Break Conditions - \"OR\" - At least one must be true"
  335. ctfbosBreakCandleThresholdOr = input.int(2 , "CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 1, group = g7, inline="g7i1",
  336. tooltip = "This many candles must be completely beyond BOS to confirm a BOS break")
  337. ctfusebosBreakCandleThresholdOr = input.bool(true,"ON",group = g7, inline="g7i1")
  338.  
  339. g8 = "BOS Break Conditions - \"AND\" - Every condition must be true"
  340. ctfbosBreakCandleThresholdAnd = input.int(2 , "CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 1, group = g8, inline="g8i1",
  341. tooltip = "This many candles must be completely beyond BOS to confirm a BOS break")
  342. ctfusebosBreakCandleThresholdAnd = input.bool(false,"ON",group = g8, inline="g8i1")
  343.  
  344. g10 = "BOS Change Settings"
  345. ctfbosConsiderNextEnabled = input.bool(true, "Consider further options within (%)", group = g10, inline="g10i1",
  346. tooltip = "When choosing a pivot point to become a new BOS, and current one passes all conditions, consider the next one if it is within this much "+
  347. "percent from the current one. \n\nFor example, in a downtrend, if price fell from 200 to 100, then rebounded to 154, then fell to 110, " +
  348. "then rebounded to 150, and then broke down past 100, we can choose 150 or 154 as the new long BOS. If this parameter's value is more than 10%, "+
  349. "even if 150 passes all our checks, we will consider 155 because 54 < 50 + 10%.")
  350. ctfbosConsiderNextDistanceThreshold = input.float(10.0, "", minval = 0, step = 0.1, group = g10, inline="g10i1")
  351. ctfbosConsiderNextConsumptionEnabled = input.bool(true, " πŸž— πŸž— Only if it would be consumed no more than (%)",group = g10, inline="g10i2",
  352. tooltip = "When considering next, only consider it if it would not be consumed by this much % by current point. \n\n" +
  353. "For example, in a downtrend, current checked Long BOS's high is at 100, and next high to be considered is at 110. " +
  354. "If this input is set to 50%, then next BOS's low, which is its origin candle's body high (open or close, whichever higher), should be no lower than 90, "+
  355. "otherwise it would be consumed more than 50%, because if, for example, it would be equal to 89, then (100 - 89) / (110 - 89) = 11 / 21 > 50%.")
  356. ctfbosConsiderNextConsumptionThreshold = input.float(60.0, "",minval = 0, step = 0.1, group = g10, inline="g10i2")
  357. ctfbosConsiderNextRecursive = input.bool(true, " πŸž— πŸž— Compare against last point recursively", group = g10,
  358. tooltip = "When finding a pivot point within the range of a prior pivot point that passed all conditions, continue checking relative to last pivot point's price. "+
  359. "Therefore, percentage range above is recursively applied to each new passing pivot point, "+
  360. "rather than just searching within that range of the first ever passing pivot point that was discovered. \n\nConsumtion is always checked against last passing point")
  361.  
  362. ctfbosIgnoreManipulationsEnabled = input.bool(true, "Ignore manipulations with a close far from the wick",group = g10,
  363. tooltip = "When a high/low is broken by a candle wick which close is at least this far away from the wick (in % relative to candle length from high to low), "+
  364. "ignore the price legs within this many candles from the breakout candle.\n\nThe price leg that includes breakout candle itself is always ignored, and handled "+
  365. "by the next setting.\n\nIf second global break in a row happens, this setting is ignored.")
  366. ctfbosIgnoreManipulationsThreshold = input.float(40.0, " πŸž— πŸž— Wick threshold (%)",minval = 0, step = 0.1, group = g10, inline="g10i3")
  367. ctfbosIgnoreManipulationsValue = input.int(1, "Candles",minval = 1, group = g10, inline="g10i3")
  368. ctfbosIgnoreManipulationsAmount = ctfbosIgnoreManipulationsEnabled ? ctfbosIgnoreManipulationsValue : 0
  369. ctfbosSameCandleLegConsumptionDelay = input.int(1, "Breakout candle price leg consumption delay, Candles", minval = 1, group = g10,
  370. tooltip = "If there is a price leg that ends on a breakout candle, it will only be consumed after this many candles close above it")
  371. ctfbosSidewaysCandleInsideThreshold = input.float(60.0, "Sideways Inclusion Candle Thershold (%)",minval = 0, maxval = 100, step = 0.1, group = g10,
  372. tooltip = "Candle is considered part of sideways movement if this much poercent of it or its body (determined by next input) is inside the bounds.")
  373. ctfbosSidewaysCandleBodyThreshold = input.float(55.0, " πŸž— πŸž— Full Body Candle Thershold (%)",minval = 0, maxval = 100, step = 0.1, group = g10,
  374. tooltip = "Candles are considered full body if their body is this much of their entire length.\n\n" +
  375. "If candle is a full body candle, its body must be inside the bounds, and if not, whole candle must be inside the bounds.")
  376.  
  377. g5 = "BOS Change Conditions - \"OR\" - At least one must be true"
  378. ctfbosFiboCorrectionWickRelativeThresholdOr = input.float(0.290, "Correction Wick (Relative Fibo) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval=1, step = 0.001, group = g5, inline="g5i1",
  379. tooltip = "Price must correct this much (low to high in a downtrend, high to low in an uptrend).\n\n"+
  380. "This move then is compared to fibo applied to whole movement (absolute low to BOS in a downtrend, absolute high to OS in an uptrend).\n\n"+
  381. "For this condition to pass, move must be at least as big as the value.\n\n"+
  382. "EXAMPLE:\n\nPrice breaks high, reaches 150, BOS is at 50. Price moved 100. If 0.382 is the condition value, "+
  383. "then a correction of at least 38.2 from 150 must happen for the low to be considered good enough to become new BOS. "+
  384. "Therfore, price must drop to at least 111.8 = 150 - 38.2.")
  385. ctfusebosFiboCorrectionWickRelativeThresholdOr = input.bool(true,"ON",group = g5, inline="g5i1")
  386. ctfbosFiboCorrectionCloseRelativeThresholdOr = input.float(0.235, "Correction Close (Relative Fibo)πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval=1, step = 0.001, group = g5, inline="g5i7",
  387. tooltip = "Price must correct this much (low to high in a downtrend, high to low in an uptrend).\n\n"+
  388. "This move then is compared to fibo applied to whole movement (absolute low to BOS in a downtrend, absolute high to OS in an uptrend).\n\n"+
  389. "For this condition to pass, move must be at least as big as the value.\n\n"+
  390. "EXAMPLE:\n\nPrice breaks high, highest close reaches 150, BOS is at 50. Price moved 100. If 0.382 is the condition value, "+
  391. "then a correction of at least 38.2 from 150 must happen for the low to be considered good enough to become new BOS. "+
  392. "Therfore, price must drop to at least 111.8 = 150 - 38.2.")
  393. ctfusebosFiboCorrectionCloseRelativeThresholdOr = input.bool(true,"ON",group = g5, inline="g5i7")
  394. ctfbosFiboReverseRelativeThresholdOr = input.float(0.160, "Reverse Correction (Relative Fibo) πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval=1, step = 0.001, group = g5, inline="g5i3",
  395. tooltip = "Price must correct this much from local inverse extreme (lowest high in a downtrend, highest low in an uptrend). "+
  396. "This move is then compared to fibo applied to whole movement (absolute low to BOS in a downtrend, absolute high to BOS in an uptrend).\n\n"+
  397. "For this condition to pass, move must be at least as big as the value.\n\n"+
  398. "EXAMPLE:\n\nPrice breaks high, reaches 150, while highest low of this whole movement was at 100. BOS is at 50. Price moved 100. If 0.382 is the condition value, "+
  399. "then a correction of at least 38.2 from 100 must happen for the low to be considered good enough to become new BOS. "+
  400. "Therefore, price must drop to at least 61.8 = 100 - 38.2.")
  401. ctfusebosFiboReverseRelativeThresholdOr = input.bool(true, "ON", group = g5, inline="g5i3")
  402. ctfbosCandleLengthThresholdOr = input.int(20, "Sideways CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 1, group = g5, inline="g5i5",
  403. tooltip = "Price must spend this many candles in a sideways price range before exiting for the level to become new BOS.\n\n"+
  404. "EXAMPLE:\n\nPrice breaks high, reaches 150, then drops to 100, then reaches 140, then drops to 130, then breaches 150. If 30 is the condition value, "+
  405. "then price must have spent at least 30 candles above 130 before breaching 150 in order for 130 to become new BOS.")
  406. ctfusebosCandleLengthThresholdOr = input.bool(true,"ON",group = g5, inline="g5i5")
  407. ctfbosAtrThresholdOr = input.float(5.00, "Correction Wick (ATR) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 0, step = 0.01, group = g5, inline="g5i6",
  408. tooltip = "Same as correction wick (fibo), but relative to price ATR. Meaning, correction from wick must be higher than this many times ATR at the time of condition check")
  409. ctfusebosAtrThresholdOr = input.bool(true,"ON",group = g5, inline="g5i6")
  410.  
  411. g6 = "BOS Change Conditions - \"AND\" - Every condition must be true"
  412. ctfbosFiboCorrectionWickRelativeThresholdAnd = input.float(0.382, "Correction Wick (Relative Fibo) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval = 1, step = 0.001, group = g6, inline="g6i1")
  413. ctfusebosFiboCorrectionWickRelativeThresholdAnd = input.bool(false,"ON",group = g6, inline="g6i1")
  414. ctfbosFiboCorrectionCloseRelativeThresholdAnd = input.float(0.236, "Correction Close (Relative Fibo)πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval = 1, step = 0.001, group = g6, inline="g6i7")
  415. ctfusebosFiboCorrectionCloseRelativeThresholdAnd = input.bool(false,"ON",group = g6, inline="g6i7")
  416. ctfbosFiboReverseRelativeThresholdAnd = input.float(0.048, "Reverse Correction (Relative Fibo) πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval=1, step = 0.001, group = g6, inline="g6i3")
  417. ctfusebosFiboReverseRelativeThresholdAnd = input.bool(false,"ON",group = g6, inline="g6i3")
  418. ctfbosCandleLengthThresholdAnd = input.int(7, "Sideways CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 1, group = g6, inline="g6i5")
  419. ctfusebosCandleLengthThresholdAnd = input.bool(true,"ON",group = g6, inline="g6i5")
  420. ctfbosAtrThresholdAnd = input.float(5.00, "Correction Wick (ATR) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 0, step = 0.01, group = g6, inline="g6i6")
  421. ctfusebosAtrThresholdAnd = input.bool(false,"ON",group = g6, inline="g6i6")
  422.  
  423. g12 = "Separate Higher Timeframe Settings"
  424. htfSettingsEnabled = input.bool(true, "ENABLE SEPARATE SETTINGS FOR HIGHER TIMEFRAMES", group = g12)
  425. htfThreshold = input.timeframe(title="Higher Timeframe starts from", defval="1W", group = g12)
  426.  
  427. htfg3 = "[HTF] HH LL Settings"
  428. htfSettingsg3 = input.bool(true, "ENABLE SEPARATE SETTINGS FOR THIS GROUP", group = htfg3)
  429. htfpivPLeft = input.int(1, "Pivot Point Period: Left", minval = 0, group = htfg3, inline="htfg3i1")
  430. htfpivPer = input.int(0, "Right", minval = 0, group = htfg3, inline="htfg3i1")
  431. htfcheckAdjacentPivotThreshold = input.float(2.5, "Pivot Point Adjacency Threshold (%)",minval = 0, step = 0.01, group = htfg3)
  432.  
  433. htfg9 = "[HTF] BOS Break Settings"
  434. htfSettingsg9 = input.bool(false, "ENABLE SEPARATE SETTINGS FOR THIS GROUP", group = htfg9)
  435. htfHLBreakThreshold = input.float(0.25, "High/Low Break Threshold (%)",minval = 0, step = 0.1, group = htfg9)
  436. htfHLBreakATREnabled = input.bool(true, " πŸž— πŸž— Use ATR instead, Len.", group = htfg9, inline = "htfg9i1")
  437. htfHLBreakATRLength = input.int(14, "",minval = 1, group = htfg9, inline = "htfg9i1")
  438. htfHLBreakATRThreshold = input.float(12, "Thr. (%)", minval = 0, step = 0.1, group = htfg9, inline = "htfg9i1")
  439. htfnewBosConfirmConditionString = input.string(newBosConfirmOptionWithin, "BOS Break is finalized when new BOS is", group = htfg9,
  440. options = [newBosConfirmOptionWithin, newBosConfirmOptionPartiallyOutside, newBosConfirmOptionBeyond])
  441. htfnewBosConfirmConditionLevel = htfnewBosConfirmConditionString == newBosConfirmOptionPartiallyOutside ? 2 :
  442. htfnewBosConfirmConditionString == newBosConfirmOptionBeyond ? 1 : 0
  443. htfnewBosThreshold = input.float(0.5, "BOS Condition Check Threshold (%)",minval = 0, step = 0.1, group = htfg9)
  444.  
  445. htfg7 = "[HTF] BOS Break Conditions - \"OR\" - At least one true"
  446. htfSettingsg7 = input.bool(true, "ENABLE SEPARATE SETTINGS FOR THIS GROUP", group = htfg7)
  447. htfbosBreakCandleThresholdOr = input.int(1 , "CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 1, group = htfg7, inline="htfg7i1")
  448. htfusebosBreakCandleThresholdOr = input.bool(true,"ON",group = htfg7, inline="htfg7i1")
  449.  
  450. htfg8 = "[HTF] BOS Break Conditions - \"AND\" - Every condition true"
  451. htfSettingsg8 = input.bool(false, "ENABLE SEPARATE SETTINGS FOR THIS GROUP", group = htfg8)
  452. htfbosBreakCandleThresholdAnd = input.int(2 , "CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 1, group = htfg8, inline="htfg8i1")
  453. htfusebosBreakCandleThresholdAnd = input.bool(false,"ON",group = htfg8, inline="htfg8i1")
  454.  
  455. htfg10 = "[HTF] BOS Change Settings"
  456. htfSettingsg10 = input.bool(false, "ENABLE SEPARATE SETTINGS FOR THIS GROUP", group = htfg10)
  457. htfbosConsiderNextEnabled = input.bool(true, "Consider further options within (%)", group = htfg10, inline="htfg10i1")
  458. htfbosConsiderNextDistanceThreshold = input.float(10.0, "",minval = 0, step = 0.1, group = htfg10, inline="htfg10i1")
  459. htfbosConsiderNextConsumptionEnabled = input.bool(true, " πŸž— πŸž— Only if it would be consumed no more than (%)",group = htfg10, inline="htfg10i2")
  460. htfbosConsiderNextConsumptionThreshold = input.float(60.0, "",minval = 0, step = 0.1, group = htfg10, inline="htfg10i2")
  461. htfbosConsiderNextRecursive = input.bool(true, "Compare against last point recursively", group = htfg10)
  462. htfbosIgnoreManipulationsEnabled = input.bool(true, "Ignore manipulations with a close far from the wick",group = htfg10)
  463. htfbosIgnoreManipulationsThreshold = input.float(40.0, " πŸž— πŸž— Wick threshold (%)",minval = 0, step = 0.1, group = htfg10, inline="htfg10i3")
  464. htfbosIgnoreManipulationsValue = input.int(2, "Candles",minval = 1, group = htfg10, inline="htfg10i3")
  465. htfbosIgnoreManipulationsAmount = htfbosIgnoreManipulationsEnabled ? htfbosIgnoreManipulationsValue : 0
  466. htfbosSameCandleLegConsumptionDelay = input.int(1, "Breakout candle price leg consumption delay, Candles", minval = 1, group = htfg10)
  467. htfbosSidewaysCandleInsideThreshold = input.float(60.0, "Sideways Inclusion Candle Thershold (%)",minval = 0, maxval = 100, step = 0.1, group = htfg10)
  468. htfbosSidewaysCandleBodyThreshold = input.float(55.0, " πŸž— πŸž— Full Body Candle Thershold (%)",minval = 0, maxval = 100, step = 0.1, group = htfg10)
  469.  
  470. htfg5 = "[HTF] BOS Change Conditions - \"OR\" - At least one true"
  471. htfSettingsg5 = input.bool(true, "ENABLE SEPARATE SETTINGS FOR THIS GROUP", group = htfg5)
  472. htfbosFiboCorrectionWickRelativeThresholdOr = input.float(0.300, "Correction Wick (Relative Fibo) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 0, maxval=1, step = 0.001, group = htfg5, inline="hg5i1")
  473. htfusebosFiboCorrectionWickRelativeThresholdOr = input.bool(true,"ON",group = htfg5, inline="hg5i1")
  474. htfbosFiboCorrectionCloseRelativeThresholdOr = input.float(0.235, "Correction Close (Relative Fibo)πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval=1, step = 0.001, group = htfg5, inline="hg5i7")
  475. htfusebosFiboCorrectionCloseRelativeThresholdOr = input.bool(true,"ON",group = htfg5, inline="hg5i7")
  476. htfbosFiboReverseRelativeThresholdOr = input.float(0.160, "Reverse Correction (Relative Fibo) πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval=1, step = 0.001, group = htfg5, inline="htfg5i3")
  477. htfusebosFiboReverseRelativeThresholdOr = input.bool(true,"ON",group = htfg5, inline="htfg5i3")
  478. htfbosCandleLengthThresholdOr = input.int(8, "Sideways CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 1, group = htfg5, inline="htfg5i5")
  479. htfusebosCandleLengthThresholdOr = input.bool(true,"ON",group = htfg5, inline="htfg5i5")
  480. htfbosAtrThresholdOr = input.float(5.00, "Correction Wick (ATR) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 0, step = 0.01, group = htfg5, inline="htfg5i6",
  481. tooltip = "Same as correction wick (fibo), but relative to price ATR")
  482. htfusebosAtrThresholdOr = input.bool(true,"ON",group = htfg5, inline="htfg5i6")
  483.  
  484.  
  485. htfg6 = "[HTF] BOS Change Conditions - \"AND\" - Every condition true"
  486. htfSettingsg6 = input.bool(true, "ENABLE SEPARATE SETTINGS FOR THIS GROUP", group = htfg6)
  487. htfbosFiboCorrectionWickRelativeThresholdAnd = input.float(0.382, "Correction Wick (Relative Fibo) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval = 1, step = 0.001, group = htfg6, inline="hg6i1")
  488. htfusebosFiboCorrectionWickRelativeThresholdAnd = input.bool(false,"ON",group = htfg6, inline="hg6i1")
  489. htfbosFiboCorrectionCloseRelativeThresholdAnd = input.float(0.236, "Correction Close (Relative Fibo)πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval = 1, step = 0.001, group = htfg6, inline="hg6i7")
  490. htfusebosFiboCorrectionCloseRelativeThresholdAnd = input.bool(false,"ON",group = htfg6, inline="hg6i7")
  491. htfbosFiboReverseRelativeThresholdAnd = input.float(0.146, "Reverse Correction (Relative Fibo) πŸž— πŸž— πŸž— πŸž—",minval = 0, maxval=1, step = 0.001, group = htfg6, inline="htfg6i3")
  492. htfusebosFiboReverseRelativeThresholdAnd = input.bool(false,"ON",group = htfg6, inline="htfg6i3")
  493. htfbosCandleLengthThresholdAnd = input.int(4, "Sideways CandlesπŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—",minval = 1, group = htfg6, inline="htfg6i5")
  494. htfusebosCandleLengthThresholdAnd = input.bool(true,"ON",group = htfg6, inline="htfg6i5")
  495. htfbosAtrThresholdAnd = input.float(5.00, "Correction Wick (ATR) πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž— πŸž—", minval = 0, step = 0.01, group = htfg6, inline="htfg6i6",
  496. tooltip = "Same as correction wick (fibo), but relative to price ATR")
  497. htfusebosAtrThresholdAnd = input.bool(false,"ON",group = htfg6, inline="htfg6i6")
  498.  
  499.  
  500. g14 = "CTF BOS Style"
  501.  
  502. colorBosLongMain = input.color(#119988, "Long Main", group = g14, inline="g14i1")
  503. colorBosLongAux = input.color(#ffff44, "Auxiliary", group = g14, inline="g14i1")
  504. colorBosShortMain = input.color(#ff4433, "Short Main", group = g14, inline="g14i1")
  505. colorBosShortAux = input.color(#ff9900, "Auxiliary", group = g14, inline="g14i1")
  506.  
  507. ctfBosT1 = input.int(90, "Transparency", 0, 100, group = g14, inline ="g14i3")
  508. ctfBosT3 = input.int(50, "", 0, 100, group = g14, inline ="g14i3")
  509. ctfBosT5 = input.int(15, "", 0, 100, group = g14, inline ="g14i3")
  510. ctfBosT2 = (ctfBosT1 + ctfBosT3) / 2
  511. ctfBosT4 = (ctfBosT5 + ctfBosT3) / 2
  512.  
  513. g14c = "CTF Classic BOS Style"
  514.  
  515. colorCBosLongMain = input.color(#666666, "Long Main", group = g14c, inline="g14i2")
  516. colorCBosLongAux = input.color(#bbbbbb, "Auxiliary", group = g14c, inline="g14i2")
  517. colorCBosShortMain = input.color(#777777, "Short Main", group = g14c, inline="g14i2")
  518. colorCBosShortAux = input.color(#888888, "Auxiliary", group = g14c, inline="g14i2")
  519. ctfCBosT = input.int(50, "Transparency multiplier (extra for classic BOS)", 0, 100, group = g14c)
  520.  
  521. ctfCBosT1 = 100 - (100 - ctfBosT1) * (100 - ctfCBosT) / 100
  522. ctfCBosT2 = 100 - (100 - ctfBosT2) * (100 - ctfCBosT) / 100
  523. ctfCBosT3 = 100 - (100 - ctfBosT3) * (100 - ctfCBosT) / 100
  524. ctfCBosT4 = 100 - (100 - ctfBosT4) * (100 - ctfCBosT) / 100
  525. ctfCBosT5 = 100 - (100 - ctfBosT5) * (100 - ctfCBosT) / 100
  526.  
  527. g15 = "HTF BOS Style"
  528.  
  529. htfcolorBosLongMain = input.color(#5566ff, "Long Main", group = g15, inline="g15i1")
  530. htfcolorBosLongAux = input.color(#7777cc, "Auxiliary", group = g15, inline="g15i1")
  531. htfcolorBosShortMain = input.color(#ff33ff, "Short Main", group = g15, inline="g15i1")
  532. htfcolorBosShortAux = input.color(#ee66cc, "Auxiliary", group = g15, inline="g15i1")
  533.  
  534. htfBosT1 = input.int(95, "Transparency", 0, 100, group = g15, inline ="g15i3")
  535. htfBosT3 = input.int(75, "", 0, 100, group = g15, inline ="g15i3")
  536. htfBosT5 = input.int(55, "", 0, 100, group = g15, inline ="g15i3")
  537. htfBosT2 = (htfBosT1 + htfBosT3) / 2
  538. htfBosT4 = (htfBosT5 + htfBosT3) / 2
  539.  
  540. g15c = "HTF Classic BOS Style"
  541.  
  542. htfcolorCBosLongMain = input.color(#999999, "Long Main", group = g15c, inline="g15i2")
  543. htfcolorCBosLongAux = input.color(#cccccc, "Auxiliary", group = g15c, inline="g15i2")
  544. htfcolorCBosShortMain = input.color(#888888, "Short Main", group = g15c, inline="g15i2")
  545. htfcolorCBosShortAux = input.color(#dddddd, "Auxiliary", group = g15c, inline="g15i2")
  546.  
  547. htfCBosT = input.int(50, "Transparency multiplier (extra for classic BOS)", 0, 100, group = g15c)
  548. htfCBosT1 = 100 - (100 - htfBosT1) * (100 - htfCBosT) / 100
  549. htfCBosT2 = 100 - (100 - htfBosT2) * (100 - htfCBosT) / 100
  550. htfCBosT3 = 100 - (100 - htfBosT3) * (100 - htfCBosT) / 100
  551. htfCBosT4 = 100 - (100 - htfBosT4) * (100 - htfCBosT) / 100
  552. htfCBosT5 = 100 - (100 - htfBosT5) * (100 - htfCBosT) / 100
  553.  
  554. g16 = "Trend Output Style"
  555.  
  556. trendBgLongMain = input.color(color.new(color.lime, 90), "Background Long Main",group = g16, inline="g16i1")
  557. trendBgLongAux = input.color(color.new(#aaaa66, 90), "Auxiliary",group = g16, inline="g16i1")
  558. trendBgShortMain = input.color(color.new(color.red, 90), "Bacground Short Main",group = g16, inline="g16i2")
  559. trendBgShortAux = input.color(color.new(color.orange, 90), "Auxiliary",group = g16, inline="g16i2")
  560. trendEdgeLongMain = input.color(color.new(color.lime, 80), "Chart Edge Long Main",group = g16, inline="g16i3")
  561. trendEdgeLongAux = input.color(color.new(#aaaa66, 80), "Auxiliary",group = g16, inline="g16i3")
  562. trendEdgeShortMain = input.color(color.new(color.red, 80), "Chart Edge Short Main",group = g16, inline="g16i4")
  563. trendEdgeShortAux = input.color(color.new(color.orange, 80), "Auxiliary",group = g16, inline="g16i4")
  564.  
  565. g1 = "Strategy: Entry/Exit Conditions"
  566.  
  567. // Risk Management
  568. g2 = "Strategy: Risk Management"
  569. riskReward = input.float(3, "Risk : Reward  1 :", group = g2, inline = "RM1", minval = 0, step = 0.1,
  570. tooltip = "Previous high or low (long/short dependant) is used to determine TP level. "+
  571. "'Risk : Reward' ratio is then used to calculate SL based of previous high/low level.\n\n"+
  572. "In short, the higher the R:R ratio, the smaller the SL since TP target is fixed by previous high/low price data.")
  573. accountRiskPercent = input.float(1, "Risk per Trade %", group = g2, inline = "RM1", minval = 0, step = 0.1,
  574. tooltip = "Percentage of portfolio you lose if trade hits SL.\n\nYou then stand to gain\n Portfolio Risk % * Risk : Reward\nif trade hits TP.")
  575.  
  576. // Backtest & Date Range
  577. gsd = 'Strategy: Backtest Settings'
  578. doStrategyBackTest = input.bool(false, "Strategy Backtest", group = gsd, inline = "DR3")
  579. limitDateStrategyTest = input.bool(true, "Limit by date", group = gsd, inline = "DR3")
  580. flipper = input.bool(false, "Trade in Reverse", group = gsd, inline = "DR3")
  581. noEntryCandleStopLoss = input.bool(false, "No Entry Candle stops", group = gsd,
  582. tooltip = "Wether to set stops only after the entry candle. When backtesting, gloss on the entry candle can be ignored due to broker emulator, meaning, "+
  583. "it won't trigger on test while it would trigger on live trade. Using this setting, this behavior can be prevented, to achieve more accurate backtest results.")
  584. startYear = input.int (2020, "Start Date  ", group = gsd, inline = 'DR1', minval = 1900, maxval = 2100)
  585. startMonth = input.int (1, "", group = gsd, inline = 'DR1',
  586. options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  587. startDate = input.int (1, "", group = gsd, inline = 'DR1',
  588. options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
  589. endYear = input.int (2100, "End Date   ", group = gsd, inline = 'DR2', minval = 1900, maxval = 2100)
  590. endMonth = input.int (1, "", group = gsd, inline = 'DR2',
  591. options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  592. endDate = input.int (1, "", group = gsd, inline = 'DR2',
  593. options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
  594. inDateRange = not limitDateStrategyTest
  595. or (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)
  596. and time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
  597. inStrategyRange = doStrategyBackTest and inDateRange
  598.  
  599. // Drawing Settings
  600. showTpSlBoxes = input.bool(true, "Show TP / SL Boxes", group = "Strategy: Drawings", inline = "D1",
  601. tooltip = "Show or hide TP and SL position boxes.\n\nNote: TradingView limits the maximum number of boxes that can be displayed to 500 "+
  602. "so they may not appear for all price data under test.")
  603. showLabels = input.bool(false, "Show Trade Exit Labels", group = "Strategy: Drawings", inline = "D2",
  604. tooltip = "Useful labels to identify Profit/Loss and cumulative portfolio capital after each trade closes.\n\n"+
  605. "Also note that TradingView limits the max number of 'boxes' that can be displayed on a chart (max 500). "+
  606. "This means when you lookbackSideways far enough on the chart you will not see the TP/SL boxes. However you can check this option to identify where trades exited.")
  607.  
  608. // --------------------------------------------------------------
  609. // 01.03 SETTINGS | DERIVED VALEUS AND ARRAYS
  610. // --------------------------------------------------------------
  611.  
  612. useHtfSettingsForCtf = htfSettingsEnabled and timeframe.in_seconds() >= timeframe.in_seconds(htfThreshold)
  613. useHtfSettingsForHtf = htfSettingsEnabled and timeframe.in_seconds(chosenHtf) >= timeframe.in_seconds(htfThreshold)
  614.  
  615. pivPLeft = useHtfSettingsForCtf and htfSettingsg3 ? htfpivPLeft : ctfpivPLeft
  616. pivPLeftM1 = math.max(0, pivPLeft - 1)
  617. pivPer = useHtfSettingsForCtf and htfSettingsg3 ? htfpivPer : ctfpivPer
  618. pivPerP1 = pivPer + 1
  619. checkAdjacentPivotThreshold = useHtfSettingsForCtf and htfSettingsg3 ? htfcheckAdjacentPivotThreshold : ctfcheckAdjacentPivotThreshold
  620.  
  621. pivPLeftHtf = useHtfSettingsForHtf and htfSettingsg3 ? htfpivPLeft : ctfpivPLeft
  622. pivPerHtf = useHtfSettingsForHtf and htfSettingsg3 ? htfpivPer : ctfpivPer
  623. checkAdjacentPivotThresholdHtf = useHtfSettingsForHtf and htfSettingsg3 ? htfcheckAdjacentPivotThreshold : ctfcheckAdjacentPivotThreshold
  624.  
  625. ctfSidewaysMultiplier = 1
  626. var htfSidewaysMultiplier = timeframe.in_seconds(chosenHtf) / timeframe.in_seconds()
  627. ctfOffset = pivPer
  628. var htfOffset = int(htfSidewaysMultiplier * (0.5 + pivPerHtf))
  629.  
  630. // TF Independent Settings
  631. // ---- Debug Settings
  632. // 00 = maxInitialCandlesToDetermineTrend
  633. // 01 = DBG_DisableTrimEnabled
  634. // 02 = DBG_ShowPriceActionRecognitionLines
  635. // 03 = DBG_ShowPriceActionRecognitionBoxes
  636. // 04 = DBG_ShowPriceActionRecognitionBounds
  637. // 05 = DBG_ShowPriceActionRecognitionSingleDirection
  638. // 06 = DBG_VerboseBosCheckDoOutput
  639. // 07 = DBG_VerboseBosCheckOnlySuccess
  640. // 08 = DBG_VerboseBosCheckDetailed
  641. // 09 = DBG_VerboseBosCheckCompact
  642. var tfIndependentSettings = array.new<float>(09 + 1)
  643.  
  644. array.set(tfIndependentSettings, 00, maxInitialCandlesToDetermineTrend)
  645. array.set(tfIndependentSettings, 01, DBG_DisableTrimEnabled ? 1 : 0)
  646. array.set(tfIndependentSettings, 02, DBG_ShowPriceActionRecognitionLines ? 1 : 0)
  647. array.set(tfIndependentSettings, 03, DBG_ShowPriceActionRecognitionBoxes ? 1 : 0)
  648. array.set(tfIndependentSettings, 04, DBG_ShowPriceActionRecognitionBounds ? 1 : 0)
  649. array.set(tfIndependentSettings, 05, DBG_ShowPriceActionRecognitionSingleDirection ? 1 : 0)
  650. array.set(tfIndependentSettings, 06, DBG_DebugEnabled and DBG_VerboseBosCheckDoOutput ? 1 : 0)
  651. array.set(tfIndependentSettings, 07, DBG_VerboseBosCheckOnlySuccess ? 1 : 0)
  652. array.set(tfIndependentSettings, 08, DBG_VerboseBosCheckDetailed ? 1 : 0)
  653. array.set(tfIndependentSettings, 09, DBG_VerboseBosCheckCompact ? 1 : 0)
  654.  
  655. // TF Sensitive settings
  656. // ---- BOS & HL Break Settings
  657. // 30 = HLBreakThreshold
  658. // 31 = HLBreakATREnabled
  659. // 32 = HLBreakATRLength
  660. // 33 = HLBreakATRThreshold
  661. // 00 = newBosConfirmConditionLevel
  662. // 01 = newBosThreshold
  663. // ---- BOS Break Conditions
  664. // 02 = bosBreakCandleThresholdOr
  665. // 03 = usebosBreakCandleThresholdOr
  666. // 04 = bosBreakCandleThresholdAnd
  667. // 05 = usebosBreakCandleThresholdAnd
  668. // ---- BOS Change Settings
  669. // 06 = bosConsiderNextEnabled
  670. // 07 = bosConsiderNextDistanceThreshold
  671. // 08 = bosConsiderNextConsumptionEnabled
  672. // 09 = bosConsiderNextConsumptionThreshold
  673. // 10 = bosConsiderNextRecursive
  674. // 11 = bosIgnoreManipulationsThreshold
  675. // 12 = bosIgnoreManipulationsAmount
  676. // 13 = bosSameCandleLegConsumptionDelay
  677. // 34 = bosSidewaysCandleInsideThreshold
  678. // 35 = bosSidewaysCandleBodyThreshold
  679. // ---- BOS Change Conditions OR
  680. // 14 = bosFiboCorrectionWickRelativeThresholdOr
  681. // 15 = usebosFiboCorrectionWickRelativeThresholdOr
  682. // 16 = bosFiboCorrectionCloseRelativeThresholdOr
  683. // 17 = usebosFiboCorrectionCloseRelativeThresholdOr
  684. // 18 = bosFiboReverseRelativeThresholdOr
  685. // 19 = usebosFiboReverseRelativeThresholdOr
  686. // 20 = bosCandleLengthThresholdOr
  687. // 21 = usebosCandleLengthThresholdOr
  688. // 36 = bosAtrThresholdOr
  689. // 37 = usebosAtrThresholdOr
  690. // ---- BOS Change Conditions AND
  691. // 22 = bosFiboCorrectionWickRelativeThresholdAnd
  692. // 23 = usebosFiboCorrectionWickRelativeThresholdAnd
  693. // 24 = bosFiboCorrectionCloseRelativeThresholdAnd
  694. // 25 = usebosFiboCorrectionCloseRelativeThresholdAnd
  695. // 26 = bosFiboReverseRelativeThresholdAnd
  696. // 27 = usebosFiboReverseRelativeThresholdAnd
  697. // 28 = bosCandleLengthThresholdAnd
  698. // 29 = usebosCandleLengthThresholdAnd
  699. // 38 = bosAtrThresholdAnd
  700. // 39 = usebosAtrThresholdAnd
  701. // ---- Miscellaneous
  702. // 50 = ctfOffset / htfOffset
  703. // 51 = ctfSidewaysMultiplier / htfSidewaysMultiplier
  704. // ---- DEBUG
  705. // 60 = DBG_VerboseBosCheckOutputCtf / DBG_VerboseBosCheckOutputHtf
  706. // 61 = DBG_ShowPriceActionRecognitionCtf / DBG_ShowPriceActionRecognitionHtf
  707. var ctfSensitiveSettings = array.new<float>(61 + 1)
  708. var htfSensitiveSettings = array.new<float>(61 + 1)
  709.  
  710. // CTF SETTINGS
  711. // Π‘TF ---- BOS & HL Break Settings
  712. array.set(ctfSensitiveSettings, 30, useHtfSettingsForCtf and htfSettingsg9 ? htfHLBreakThreshold : ctfHLBreakThreshold)
  713. array.set(ctfSensitiveSettings, 31, (useHtfSettingsForCtf and htfSettingsg9 ? htfHLBreakATREnabled : ctfHLBreakATREnabled) ? 1 : 0)
  714. array.set(ctfSensitiveSettings, 32, useHtfSettingsForCtf and htfSettingsg9 ? htfHLBreakATRLength : ctfHLBreakATRLength)
  715. array.set(ctfSensitiveSettings, 33, useHtfSettingsForCtf and htfSettingsg9 ? htfHLBreakATRThreshold : ctfHLBreakATRThreshold)
  716. array.set(ctfSensitiveSettings, 00, useHtfSettingsForCtf and htfSettingsg9 ? htfnewBosConfirmConditionLevel : ctfnewBosConfirmConditionLevel)
  717. array.set(ctfSensitiveSettings, 01, useHtfSettingsForCtf and htfSettingsg9 ? htfnewBosThreshold : ctfnewBosThreshold)
  718. // CTF ---- BOS Break Conditions
  719. array.set(ctfSensitiveSettings, 02, useHtfSettingsForCtf and htfSettingsg7 ? htfbosBreakCandleThresholdOr : ctfbosBreakCandleThresholdOr)
  720. array.set(ctfSensitiveSettings, 03, (useHtfSettingsForCtf and htfSettingsg7 ? htfusebosBreakCandleThresholdOr : ctfusebosBreakCandleThresholdOr) ? 1 : 0)
  721. array.set(ctfSensitiveSettings, 04, useHtfSettingsForCtf and htfSettingsg8 ? htfbosBreakCandleThresholdAnd : ctfbosBreakCandleThresholdAnd)
  722. array.set(ctfSensitiveSettings, 05, (useHtfSettingsForCtf and htfSettingsg8 ? htfusebosBreakCandleThresholdAnd : ctfusebosBreakCandleThresholdAnd) ? 1 : 0)
  723. // CTF ---- BOS Change Settings
  724. array.set(ctfSensitiveSettings, 06, (useHtfSettingsForCtf and htfSettingsg10 ? htfbosConsiderNextEnabled : ctfbosConsiderNextEnabled) ? 1 : 0)
  725. array.set(ctfSensitiveSettings, 07, useHtfSettingsForCtf and htfSettingsg10 ? htfbosConsiderNextDistanceThreshold : ctfbosConsiderNextDistanceThreshold)
  726. array.set(ctfSensitiveSettings, 08, (useHtfSettingsForCtf and htfSettingsg10 ? htfbosConsiderNextConsumptionEnabled : ctfbosConsiderNextConsumptionEnabled) ? 1 : 0)
  727. array.set(ctfSensitiveSettings, 09, useHtfSettingsForCtf and htfSettingsg10 ? htfbosConsiderNextConsumptionThreshold : ctfbosConsiderNextConsumptionThreshold)
  728. array.set(ctfSensitiveSettings, 10, (useHtfSettingsForCtf and htfSettingsg10 ? htfbosConsiderNextRecursive : ctfbosConsiderNextRecursive) ? 1 : 0)
  729. array.set(ctfSensitiveSettings, 11, useHtfSettingsForCtf and htfSettingsg10 ? htfbosIgnoreManipulationsThreshold : ctfbosIgnoreManipulationsThreshold)
  730. array.set(ctfSensitiveSettings, 12, useHtfSettingsForCtf and htfSettingsg10 ? htfbosIgnoreManipulationsAmount : ctfbosIgnoreManipulationsAmount)
  731. array.set(ctfSensitiveSettings, 13, useHtfSettingsForCtf and htfSettingsg10 ? htfbosSameCandleLegConsumptionDelay : ctfbosSameCandleLegConsumptionDelay)
  732. array.set(ctfSensitiveSettings, 34, useHtfSettingsForCtf and htfSettingsg10 ? htfbosSidewaysCandleInsideThreshold : ctfbosSidewaysCandleInsideThreshold)
  733. array.set(ctfSensitiveSettings, 35, useHtfSettingsForCtf and htfSettingsg10 ? htfbosSidewaysCandleBodyThreshold : ctfbosSidewaysCandleBodyThreshold)
  734. // CTF ---- BOS Change Conditions OR
  735. array.set(ctfSensitiveSettings, 14, useHtfSettingsForCtf and htfSettingsg5 ? htfbosFiboCorrectionWickRelativeThresholdOr : ctfbosFiboCorrectionWickRelativeThresholdOr)
  736. array.set(ctfSensitiveSettings, 15, (useHtfSettingsForCtf and htfSettingsg5 ? htfusebosFiboCorrectionWickRelativeThresholdOr : ctfusebosFiboCorrectionWickRelativeThresholdOr) ? 1 : 0)
  737. array.set(ctfSensitiveSettings, 16, useHtfSettingsForCtf and htfSettingsg5 ? htfbosFiboCorrectionCloseRelativeThresholdOr : ctfbosFiboCorrectionCloseRelativeThresholdOr)
  738. array.set(ctfSensitiveSettings, 17, (useHtfSettingsForCtf and htfSettingsg5 ? htfusebosFiboCorrectionCloseRelativeThresholdOr : ctfusebosFiboCorrectionCloseRelativeThresholdOr) ? 1 : 0)
  739. array.set(ctfSensitiveSettings, 18, useHtfSettingsForCtf and htfSettingsg5 ? htfbosFiboReverseRelativeThresholdOr : ctfbosFiboReverseRelativeThresholdOr)
  740. array.set(ctfSensitiveSettings, 19, (useHtfSettingsForCtf and htfSettingsg5 ? htfusebosFiboReverseRelativeThresholdOr : ctfusebosFiboReverseRelativeThresholdOr) ? 1 : 0)
  741. array.set(ctfSensitiveSettings, 20, useHtfSettingsForCtf and htfSettingsg5 ? htfbosCandleLengthThresholdOr : ctfbosCandleLengthThresholdOr)
  742. array.set(ctfSensitiveSettings, 21, (useHtfSettingsForCtf and htfSettingsg5 ? htfusebosCandleLengthThresholdOr : ctfusebosCandleLengthThresholdOr) ? 1 : 0)
  743. array.set(ctfSensitiveSettings, 36, useHtfSettingsForCtf and htfSettingsg5 ? htfbosAtrThresholdOr : ctfbosAtrThresholdOr)
  744. array.set(ctfSensitiveSettings, 37, (useHtfSettingsForCtf and htfSettingsg5 ? htfusebosAtrThresholdOr : ctfusebosAtrThresholdOr) ? 1 : 0)
  745. // CTF ---- BOS Change Conditions AND
  746. array.set(ctfSensitiveSettings, 22, useHtfSettingsForCtf and htfSettingsg6 ? htfbosFiboCorrectionWickRelativeThresholdAnd : ctfbosFiboCorrectionWickRelativeThresholdAnd)
  747. array.set(ctfSensitiveSettings, 23, (useHtfSettingsForCtf and htfSettingsg6 ? htfusebosFiboCorrectionWickRelativeThresholdAnd : ctfusebosFiboCorrectionWickRelativeThresholdAnd) ? 1 : 0)
  748. array.set(ctfSensitiveSettings, 24, useHtfSettingsForCtf and htfSettingsg6 ? htfbosFiboCorrectionCloseRelativeThresholdAnd : ctfbosFiboCorrectionCloseRelativeThresholdAnd)
  749. array.set(ctfSensitiveSettings, 25, (useHtfSettingsForCtf and htfSettingsg6 ? htfusebosFiboCorrectionCloseRelativeThresholdAnd : ctfusebosFiboCorrectionCloseRelativeThresholdAnd) ? 1 : 0)
  750. array.set(ctfSensitiveSettings, 26, useHtfSettingsForCtf and htfSettingsg6 ? htfbosFiboReverseRelativeThresholdAnd : ctfbosFiboReverseRelativeThresholdAnd)
  751. array.set(ctfSensitiveSettings, 27, (useHtfSettingsForCtf and htfSettingsg6 ? htfusebosFiboReverseRelativeThresholdAnd : ctfusebosFiboReverseRelativeThresholdAnd) ? 1 : 0)
  752. array.set(ctfSensitiveSettings, 28, useHtfSettingsForCtf and htfSettingsg6 ? htfbosCandleLengthThresholdAnd : ctfbosCandleLengthThresholdAnd)
  753. array.set(ctfSensitiveSettings, 29, (useHtfSettingsForCtf and htfSettingsg6 ? htfusebosCandleLengthThresholdAnd : ctfusebosCandleLengthThresholdAnd) ? 1 : 0)
  754. array.set(ctfSensitiveSettings, 38, useHtfSettingsForCtf and htfSettingsg5 ? htfbosAtrThresholdAnd : ctfbosAtrThresholdAnd)
  755. array.set(ctfSensitiveSettings, 39, (useHtfSettingsForCtf and htfSettingsg5 ? htfusebosAtrThresholdAnd : ctfusebosAtrThresholdAnd) ? 1 : 0)
  756. // CTF ---- Miscellaneous
  757. array.set(ctfSensitiveSettings, 50, ctfOffset)
  758. array.set(ctfSensitiveSettings, 51, ctfSidewaysMultiplier)
  759. // CTF ---- DEBUG
  760. array.set(ctfSensitiveSettings, 60, (DBG_VerboseBosCheckOutputCtf) ? 1 : 0)
  761. array.set(ctfSensitiveSettings, 61, (DBG_DebugEnabled and DBG_ShowPriceActionRecognitionEnabled and DBG_ShowPriceActionRecognitionCtf) ? 1 : 0)
  762.  
  763. // HTF SETTINGS
  764. // HTF ---- BOS & HL Break Settings
  765. array.set(htfSensitiveSettings, 30, useHtfSettingsForHtf and htfSettingsg9 ? htfHLBreakThreshold : ctfHLBreakThreshold)
  766. array.set(htfSensitiveSettings, 31, (useHtfSettingsForHtf and htfSettingsg9 ? htfHLBreakATREnabled : ctfHLBreakATREnabled) ? 1 : 0)
  767. array.set(htfSensitiveSettings, 32, useHtfSettingsForHtf and htfSettingsg9 ? htfHLBreakATRLength : ctfHLBreakATRLength)
  768. array.set(htfSensitiveSettings, 33, useHtfSettingsForHtf and htfSettingsg9 ? htfHLBreakATRThreshold : ctfHLBreakATRThreshold)
  769. array.set(htfSensitiveSettings, 00, useHtfSettingsForHtf and htfSettingsg9 ? htfnewBosConfirmConditionLevel : ctfnewBosConfirmConditionLevel)
  770. array.set(htfSensitiveSettings, 01, useHtfSettingsForHtf and htfSettingsg9 ? htfnewBosThreshold : ctfnewBosThreshold)
  771. // HTF ---- BOS Break Conditions
  772. array.set(htfSensitiveSettings, 02, useHtfSettingsForHtf and htfSettingsg7 ? htfbosBreakCandleThresholdOr : ctfbosBreakCandleThresholdOr)
  773. array.set(htfSensitiveSettings, 03, (useHtfSettingsForHtf and htfSettingsg7 ? htfusebosBreakCandleThresholdOr : ctfusebosBreakCandleThresholdOr) ? 1 : 0)
  774. array.set(htfSensitiveSettings, 04, useHtfSettingsForHtf and htfSettingsg8 ? htfbosBreakCandleThresholdAnd : ctfbosBreakCandleThresholdAnd)
  775. array.set(htfSensitiveSettings, 05, (useHtfSettingsForHtf and htfSettingsg8 ? htfusebosBreakCandleThresholdAnd : ctfusebosBreakCandleThresholdAnd) ? 1 : 0)
  776. // HTF ---- BOS Change Settings
  777. array.set(htfSensitiveSettings, 06, (useHtfSettingsForHtf and htfSettingsg10 ? htfbosConsiderNextEnabled : ctfbosConsiderNextEnabled) ? 1 : 0)
  778. array.set(htfSensitiveSettings, 07, useHtfSettingsForHtf and htfSettingsg10 ? htfbosConsiderNextDistanceThreshold : ctfbosConsiderNextDistanceThreshold)
  779. array.set(htfSensitiveSettings, 08, (useHtfSettingsForHtf and htfSettingsg10 ? htfbosConsiderNextConsumptionEnabled : ctfbosConsiderNextConsumptionEnabled) ? 1 : 0)
  780. array.set(htfSensitiveSettings, 09, useHtfSettingsForHtf and htfSettingsg10 ? htfbosConsiderNextConsumptionThreshold : ctfbosConsiderNextConsumptionThreshold)
  781. array.set(htfSensitiveSettings, 10, (useHtfSettingsForHtf and htfSettingsg10 ? htfbosConsiderNextRecursive : ctfbosConsiderNextRecursive) ? 1 : 0)
  782. array.set(htfSensitiveSettings, 11, useHtfSettingsForHtf and htfSettingsg10 ? htfbosIgnoreManipulationsThreshold : ctfbosIgnoreManipulationsThreshold)
  783. array.set(htfSensitiveSettings, 12, useHtfSettingsForHtf and htfSettingsg10 ? htfbosIgnoreManipulationsAmount : ctfbosIgnoreManipulationsAmount)
  784. array.set(htfSensitiveSettings, 13, useHtfSettingsForHtf and htfSettingsg10 ? htfbosSameCandleLegConsumptionDelay : ctfbosSameCandleLegConsumptionDelay)
  785. array.set(htfSensitiveSettings, 34, useHtfSettingsForHtf and htfSettingsg10 ? htfbosSidewaysCandleInsideThreshold : ctfbosSidewaysCandleInsideThreshold)
  786. array.set(htfSensitiveSettings, 35, useHtfSettingsForHtf and htfSettingsg10 ? htfbosSidewaysCandleBodyThreshold : ctfbosSidewaysCandleBodyThreshold)
  787. // HTF ---- BOS Change Conditions OR
  788. array.set(htfSensitiveSettings, 14, useHtfSettingsForHtf and htfSettingsg5 ? htfbosFiboCorrectionWickRelativeThresholdOr : ctfbosFiboCorrectionWickRelativeThresholdOr)
  789. array.set(htfSensitiveSettings, 15, (useHtfSettingsForHtf and htfSettingsg5 ? htfusebosFiboCorrectionWickRelativeThresholdOr : ctfusebosFiboCorrectionWickRelativeThresholdOr) ? 1 : 0)
  790. array.set(htfSensitiveSettings, 16, useHtfSettingsForHtf and htfSettingsg5 ? htfbosFiboCorrectionCloseRelativeThresholdOr : ctfbosFiboCorrectionCloseRelativeThresholdOr)
  791. array.set(htfSensitiveSettings, 17, (useHtfSettingsForHtf and htfSettingsg5 ? htfusebosFiboCorrectionCloseRelativeThresholdOr : ctfusebosFiboCorrectionCloseRelativeThresholdOr) ? 1 : 0)
  792. array.set(htfSensitiveSettings, 18, useHtfSettingsForHtf and htfSettingsg5 ? htfbosFiboReverseRelativeThresholdOr : ctfbosFiboReverseRelativeThresholdOr)
  793. array.set(htfSensitiveSettings, 19, (useHtfSettingsForHtf and htfSettingsg5 ? htfusebosFiboReverseRelativeThresholdOr : ctfusebosFiboReverseRelativeThresholdOr) ? 1 : 0)
  794. array.set(htfSensitiveSettings, 20, useHtfSettingsForHtf and htfSettingsg5 ? htfbosCandleLengthThresholdOr : ctfbosCandleLengthThresholdOr)
  795. array.set(htfSensitiveSettings, 21, (useHtfSettingsForHtf and htfSettingsg5 ? htfusebosCandleLengthThresholdOr : ctfusebosCandleLengthThresholdOr) ? 1 : 0)
  796. // HTF ---- BOS Change Conditions AND
  797. array.set(htfSensitiveSettings, 22, useHtfSettingsForHtf and htfSettingsg6 ? htfbosFiboCorrectionWickRelativeThresholdAnd : ctfbosFiboCorrectionWickRelativeThresholdAnd)
  798. array.set(htfSensitiveSettings, 23, (useHtfSettingsForHtf and htfSettingsg6 ? htfusebosFiboCorrectionWickRelativeThresholdAnd : ctfusebosFiboCorrectionWickRelativeThresholdAnd) ? 1 : 0)
  799. array.set(htfSensitiveSettings, 24, useHtfSettingsForHtf and htfSettingsg6 ? htfbosFiboCorrectionCloseRelativeThresholdAnd : ctfbosFiboCorrectionCloseRelativeThresholdAnd)
  800. array.set(htfSensitiveSettings, 25, (useHtfSettingsForHtf and htfSettingsg6 ? htfusebosFiboCorrectionCloseRelativeThresholdAnd : ctfusebosFiboCorrectionCloseRelativeThresholdAnd) ? 1 : 0)
  801. array.set(htfSensitiveSettings, 26, useHtfSettingsForHtf and htfSettingsg6 ? htfbosFiboReverseRelativeThresholdAnd : ctfbosFiboReverseRelativeThresholdAnd)
  802. array.set(htfSensitiveSettings, 27, (useHtfSettingsForHtf and htfSettingsg6 ? htfusebosFiboReverseRelativeThresholdAnd : ctfusebosFiboReverseRelativeThresholdAnd) ? 1 : 0)
  803. array.set(htfSensitiveSettings, 28, useHtfSettingsForHtf and htfSettingsg6 ? htfbosCandleLengthThresholdAnd : ctfbosCandleLengthThresholdAnd)
  804. array.set(htfSensitiveSettings, 29, (useHtfSettingsForHtf and htfSettingsg6 ? htfusebosCandleLengthThresholdAnd : ctfusebosCandleLengthThresholdAnd) ? 1 : 0)
  805. // HTF ---- Miscellaneous
  806. array.set(htfSensitiveSettings, 50, htfOffset)
  807. array.set(htfSensitiveSettings, 51, htfSidewaysMultiplier)
  808. // HTF ---- DEBUG
  809. array.set(htfSensitiveSettings, 60, (DBG_VerboseBosCheckOutputHtf) ? 1 : 0)
  810. array.set(htfSensitiveSettings, 61, (DBG_DebugEnabled and DBG_ShowPriceActionRecognitionEnabled and DBG_ShowPriceActionRecognitionHtf) ? 1 : 0)
  811.  
  812. // --------------------------------------------------------------
  813. // 02 PRICE DATA ACQUISITION
  814. // --------------------------------------------------------------
  815.  
  816. // --------------------------------------------------------------
  817. // 02.01 PRICE DATA ACQUISITION | CTF
  818. // --------------------------------------------------------------
  819.  
  820. // CTF Arrays with price data
  821. var carHigh = array.new<float>(1,na)
  822. var carLow = array.new<float>(1,na)
  823. var carOpen = array.new<float>(1,na)
  824. var carClose = array.new<float>(1,na)
  825. var carMaxOC = array.new<float>(1,na)
  826. var carMinOC = array.new<float>(1,na)
  827. // Redundant for CTF but need it for algo being TF agnostic
  828. var cBarIndex = array.new<int>(0,0)
  829. // Pivot point value, if present at this bar (offset by pivPer)
  830. var carPivotH = array.new<float>(1,na)
  831. var carPivotL = array.new<float>(1,na)
  832.  
  833. // CTF Values
  834. cHigh = high[pivPer]
  835. cLow = low[pivPer]
  836. cOpen = open[pivPer]
  837. cClose = close[pivPer]
  838. cMaxOC = math.max(cOpen, cClose)
  839. cMinOC = math.min(cOpen, cClose)
  840.  
  841. var float cPivotH = na
  842. var float cPivotL = na
  843.  
  844. // Get pivot points, Not as simple as it may seem!
  845. cPivotH := ta.pivothigh(pivPLeft, pivPer)
  846. cPivotL := ta.pivotlow(pivPLeft, pivPer)
  847. //Also look for adjacent spots since sometimes pivot point might be a bit off, but still valid
  848. adjPivotH = ta.pivothigh(pivPLeftM1, pivPerP1)
  849. adjPivotL = ta.pivotlow(pivPLeftM1, pivPerP1)
  850. if pivPLeft > 1 and checkAdjacentPivotThreshold > 0
  851. threshold = (cOpen + cHigh + cLow + cClose) / 4 * checkAdjacentPivotThreshold / 100
  852. // Conditions for adjacent pivot described above in the input section
  853. if not cPivotH[1] and not cPivotH and adjPivotH
  854. and array.get(carMaxOC, 0) - cMaxOC < threshold
  855. and adjPivotH - cHigh < threshold
  856. cPivotH := adjPivotH
  857. // Doctoring the data to take correct price value into account if this becomes BOS
  858. cHigh := cPivotH
  859. if not cPivotL[1] and not cPivotL and adjPivotL
  860. and cMinOC - array.get(carMinOC, 0) < threshold
  861. and cLow - adjPivotL < threshold
  862. cPivotL := adjPivotL
  863. cLow := cPivotL
  864.  
  865. // Populate arrays with data
  866. array.unshift(carHigh, cHigh)
  867. array.unshift(carLow, cLow)
  868. array.unshift(carOpen, cOpen)
  869. array.unshift(carClose, cClose)
  870. array.unshift(carMaxOC, cMaxOC)
  871. array.unshift(carMinOC, cMinOC)
  872. array.push(cBarIndex, bar_index)
  873. array.unshift(carPivotH, cPivotH)
  874. array.unshift(carPivotL, cPivotL)
  875.  
  876. // --------------------------------------------------------------
  877. // 02.02 PRICE DATA ACQUISITION | HTF
  878. // --------------------------------------------------------------
  879.  
  880. // HTF Arrays with price data
  881. var harHigh = array.new<float>(1,na)
  882. var harLow = array.new<float>(1,na)
  883. var harOpen = array.new<float>(1,na)
  884. var harClose = array.new<float>(1,na)
  885. var harMaxOC = array.new<float>(1,na)
  886. var harMinOC = array.new<float>(1,na)
  887. // Converts HTF bars into CTF bars
  888. var harBarIndex = array.new<int>(0,0)
  889. // HTF Pivot data
  890. var harPivotH = array.new<float>(1,na)
  891. var harPivotL = array.new<float>(1,na)
  892.  
  893. // ta.pivot fails on htf data when there's uncommon situation
  894. // like russian stocks hiatus, 24 Feb 2022 CTF 4H HTF 1D
  895. // so we have to make our own
  896. tavapivothigh(left, right, aHigh, hHigh, fHigh, offset = false) =>
  897. lfrom = offset ? 1 : 0
  898. lto = left == 0 ? na : left - 1
  899. rfrom = 0
  900. rto = right == 0 ? na : right - 1
  901. if array.size(aHigh) < left or offset and left <= 1
  902. na
  903. else
  904. pivot = offset ? array.get(aHigh, 0) : hHigh
  905. if offset and right > 0
  906. if hHigh >= pivot
  907. pivot := na
  908. lto := na
  909. rto := na
  910. for i = lfrom to lto
  911. if array.get(aHigh, i) >= pivot
  912. pivot := na
  913. rto := na
  914. break
  915. for i = rfrom to rto
  916. if array.get(fHigh, i) >= pivot
  917. pivot := na
  918. break
  919. pivot
  920.  
  921. tavapivotlow(left, right, aLow, hLow, fLow, offset = false) =>
  922. lfrom = offset ? 1 : 0
  923. lto = left == 0 ? na : left - 1
  924. rfrom = 0
  925. rto = right == 0 ? na : right - 1
  926. if array.size(aLow) < left or offset and left <= 1
  927. na
  928. else
  929. pivot = offset ? array.get(aLow, 0) : hLow
  930. if offset and right > 0
  931. if hLow <= pivot
  932. pivot := na
  933. lto := na
  934. rto := na
  935. for i = lfrom to lto
  936. if array.get(aLow, i) <= pivot
  937. pivot := na
  938. rto := na
  939. break
  940. for i = rfrom to rto
  941. if array.get(fLow, i) <= pivot
  942. pivot := na
  943. break
  944. pivot
  945.  
  946. // Must put this in a function because it's not allowed to be called in an "if" conditional
  947. getHTFSecurity() =>
  948. request.security(syminfo.tickerid,
  949. // DUPLICATE CODE
  950. // BECAUSE OF PINESCRIPT SHENANIGANS
  951. not calculateHtfTrendEnabled ? "1M" : chooseHtfSkip ?
  952. (timeframe.in_seconds() <= timeframe.in_seconds("1") ? "5" :
  953. timeframe.in_seconds() <= timeframe.in_seconds("5") ? "30" :
  954. timeframe.in_seconds() <= timeframe.in_seconds("30") ? "240" :
  955. timeframe.in_seconds() <= timeframe.in_seconds("240") ? "1D" :
  956. timeframe.in_seconds() <= timeframe.in_seconds("1D") ? "1W" :
  957. timeframe.in_seconds() <= timeframe.in_seconds("1W") ? "1M" : "1M")
  958. :
  959. (timeframe.in_seconds() < timeframe.in_seconds("1") ? "1" :
  960. timeframe.in_seconds() < timeframe.in_seconds("5") ? "5" :
  961. timeframe.in_seconds() < timeframe.in_seconds("15") ? "15" :
  962. timeframe.in_seconds() < timeframe.in_seconds("30") ? "30" :
  963. timeframe.in_seconds() < timeframe.in_seconds("60") ? "60" :
  964. timeframe.in_seconds() < timeframe.in_seconds("240") ? "240" :
  965. timeframe.in_seconds() < timeframe.in_seconds("1D") ? "1D" :
  966. timeframe.in_seconds() < timeframe.in_seconds("1W") ? "1W" :
  967. timeframe.in_seconds() < timeframe.in_seconds("1M") ? "1M" : "1M")
  968. // DUPLICATE CODE END
  969. , [open, high, low, close], gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)
  970.  
  971. newHtfDataThisTick = false
  972. var htfBarIndex = -1
  973. if calculateHtfTrend
  974. [_o,_h,_l,_c] = getHTFSecurity()
  975. if _o
  976. newHtfDataThisTick := true
  977. htfBarIndex:= htfBarIndex + 1
  978. // Manually shift values back in time
  979. // Otherwise it fails in uncommon situations
  980. // like on russian stocks hiatus in 24 Feb 2022 CTF 4H HTF 1D
  981. // Because [] values on htf skips candles
  982. var fOpen = array.new<float>(1 + pivPerHtf, _o)
  983. var fHigh = array.new<float>(1 + pivPerHtf, _h)
  984. var fLow = array.new<float>(1 + pivPerHtf, _l)
  985. var fClose = array.new<float>(1 + pivPerHtf, _c)
  986. array.push(fOpen, _o)
  987. array.push(fHigh, _h)
  988. array.push(fLow, _l)
  989. array.push(fClose, _c)
  990. hOpen = array.shift(fOpen)
  991. hHigh = array.shift(fHigh)
  992. hLow = array.shift(fLow)
  993. hClose = array.shift(fClose)
  994. hMaxOC = math.max(hOpen, hClose)
  995. hMinOC = math.min(hOpen, hClose)
  996. // At this point we got an effect of value[1 + pivPerHtf] without data loss
  997. // ta.pivot also fails in such cases so we have to calculate those manually
  998. hPivotH = tavapivothigh(pivPLeftHtf, pivPerHtf, harHigh, hHigh, fHigh)
  999. hPivotL = tavapivotlow(pivPLeftHtf, pivPerHtf, harLow, hLow, fLow)
  1000. hAdjPivotH = tavapivothigh(pivPLeftHtf, pivPerHtf, harHigh, hHigh, fHigh, true)
  1001. hAdjPivotL = tavapivotlow(pivPLeftHtf, pivPerHtf, harLow, hLow, fLow, true)
  1002. if pivPLeftHtf > 1 and checkAdjacentPivotThresholdHtf > 0
  1003. threshold = (hOpen + hHigh + hLow + hClose) / 4 * checkAdjacentPivotThresholdHtf / 100
  1004. if not hPivotH and hAdjPivotH
  1005. and array.get(harMaxOC, 0) - hMaxOC < threshold
  1006. and hAdjPivotH - hHigh < threshold
  1007. hPivotH := hAdjPivotH
  1008. hHigh := hPivotH
  1009. if not hPivotL and hAdjPivotL
  1010. and hMinOC - array.get(harMinOC, 0) < threshold
  1011. and hLow - hAdjPivotL < threshold
  1012. hPivotL := hAdjPivotL
  1013. hLow := hPivotL
  1014. // Now we can populate arrays with data just like CTF
  1015. array.unshift(harOpen, hOpen)
  1016. array.unshift(harHigh, hHigh)
  1017. array.unshift(harLow, hLow)
  1018. array.unshift(harClose, hClose)
  1019. array.unshift(harMaxOC, hMaxOC)
  1020. array.unshift(harMinOC, hMinOC)
  1021. array.push(harBarIndex, bar_index)
  1022. array.unshift(harPivotH, hPivotH)
  1023. array.unshift(harPivotL, hPivotL)
  1024.  
  1025. // --------------------------------------------------------------
  1026. // 03 GLOBAL VARIABLES
  1027. // --------------------------------------------------------------
  1028.  
  1029. // Price Leg Arrays
  1030. // ---- Float
  1031. // 00 = LocalH
  1032. // 01 = LocalL
  1033. // 02 = Close
  1034. // 03 = Reverse
  1035. // ---- Int
  1036. // 00 = LocalHB
  1037. // 01 = LocalLB
  1038. // 02 = SidewaysL
  1039. // 03 = SidewaysR
  1040. // 04 = SidewaysF
  1041. plFlt = 4
  1042. plInt = 5
  1043.  
  1044. // CTF Arrays
  1045. var carLFlt = array.new<float>(0)
  1046. var carLInt = array.new<int>(0)
  1047. var carSFlt = array.new<float>(0)
  1048. var carSInt = array.new<int>(0)
  1049.  
  1050. // HTF Arrays
  1051. var harLFlt = array.new<float>(0)
  1052. var harLInt = array.new<int>(0)
  1053. var harSFlt = array.new<float>(0)
  1054. var harSInt = array.new<int>(0)
  1055.  
  1056. // Algorithm state
  1057. // Trend, BOS testing/breaching, Executes
  1058. // Algorithm Values / Bars
  1059. // Prior & Global High/Low | Short & Long BOS Levels | Next Close & Reverse Values
  1060.  
  1061. // CTF State
  1062. var carStateInt = array.new<int>(2, 0)
  1063. var carStateBool = array.new<bool>(2, false)
  1064. var carValuesFlt = array.new<float>(20, na)
  1065. var carValuesInt = array.new<int>(8, na)
  1066.  
  1067. // HTF State
  1068. var harStateInt = array.new<int>(2, 0)
  1069. var harStateBool = array.new<bool>(2, false)
  1070. var harValuesFlt = array.new<float>(20, na)
  1071. var harValuesInt = array.new<int>(8, na)
  1072.  
  1073. // --------------------------------------------------------------
  1074. // 04 FUNCTIONS
  1075. // --------------------------------------------------------------
  1076.  
  1077. // --------------------------------------------------------------
  1078. // 04.01 FUNCTIONS: LIBRARY
  1079. // --------------------------------------------------------------
  1080.  
  1081. debugTextX(x, title, bool below = false, color bgcolor = #00065586) =>
  1082. if SHOW_DEBUG_TEXT
  1083. label.new(x, 0, str.tostring(title), yloc = below ? yloc.belowbar : yloc.abovebar, color = bgcolor, textcolor = color.white)
  1084.  
  1085. debugText(title, bool below = false, color bgcolor = #00065586) =>
  1086. if SHOW_DEBUG_TEXT
  1087. label.new(bar_index, 0, str.tostring(title), yloc = below ? yloc.belowbar : yloc.abovebar, color = bgcolor, textcolor = color.white)
  1088.  
  1089. debugLine(x1, y1, x2, y2, color, width) =>
  1090. if SHOW_DEBUG_LINES
  1091. line.new(x1, y1, x2, y2, color = color, width = width)
  1092.  
  1093. debugBox(x1, y1, x2, y2, border_color, border_width, bg_color) =>
  1094. if SHOW_DEBUG_BOXES
  1095. box.new(x1, y1, x2, y2, border_color, border_width, bgcolor = bg_color)
  1096.  
  1097. // --------------------------------------------------------------
  1098. // 04.02 FUNCTIONS: VARIABLES
  1099. // --------------------------------------------------------------
  1100.  
  1101.  
  1102. // --------------------------------------------------------------
  1103. // 04.03 FUNCTIONS: OUTPUT
  1104. // --------------------------------------------------------------
  1105.  
  1106. printTpSlDrawing(left, right, entryPrice, slPrice, tpPrice, tpHit, slHit) =>
  1107. if showTpSlBoxes
  1108. colorProfitBright = color.new(color.green, 20)
  1109. colorProfitDim = color.new(color.green, 80)
  1110. colorNeutralBright = color.new(color.gray, 20)
  1111. colorNeutralDim = color.new(color.gray, 80)
  1112. colorLossBright = color.new(color.red, 20)
  1113. colorLossDim = color.new(color.red, 80)
  1114. offsetRight = right == left ? right + 1 : right
  1115. line.new(x1 = left, y1 = tpPrice, x2 = offsetRight, y2 = tpPrice, color = colorProfitBright, width = 2)
  1116. box.new(left, tpPrice, offsetRight, entryPrice, na, bgcolor = tpHit ? colorProfitDim : colorNeutralDim)
  1117. line.new(x1 = left, y1 = entryPrice, x2 = offsetRight, y2 = entryPrice, color = colorNeutralBright, width = 2)
  1118. box.new(left, entryPrice, offsetRight, slPrice, na, bgcolor = slHit ? colorLossDim : colorNeutralDim)
  1119. line.new(x1 = left, y1 = slPrice, x2 = offsetRight, y2 = slPrice, color = colorLossBright, width = 2)
  1120.  
  1121. printTpSlDrawingActive(left,right,entryPrice,slPrice,tpPrice) =>
  1122. printTpSlDrawing(left,right,entryPrice,slPrice,tpPrice,false,false)
  1123.  
  1124. printLabel(globalTrendDirection, bar, title, above, strong) =>
  1125. longColor = globalTrendDirection == 1 ? (strong?color.green:color.yellow) : (strong ? color.red : color.orange)
  1126. shortColor = globalTrendDirection == -1 ? (strong?color.red:color.orange) : (strong ? color.green : color.yellow)
  1127. label.new(bar, 0, title, yloc = above ? yloc.abovebar:yloc.belowbar, style = above ? label.style_label_down : label.style_label_up, color = above ? longColor : shortColor)
  1128.  
  1129. printPriceLeg(tfSensitiveSettings, tfIndependentSettings, aBarIndex, x1, y1, x2, y2, y3, y4, sl, sr, long, highlighted, faded = false) =>
  1130. offset = int(array.get(tfSensitiveSettings, 50))
  1131. swMult = int(array.get(tfSensitiveSettings, 51))
  1132. mainColor = color.new(long ? (faded ? color.green : color.lime) : (faded ? color.purple : color.fuchsia), highlighted ? 0 : 30)
  1133. boundColor = color.new(long ? (faded ? color.green : color.lime) : (faded ? color.purple : color.fuchsia), highlighted ? 50 : 70)
  1134. int newX1 = array.get(aBarIndex, x1) - offset
  1135. int newX2 = array.get(aBarIndex, x2) - offset
  1136. color bgColor = na
  1137. if array.get(tfIndependentSettings, 02)
  1138. debugLine(newX1, y1, newX2, y2, mainColor, highlighted ? 3 : faded ? 1 : 2)
  1139. if array.get(tfIndependentSettings, 03)
  1140. debugBox(math.max(newX1, newX2) - sl * swMult, y3, math.max(newX1,newX2) + sr * swMult - 1 , y4, mainColor, 1, bgColor)
  1141. if array.get(tfIndependentSettings, 04)
  1142. debugBox(math.max(newX1, newX2) - sl * swMult, y1, math.max(newX1,newX2) + sr * swMult - 1 , y2, boundColor, 1, bgColor)
  1143.  
  1144. // --------------------------------------------------------------
  1145. // 04.04 FUNCTIONS: ARRAYS
  1146. // --------------------------------------------------------------
  1147.  
  1148. // --------------------------------------------------------------
  1149. // 04.04.01 FUNCTIONS: ARRAYS | Price Leg Data
  1150. // --------------------------------------------------------------
  1151.  
  1152. arRemove(array<float> aFlt, array<int> aInt, int id) =>
  1153. array.remove(aFlt, id * plFlt)
  1154. array.remove(aInt, id * plInt)
  1155. array.remove(aFlt, id * plFlt)
  1156. array.remove(aInt, id * plInt)
  1157. array.remove(aFlt, id * plFlt)
  1158. array.remove(aFlt, id * plFlt)
  1159. array.remove(aInt, id * plInt)
  1160. array.remove(aInt, id * plInt)
  1161. array.remove(aInt, id * plInt)
  1162.  
  1163. arPop(array<float> aFlt, array<int> aInt) =>
  1164. // Popping in forward order because we pushed in reverse order
  1165. [array.pop(aFlt),
  1166. array.pop(aInt),
  1167. array.pop(aFlt),
  1168. array.pop(aInt),
  1169. array.pop(aFlt),
  1170. array.pop(aFlt),
  1171. array.pop(aInt),
  1172. array.pop(aInt),
  1173. array.pop(aInt)]
  1174.  
  1175. arPush(array<float> aFlt, array<int> aInt, float a_h = na, int a_hb = na, float a_l = na, int a_lb = na, float a_c = na, float a_r = na, int a_sl = na, int a_sr = na, int a_sf = na) =>
  1176. // Pushing in reverse order, so we could pop in normal order
  1177. array.push(aInt, a_sf)
  1178. array.push(aInt, a_sr)
  1179. array.push(aInt, a_sl)
  1180. array.push(aFlt, a_r)
  1181. array.push(aFlt, a_c)
  1182. array.push(aInt, a_lb)
  1183. array.push(aFlt, a_l)
  1184. array.push(aInt, a_hb)
  1185. array.push(aFlt, a_h)
  1186.  
  1187. arClear(array<float> aFlt, array<int> aInt, long, tfSensitiveSettings, tfIndependentSettings, aBarIndex, debugOutput) =>
  1188. if debugOutput
  1189. if array.get(tfSensitiveSettings, 61)
  1190. while array.size(aFlt) > 0
  1191. [curH, curHB, curL, curLB, curC, curR, curSL, curSR, curSF] = arPop(aFlt, aInt)
  1192. if (curH and curL)
  1193. printPriceLeg(tfSensitiveSettings, tfIndependentSettings, aBarIndex, curHB, curH, curLB, curL, curC, curR, curSL, curSR, long, false, true)
  1194. array.clear(aFlt)
  1195. array.clear(aInt)
  1196.  
  1197. arInit(array<float> aLFlt, array<int> aLInt, array<float> aSFlt, array<int> aSInt, array<float> tfSensitiveSettings = na, array<float> tfIndependentSettings = na, array<int> aBarIndex = na, debugOutput = false) =>
  1198. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, debugOutput)
  1199. arClear(aSFlt, aSInt, false, tfSensitiveSettings, tfIndependentSettings, aBarIndex, debugOutput)
  1200. arPush(aLFlt, aLInt)
  1201. arPush(aSFlt, aSInt)
  1202.  
  1203. arGetFlt(int parameter, array<float> aFlt, int id) => array.get(aFlt, id * plFlt + plFlt - parameter - 1)
  1204. arGetInt(int parameter, array<int> aInt, int id) => array.get(aInt, id * plInt + plInt - parameter - 1)
  1205. arSetFlt(int parameter, array<float> aFlt, int id, float value) => array.set(aFlt, id * plFlt + plFlt - parameter - 1, value)
  1206. arSetInt(int parameter, array<int> aInt, int id, int value) => array.set(aInt, id * plInt + plInt - parameter - 1, value)
  1207. arSizeFlt(array<float> aFlt) => array.size(aFlt) / plFlt
  1208. arSizeInt(array<int> aInt) => array.size(aInt) / plInt
  1209.  
  1210. // Split all entries later than splitBar into other array
  1211. // First entry with "na" in LocalH will alwaays get split
  1212. arSplit(array<float> aFlt, array<int> aInt, array<float> sFlt, array<int> sInt, bool long = false, int splitBar = na) =>
  1213. while array.size(aFlt) > 0
  1214. if not na(splitBar)
  1215. aSize = arSizeFlt(aFlt)
  1216. if arGetInt(long ? 01 : 00, aInt, aSize - 1) <= splitBar
  1217. and not na(arGetFlt(00, aFlt, aSize - 1))
  1218. and not na(arGetFlt(01, aFlt, aSize - 1))
  1219. break
  1220. array.push(sFlt, array.pop(aFlt))
  1221. array.push(sFlt, array.pop(aFlt))
  1222. array.push(sFlt, array.pop(aFlt))
  1223. array.push(sFlt, array.pop(aFlt))
  1224. array.push(sInt, array.pop(aInt))
  1225. array.push(sInt, array.pop(aInt))
  1226. array.push(sInt, array.pop(aInt))
  1227. array.push(sInt, array.pop(aInt))
  1228. array.push(sInt, array.pop(aInt))
  1229.  
  1230. // Get last price bar before the bar specified
  1231. arGetLastBar(array<int> aInt, int i) =>
  1232. if i - 1 < 0 or i > arSizeInt(aInt)
  1233. na
  1234. else
  1235. math.max(arGetInt( 00, aInt,i - 1), arGetInt(01, aInt, i - 1))
  1236.  
  1237. // --------------------------------------------------------------
  1238. // 04.04.02 FUNCTIONS: ARRAYS | Current Data
  1239. // --------------------------------------------------------------
  1240.  
  1241. arStateGet(aStateInt, aStateBool) =>
  1242. [array.get(aStateInt, 0),
  1243. array.get(aStateBool, 0),
  1244. array.get(aStateBool, 1),
  1245. array.get(aStateInt, 1)]
  1246.  
  1247.  
  1248. arStateSet(aStateInt, aStateBool, int gTD, bool bT, bool bB, int bBC) =>
  1249. array.set(aStateInt, 0, gTD)
  1250. array.set(aStateBool, 0, bT)
  1251. array.set(aStateBool, 1, bB)
  1252. array.set(aStateInt, 1, bBC)
  1253.  
  1254. arValuesGet(aValuesFlt, aValuesInt) =>
  1255. [array.get(aValuesFlt, 0),
  1256. array.get(aValuesInt, 0),
  1257. array.get(aValuesFlt, 1),
  1258. array.get(aValuesInt, 1),
  1259. array.get(aValuesFlt, 2),
  1260. array.get(aValuesInt, 2),
  1261. array.get(aValuesFlt, 3),
  1262. array.get(aValuesInt, 3),
  1263. array.get(aValuesFlt, 4),
  1264. array.get(aValuesFlt, 5),
  1265. array.get(aValuesFlt, 6),
  1266. array.get(aValuesFlt, 7),
  1267. array.get(aValuesFlt, 8),
  1268. array.get(aValuesFlt, 9),
  1269. array.get(aValuesFlt, 10),
  1270. array.get(aValuesFlt, 11),
  1271. array.get(aValuesFlt, 12),
  1272. array.get(aValuesFlt, 13),
  1273. array.get(aValuesInt, 4),
  1274. array.get(aValuesFlt, 14),
  1275. array.get(aValuesFlt, 15),
  1276. array.get(aValuesInt, 5),
  1277. array.get(aValuesFlt, 16),
  1278. array.get(aValuesFlt, 17),
  1279. array.get(aValuesInt, 6),
  1280. array.get(aValuesFlt, 18),
  1281. array.get(aValuesFlt, 19),
  1282. array.get(aValuesInt, 7)]
  1283.  
  1284. arValuesSet(aValuesFlt, aValuesInt, float pH, int pHB, float pL, int pLB, float gH, int gHB, float gL, int gLB,
  1285. float gHC, float gLC, float nHC, float nLC,
  1286. float varLClose, float varLReverse, float varSClose, float varSReverse,
  1287. float bosLH, float bosLL, int bosLB, float bosSH, float bosSL, int bosSB,
  1288. float cbosLH, float cbosLL, int cbosLB, float cbosSH, float cbosSL, int cbosSB) =>
  1289. array.set(aValuesFlt, 0, pH)
  1290. array.set(aValuesInt, 0, pHB)
  1291. array.set(aValuesFlt, 1, pL)
  1292. array.set(aValuesInt, 1, pLB)
  1293. array.set(aValuesFlt, 2, gH)
  1294. array.set(aValuesInt, 2, gHB)
  1295. array.set(aValuesFlt, 3, gL)
  1296. array.set(aValuesInt, 3, gLB)
  1297. array.set(aValuesFlt, 4, gHC)
  1298. array.set(aValuesFlt, 5, gLC)
  1299. array.set(aValuesFlt, 6, nHC)
  1300. array.set(aValuesFlt, 7, nLC)
  1301. array.set(aValuesFlt, 8, varLClose)
  1302. array.set(aValuesFlt, 9, varLReverse)
  1303. array.set(aValuesFlt, 10, varSClose)
  1304. array.set(aValuesFlt, 11, varSReverse)
  1305. array.set(aValuesFlt, 12, bosLH)
  1306. array.set(aValuesFlt, 13, bosLL)
  1307. array.set(aValuesInt, 4, bosLB)
  1308. array.set(aValuesFlt, 14, bosSH)
  1309. array.set(aValuesFlt, 15, bosSL)
  1310. array.set(aValuesInt, 5, bosSB)
  1311. array.set(aValuesFlt, 16, cbosLH)
  1312. array.set(aValuesFlt, 17, cbosLL)
  1313. array.set(aValuesInt, 6, cbosLB)
  1314. array.set(aValuesFlt, 18, cbosSH)
  1315. array.set(aValuesFlt, 19, cbosSL)
  1316. array.set(aValuesInt, 7, cbosSB)
  1317.  
  1318.  
  1319. // --------------------------------------------------------------
  1320. // 04.05 FUNCTIONS: INDICATOR LOGIC
  1321. // --------------------------------------------------------------
  1322.  
  1323. var failWR = array.new<float>(0)
  1324. var failCR = array.new<float>(0)
  1325. var failRR = array.new<float>(0)
  1326. var failSW = array.new<float>(0)
  1327. var failWA = array.new<float>(0)
  1328. var succWR = array.new<float>(0)
  1329. var succCR = array.new<float>(0)
  1330. var succRR = array.new<float>(0)
  1331. var succSW = array.new<float>(0)
  1332. var succWA = array.new<float>(0)
  1333.  
  1334. checkConditionGlobalChange(tfSensitiveSettings, tfIndependentSettings, array<int> aBarIndex, localB, globalH, globalL, localH, localL, localClose, localReverse, localSidewaysL, localSidewaysR, atr, long) =>
  1335. usebosFiboCorrectionWickRelativeThresholdOr = array.get(tfSensitiveSettings, 15)
  1336. usebosFiboCorrectionCloseRelativeThresholdOr = array.get(tfSensitiveSettings, 17)
  1337. usebosFiboReverseRelativeThresholdOr = array.get(tfSensitiveSettings, 19)
  1338. usebosCandleLengthThresholdOr = array.get(tfSensitiveSettings, 21)
  1339. usebosAtrThresholdOr = array.get(tfSensitiveSettings, 37)
  1340. // Condition 1/2: Relative Correction Threshold Reached
  1341. // Price corrected at least this much %, when compared to distance between global high and low, measured to wick or to body
  1342. // LONG: localClose is highest close, SHORT: localClose is lowest close
  1343. localH2 = long ? localClose : localH
  1344. localL2 = long ? localL : localClose
  1345. wickRelative = (localH - localL) / (globalH - globalL)
  1346. correctionWickRelativeThresholdReachedOr = wickRelative >= array.get(tfSensitiveSettings, 14)
  1347. correctionWickRelativeThresholdReachedAnd = wickRelative >= array.get(tfSensitiveSettings, 22)
  1348. closeRelative = (localH2 - localL2) / (globalH - globalL)
  1349. correctionCloseRelativeThresholdReachedOr = closeRelative >= array.get(tfSensitiveSettings, 16)
  1350. correctionCloseRelativeThresholdReachedAnd = closeRelative >= array.get(tfSensitiveSettings, 24)
  1351. // Condition 3: Reverse Threshold Relative Reached
  1352. // Price corrected from reverse low at least this much %, when compared to distance between global high and low
  1353. // LONG: localReverse is highest low, SHORT: localReverse is lowest high
  1354. localH3 = long ? localReverse : localH
  1355. localL3 = long ? localL : localReverse
  1356. reverseRelative = (localH3 - localL3) / (globalH - globalL)
  1357. reverseRelativeThresholdReachedOr = reverseRelative >= array.get(tfSensitiveSettings, 18)
  1358. reverseRelativeThresholdReachedAnd = reverseRelative >= array.get(tfSensitiveSettings, 26)
  1359. // Condition 4: Price has spent in a sideways movement at least this much
  1360. sideways = math.max(0, localSidewaysL + localSidewaysR - 1)
  1361. sidewaysThresholdReachedOr = sideways >= array.get(tfSensitiveSettings, 20)
  1362. sidewaysThresholdReachedAnd = sideways >= array.get(tfSensitiveSettings, 28)
  1363. // Condition 5: Price has corrected from wick this much relative to its ATR
  1364. wickAbsolute = (localH - localL) / atr
  1365. atrThresholdReachedOr = wickAbsolute >= array.get(tfSensitiveSettings, 36)
  1366. atrThresholdReachedAnd = wickAbsolute >= array.get(tfSensitiveSettings, 38)
  1367. // Conditions combined
  1368. orUsed = usebosFiboCorrectionWickRelativeThresholdOr
  1369. or usebosFiboCorrectionCloseRelativeThresholdOr
  1370. or usebosFiboReverseRelativeThresholdOr
  1371. or usebosCandleLengthThresholdOr
  1372. or usebosAtrThresholdOr
  1373. orConditions = not orUsed
  1374. or correctionWickRelativeThresholdReachedOr and usebosFiboCorrectionWickRelativeThresholdOr
  1375. or correctionCloseRelativeThresholdReachedOr and usebosFiboCorrectionCloseRelativeThresholdOr
  1376. or reverseRelativeThresholdReachedOr and usebosFiboReverseRelativeThresholdOr
  1377. or sidewaysThresholdReachedOr and usebosCandleLengthThresholdOr
  1378. or atrThresholdReachedOr and usebosAtrThresholdOr
  1379. andConditions = (correctionWickRelativeThresholdReachedAnd or not array.get(tfSensitiveSettings, 23))
  1380. and (correctionCloseRelativeThresholdReachedAnd or not array.get(tfSensitiveSettings, 25))
  1381. and (reverseRelativeThresholdReachedAnd or not array.get(tfSensitiveSettings, 27))
  1382. and (sidewaysThresholdReachedAnd or not array.get(tfSensitiveSettings, 29))
  1383. and (atrThresholdReachedAnd or not array.get(tfSensitiveSettings, 39))
  1384. globalChange = orConditions and andConditions
  1385. detailedResults = array.get(tfIndependentSettings, 08)
  1386. if array.get(tfIndependentSettings, 06) and (globalChange or not array.get(tfIndependentSettings, 07))
  1387. and array.get(tfSensitiveSettings, 60)
  1388. offset = int(array.get(tfSensitiveSettings, 50))
  1389. x = array.get(aBarIndex, localB) - offset
  1390. debugTextX(x,
  1391. array.get(tfIndependentSettings, 09) ?
  1392. (globalChange ? "PASS" : "FAIL") + "\n" +
  1393. "" + (correctionWickRelativeThresholdReachedOr ? "V" : "-") +
  1394. (correctionWickRelativeThresholdReachedAnd ? "V" : "-") + (detailedResults ? " " + str.tostring(math.round(wickRelative, 3)) : "") + "\n" +
  1395. "" + (correctionCloseRelativeThresholdReachedOr ? "V" : "-") +
  1396. (correctionCloseRelativeThresholdReachedAnd ? "V" : "-") + (detailedResults ? " " + str.tostring(math.round(closeRelative, 3)) : "") + "\n" +
  1397. "" + (reverseRelativeThresholdReachedOr ? "V" : "-") +
  1398. (reverseRelativeThresholdReachedAnd ? "V" : "-") + (detailedResults ? " " + str.tostring(math.round(reverseRelative, 3)) : "") + "\n" +
  1399. "" + (sidewaysThresholdReachedOr ? "V" : "-") +
  1400. (sidewaysThresholdReachedAnd ? "V" : "-") + (detailedResults ? " " + str.tostring(sideways) : "")+ "\n"+
  1401. "" + (atrThresholdReachedOr ? "V" : "-") +
  1402. (atrThresholdReachedAnd ? "V" : "-") + (detailedResults ? " " + str.tostring(math.round(wickAbsolute, 3)) : "")
  1403. :
  1404. "GHL " + str.tostring(globalH, format.mintick) + " - " + str.tostring(globalL, format.mintick) + "\n" +
  1405. "ATR " + str.tostring(atr, format.mintick) + " - " + str.tostring(atr / (localH + localL) * 2 * 100,"#.##") + "%\n\n" +
  1406. "HL " + str.tostring(localH, format.mintick) + " - " + str.tostring(localL, format.mintick) + "\n" +
  1407. "CR " + str.tostring(localClose, format.mintick) + " - " + str.tostring(localReverse, format.mintick) + "\n" +
  1408. "SW " + str.tostring(localSidewaysL) + " + " + str.tostring(localSidewaysR) + " = " + str.tostring(sideways) + "\n\n" +
  1409. (globalChange ? "PASS" : "FAIL") + "\n" +
  1410. "WR " + str.tostring(correctionWickRelativeThresholdReachedOr) + " - " +
  1411. str.tostring(correctionWickRelativeThresholdReachedAnd) + (detailedResults ? " " + str.tostring(math.round(wickRelative, 3)) : "") + "\n" +
  1412. "CR " + str.tostring(correctionCloseRelativeThresholdReachedOr) + " - " +
  1413. str.tostring(correctionCloseRelativeThresholdReachedAnd) + (detailedResults ? " " + str.tostring(math.round(closeRelative, 3)) : "") + "\n" +
  1414. "RR " + str.tostring(reverseRelativeThresholdReachedOr) + " - " +
  1415. str.tostring(reverseRelativeThresholdReachedAnd) + (detailedResults ? " " + str.tostring(math.round(reverseRelative, 3)) : "") + "\n" +
  1416. "SW " + str.tostring(sidewaysThresholdReachedOr) + " - " +
  1417. str.tostring(sidewaysThresholdReachedAnd) + (detailedResults ? " " + str.tostring(sideways) : "")+ "\n"+
  1418. "WA " + str.tostring(atrThresholdReachedOr) + " - " +
  1419. str.tostring(atrThresholdReachedAnd) + (detailedResults ? " " + str.tostring(math.round(wickAbsolute, 3)) : ""),
  1420. bgcolor = globalChange ? #00065586 : #55002f86)
  1421. globalChange
  1422.  
  1423. checkConditionBosBreak(tfSensitiveSettings, bosBreachCandles) =>
  1424. usebosBreakCandleThresholdOr = array.get(tfSensitiveSettings, 03)
  1425. bosBreakCandleThresholdReachedOr = bosBreachCandles >= array.get(tfSensitiveSettings, 02)
  1426. bosBreakCandleThresholdReachedAnd = bosBreachCandles >= array.get(tfSensitiveSettings, 04)
  1427. orUsed = usebosBreakCandleThresholdOr
  1428. or false //In case we make more conditions in the future
  1429. orConditions = not orUsed
  1430. or bosBreakCandleThresholdReachedOr and usebosBreakCandleThresholdOr
  1431. andConditions = (bosBreakCandleThresholdReachedAnd or not array.get(tfSensitiveSettings, 05))
  1432. and true //In case we make more conditions in the future
  1433. bosBreakCondition = orConditions and andConditions
  1434. bosBreakCondition
  1435.  
  1436. checkConditionNewBosBeyondOld(tfSensitiveSettings, int globalTrendDirection, float highLow, float openClose, float bosLH, float bosLL, float bosSH, float bosSL) =>
  1437. newBosThreshold = array.get(tfSensitiveSettings, 01)
  1438. newBosConfirmConditionLevel = array.get(tfSensitiveSettings, 00)
  1439. if newBosConfirmConditionLevel == 2
  1440. (globalTrendDirection == 1 and openClose > bosLH * (100 + newBosThreshold) / 100)
  1441. or (globalTrendDirection == -1 and openClose * (100 + newBosThreshold) / 100 < bosSL)
  1442. else
  1443. (globalTrendDirection == 1 and highLow > (newBosConfirmConditionLevel ? bosLH : bosLL) * (100 + newBosThreshold) / 100)
  1444. or (globalTrendDirection == -1 and highLow * (100 + newBosThreshold) / 100 < (newBosConfirmConditionLevel ? bosSL : bosSH))
  1445.  
  1446. checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) =>
  1447. array.get(tfSensitiveSettings, 12) and (tHigh - tClose) / (tHigh - tLow) >= array.get(tfSensitiveSettings, 11) / 100 and globalHB < barIndex - 1
  1448.  
  1449. checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) =>
  1450. array.get(tfSensitiveSettings, 12) and (tClose - tLow) / (tHigh - tLow) >= array.get(tfSensitiveSettings, 11) / 100 and globalLB < barIndex - 1
  1451.  
  1452. findGlobalLow(float globalL, int globalLB, float currentL, int barIndex, array<float> aFlt, array<int> aInt, bool checkNa = false, int ignoreAmount = 0) =>
  1453. g_l = globalL
  1454. g_lb = globalLB
  1455. if na(g_l) or g_l > currentL
  1456. g_l := currentL
  1457. g_lb := barIndex
  1458. aSize = arSizeFlt(aFlt)
  1459. if aSize > ignoreAmount
  1460. for i = ignoreAmount to aSize - 1
  1461. if checkNa
  1462. if na(arGetFlt(00, aFlt, i))
  1463. continue
  1464. lastLocalL = arGetFlt(01, aFlt, i)
  1465. if na(g_l) or g_l > lastLocalL
  1466. g_l := lastLocalL
  1467. g_lb := arGetInt(01, aInt, i)
  1468. [g_l, g_lb]
  1469.  
  1470. findGlobalHigh(float globalH, int globalHB, float currentH, int barIndex, array<float> aFlt, array<int> aInt, bool checkNa = false, int ignoreAmount = 0) =>
  1471. g_h = globalH
  1472. g_hb = globalHB
  1473. if na(g_h) or g_h < currentH
  1474. g_h := currentH
  1475. g_hb := barIndex
  1476. aSize = arSizeFlt(aFlt)
  1477. if aSize > ignoreAmount
  1478. for i = ignoreAmount to aSize - 1
  1479. if checkNa
  1480. if na(arGetFlt(01, aFlt, i))
  1481. continue
  1482. lastLocalH = arGetFlt(00, aFlt, i)
  1483. if na(g_h) or g_h < lastLocalH
  1484. g_h := lastLocalH
  1485. g_hb := arGetInt(00, aInt, i)
  1486. [g_h, g_hb]
  1487.  
  1488. // --------------------------------------------------------------
  1489. // 05 GET RISK SIZE
  1490. // --------------------------------------------------------------
  1491.  
  1492. // // Entry size will be = risk_size/close
  1493. // var start_price = 0.0
  1494. // var float risk_size = na
  1495. // if doStrategyBackTest and barstate.isfirst
  1496. // strategy.entry( "Get order size", strategy.long)
  1497. // start_price := close
  1498. // if doStrategyBackTest and ta.barssince(barstate.isfirst) >= 1 and strategy.position_entry_name == "Get order size"
  1499. // risk_size := strategy.position_size * start_price
  1500. // strategy.close("Get order size",comment="Got order size")
  1501.  
  1502. // --------------------------------------------------------------
  1503. // 06 MAIN CODE
  1504. // --------------------------------------------------------------
  1505.  
  1506. // --------------------------------------------------------------
  1507. // 06.01 BEGIN TREND & BOS DETERMINATION ALGORITHM
  1508. // --------------------------------------------------------------
  1509.  
  1510. algoTrendAndBosTick(int barIndex, array<int> aBarIndex, array<float> aHigh, array<float> aLow, array<float> aOpen, array<float> aClose, array<float> aMaxOC, array<float> aMinOC, array<float> aPivotH, array<float> aPivotL, array<float> aLFlt, array<int> aLInt, array<float> aSFlt, array<int> aSInt, array<float> tfSensitiveSettings, array<float> tfIndependentSettings, array<int> aStateInt, array<bool> aStateBool, array<float> aValuesFlt, array<int> aValuesInt, bool finalize) =>
  1511.  
  1512. // CONSTS
  1513.  
  1514. // Theese are used to feed annoying "must define na type" user function bug by TradingView
  1515. float _fna = na
  1516. int _ina = na
  1517.  
  1518. // ARRAYS
  1519.  
  1520. // Temporary "Split" arrays, to temporary hold split members
  1521. var sFlt = array.new<float>(0)
  1522. var sInt = array.new<int>(0)
  1523.  
  1524. // VARIABLES
  1525.  
  1526. // Variables for trend acquisition
  1527. var float acqLocalH = na
  1528. var int acqLocalHB = na
  1529. var float acqLocalL = na
  1530. var int acqLocalLB = na
  1531. var int firstCandleState = na
  1532.  
  1533. // Algo internal states
  1534. executeGlobalTrendChange = false
  1535. executeBosLevelSearch = false
  1536. executeBosLevelUpdate = false
  1537.  
  1538. // ATR
  1539. var float atr = na
  1540.  
  1541. // Classic BOS Bar
  1542. int classicB = na
  1543.  
  1544. [globalTrendDirection, bosTesting, bosBreached, bosBreachCandles] = arStateGet(aStateInt, aStateBool)
  1545. [priorH, priorHB, priorL, priorLB, globalH, globalHB, globalL, globalLB,
  1546. globalHC, globalLC, curNextHC, curNextLC,
  1547. curLNextClose, curLNextReverse, curSNextClose, curSNextReverse,
  1548. bosLH, bosLL, bosLB, bosSH, bosSL, bosSB,
  1549. cbosLH, cbosLL, cbosLB, cbosSH, cbosSL, cbosSB] = arValuesGet(aValuesFlt, aValuesInt)
  1550.  
  1551. tHigh = array.get(aHigh, 0)
  1552. tLow = array.get(aLow, 0)
  1553. tOpen = array.get(aOpen, 0)
  1554. tClose = array.get(aClose, 0)
  1555. tMaxOC = array.get(aMaxOC, 0)
  1556. tMinOC = array.get(aMinOC, 0)
  1557. tPivotH = array.get(aPivotH, 0)
  1558. tPivotL = array.get(aPivotL, 0)
  1559.  
  1560. // Reset algo state
  1561. bosTesting := false
  1562. prevBreachCandles = bosBreachCandles
  1563. bosBreachCandles := 0
  1564.  
  1565. // Globally used settings
  1566. bosIgnoreManipulationsAmount = int(array.get(tfSensitiveSettings, 12))
  1567. useAtrBreakThreshold = array.get(tfSensitiveSettings, 31)
  1568. bosSameCandleLegConsumptionDelay = int(array.get(tfSensitiveSettings, 13))
  1569. bosConsiderNextEnabled = array.get(tfSensitiveSettings, 06)
  1570. bosConsiderNextRecursive = array.get(tfSensitiveSettings, 10)
  1571. bosConsiderNextConsumptionThreshold = array.get(tfSensitiveSettings, 09)
  1572. bosConsiderNextDistanceThreshold = array.get(tfSensitiveSettings, 07)
  1573. bosConsiderNextConsumptionEnabled = array.get(tfSensitiveSettings, 08)
  1574.  
  1575.  
  1576.  
  1577. // --------------------------------------------------------------
  1578. // 06.01.01 TREND & BOS | Initial Calculations
  1579. // --------------------------------------------------------------
  1580.  
  1581. // ATR
  1582. atrLength = array.get(tfSensitiveSettings, 32)
  1583. if na(atr)
  1584. atr := tHigh - tLow
  1585. else
  1586. prevClose = array.get(aClose, 1)
  1587. curAtr = math.max(tHigh - tLow, math.abs(tHigh - prevClose), math.abs(tLow - prevClose))
  1588. atr := (atr * (atrLength - 1) + curAtr ) / atrLength
  1589.  
  1590. // --------------------------------------------------------------
  1591. // 06.01.02 TREND & BOS | Init & Acquire Trend
  1592. // --------------------------------------------------------------
  1593.  
  1594. // Initial trend forced Long
  1595. if globalTrendDirection == 2
  1596. priorH := tHigh
  1597. priorHB := barIndex
  1598. priorL := tLow
  1599. priorLB := barIndex
  1600. globalH := priorH
  1601. globalHB := priorHB
  1602. globalL := priorL
  1603. globalLB := priorLB
  1604. globalTrendDirection := -1
  1605. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1606. executeGlobalTrendChange := true
  1607. executeBosLevelUpdate := true
  1608. // Initial trend forced Short
  1609. else if globalTrendDirection == -2
  1610. priorH := tHigh
  1611. priorHB := barIndex
  1612. priorL := tLow
  1613. priorLB := barIndex
  1614. globalH := priorH
  1615. globalHB := priorHB
  1616. globalL := priorL
  1617. globalLB := priorLB
  1618. globalTrendDirection := 1
  1619. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1620. executeGlobalTrendChange := true
  1621. executeBosLevelUpdate := true
  1622. // Initial trend acquisition
  1623. else if globalTrendDirection == 0
  1624. MAX_INITIAL_CANDLES = array.get(tfIndependentSettings, 00)
  1625. globalHC := na(globalHC) or tMaxOC > globalHC ? tMaxOC : globalHC
  1626. globalLC := na(globalLC) or tMinOC < globalLC ? tMinOC : globalLC
  1627. // Step 1 : Record first available high/low once acquired, and rewrite them as long as both are pierced in same candle
  1628. if na(priorH) or (na(globalH) and tHigh > priorH and tLow < priorL)
  1629. priorH := tHigh
  1630. priorHB := barIndex
  1631. priorL := tLow
  1632. priorLB := barIndex
  1633. firstCandleState := tClose >= tOpen ? 1 : -1
  1634. // Step 2 : Record first price leg once price exceeds first available values
  1635. else if na(globalH) or barIndex - priorHB > MAX_INITIAL_CANDLES
  1636. // If price breached prior up (and only up)
  1637. // First leg is up
  1638. if tHigh > priorH
  1639. globalH := tHigh
  1640. globalHB := barIndex
  1641. globalL := priorL
  1642. globalLB := priorLB
  1643. // If price breached prior down (and only down)
  1644. // First leg is down
  1645. else if tLow < priorL
  1646. globalL := tLow
  1647. globalLB := barIndex
  1648. globalH := priorH
  1649. globalHB := priorHB
  1650. // Determine trend regardless if waited enough
  1651. if barIndex - priorHB > MAX_INITIAL_CANDLES
  1652. if na(globalH)
  1653. globalL := priorL
  1654. globalLB := priorLB
  1655. globalH := priorH
  1656. globalHB := priorHB
  1657. globalTrendDirection := -firstCandleState
  1658. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1659. executeGlobalTrendChange := true
  1660. executeBosLevelUpdate := true
  1661. // Step 3 : Record locals and determine trend when possible
  1662. else
  1663. // Record new local high as long as we don't have both pivots in a downtrend
  1664. if tPivotH and (na(acqLocalH) or tPivotH > acqLocalH) and (na(tPivotL) or globalLB < globalHB)
  1665. acqLocalH := tPivotH
  1666. acqLocalHB := barIndex
  1667. // Record new local low as we don't have both pivots in an uptrend
  1668. if tPivotL and (na(acqLocalL) or tPivotL < acqLocalL) and (na(tPivotH) or globalHB < globalLB)
  1669. acqLocalL := tPivotL
  1670. acqLocalLB := barIndex
  1671. // If breached global high or waited enough
  1672. if tHigh > globalH or barIndex - globalLB > MAX_INITIAL_CANDLES
  1673. // If with local low or first leg is down or enough candles passed
  1674. // We got a long trend!
  1675. if acqLocalL or globalHB < globalLB or barIndex - globalLB > MAX_INITIAL_CANDLES
  1676. globalL := na(acqLocalL) ? globalL : acqLocalL
  1677. globalLB := na(acqLocalL) ? globalLB : acqLocalLB
  1678. globalTrendDirection := -1
  1679. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1680. executeGlobalTrendChange := true
  1681. executeBosLevelUpdate := true
  1682. // Update global high for now
  1683. if tHigh > globalH
  1684. globalH := tHigh
  1685. globalHB := barIndex
  1686. // Breached global Low or waited enough
  1687. else if tLow < globalL or barIndex - globalHB > MAX_INITIAL_CANDLES
  1688. // If with local high or first leg is up
  1689. // We got a short trend!
  1690. if acqLocalH or globalLB < globalHB or barIndex - globalHB > MAX_INITIAL_CANDLES
  1691. globalH := na(acqLocalH) ? globalH : acqLocalH
  1692. globalHB := na(acqLocalH) ? globalHB : acqLocalHB
  1693. globalTrendDirection := 1
  1694. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1695. executeGlobalTrendChange := true
  1696. executeBosLevelUpdate := true
  1697. // Update global low for now
  1698. if tLow < globalL
  1699. globalL := tLow
  1700. globalLB := barIndex
  1701.  
  1702. // --------------------------------------------------------------
  1703. // 06.01.03 TREND & BOS | Accumulated values
  1704. // --------------------------------------------------------------
  1705. if globalTrendDirection != 0
  1706. curNextHC := na(curNextHC) or curNextHC < tMaxOC ? tMaxOC : curNextHC
  1707. curNextLC := na(curNextLC) or curNextLC > tMinOC ? tMinOC : curNextLC
  1708. curLNextClose := na(curLNextClose) or curLNextClose < tMaxOC ? tMaxOC : curLNextClose
  1709. curLNextReverse := na(curLNextReverse) or curLNextReverse < tLow ? tLow : curLNextReverse
  1710. curSNextClose := na(curSNextClose) or curSNextClose > tMinOC ? tMinOC : curSNextClose
  1711. curSNextReverse := na(curSNextReverse) or curSNextReverse > tHigh ? tHigh : curSNextReverse
  1712. if globalTrendDirection == 1
  1713. globalHC := curNextHC
  1714. if globalTrendDirection == -1
  1715. globalLC := curNextLC
  1716.  
  1717.  
  1718. // --------------------------------------------------------------
  1719. // 06.01.04 TREND & BOS | Long Breaching
  1720. // --------------------------------------------------------------
  1721.  
  1722. if globalTrendDirection == 1
  1723. // If in bos breach mode
  1724. // Determine if resumed prior trend
  1725. if bosBreached
  1726. // If breached priorL
  1727. // Then resumed prior trend
  1728. if tLow < priorL - (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : priorL * array.get(tfSensitiveSettings, 30)) / 100
  1729. // When resuming prior short trend, global low should be like if this were a short trend leg
  1730. // When in short trend, we breach global low
  1731. // Here, we breach prior low, but in terms of price action, we have a certain low we fell from downwards
  1732. // That should be our global low for purpose of bos search
  1733. [curL, curLB] = findGlobalLow(_fna, _ina, _fna, barIndex, aSFlt, aSInt, true, 1)
  1734. globalL := na(curL) ? priorL : curL
  1735. globalLB := na(curL) ? priorLB : curLB
  1736. bosBreached := false
  1737. executeBosLevelSearch := true
  1738. executeGlobalTrendChange := true
  1739. executeBosLevelUpdate := true
  1740.  
  1741. // If not in bos breach mode
  1742. else
  1743. // Determine if testing bos
  1744. bosTesting := (tLow < bosSH or array.get(aLow, 1) < bosSH)
  1745. // Determine if breaching bos - when enough candles are observed confirm bos breach
  1746. if tHigh < bosSL * (100 - array.get(tfSensitiveSettings, 01)) / 100
  1747. bosBreachCandles := prevBreachCandles + 1
  1748. // BOS Break!
  1749. if checkConditionBosBreak(tfSensitiveSettings, bosBreachCandles)
  1750. priorH := globalH
  1751. priorHB := globalHB
  1752. bosBreached := true
  1753. executeBosLevelSearch := true
  1754. executeGlobalTrendChange := true
  1755.  
  1756. // --------------------------------------------------------------
  1757. // 06.01.05 TREND & BOS | SHORT Breaching
  1758. // --------------------------------------------------------------
  1759.  
  1760. else if globalTrendDirection == -1
  1761. // If in bos breach mode
  1762. // Determine if resumed prior trend
  1763. if bosBreached
  1764. // If breached priorH
  1765. // Then resumed prior trend
  1766. if tHigh > priorH + (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : priorH * array.get(tfSensitiveSettings, 30)) / 100
  1767. // When resuming prior long trend, global high should be like if this were a long trend leg
  1768. // When in long trend, we breach global high
  1769. // Here, we breach prior high, but in terms of price action, we have a certain high we pumped from upwards
  1770. // That should be our global high for purpose of bos search
  1771. [curH, curHB] = findGlobalHigh(_fna, _ina, _fna, barIndex, aLFlt, aLInt, true, 1)
  1772. globalH := na(curH) ? priorH : curH
  1773. globalHB := na(curH) ? priorHB : curHB
  1774. bosBreached := false
  1775. executeBosLevelSearch := true
  1776. executeGlobalTrendChange := true
  1777. executeBosLevelUpdate := true
  1778.  
  1779. // If not in bos breach mode
  1780. else
  1781. // Determine if testing bos
  1782. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  1783. // Determine if breaching bos - when enough candles are observed confirm bos breach
  1784. if tLow > bosLH * (100 + array.get(tfSensitiveSettings, 01)) / 100
  1785. bosBreachCandles := prevBreachCandles + 1
  1786. // BOS Break!
  1787. if checkConditionBosBreak(tfSensitiveSettings, bosBreachCandles)
  1788. priorL := globalL
  1789. priorLB := globalLB
  1790. bosBreached := true
  1791. executeBosLevelSearch := true
  1792. executeGlobalTrendChange := true
  1793.  
  1794. // --------------------------------------------------------------
  1795. // 06.01.06 TREND & BOS | New HH / LL
  1796. // --------------------------------------------------------------
  1797.  
  1798. if globalTrendDirection != 0
  1799. if globalTrendDirection == 1 and not executeGlobalTrendChange
  1800. // If broke global high
  1801. // Record new high, update BOS if possible
  1802. if tHigh > globalH + (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : globalH * array.get(tfSensitiveSettings, 30)) / 100
  1803. executeBosLevelSearch := true
  1804. else if globalTrendDirection == -1 and not executeGlobalTrendChange
  1805. // If broke global low
  1806. // Record new low, update BOS if possible
  1807. if tLow < globalL - (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : globalL * array.get(tfSensitiveSettings, 30)) / 100
  1808. executeBosLevelSearch := true
  1809. nextTrend = globalTrendDirection == 0 ? 0 : executeGlobalTrendChange ? -globalTrendDirection : globalTrendDirection
  1810.  
  1811. // --------------------------------------------------------------
  1812. // 06.01.07 TREND & BOS | Price Legs
  1813. // --------------------------------------------------------------
  1814.  
  1815. // Possible BOS breaching means we are uncertain where the trend will go
  1816. // So we record price legs both ways always, the only exception is initial state with no trend
  1817.  
  1818. // --------------------------------------------------------------
  1819. // 06.01.07.01 TREND & BOS | Price Legs | Long New Candle Data
  1820. // --------------------------------------------------------------
  1821.  
  1822. if globalTrendDirection != 0
  1823.  
  1824. [curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF] = arPop(aLFlt, aLInt)
  1825.  
  1826. // If encountered pivot low
  1827. // If it's a lower or first local low
  1828. // Record it and all its data
  1829. // Otherwise it's ignored
  1830. if tPivotL and (curLLocalH or tPivotH) and (na(curLLocalL) or tPivotL < curLLocalL)
  1831. // A special case of high and low in one candle
  1832. if tPivotH and na(curLLocalH)
  1833. curLLocalH := tPivotH
  1834. curLLocalHB := barIndex
  1835. curLLocalL := tPivotL
  1836. curLLocalLB := barIndex
  1837. curLSidewaysL := na
  1838. // Reverse close low that has been calculated so far is now included and consumed
  1839. curLClose := na(curLClose) ? curLNextClose : math.max(curLClose, curLNextClose)
  1840. curLNextClose := na
  1841. curLReverse := na(curLReverse) ? curLNextReverse : math.max(curLReverse, curLNextReverse)
  1842. curLNextReverse := na
  1843. // If encountered a pivot high
  1844. if tPivotH
  1845. // If we have a local price leg (both high and low)
  1846. if curLLocalL
  1847. // Pack last price leg into array, begin a new one
  1848. arPush(aLFlt, aLInt, curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF)
  1849. curLLocalH := na
  1850. curLLocalL := na
  1851. curLClose := na
  1852. curLReverse := na
  1853. curLSidewaysL := na
  1854. if na(curLLocalH) or tPivotH > curLLocalH
  1855. curLLocalH := tPivotH
  1856. curLLocalHB := barIndex
  1857.  
  1858. arPush(aLFlt, aLInt, curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF)
  1859.  
  1860. // --------------------------------------------------------------
  1861. // 06.01.07.02 TREND & BOS | Price Legs | Short New Candle Data
  1862. // --------------------------------------------------------------
  1863.  
  1864. if globalTrendDirection != 0
  1865.  
  1866. [curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF] = arPop(aSFlt, aSInt)
  1867.  
  1868. // If encountered pivot high
  1869. // If it's a higher or first local high
  1870. // Record it and all its data
  1871. // Otherwise it's ignored
  1872. if tPivotH and (tPivotL or curSLocalL) and (na(curSLocalH) or tPivotH > curSLocalH)
  1873. // A special case of high and low in one candle
  1874. if tPivotL and na(curSLocalL)
  1875. curSLocalL := tPivotL
  1876. curSLocalLB := barIndex
  1877. curSLocalH := tPivotH
  1878. curSLocalHB := barIndex
  1879. curSSidewaysL := na
  1880. // Reverse high and close that has been calculated so far is now included and consumed
  1881. curSClose := na(curSClose) ? curSNextClose : math.min(curSClose, curSNextClose)
  1882. curSNextClose := na
  1883. curSReverse := na(curSReverse) ? curSNextReverse : math.min(curSReverse, curSNextReverse)
  1884. curSNextReverse := na
  1885. // If encountered a pivot low
  1886. if tPivotL
  1887. // If we have a local price leg (both high and low)
  1888. if curSLocalH
  1889. // Pack last price leg into array, begin a new one
  1890. arPush(aSFlt, aSInt, curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF)
  1891. curSLocalH := na
  1892. curSLocalL := na
  1893. curSClose := na
  1894. curSReverse := na
  1895. curSSidewaysL := na
  1896. if na(curSLocalL) or tPivotL < curSLocalL
  1897. curSLocalL := tPivotL
  1898. curSLocalLB := barIndex
  1899. arPush(aSFlt, aSInt, curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF)
  1900.  
  1901. // --------------------------------------------------------------
  1902. // 06.01.07.03 TREND & BOS | Price Legs | Long Merge
  1903. // --------------------------------------------------------------
  1904.  
  1905. if globalTrendDirection != 0
  1906.  
  1907. // Extra offset is nullified in case this candle is breaching BOS and isn't a manipulation
  1908. // In this case we are merging right before a bos level search to include candles that would otherwise be excluded
  1909. // Otherwise we don't merge bars within this offset, so they don't overwrite previous legs that could be used
  1910. splitOffset = 1 + (executeBosLevelSearch
  1911. and ((not checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) and nextTrend == 1)
  1912. or (not checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) and nextTrend == -1)) ? 0 :
  1913. bosIgnoreManipulationsAmount)
  1914. // Combine all price legs that get overwritten by price legs after the offset (in case of manipulation)
  1915. i = arSizeFlt(aLFlt)
  1916. // Go on while there are at least two legs left to compare
  1917. while i >= 2
  1918. i := i - 1
  1919. if splitOffset and arGetInt(01, aLInt, i) > barIndex - splitOffset
  1920. continue
  1921. // Attempt merger of current leg with the previous one
  1922. prevL = arGetFlt(01, aLFlt, i - 1)
  1923. curL = arGetFlt(01, aLFlt, i)
  1924. // Only legs with a lower low should be kept alive
  1925. if na(curL) or prevL < curL
  1926. continue
  1927. // Take its reverse low and close if higher
  1928. arSetFlt(02, aLFlt, i, math.max(arGetFlt(02, aLFlt, i), arGetFlt(02, aLFlt, i - 1)))
  1929. arSetFlt(03, aLFlt, i, math.max(arGetFlt(03, aLFlt, i), arGetFlt(03, aLFlt, i - 1)))
  1930. // Take its local high if it's higher
  1931. prevH = arGetFlt(00, aLFlt, i - 1)
  1932. if prevH > arGetFlt(00, aLFlt, i)
  1933. arSetFlt(00, aLFlt, i, prevH)
  1934. arSetInt(00, aLInt, i, arGetInt(00, aLInt, i - 1))
  1935. arSetInt(02, aLInt, i, _ina)
  1936. // Remove consumed leg
  1937. arRemove(aLFlt, aLInt, i - 1)
  1938.  
  1939. // --------------------------------------------------------------
  1940. // 06.01.07.04 TREND & BOS | Price Legs | Short Merge
  1941. // --------------------------------------------------------------
  1942.  
  1943. if globalTrendDirection != 0
  1944.  
  1945. // Extra offset is nullified in case this candle is breaching BOS and isn't a manipulation
  1946. // In this case we are merging right before a bos level search to include candles that would otherwise be excluded
  1947. // Otherwise we don't merge bars within this offset, so they don't overwrite previous legs that could be used
  1948. splitOffset = 1 + (executeBosLevelSearch
  1949. and ((not checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) and nextTrend == 1)
  1950. or (not checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) and nextTrend == -1)) ? 0 :
  1951. bosIgnoreManipulationsAmount)
  1952. // Combine all price legs that get overwritten by price legs after the offset (in case of manipulation)
  1953. i = arSizeFlt(aSFlt)
  1954. // Go on while there are at least two legs left to compare
  1955. while i >= 2
  1956. i := i - 1
  1957. if splitOffset and arGetInt(00, aSInt, i) > barIndex - splitOffset
  1958. continue
  1959. // Attempt merger of current leg with the previous one
  1960. prevH = arGetFlt(00, aSFlt, i - 1)
  1961. curH = arGetFlt(00, aSFlt, i)
  1962. // Only legs with a lower low should be kept alive
  1963. if na(curH) or prevH > curH
  1964. continue
  1965. // Take its reverse low and close if higher
  1966. arSetFlt(02, aSFlt, i, math.min(arGetFlt(02, aSFlt, i), arGetFlt(02, aSFlt, i - 1)))
  1967. arSetFlt(03, aSFlt, i, math.min(arGetFlt(03, aSFlt, i), arGetFlt(03, aSFlt, i - 1)))
  1968. // Take its local low if it's lower
  1969. prevL = arGetFlt(01, aSFlt, i - 1)
  1970. if prevL < arGetFlt(01, aSFlt, i)
  1971. arSetFlt(01, aSFlt, i, prevL)
  1972. arSetInt(01, aSInt, i, arGetInt(01, aSInt, i - 1))
  1973. arSetInt(02, aSInt, i, _ina)
  1974. // Remove consumed leg
  1975. arRemove(aSFlt, aSInt, i - 1)
  1976.  
  1977. // --------------------------------------------------------------
  1978. // 06.01.07.05 TREND & BOS | Price Legs | Long Sideways
  1979. // --------------------------------------------------------------
  1980.  
  1981. // What part of candle should be inside for it to be considered inside
  1982. candleTotal = 100
  1983. candlePercent = array.get(tfSensitiveSettings, 34)
  1984. candleRemainder = candleTotal - candlePercent
  1985. // What percent of candle should the body be for it to be considered a full body candle
  1986. candleFullBodyPercent = array.get(tfSensitiveSettings, 35)
  1987. // If candle is full body, at least candlePercent of candle's body must be inside
  1988. // If candle is not full body, at least candlePercent of whole candle must be inside
  1989.  
  1990. if globalTrendDirection != 0
  1991. maxI = arSizeFlt(aLFlt) - 1
  1992. maxI := maxI < 0 ? na : maxI
  1993. for i = 0 to maxI
  1994. // If localLow is filled then this is a complete price leg
  1995. localH = arGetFlt(00, aLFlt, i)
  1996. localL = arGetFlt(01, aLFlt, i)
  1997. if localL
  1998. // If sidewaysL is na then sideways L needs recalculation
  1999. if na(arGetInt(02, aLInt, i))
  2000. sidewaysL = 0
  2001. localClose = arGetFlt(02, aLFlt, i)
  2002. localReverse = arGetFlt(03, aLFlt, i)
  2003. startBar = arGetInt(01, aLInt, i)
  2004. lookbackStart = barIndex - startBar
  2005. lookbackEnd = barIndex - math.min(arGetInt(00, aLInt, i), math.max(nz(bosLB), nz(bosSB), nz(arGetLastBar(aLInt, i))))
  2006. lookbackEnd := lookbackEnd <= lookbackStart ? na : lookbackEnd
  2007. arSetInt(03, aLInt, i, lookbackStart)
  2008. arSetInt(04, aLInt, i, 0)
  2009. for j = lookbackStart to lookbackEnd
  2010. max = array.get(aHigh, j)
  2011. min = array.get(aLow, j)
  2012. maxOC = array.get(aMaxOC, j)
  2013. minOC = array.get(aMinOC, j)
  2014. // if full body candle compare body
  2015. if (maxOC - minOC) / (max - min) > candleFullBodyPercent / candleTotal
  2016. max := maxOC
  2017. min := minOC
  2018. bodyLow = (max * candleRemainder + min * candlePercent) / candleTotal
  2019. bodyHigh = (max * candlePercent + min * candleRemainder) / candleTotal
  2020. if bodyLow > localH or bodyHigh < localL
  2021. break
  2022. curReverse = array.get(aLow, j)
  2023. curClose = array.get(aMaxOC, j)
  2024. if na(localClose) or curClose > localClose
  2025. localClose := curClose
  2026. if na(localReverse) or curReverse > localReverse
  2027. localReverse := curReverse
  2028. sidewaysL := j - lookbackStart
  2029. arSetInt(02, aLInt, i, sidewaysL)
  2030. arSetFlt(02, aLFlt, i, math.min(localH, localClose))
  2031. arSetFlt(03, aLFlt, i, localReverse)
  2032. // If still moving sideways R
  2033. if arGetInt(04, aLInt, i) == 0
  2034. max = tHigh
  2035. min = tLow
  2036. // if full body candle compare body
  2037. if (tMaxOC - tMinOC) / (max - min) > candleFullBodyPercent / candleTotal
  2038. max := tMaxOC
  2039. min := tMinOC
  2040. bodyHigh = (max * candlePercent + min * candleRemainder) / candleTotal
  2041. if bodyHigh > localH
  2042. arSetInt(04, aLInt, i, 1)
  2043. else
  2044. arSetInt(03, aLInt, i, arGetInt(03, aLInt, i) + 1)
  2045.  
  2046. // --------------------------------------------------------------
  2047. // 06.01.07.06 TREND & BOS | Price Legs | Short Sideways
  2048. // --------------------------------------------------------------
  2049.  
  2050. if globalTrendDirection != 0
  2051. // Sideways update for short legs
  2052. maxI = arSizeFlt(aSFlt) - 1
  2053. maxI := maxI < 0 ? na : maxI
  2054. for i = 0 to maxI
  2055. // If localHigh is filled then this is a complete price leg
  2056. localH = arGetFlt(00, aSFlt, i)
  2057. localL = arGetFlt(01, aSFlt, i)
  2058. if localH
  2059. // If sidewaysL = na then sideways L needs recalculation
  2060. if na(arGetInt(02, aSInt, i))
  2061. sidewaysL = 0
  2062. localClose = arGetFlt(02, aSFlt, i)
  2063. localReverse = arGetFlt(03, aSFlt, i)
  2064. startBar = arGetInt(00, aSInt, i)
  2065. lookbackStart = barIndex - startBar
  2066. lookbackEnd = barIndex - math.min(arGetInt(01, aSInt, i), math.max(nz(bosLB), nz(bosSB), nz(arGetLastBar(aSInt, i))))
  2067. lookbackEnd := lookbackEnd <= lookbackStart ? na : lookbackEnd
  2068. arSetInt(03, aSInt, i, lookbackStart)
  2069. arSetInt(04, aSInt, i, 0)
  2070. for j = lookbackStart to lookbackEnd
  2071. max = array.get(aHigh, j)
  2072. min = array.get(aLow, j)
  2073. maxOC = array.get(aMaxOC, j)
  2074. minOC = array.get(aMinOC, j)
  2075. // if full body candle compare body
  2076. if (maxOC - minOC) / (max - min) > candleFullBodyPercent / candleTotal
  2077. max := maxOC
  2078. min := minOC
  2079. bodyLow = (max * candleRemainder + min * candlePercent) / candleTotal
  2080. bodyHigh = (max * candlePercent + min * candleRemainder) / candleTotal
  2081. if bodyHigh > localH or bodyLow < localL
  2082. break
  2083. curReverse = array.get(aHigh, j)
  2084. curClose = array.get(aMinOC, j)
  2085. if na(localClose) or curClose < localClose
  2086. localClose := curClose
  2087. if na(localReverse) or curReverse < localReverse
  2088. localReverse := curReverse
  2089. sidewaysL := j - lookbackStart
  2090. arSetInt(02, aSInt, i, sidewaysL)
  2091. arSetFlt(02, aSFlt, i, math.max(localL, localClose))
  2092. arSetFlt(03, aSFlt, i, localReverse)
  2093. // If still moving sideways R
  2094. if arGetInt(04, aSInt, i) == 0
  2095. max = tHigh
  2096. min = tLow
  2097. // if full body candle compare body
  2098. if (tMaxOC - tMinOC) / (max - min) > candleFullBodyPercent / candleTotal
  2099. max := tMaxOC
  2100. min := tMinOC
  2101. bodyLow = (max * candleRemainder + min * candlePercent) / candleTotal
  2102. if bodyLow < localL
  2103. arSetInt(04, aSInt, i, 1)
  2104. else
  2105. arSetInt(03, aSInt, i, arGetInt(03, aSInt, i) + 1)
  2106.  
  2107. // --------------------------------------------------------------
  2108. // 06.01.08 TREND & BOS | BOS Search
  2109. // --------------------------------------------------------------
  2110.  
  2111. // First check if we have to consume price leg we left behind
  2112. // If that leg was overwritten we instead remove it completely
  2113. breakoutPriceLegConsumption = false
  2114. // Long check:
  2115. if nextTrend == 1 and not executeBosLevelSearch
  2116. // a) we have long price legs
  2117. if array.size(aLFlt) > 0
  2118. // Ensure price leg is full
  2119. // b) [0] price leg low bar is on or before global high bar
  2120. // c) [0] price leg low bar is far away from barIndex (user input value)
  2121. localLowB = arGetInt(01, aLInt, 0)
  2122. if arGetFlt(01, aLFlt, 0) and localLowB <= globalHB and barIndex - localLowB >= bosSameCandleLegConsumptionDelay
  2123. localLow = array.get(aMinOC, barIndex - localLowB)
  2124. consumed = false
  2125. // d) price low never went below that price leg's body low (meaning, potential BOS was not consumed)
  2126. for i = 0 to bosSameCandleLegConsumptionDelay - 1
  2127. if array.get(aLow, i) <= localLow
  2128. arRemove(aLFlt, aLInt, 0)
  2129. if array.size(aLFlt) == 0
  2130. arPush(aLFlt, aLInt)
  2131. consumed := true
  2132. break
  2133. // Then do intermittent bos search to consume that leg
  2134. if not consumed
  2135. breakoutPriceLegConsumption := true
  2136. // Short check:
  2137. if nextTrend == -1 and not executeBosLevelSearch
  2138. // a) we have short price legs
  2139. if array.size(aSFlt) > 0
  2140. // Ensure price leg is full
  2141. // b) [0] price leg high bar is on or before global low bar
  2142. // c) [0] price leg high bar is far away from barIndex (user input value)
  2143. localHighB = arGetInt(00, aSInt, 0)
  2144. if arGetFlt(00, aSFlt, 0) and localHighB <= globalLB and barIndex - localHighB >= bosSameCandleLegConsumptionDelay
  2145. localHigh = array.get(aMaxOC, barIndex - localHighB)
  2146. consumed = false
  2147. // d) price high never went above that price leg's body high (meaning, potential BOS was not consumed)
  2148. for i = 0 to bosSameCandleLegConsumptionDelay
  2149. if array.get(aHigh, i) >= localHigh
  2150. arRemove(aSFlt, aSInt, 0)
  2151. if array.size(aSFlt) == 0
  2152. arPush(aSFlt, aSInt)
  2153. consumed := true
  2154. break
  2155. // Then do intermittent bos search to consume that leg
  2156. if not consumed
  2157. breakoutPriceLegConsumption := true
  2158.  
  2159. // --------------------------------------------------------------
  2160. // 06.01.08.01 TREND & BOS | Long BOS Search
  2161. // --------------------------------------------------------------
  2162.  
  2163. if nextTrend == 1 and (executeBosLevelSearch or breakoutPriceLegConsumption)
  2164.  
  2165. // If we are doing bos search after trend break or to consume breakout price leg then this is a special case
  2166. intermediarySearch = breakoutPriceLegConsumption or (bosBreached and executeGlobalTrendChange)
  2167.  
  2168. // If intermittent search after bos break:
  2169. // Split away legs that were achieved above current global high
  2170. if intermediarySearch
  2171. [_void, splitBar] = findGlobalHigh(globalH, globalHB, tHigh, barIndex, aSFlt, aSInt)
  2172. // If consuming breakout legs, we must offset by user's input value, because we could have made closer global highs afterwards,
  2173. // but still need to consume only the original leg
  2174. if breakoutPriceLegConsumption
  2175. splitBar := barIndex - bosSameCandleLegConsumptionDelay
  2176. arSplit(aLFlt, aLInt, sFlt, sInt, true, splitBar)
  2177. // If normal search:
  2178. // Split away entry added in the last bars in case this candle is a manipulation
  2179. else
  2180. splitOffset = 1 + (checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) ? bosIgnoreManipulationsAmount : 0)
  2181. arSplit(aLFlt, aLInt, sFlt, sInt, true, barIndex - splitOffset)
  2182.  
  2183. // Output where we're checking Long BOS
  2184. if array.get(tfIndependentSettings, 06) and array.get(tfSensitiveSettings, 60)
  2185. debugText("BL\nS" + (breakoutPriceLegConsumption ? "B" : intermediarySearch ? "I" : "") +
  2186. "\nS"+str.tostring(arSizeFlt(sFlt))+"\nO"+str.tostring(arSizeFlt(aLFlt)), true)
  2187.  
  2188. // Remember first and last that passed our conditions
  2189. float firstPassedL = na
  2190. int firstPassedLB = na
  2191. float lastPassedL = na
  2192. int lastPassedLB = na
  2193. bool found = false
  2194. // Iterate until we find suitable LocalH or exhaust all options
  2195. while not found and array.size(aLFlt) > 0
  2196. // Get next leg to examine
  2197. [curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF] = arPop(aLFlt, aLInt)
  2198. found := checkConditionGlobalChange(tfSensitiveSettings, tfIndependentSettings, aBarIndex, curLLocalLB,
  2199. globalH, globalLC, curLLocalH, curLLocalL, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, atr, true)
  2200. if array.get(tfSensitiveSettings, 61)
  2201. printPriceLeg(tfSensitiveSettings, tfIndependentSettings, aBarIndex,
  2202. curLLocalHB, curLLocalH, curLLocalLB, curLLocalL, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, true, found)
  2203. // Record last passed High
  2204. if found
  2205. lastPassedL := curLLocalL
  2206. lastPassedLB := curLLocalLB
  2207. // Record first passed High
  2208. if na(firstPassedL)
  2209. firstPassedL := curLLocalL
  2210. firstPassedLB := curLLocalLB
  2211. // If we can look further, have price legs remaining, and have something to compare to
  2212. aSize = arSizeFlt(aLFlt)
  2213. if bosConsiderNextEnabled and aSize > 0 and firstPassedL
  2214. // If next point is within range - clear found flag to allow while loop to continue
  2215. // Also ensure next point isn't within old BOS in case we're breaching
  2216. float nextH = na
  2217. nextL = arGetFlt(01, aLFlt, aSize - 1)
  2218. nextMinOC = array.get(aMinOC, barIndex - arGetInt(01, aLInt, aSize - 1))
  2219. nextDiff = (globalH - nextL) / (globalH - (bosConsiderNextRecursive ? lastPassedL : firstPassedL)) - 1
  2220. nextConsumed = false
  2221. if bosConsiderNextConsumptionEnabled
  2222. nextConsumed := (nextMinOC - lastPassedL) / (nextMinOC - nextL) > bosConsiderNextConsumptionThreshold / 100
  2223. if not nextConsumed and nextDiff < bosConsiderNextDistanceThreshold / 100
  2224. and (not bosBreached or checkConditionNewBosBeyondOld(tfSensitiveSettings, nextTrend, nextL, nextMinOC, bosLH, bosLL, bosSH, bosSL))
  2225. found := false
  2226. // If next point is not within range - break
  2227. else
  2228. break
  2229. // If we still have a localL value here that means we found a suitable local low to use as a new BOS
  2230. if lastPassedL
  2231. // Relocate global low and BOS on it
  2232. executeBosLevelUpdate := true
  2233. globalL := lastPassedL
  2234. globalLB := lastPassedLB
  2235. globalLC := math.max(curNextLC, globalL)
  2236. // Set classic BOS bar in case we have an unused price leg, and that BOS is outside selected BOS by at least the threshold
  2237. if array.size(aLInt) > 0
  2238. // First we pick the last price leg, which is by definition the highest
  2239. classicB := arGetInt(01, aLInt, 0)
  2240. classicMinOC = array.get(aMinOC, barIndex - classicB)
  2241. classicDiff = 1 - classicMinOC / lastPassedL
  2242. // Classic BOS' closer boundary must be away from algorithm-chosen BOS by at least this much, otherwise it's discarded
  2243. if classicDiff <= array.get(tfSensitiveSettings, 01) / 100
  2244. classicB := na
  2245. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, true)
  2246. arSplit(sFlt, sInt, aLFlt, aLInt)
  2247. if array.size(aLFlt) == 0
  2248. arPush(aLFlt, aLInt)
  2249. // If not doing intermittent search
  2250. if not intermediarySearch
  2251. // Update global high
  2252. globalH := tHigh
  2253. globalHB := barIndex
  2254. // Waste short accumulated values
  2255. curSNextClose := na
  2256. curSNextReverse := na
  2257. // If not in bos breached mode the next lowest close is reset here
  2258. // Otherwise it can also be reset if we confirm trend change and establish new BOS later
  2259. if not bosBreached
  2260. curNextLC := tMinOC
  2261.  
  2262. // --------------------------------------------------------------
  2263. // 06.01.08.02 TREND & BOS | Short BOS Search
  2264. // --------------------------------------------------------------
  2265.  
  2266. else if nextTrend == -1 and (executeBosLevelSearch or breakoutPriceLegConsumption)
  2267.  
  2268. // If we are doing bos search after trend break or to consume breakout price leg then this is a special case
  2269. intermediarySearch = breakoutPriceLegConsumption or (bosBreached and executeGlobalTrendChange)
  2270.  
  2271. // Split away legs that were achieved below current global low
  2272. if intermediarySearch
  2273. [_void, splitBar] = findGlobalLow(globalL, globalLB, tLow, barIndex, aLFlt, aLInt)
  2274. // If consuming breakout legs, we must offset by user's input value, because we could have made closer global lows afterwards,
  2275. // but still need to consume only the original leg
  2276. if breakoutPriceLegConsumption
  2277. splitBar := barIndex - bosSameCandleLegConsumptionDelay
  2278. arSplit(aSFlt, aSInt, sFlt, sInt, false, splitBar)
  2279. // If normal search:
  2280. // split away entry added in the last bars in case this candle is a manipulation
  2281. else
  2282. splitOffset = 1 + (checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) ? bosIgnoreManipulationsAmount : 0)
  2283. arSplit(aSFlt, aSInt, sFlt, sInt, false, barIndex - splitOffset)
  2284.  
  2285. // Output where we're checking Short BOS
  2286. if array.get(tfIndependentSettings, 06) and array.get(tfSensitiveSettings, 60)
  2287. debugText("BS\nS" + (breakoutPriceLegConsumption ? "B" : intermediarySearch ? "I" : "") +
  2288. "\nS"+str.tostring(arSizeFlt(sFlt))+"\nO"+str.tostring(arSizeFlt(aSFlt)), true)
  2289.  
  2290. // Remember first and last that passed our conditions
  2291. float firstPassedH = na
  2292. int firstPassedHB = na
  2293. float lastPassedH = na
  2294. int lastPassedHB = na
  2295. bool found = false
  2296. // Iterate until we find suitable LocalH or exhaust all options
  2297. while not found and array.size(aSFlt) > 0
  2298. // Get next leg to examine
  2299. [curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF] = arPop(aSFlt, aSInt)
  2300. found := checkConditionGlobalChange(tfSensitiveSettings, tfIndependentSettings, aBarIndex, curSLocalHB,
  2301. globalHC, globalL, curSLocalH, curSLocalL, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, atr, false)
  2302. if array.get(tfSensitiveSettings, 61)
  2303. printPriceLeg(tfSensitiveSettings, tfIndependentSettings, aBarIndex,
  2304. curSLocalHB, curSLocalH, curSLocalLB, curSLocalL, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, false, found)
  2305. // Record last passed High
  2306. if found
  2307. lastPassedH := curSLocalH
  2308. lastPassedHB := curSLocalHB
  2309. // Record first passed High
  2310. if na(firstPassedH)
  2311. firstPassedH := curSLocalH
  2312. firstPassedHB := curSLocalHB
  2313. // If we can look further, have price legs remaining, and have something to compare to
  2314. aSize = arSizeFlt(aSFlt)
  2315. if bosConsiderNextEnabled and aSize > 0 and firstPassedH
  2316. // If next point is within range - clear found flag to allow while loop to continue
  2317. // Also ensure next point isn't within old BOS in case we're breaching
  2318. float nextL = na
  2319. nextH = arGetFlt(00, aSFlt, aSize - 1)
  2320. nextMaxOC = array.get(aMaxOC, barIndex - arGetInt(00, aSInt, aSize - 1))
  2321. nextDiff = (nextH - globalL) / ((bosConsiderNextRecursive ? lastPassedH : firstPassedH) - globalL) - 1
  2322. nextConsumed = false
  2323. if bosConsiderNextConsumptionEnabled
  2324. nextConsumed := (lastPassedH - nextMaxOC) / (nextH - nextMaxOC) > bosConsiderNextConsumptionThreshold / 100
  2325. if not nextConsumed and nextDiff < bosConsiderNextDistanceThreshold / 100
  2326. and (not bosBreached or checkConditionNewBosBeyondOld(tfSensitiveSettings, nextTrend, nextH, nextMaxOC, bosLH, bosLL, bosSH, bosSL))
  2327. found := false
  2328. // If next point is not within range - break
  2329. else
  2330. break
  2331. // If we still have a localL value here that means we found a suitable local high to use as a new BOS
  2332. if lastPassedH
  2333. // Relocate global high and place BOS on it
  2334. executeBosLevelUpdate := true
  2335. globalH := lastPassedH
  2336. globalHB := lastPassedHB
  2337. globalHC := math.min(curNextHC, globalH)
  2338. // Set classic BOS bar in case we have an unused price leg, and that BOS is outside selected BOS by at least the threshold
  2339. if array.size(aSInt) > 0
  2340. // First we pick the last price leg, which is by definition the highest
  2341. classicB := arGetInt(00, aSInt, 0)
  2342. classicMaxOC = array.get(aMaxOC, barIndex - classicB)
  2343. classicDiff = classicMaxOC / lastPassedH - 1
  2344. // Classic BOS' closer boundary must be away from algorithm-chosen BOS by at least this much, otherwise it's discarded
  2345. if classicDiff <= array.get(tfSensitiveSettings, 01) / 100
  2346. classicB := na
  2347. arClear(aSFlt, aSInt, false, tfSensitiveSettings, tfIndependentSettings, aBarIndex, true)
  2348. arSplit(sFlt, sInt, aSFlt, aSInt)
  2349. if array.size(aSFlt) == 0
  2350. arPush(aSFlt, aSInt)
  2351. // If not doing intermittent search
  2352. if not intermediarySearch
  2353. // Update global low
  2354. globalL := tLow
  2355. globalLB := barIndex
  2356. // Waste long accumulated values
  2357. curLNextClose := na
  2358. curLNextReverse := na
  2359. // If not in bos breached mode the next highest close is reset here
  2360. // Otherwise it can also be reset if we confirm trend change and establish new BOS later
  2361. if not bosBreached
  2362. curNextHC := tMaxOC
  2363.  
  2364. // --------------------------------------------------------------
  2365. // 06.01.09 TREND & BOS | TREND CHANGE
  2366. // --------------------------------------------------------------
  2367.  
  2368. if executeGlobalTrendChange
  2369. // Global trend changed to SHORT
  2370. if globalTrendDirection == 1
  2371. globalTrendDirection := -1
  2372. [g_l, g_lb] = findGlobalLow(globalL, globalLB, tLow, barIndex, aLFlt, aLInt)
  2373. globalL := g_l
  2374. globalLB := g_lb
  2375. // Global trend changed to LONG
  2376. else
  2377. globalTrendDirection := 1
  2378. [g_h, g_hb] = findGlobalHigh(globalH, globalHB, tHigh, barIndex, aSFlt, aSInt)
  2379. globalH := g_h
  2380. globalHB := g_hb
  2381.  
  2382. // --------------------------------------------------------------
  2383. // 06.01.10 BOS LEVEL UPDATE
  2384. // --------------------------------------------------------------
  2385.  
  2386. // First confirm BOS breach if breached, otherwise no BOS Update occurs
  2387. if executeBosLevelUpdate
  2388. if bosBreached
  2389. highLow = globalTrendDirection == 1 ? globalL : globalH
  2390. openClose = globalTrendDirection == 1 ? array.get(aMinOC, barIndex - globalLB) : array.get(aMaxOC, barIndex - globalHB)
  2391. if checkConditionNewBosBeyondOld(tfSensitiveSettings, globalTrendDirection, highLow, openClose, bosLH, bosLL, bosSH, bosSL)
  2392. bosBreached := false
  2393.  
  2394. // --------------------------------------------------------------
  2395. // 06.01.10.01 BOS LEVEL UPDATE | LONG
  2396. // --------------------------------------------------------------
  2397.  
  2398. if globalTrendDirection == 1 and not bosBreached
  2399. // Waste short entries
  2400. arClear(aSFlt, aSInt, false, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2401. arPush(aSFlt, aSInt)
  2402. // Waste accumulated next lowest close
  2403. curNextLC := tMinOC
  2404. // Change BOS
  2405. if true
  2406. bosSB := globalLB
  2407. lookbackBos = barIndex - bosSB
  2408. bosSH := array.get(aMinOC, lookbackBos)
  2409. bosSL := array.get(aLow, lookbackBos)
  2410. // If current candle completely consumed BOS then ignore it
  2411. beginI = tMinOC <= bosSL ? 1 : 0
  2412. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2413. // Look ahead to narrow down the volume level range
  2414. for i = beginI to lookbackBos - 1
  2415. bosSH := math.min(bosSH, array.get(aMinOC, i))
  2416. // Immediately check for testing
  2417. bosTesting := (tLow < bosSH or array.get(aLow, 1) < bosSH)
  2418. // Change CBOS
  2419. if not na(classicB)
  2420. cbosSB := classicB
  2421. lookbackBos = barIndex - cbosSB
  2422. cbosSH := array.get(aMinOC, lookbackBos)
  2423. cbosSL := array.get(aLow, lookbackBos)
  2424. // If current candle completely consumed BOS then ignore it
  2425. beginI = tMinOC <= cbosSL ? 1 : 0
  2426. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2427. // Look ahead to narrow down the volume level range
  2428. for i = beginI to lookbackBos - 1
  2429. cbosSH := math.min(cbosSH, array.get(aMinOC, i))
  2430. bosLH := na
  2431. bosLL := na
  2432. cbosLH := na
  2433. cbosLL := na
  2434.  
  2435. // --------------------------------------------------------------
  2436. // 06.01.10.02 BOS LEVEL UPDATE | SHORT
  2437. // --------------------------------------------------------------
  2438.  
  2439. if globalTrendDirection == -1 and not bosBreached
  2440. // Waste long entries
  2441. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2442. arPush(aLFlt, aLInt)
  2443. // Waste accumulated next highest close
  2444. curNextHC := tMaxOC
  2445. // Change BOS
  2446. if true
  2447. bosLB := globalHB
  2448. lookbackBos = barIndex - bosLB
  2449. bosLL := array.get(aMaxOC, lookbackBos)
  2450. bosLH := array.get(aHigh, lookbackBos)
  2451. // If current candle completely consumed BOS then ignore it
  2452. beginI = tMaxOC >= bosLH ? 1 : 0
  2453. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2454. // Look ahead to narrow down the volume level range
  2455. for i = beginI to lookbackBos - 1
  2456. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2457. // Immediately check for testing
  2458. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2459. // Change CBOS
  2460. if not na(classicB)
  2461. cbosLB := classicB
  2462. lookbackBos = barIndex - cbosLB
  2463. cbosLL := array.get(aMaxOC, lookbackBos)
  2464. cbosLH := array.get(aHigh, lookbackBos)
  2465. // If current candle completely consumed BOS then ignore it
  2466. beginI = tMaxOC >= cbosLH ? 1 : 0
  2467. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2468. // Look ahead to narrow down the volume level range
  2469. for i = beginI to lookbackBos - 1
  2470. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2471. bosSH := na
  2472. bosSL := na
  2473. cbosSH := na
  2474. cbosSL := na
  2475.  
  2476. // --------------------------------------------------------------
  2477. // 06.01.11 TRIM ARRAYS
  2478. // --------------------------------------------------------------
  2479.  
  2480. if not array.get(tfIndependentSettings, 01)
  2481. maxLookback = math.max(2, barIndex - math.min(bosLB, bosSB)) + 1
  2482. while array.size(aHigh) > maxLookback
  2483. array.pop(aHigh)
  2484. array.pop(aLow)
  2485. array.pop(aOpen)
  2486. array.pop(aClose)
  2487. array.pop(aMaxOC)
  2488. array.pop(aMinOC)
  2489. array.pop(aPivotH)
  2490. array.pop(aPivotL)
  2491.  
  2492. // --------------------------------------------------------------
  2493. // 06.01.12 LAST BAR CLEANING
  2494. // --------------------------------------------------------------
  2495.  
  2496. // Cleaning house on last bar
  2497. if finalize
  2498. // Clear arrays so we can see their contents
  2499. arInit(aLFlt, aLInt, aSFlt, aSInt, tfSensitiveSettings, tfIndependentSettings, aBarIndex, true)
  2500.  
  2501. // --------------------------------------------------------------
  2502. // 06.01.13 FINISHED, PACK DATA
  2503. // --------------------------------------------------------------
  2504.  
  2505. arStateSet(aStateInt, aStateBool, globalTrendDirection, bosTesting, bosBreached, bosBreachCandles)
  2506. arValuesSet(aValuesFlt, aValuesInt, priorH, priorHB, priorL, priorLB, globalH, globalHB, globalL, globalLB,
  2507. globalHC, globalLC, curNextHC, curNextLC,
  2508. curLNextClose, curLNextReverse, curSNextClose, curSNextReverse,
  2509. bosLH, bosLL, bosLB, bosSH, bosSL, bosSB,
  2510. cbosLH, cbosLL, cbosLB, cbosSH, cbosSL, cbosSB)
  2511.  
  2512. // --------------------------------------------------------------
  2513. // 06.01 END TREND & BOS DETERMINATION ALGORITHM
  2514. // --------------------------------------------------------------
  2515. if globalTrendDirection != 0
  2516. if globalTrendDirection == -1 and not bosBreached
  2517. // Waste long entries
  2518. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2519. arPush(aLFlt, aLInt)
  2520. // Waste accumulated next highest close
  2521. curNextHC := tMaxOC
  2522. // Change BOS
  2523. if true
  2524. bosLB := globalHB
  2525. lookbackBos = barIndex - bosLB
  2526. bosLL := array.get(aMaxOC, lookbackBos)
  2527. bosLH := array.get(aHigh, lookbackBos)
  2528. // If current candle completely consumed BOS then ignore it
  2529. beginI = tMaxOC >= bosLH ? 1 : 0
  2530. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2531. // Look ahead to narrow down the volume level range
  2532. for i = beginI to lookbackBos - 1
  2533. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2534. // Immediately check for testing
  2535. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2536. // Change CBOS
  2537. if not na(classicB)
  2538. cbosLB := classicB
  2539. lookbackBos = barIndex - cbosLB
  2540. cbosLL := array.get(aMaxOC, lookbackBos)
  2541. cbosLH := array.get(aHigh, lookbackBos)
  2542. // If current candle completely consumed BOS then ignore it
  2543. beginI = tMaxOC >= cbosLH ? 1 : 0
  2544. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2545. // Look ahead to narrow down the volume level range
  2546. for i = beginI to lookbackBos - 1
  2547. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2548. bosSH := na
  2549. bosSL := na
  2550. cbosSH := na
  2551. cbosSL := na
  2552.  
  2553. if globalTrendDirection != 0
  2554. if globalTrendDirection == -1 and not bosBreached
  2555. // Waste long entries
  2556. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2557. arPush(aLFlt, aLInt)
  2558. // Waste accumulated next highest close
  2559. curNextHC := tMaxOC
  2560. // Change BOS
  2561. if true
  2562. bosLB := globalHB
  2563. lookbackBos = barIndex - bosLB
  2564. bosLL := array.get(aMaxOC, lookbackBos)
  2565. bosLH := array.get(aHigh, lookbackBos)
  2566. // If current candle completely consumed BOS then ignore it
  2567. beginI = tMaxOC >= bosLH ? 1 : 0
  2568. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2569. // Look ahead to narrow down the volume level range
  2570. for i = beginI to lookbackBos - 1
  2571. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2572. // Immediately check for testing
  2573. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2574. // Change CBOS
  2575. if not na(classicB)
  2576. cbosLB := classicB
  2577. lookbackBos = barIndex - cbosLB
  2578. cbosLL := array.get(aMaxOC, lookbackBos)
  2579. cbosLH := array.get(aHigh, lookbackBos)
  2580. // If current candle completely consumed BOS then ignore it
  2581. beginI = tMaxOC >= cbosLH ? 1 : 0
  2582. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2583. // Look ahead to narrow down the volume level range
  2584. for i = beginI to lookbackBos - 1
  2585. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2586. bosSH := na
  2587. bosSL := na
  2588. cbosSH := na
  2589. cbosSL := na
  2590.  
  2591. if globalTrendDirection != 0
  2592. if globalTrendDirection == -1 and not bosBreached
  2593. // Waste long entries
  2594. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2595. arPush(aLFlt, aLInt)
  2596. // Waste accumulated next highest close
  2597. curNextHC := tMaxOC
  2598. // Change BOS
  2599. if true
  2600. bosLB := globalHB
  2601. lookbackBos = barIndex - bosLB
  2602. bosLL := array.get(aMaxOC, lookbackBos)
  2603. bosLH := array.get(aHigh, lookbackBos)
  2604. // If current candle completely consumed BOS then ignore it
  2605. beginI = tMaxOC >= bosLH ? 1 : 0
  2606. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2607. // Look ahead to narrow down the volume level range
  2608. for i = beginI to lookbackBos - 1
  2609. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2610. // Immediately check for testing
  2611. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2612. // Change CBOS
  2613. if not na(classicB)
  2614. cbosLB := classicB
  2615. lookbackBos = barIndex - cbosLB
  2616. cbosLL := array.get(aMaxOC, lookbackBos)
  2617. cbosLH := array.get(aHigh, lookbackBos)
  2618. // If current candle completely consumed BOS then ignore it
  2619. beginI = tMaxOC >= cbosLH ? 1 : 0
  2620. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2621. // Look ahead to narrow down the volume level range
  2622. for i = beginI to lookbackBos - 1
  2623. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2624. bosSH := na
  2625. bosSL := na
  2626. cbosSH := na
  2627. cbosSL := na
  2628.  
  2629. // --------------------------------------------------------------
  2630. // 06.02 ALGORITHM EXECUTION
  2631. // --------------------------------------------------------------
  2632.  
  2633. // This condition only processes selected bars (for debug purposes) an doesn't try to acquire any longer
  2634. inRange = not (bar_index < DBG_FirstBar or bar_index > DBG_LastBar)
  2635. if inRange
  2636. if initialCtfTrendDirection != 0
  2637. array.set(carStateInt, 0, initialCtfTrendDirection)
  2638. initialCtfTrendDirection := 0
  2639. algoTrendAndBosTick(bar_index, cBarIndex,
  2640. carHigh, carLow, carOpen, carClose, carMaxOC, carMinOC, carPivotH, carPivotL,
  2641. carLFlt, carLInt, carSFlt, carSInt,
  2642. ctfSensitiveSettings, tfIndependentSettings, carStateInt, carStateBool, carValuesFlt, carValuesInt,
  2643. barstate.islastconfirmedhistory or bar_index == DBG_LastBar)
  2644. // HTF Trend, Called every tick there was new data, and once to finalize
  2645. if inRange and calculateHtfTrend and htfBarIndex >=0 and (newHtfDataThisTick or barstate.islastconfirmedhistory)
  2646. if initialHtfTrendDirection != 0
  2647. array.set(harStateInt, 0, initialHtfTrendDirection)
  2648. initialHtfTrendDirection := 0
  2649. algoTrendAndBosTick(htfBarIndex, harBarIndex,
  2650. harHigh, harLow, harOpen, harClose, harMaxOC, harMinOC, harPivotH, harPivotL,
  2651. harLFlt, harLInt, harSFlt, harSInt,
  2652. htfSensitiveSettings, tfIndependentSettings, harStateInt, harStateBool, harValuesFlt, harValuesInt,
  2653. barstate.islastconfirmedhistory or bar_index == DBG_LastBar)
  2654. // Discard trend on last bar if processing select bars
  2655. if bar_index == DBG_LastBar + 1
  2656. array.set(carStateInt, 0, 0)
  2657. array.set(harStateInt, 0, 0)
  2658.  
  2659. // --------------------------------------------------------------
  2660. // 06.03 POST-PROCESSING
  2661. // --------------------------------------------------------------
  2662.  
  2663. // --------------------------------------------------------------
  2664. // 06.03.01 GET DATA
  2665. // --------------------------------------------------------------
  2666.  
  2667. [globalTrendDirection, bosTesting, bosBreached, bosBreachCandles] = arStateGet(carStateInt, carStateBool)
  2668. [priorH, priorHB, priorL, priorLB, globalH, globalHB, globalL, globalLB,
  2669. globalHC, globalLC, curNextHC, curNextLC,
  2670. curLNextClose, curLNextReverse, curSNextClose, curSNextReverse,
  2671. bosLH, bosLL, bosLB, bosSH, bosSL, bosSB,
  2672. cbosLH, cbosLL, cbosLB, cbosSH, cbosSL, cbosSB] = arValuesGet(carValuesFlt, carValuesInt)
  2673.  
  2674. [htfglobalTrendDirection, htfbosTesting, htfbosBreached, htfbosBreachCandles] = arStateGet(harStateInt, harStateBool)
  2675. [htfpriorH, _htfpriorHB, htfpriorL, _htfpriorLB, htfglobalH, _htfglobalHB, htfglobalL, _htfglobalLB,
  2676. _htfGlobalHC, _htfGlobalLC, _htfCurNextHC, _htfCurNextLC,
  2677. htfcurLNextClose, htfcurLNextReverse, htfcurSNextClose, htfcurSNextReverse,
  2678. htfbosLH, htfbosLL, _htfbosLB, htfbosSH, htfbosSL, _htfbosSB,
  2679. htfcbosLH, htfcbosLL, _htfcbosLB, htfcbosSH, htfcbosSL, _htfcbosSB] = arValuesGet(harValuesFlt, harValuesInt)
  2680.  
  2681. htfglobalHB = array.get(harBarIndex, _htfglobalHB)
  2682. htfglobalLB = array.get(harBarIndex, _htfglobalLB)
  2683. htfbosLB = array.get(harBarIndex, _htfbosLB)
  2684. htfbosSB = array.get(harBarIndex, _htfbosSB)
  2685. htfcbosLB = array.get(harBarIndex, _htfcbosLB)
  2686. htfcbosSB = array.get(harBarIndex, _htfcbosSB)
  2687.  
  2688. priorTrend = globalTrendDirection == 0 ? 0 : bosBreached ? -globalTrendDirection : globalTrendDirection
  2689. htfpriorTrend = htfglobalTrendDirection == 0 ? 0 : htfbosBreached ? -htfglobalTrendDirection : htfglobalTrendDirection
  2690.  
  2691. // --------------------------------------------------------------
  2692. // 06.03.02 TRIGGERS, ALERTS, EVENTS
  2693. // --------------------------------------------------------------
  2694.  
  2695. eventTrendBreak = bosBreached == true and bosBreached[1] == false
  2696. eventTrendReversalConfirmed = bosBreached == false and bosBreached[1] == true and globalTrendDirection == globalTrendDirection[1]
  2697. eventTrendResume = bosBreached == false and bosBreached[1] == true and globalTrendDirection != globalTrendDirection[1]
  2698. htfeventTrendBreak = htfbosBreached == true and htfbosBreached[1] == false
  2699. htfeventTrendReversalConfirmed = htfbosBreached == false and htfbosBreached[1] == true and htfglobalTrendDirection == htfglobalTrendDirection[1]
  2700. htfeventTrendResume = htfbosBreached == false and htfbosBreached[1] == true and htfglobalTrendDirection != htfglobalTrendDirection[1]
  2701.  
  2702. // --------------------------------------------------------------
  2703. // 07 OUTPUT
  2704. // --------------------------------------------------------------
  2705.  
  2706. // --------------------------------------------------------------
  2707. // 07.01 OUTPUT: DEBUG OUTPUT PLOTS
  2708. // --------------------------------------------------------------
  2709.  
  2710. float outhtfOpen = na
  2711. float outhtfHigh = na
  2712. float outhtfLow = na
  2713. float outhtfClose = na
  2714. float outhtfPivotHigh = na
  2715. float outhtfPivotLow = na
  2716. if array.size(harHigh) > 0 and newHtfDataThisTick
  2717. //debugText(bar_index)
  2718. if DBG_ShowHTFDebugPlots and DBG_DebugEnabled
  2719. outhtfOpen := array.get(harOpen, 0)
  2720. outhtfHigh := array.get(harHigh, 0)
  2721. outhtfLow := array.get(harLow, 0)
  2722. outhtfClose := array.get(harClose, 0)
  2723. if DBG_ShowHTFDebugPlots and DBG_DebugEnabled or showPPEnabled and showPPHtf
  2724. outhtfPivotHigh := na(array.get(harPivotH, 0)) ? array.get(harPivotH, 1) : na
  2725. outhtfPivotLow := na(array.get(harPivotL, 0)) ? array.get(harPivotL, 1) : na
  2726. // HTF Plots
  2727. plot(outhtfOpen, "HTF Open", color.purple, 2, offset = -htfOffset)
  2728. plot(outhtfHigh, "HTF High", color.fuchsia, 2, offset = -htfOffset)
  2729. plot(outhtfLow, "HTF Low", color.fuchsia, 2, offset = -htfOffset)
  2730. plot(outhtfClose, "HTF Close", color.purple, 2, offset = -htfOffset)
  2731. plotshape(DBG_ShowHTFDebugPlots ? outhtfPivotHigh : na, "HTF Pivot H", shape.xcross, location.absolute, color.fuchsia,
  2732. -htfOffset - htfSidewaysMultiplier, size = size.small)
  2733. plotshape(DBG_ShowHTFDebugPlots ? outhtfPivotLow : na, "HTF Pivot L", shape.xcross, location.absolute, color.fuchsia,
  2734. -htfOffset - htfSidewaysMultiplier, size = size.small)
  2735. // HLC / NHLC
  2736. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots and globalTrendDirection == 1 ? globalHC : na, "globalHC", color.red, linewidth = 3)
  2737. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots and globalTrendDirection == -1 ? globalLC : na, "globalLC", color.green, linewidth = 3)
  2738. // Maybe this was better? But uses x2 plots since color not const
  2739. // plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? globalHC : na, "globalHC", globalTrendDirection == 1 ? na : color.red, linewidth = 3)
  2740. // plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? globalLC : na, "globalLC", globalTrendDirection == -1 ? na : color.green, linewidth = 3)
  2741. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? curNextHC : na, "curNextHC", color.new(color.red, 60), linewidth = 2)
  2742. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? curNextLC : na, "curNextLC", color.new(color.green, 60), linewidth = 2)
  2743.  
  2744. // --------------------------------------------------------------
  2745. // 07.02 OUTPUT: PIVOT POINTS
  2746. // --------------------------------------------------------------
  2747.  
  2748. plotshape(showPPEnabled and showPPCtf and not cPivotH ? cPivotH[1] : na, 'CTF High', shape.triangledown,
  2749. location.abovebar, color.new(color.green,50), -ctfOffset - 1, "H", color.new(color.green,50))
  2750. plotshape(showPPEnabled and showPPCtf and not cPivotL ? cPivotL[1] : na, 'CTF Low', shape.triangleup,
  2751. location.belowbar, color.new(color.red,50), -ctfOffset - 1, "L", color.new(color.red,50))
  2752.  
  2753. plotshape(showPPEnabled and showPPHtf ? outhtfPivotHigh : na, 'HTF High', shape.triangledown,
  2754. location.abovebar, color.new(color.green,30), -htfOffset - htfSidewaysMultiplier, '[H]', color.new(color.green,30))
  2755. plotshape(showPPEnabled and showPPHtf ? outhtfPivotLow : na, 'HTF Low', shape.triangleup,
  2756. location.belowbar, color.new(color.red,30), -htfOffset - htfSidewaysMultiplier, '[L]', color.new(color.red,30))
  2757.  
  2758. // --------------------------------------------------------------
  2759. // 07.03 OUTPUT: BOS
  2760. // --------------------------------------------------------------
  2761.  
  2762. // --------------------------------------------------------------
  2763. // 07.03.01 OUTPUT: CTF BOS
  2764. // --------------------------------------------------------------
  2765.  
  2766. // --------------------------------------------------------------
  2767. // 07.03.01.01 OUTPUT: CTF BOS | LONG
  2768. // --------------------------------------------------------------
  2769.  
  2770. // CTF Long BOS Colors
  2771. colorBosLongLineMainNormal = color.new(colorBosLongMain, ctfBosT3)
  2772. colorBosLongLineMainTesting = color.new(colorBosLongMain, ctfBosT4)
  2773. colorBosLongLineMainBreached = color.new(colorBosLongMain, ctfBosT5)
  2774. colorBosLongLineAuxNormal = color.new(colorBosLongAux, ctfBosT2)
  2775. colorBosLongLineAuxTesting = color.new(colorBosLongAux, ctfBosT3)
  2776. colorBosLongLineAuxBreached = color.new(colorBosLongMain, ctfBosT3)
  2777. colorBosLongBgNormal = color.new(colorBosLongAux, ctfBosT1)
  2778. colorBosLongBgTesting = color.new(colorBosLongAux, ctfBosT2)
  2779. colorBosLongBgBreached = color.new(colorBosLongMain, ctfBosT2)
  2780.  
  2781. // CTF Long BOS Draw
  2782. drawLongBos = showBosEnabled and priorTrend == -1
  2783. longBosUnchanged = bosLH == bosLH[1] and bosLL == bosLL[1]
  2784. drawLongBosRegular = drawLongBos and longBosUnchanged
  2785. drawLongBosOrigin = drawLongBos and not longBosUnchanged
  2786. colorBosLongLineMain = not drawLongBosRegular or not showBosCtfMainLine ? na :
  2787. bosTesting ? colorBosLongLineMainTesting : bosBreached ? colorBosLongLineMainBreached : colorBosLongLineMainNormal
  2788. colorBosLongLineAux = not drawLongBosRegular or not showBosCtfAuxLine? na :
  2789. bosTesting ? colorBosLongLineAuxTesting : bosBreached ? colorBosLongLineAuxBreached : colorBosLongLineAuxNormal
  2790. colorBosLongFill = not drawLongBosRegular or not showBosCtfFill ? na :
  2791. bosTesting ? colorBosLongBgTesting : bosBreached ? colorBosLongBgBreached : colorBosLongBgNormal
  2792. if drawLongBosOrigin and bosLH
  2793. if showBosCtfOriginMainLine
  2794. line.new(bosLB - ctfOffset, bosLH, bar_index - ctfOffset, bosLH, color = colorBosLongLineMainNormal, width = showBosCtfWidthMain)
  2795. if showBosCtfOriginAuxLine
  2796. line.new(bosLB - ctfOffset, bosLL, bar_index - ctfOffset, bosLL, color = colorBosLongLineAuxNormal, width = showBosCtfWidthAux)
  2797. if showBosCtfOriginFill
  2798. box.new(bosLB - ctfOffset, bosLH, bar_index - ctfOffset, bosLL, na, bgcolor = colorBosLongBgNormal)
  2799.  
  2800. // --------------------------------------------------------------
  2801. // 07.03.01.02 OUTPUT: CTF BOS | SHORT
  2802. // --------------------------------------------------------------
  2803.  
  2804. // CTF Short BOS Colors
  2805. colorBosShortLineMainNormal = color.new(colorBosShortMain, ctfBosT3)
  2806. colorBosShortLineMainTesting = color.new(colorBosShortMain, ctfBosT4)
  2807. colorBosShortLineMainBreached = color.new(colorBosShortMain, ctfBosT5)
  2808. colorBosShortLineAuxNormal = color.new(colorBosShortAux, ctfBosT2)
  2809. colorBosShortLineAuxTesting = color.new(colorBosShortAux, ctfBosT3)
  2810. colorBosShortLineAuxBreached = color.new(colorBosShortMain, ctfBosT3)
  2811. colorBosShortBgNormal = color.new(colorBosShortAux, ctfBosT1)
  2812. colorBosShortBgTesting = color.new(colorBosShortAux, ctfBosT2)
  2813. colorBosShortBgBreached = color.new(colorBosShortMain, ctfBosT2)
  2814.  
  2815. // CTF Short BOS Draw
  2816. drawShortBos = showBosEnabled and priorTrend == 1
  2817. shortBosUnchanged = bosSH == bosSH[1] and bosSL == bosSL[1]
  2818. drawShortBosRegular = drawShortBos and shortBosUnchanged
  2819. drawShortBosOrigin = drawShortBos and not shortBosUnchanged
  2820. colorBosShortLineAux = not drawShortBosRegular or not showBosCtfAuxLine? na :
  2821. bosTesting ? colorBosShortLineAuxTesting : bosBreached ? colorBosShortLineAuxBreached : colorBosShortLineAuxNormal
  2822. colorBosShortLineMain = not drawShortBosRegular or not showBosCtfMainLine? na :
  2823. bosTesting ? colorBosShortLineMainTesting : bosBreached ? colorBosShortLineMainBreached : colorBosShortLineMainNormal
  2824. colorBosShortFill = not drawShortBosRegular or not showBosCtfFill? na :
  2825. bosTesting ? colorBosShortBgTesting : bosBreached ? colorBosShortBgBreached : colorBosShortBgNormal
  2826. if drawShortBosOrigin and bosSH
  2827. if showBosCtfOriginAuxLine
  2828. line.new(bosSB - ctfOffset, bosSH, bar_index - ctfOffset, bosSH, color = colorBosShortLineAuxNormal, width = showBosCtfWidthAux)
  2829. if showBosCtfOriginMainLine
  2830. line.new(bosSB - ctfOffset, bosSL, bar_index - ctfOffset, bosSL, color = colorBosShortLineMainNormal, width = showBosCtfWidthMain)
  2831. if showBosCtfOriginFill
  2832. box.new(bosSB - ctfOffset, bosSH, bar_index - ctfOffset, bosSL, na, bgcolor = colorBosShortBgNormal)
  2833.  
  2834. // --------------------------------------------------------------
  2835. // 07.03.01.03 OUTPUT: CTF BOS | PLOT
  2836. // --------------------------------------------------------------
  2837.  
  2838. ctfblh = plot(priorTrend == 1 ? bosSL : bosLH, "CTF BOS Main", priorTrend == 1 ? colorBosShortLineMain : colorBosLongLineMain, showBosCtfWidthMain,
  2839. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2840. ctfbll = plot(priorTrend == 1 ? bosSH : bosLL, "CTF BOS Aux", priorTrend == 1 ? colorBosShortLineAux : colorBosLongLineAux, showBosCtfWidthAux,
  2841. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2842. fill(ctfblh, ctfbll, priorTrend == 1 ? colorBosShortFill : colorBosLongFill , "CTF BOS Fill", editable = false)
  2843.  
  2844. // --------------------------------------------------------------
  2845. // 07.03.02 OUTPUT: CTF CLASSIC BOS
  2846. // --------------------------------------------------------------
  2847.  
  2848. // --------------------------------------------------------------
  2849. // 07.03.02.01 OUTPUT: CTF CLASSIC BOS | LONG
  2850. // --------------------------------------------------------------
  2851.  
  2852. // CTF Long Classic BOS Colors
  2853. colorCBosLongLineMainNormal = color.new(colorCBosLongMain, ctfCBosT3)
  2854. colorCBosLongLineAuxNormal = color.new(colorCBosLongAux, ctfCBosT2)
  2855. colorCBosLongBgNormal = color.new(colorCBosLongAux, ctfCBosT1)
  2856.  
  2857. // CTF Long Classic BOS Draw
  2858. var float hiddenLH = na
  2859. if showCBosHideCollision and cbosLH == htfbosLH
  2860. hiddenLH := htfbosLH
  2861. drawLongCBos = showBosEnabled and showCBosEnabled and priorTrend == -1 and (na(hiddenLH) or cbosLH != hiddenLH)
  2862. longCBosUnchanged = cbosLH == cbosLH[1] and cbosLL == cbosLL[1]
  2863. drawLongCBosRegular = drawLongCBos and longCBosUnchanged
  2864. drawLongCBosOrigin = drawLongCBos and not longCBosUnchanged
  2865. colorCBosLongLineMain = not drawLongCBosRegular or not showCBosCtfAuxLine ? na : colorCBosLongLineMainNormal
  2866. colorCBosLongLineAux = not drawLongCBosRegular or not showCBosCtfAuxLine ? na : colorCBosLongLineAuxNormal
  2867. colorCBosLongFill = not drawLongCBosRegular or not showCBosCtfFill ? na : colorCBosLongBgNormal
  2868. if drawLongCBosOrigin and cbosLH
  2869. if showCBosCtfOriginMainLine
  2870. line.new(cbosLB - ctfOffset, cbosLH, bar_index - ctfOffset, cbosLH, color = colorCBosLongLineMainNormal, width = showCBosCtfWidthMain)
  2871. if showCBosCtfOriginAuxLine
  2872. line.new(cbosLB - ctfOffset, cbosLL, bar_index - ctfOffset, cbosLL, color = colorCBosLongLineAuxNormal, width = showCBosCtfWidthAux)
  2873. if showCBosCtfOriginFill
  2874. box.new(cbosLB - ctfOffset, cbosLH, bar_index - ctfOffset, cbosLL, na, bgcolor = colorCBosLongBgNormal)
  2875.  
  2876. // --------------------------------------------------------------
  2877. // 07.03.02.02 OUTPUT: CTF CLASSIC BOS | SHORT
  2878. // --------------------------------------------------------------
  2879.  
  2880. // CTF Short Classic BOS Colors
  2881. colorCBosShortLineMainNormal = color.new(colorCBosShortMain, ctfCBosT3)
  2882. colorCBosShortLineAuxNormal = color.new(colorCBosShortAux, ctfCBosT2)
  2883. colorCBosShortBgNormal = color.new(colorCBosShortAux, ctfCBosT1)
  2884.  
  2885. // CTF Short Classic BOS Draw
  2886. var float hiddenSL = na
  2887. if showCBosHideCollision and cbosSL == htfbosSL
  2888. hiddenSL := htfbosSL
  2889. drawShortCBos = showBosEnabled and showCBosEnabled and priorTrend == 1 and (na(hiddenSL) or cbosSL != hiddenSL)
  2890. shortCBosUnchanged = cbosSH == cbosSH[1] and cbosSL == cbosSL[1]
  2891. drawShortCBosRegular = drawShortCBos and shortCBosUnchanged
  2892. drawShortCBosOrigin = drawShortCBos and not shortCBosUnchanged
  2893. colorCBosShortLineAux = not drawShortCBosRegular or not showCBosCtfAuxLine ? na : colorCBosShortLineAuxNormal
  2894. colorCBosShortLineMain = not drawShortCBosRegular or not showCBosCtfAuxLine ? na : colorCBosShortLineMainNormal
  2895. colorCBosShortFill = not drawShortCBosRegular or not showCBosCtfFill? na : colorCBosShortBgNormal
  2896. if drawShortCBosOrigin and cbosSH
  2897. if showCBosCtfOriginAuxLine
  2898. line.new(cbosSB - ctfOffset, cbosSH, bar_index - ctfOffset, cbosSH, color = colorCBosShortLineAuxNormal, width = showCBosCtfWidthAux)
  2899. if showCBosCtfOriginMainLine
  2900. line.new(cbosSB - ctfOffset, cbosSL, bar_index - ctfOffset, cbosSL, color = colorCBosShortLineMainNormal, width = showCBosCtfWidthMain)
  2901. if showCBosCtfOriginFill
  2902. box.new(cbosSB - ctfOffset, cbosSH, bar_index - ctfOffset, cbosSL, na, bgcolor = colorCBosShortBgNormal)
  2903.  
  2904. // --------------------------------------------------------------
  2905. // 07.03.02.03 OUTPUT: CTF CLASSIC BOS | PLOT
  2906. // --------------------------------------------------------------
  2907.  
  2908. ctfcblh = plot(priorTrend == 1 ? cbosSL : cbosLH, "CTF CBOS Main", priorTrend == 1 ? colorCBosShortLineMain : colorCBosLongLineMain, showCBosCtfWidthMain,
  2909. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2910. ctfcbll = plot(priorTrend == 1 ? cbosSH : cbosLL, "CTF CBOS Aux", priorTrend == 1 ? colorCBosShortLineAux : colorCBosLongLineAux, showCBosCtfWidthAux,
  2911. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2912. fill(ctfcblh, ctfcbll, priorTrend == 1 ? colorCBosShortFill : colorCBosLongFill, "CTF CBOS Fill", editable = false)
  2913.  
  2914. // --------------------------------------------------------------
  2915. // 07.03.03 OUTPUT: HTF BOS
  2916. // --------------------------------------------------------------
  2917.  
  2918. // --------------------------------------------------------------
  2919. // 07.03.03.01 OUTPUT: HTF BOS | LONG
  2920. // --------------------------------------------------------------
  2921.  
  2922. // HTF Long BOS Colors
  2923. htfcolorBosLongLineMainNormal = color.new(htfcolorBosLongMain, htfBosT3)
  2924. htfcolorBosLongLineMainTesting = color.new(htfcolorBosLongMain, htfBosT4)
  2925. htfcolorBosLongLineMainBreached = color.new(htfcolorBosLongMain, htfBosT5)
  2926. htfcolorBosLongLineAuxNormal = color.new(htfcolorBosLongAux, htfBosT2)
  2927. htfcolorBosLongLineAuxTesting = color.new(htfcolorBosLongAux, htfBosT3)
  2928. htfcolorBosLongLineAuxBreached = color.new(htfcolorBosLongMain, htfBosT2)
  2929. htfcolorBosLongBgNormal = color.new(htfcolorBosLongAux, htfBosT1)
  2930. htfcolorBosLongBgTesting = color.new(htfcolorBosLongAux, htfBosT2)
  2931. htfcolorBosLongBgBreached = color.new(htfcolorBosLongMain, htfBosT2)
  2932.  
  2933. // HTF Long BOS Draw
  2934. htfdrawLongBos = showBosEnabled and htfpriorTrend == -1
  2935. htflongBosUnchanged = htfbosLH == htfbosLH[1] and htfbosLL == htfbosLL[1]
  2936. htfdrawLongBosRegular = htfdrawLongBos and htflongBosUnchanged
  2937. htfdrawLongBosOrigin = htfdrawLongBos and not htflongBosUnchanged
  2938. htfcolorBosLongLineMain = not htfdrawLongBosRegular or not showBosHtfMainLine ? na :
  2939. htfbosTesting ? htfcolorBosLongLineMainTesting : htfbosBreached ? htfcolorBosLongLineMainBreached : htfcolorBosLongLineMainNormal
  2940. htfcolorBosLongLineAux = not htfdrawLongBosRegular or not showBosHtfAuxLine ? na :
  2941. htfbosTesting ? htfcolorBosLongLineAuxTesting : htfbosBreached ? htfcolorBosLongLineAuxBreached : htfcolorBosLongLineAuxNormal
  2942. htfcolorBosLongFill = not htfdrawLongBosRegular or not showBosHtfFill ? na :
  2943. htfbosTesting ? htfcolorBosLongBgTesting : htfbosBreached ? htfcolorBosLongBgBreached : htfcolorBosLongBgNormal
  2944. if htfdrawLongBosOrigin and htfbosLH
  2945. if showBosHtfOriginMainLine
  2946. line.new(htfbosLB - htfOffset, htfbosLH, bar_index - htfOffset, htfbosLH, color = htfcolorBosLongLineMainNormal, width = showBosHtfWidthMain)
  2947. if showBosHtfOriginAuxLine
  2948. line.new(htfbosLB - htfOffset, htfbosLL, bar_index - htfOffset, htfbosLL, color = htfcolorBosLongLineAuxNormal, width = showBosHtfWidthAux)
  2949. if showBosHtfOriginFill
  2950. box.new(htfbosLB - htfOffset, htfbosLH, bar_index - htfOffset, htfbosLL, na, bgcolor = htfcolorBosLongBgNormal)
  2951.  
  2952. // --------------------------------------------------------------
  2953. // 07.03.03.02 OUTPUT: HTF BOS | SHORT
  2954. // --------------------------------------------------------------
  2955.  
  2956. // HTF Short BOS Colors
  2957. htfcolorBosShortLineMainNormal = color.new(htfcolorBosShortMain, htfBosT3)
  2958. htfcolorBosShortLineMainTesting = color.new(htfcolorBosShortMain, htfBosT4)
  2959. htfcolorBosShortLineMainBreached = color.new(htfcolorBosShortMain, htfBosT5)
  2960. htfcolorBosShortLineAuxNormal = color.new(htfcolorBosShortAux, htfBosT2)
  2961. htfcolorBosShortLineAuxTesting = color.new(htfcolorBosShortAux, htfBosT3)
  2962. htfcolorBosShortLineAuxBreached = color.new(htfcolorBosShortMain, htfBosT3)
  2963. htfcolorBosShortBgNormal = color.new(htfcolorBosShortAux, htfBosT1)
  2964. htfcolorBosShortBgTesting = color.new(htfcolorBosShortAux, htfBosT2)
  2965. htfcolorBosShortBgBreached = color.new(htfcolorBosShortMain, htfBosT2)
  2966.  
  2967. // HTF Short BOS Draw
  2968. htfdrawShortBos = showBosEnabled and htfpriorTrend == 1
  2969. htfshortBosUnchanged = htfbosSH == htfbosSH[1] and htfbosSL == htfbosSL[1]
  2970. htfdrawShortBosRegular = htfdrawShortBos and htfshortBosUnchanged
  2971. htfdrawShortBosOrigin = htfdrawShortBos and not htfshortBosUnchanged
  2972. htfcolorBosShortLineAux = not htfdrawShortBosRegular or not showBosHtfAuxLine ? na :
  2973. htfbosTesting ? htfcolorBosShortLineAuxTesting : htfbosBreached ? htfcolorBosShortLineAuxBreached : htfcolorBosShortLineAuxNormal
  2974. htfcolorBosShortLineMain = not htfdrawShortBosRegular or not showBosHtfMainLine? na :
  2975. htfbosTesting ? htfcolorBosShortLineMainTesting : htfbosBreached ? htfcolorBosShortLineMainBreached : htfcolorBosShortLineMainNormal
  2976. htfcolorBosShortFill = not htfdrawShortBosRegular or not showBosHtfFill ? na :
  2977. htfbosTesting ? htfcolorBosShortBgTesting : htfbosBreached ? htfcolorBosShortBgBreached : htfcolorBosShortBgNormal
  2978. if htfdrawShortBosOrigin and htfbosSH
  2979. if showBosHtfOriginAuxLine
  2980. line.new(htfbosSB - htfOffset, htfbosSH, bar_index - htfOffset, htfbosSH, color = htfcolorBosShortLineAuxNormal, width = showBosHtfWidthAux)
  2981. if showBosHtfOriginMainLine
  2982. line.new(htfbosSB - htfOffset, htfbosSL, bar_index - htfOffset, htfbosSL, color = htfcolorBosShortLineMainNormal, width = showBosHtfWidthMain)
  2983. if showBosHtfOriginFill
  2984. box.new(htfbosSB - htfOffset, htfbosSH, bar_index - htfOffset, htfbosSL, na, bgcolor = htfcolorBosShortBgNormal)
  2985.  
  2986. // --------------------------------------------------------------
  2987. // 07.03.03.03 OUTPUT: HTF BOS | PLOT
  2988. // --------------------------------------------------------------
  2989.  
  2990. htfblh = plot(htfpriorTrend == 1 ? htfbosSL : htfbosLH, "HTF BOS Main", htfpriorTrend == 1 ? htfcolorBosShortLineMain : htfcolorBosLongLineMain, showBosHtfWidthMain,
  2991. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2992. htfbll = plot(htfpriorTrend == 1 ? htfbosSH : htfbosLL, "HTF BOS Aux", htfpriorTrend == 1 ? htfcolorBosShortLineAux : htfcolorBosLongLineAux, showBosHtfWidthAux,
  2993. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2994. fill(htfblh, htfbll, htfpriorTrend == 1 ? htfcolorBosShortFill : htfcolorBosLongFill , "HTF BOS Fill", editable = false)
  2995.  
  2996. // --------------------------------------------------------------
  2997. // 07.03.04 OUTPUT: HTF CLASSIC BOS
  2998. // --------------------------------------------------------------
  2999.  
  3000. // --------------------------------------------------------------
  3001. // 07.03.04.01 OUTPUT: HTF CLASSIC BOS | LONG
  3002. // --------------------------------------------------------------
  3003.  
  3004. // HTF Long Classic BOS Colors
  3005. htfcolorCBosLongLineMainNormal = color.new(htfcolorCBosLongMain, htfCBosT3)
  3006. htfcolorCBosLongLineAuxNormal = color.new(htfcolorCBosLongAux, htfCBosT2)
  3007. htfcolorCBosLongBgNormal = color.new(htfcolorCBosLongAux, htfCBosT1)
  3008.  
  3009. // HTF Long Classic BOS Draw
  3010. htfdrawLongCBos = showBosEnabled and showCBosEnabled and htfpriorTrend == -1
  3011. htflongCBosUnchanged = htfcbosLH == htfcbosLH[1] and htfcbosLL == htfcbosLL[1]
  3012. htfdrawLongCBosRegular = htfdrawLongCBos and htflongCBosUnchanged
  3013. htfdrawLongCBosOrigin = htfdrawLongCBos and not htflongCBosUnchanged
  3014. htfcolorCBosLongLineMain = not htfdrawLongCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosLongLineMainNormal
  3015. htfcolorCBosLongLineAux = not htfdrawLongCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosLongLineAuxNormal
  3016. htfcolorCBosLongFill = not htfdrawLongCBosRegular or not showCBosHtfFill ? na : htfcolorCBosLongBgNormal
  3017. if htfdrawLongCBosOrigin and htfcbosLH
  3018. if showCBosHtfOriginMainLine
  3019. line.new(htfcbosLB - htfOffset, htfcbosLH, bar_index - htfOffset, htfcbosLH, color = htfcolorCBosLongLineMainNormal, width = showCBosHtfWidthMain)
  3020. if showCBosHtfOriginAuxLine
  3021. line.new(htfcbosLB - htfOffset, htfcbosLL, bar_index - htfOffset, htfcbosLL, color = htfcolorCBosLongLineAuxNormal, width = showCBosHtfWidthAux)
  3022. if showCBosHtfOriginFill
  3023. box.new(htfcbosLB - htfOffset, htfcbosLH, bar_index - htfOffset, htfcbosLL, na, bgcolor = htfcolorCBosLongBgNormal)
  3024.  
  3025. // --------------------------------------------------------------
  3026. // 07.03.04.02 OUTPUT: HTF CLASSIC BOS | SHORT
  3027. // --------------------------------------------------------------
  3028.  
  3029. // HTF Short Classic BOS Colors
  3030. htfcolorCBosShortLineMainNormal = color.new(htfcolorCBosShortMain, htfCBosT3)
  3031. htfcolorCBosShortLineAuxNormal = color.new(htfcolorCBosShortAux, htfCBosT2)
  3032. htfcolorCBosShortBgNormal = color.new(htfcolorCBosShortAux, htfCBosT1)
  3033.  
  3034. // HTF Short Classic BOS Draw
  3035. htfdrawShortCBos = showBosEnabled and showCBosEnabled and htfpriorTrend == 1
  3036. htfshortCBosUnchanged = htfcbosSH == htfcbosSH[1] and htfcbosSL == htfcbosSL[1]
  3037. htfdrawShortCBosRegular = htfdrawShortCBos and htfshortCBosUnchanged
  3038. htfdrawShortCBosOrigin = htfdrawShortCBos and not htfshortCBosUnchanged
  3039. htfcolorCBosShortLineAux = not htfdrawShortCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosShortLineAuxNormal
  3040. htfcolorCBosShortLineMain = not htfdrawShortCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosShortLineMainNormal
  3041. htfcolorCBosShortFill = not htfdrawShortCBosRegular or not showCBosHtfFill ? na : htfcolorCBosShortBgNormal
  3042. if htfdrawShortCBosOrigin and htfcbosSH
  3043. if showCBosHtfOriginAuxLine
  3044. line.new(htfcbosSB - htfOffset, htfcbosSH, bar_index - htfOffset, htfcbosSH, color = htfcolorCBosShortLineAuxNormal, width = showCBosHtfWidthAux)
  3045. if showCBosHtfOriginMainLine
  3046. line.new(htfcbosSB - htfOffset, htfcbosSL, bar_index - htfOffset, htfcbosSL, color = htfcolorCBosShortLineMainNormal, width = showCBosHtfWidthMain)
  3047. if showCBosHtfOriginFill
  3048. box.new(htfcbosSB - htfOffset, htfcbosSH, bar_index - ctfOffset, htfcbosSL, na, bgcolor = htfcolorCBosShortBgNormal)
  3049.  
  3050. // --------------------------------------------------------------
  3051. // 07.03.04.03 OUTPUT: HTF CLASSIC BOS | PLOT
  3052. // --------------------------------------------------------------
  3053.  
  3054. htfcblh = plot(htfpriorTrend == 1 ? htfcbosSL : htfcbosLH, "HTF CBOS Main", htfpriorTrend == 1 ? htfcolorCBosShortLineMain : htfcolorCBosLongLineMain, showCBosHtfWidthMain,
  3055. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  3056. htfcbll = plot(htfpriorTrend == 1 ? htfcbosSH : htfcbosLL, "HTF CBOS Aux", htfpriorTrend == 1 ? htfcolorCBosShortLineAux : htfcolorCBosLongLineAux, showCBosHtfWidthAux,
  3057. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  3058. fill(htfcblh, htfcbll, htfpriorTrend == 1 ? htfcolorCBosShortFill : htfcolorCBosLongFill, "HTF CBOS Fill", editable = false)
  3059.  
  3060. // --------------------------------------------------------------
  3061. // 07.04 OUTPUT: HH/LL LEVELS
  3062. // --------------------------------------------------------------
  3063.  
  3064. // In case we need to free more plots, we can use const color here, at the cost of switching to linebr style and losing one point on line switch
  3065.  
  3066. // Highs
  3067. colorHighPrior = color.new(color.green, 70)
  3068. colorHighGlobal = color.new(color.yellow, 70)
  3069. outPriorH = bosBreached ? priorH : globalH
  3070. outputPriorH = showHhLlLevels and priorTrend == 1
  3071. outputGlobalH = showHhLlLevels and globalTrendDirection == 1 and bosBreached
  3072. plot(outputPriorH ? outPriorH : na, "Prior High", outputPriorH and outPriorH == outPriorH[1] ? colorHighPrior : na, offset = -ctfOffset,
  3073. editable = false, display = display.pane + display.data_window + display.status_line)
  3074. plot(outputGlobalH ? globalH : na, "Global High", outputGlobalH and globalH == globalH[1] ? colorHighGlobal : na, offset = -ctfOffset,
  3075. editable = false, display = display.pane + display.data_window + display.status_line)
  3076. if showHhLlLevelsOrigin
  3077. if outputPriorH and globalTrendDirection[1] == 0
  3078. line.new(globalHB - ctfOffset, outPriorH, bar_index - ctfOffset, outPriorH, color = colorHighPrior, width = 1)
  3079. if outputGlobalH and not bosBreached[1]
  3080. line.new(globalHB - ctfOffset, globalH, bar_index - ctfOffset, globalH, color = colorHighGlobal, width = 1)
  3081. // Lows
  3082. colorLowPrior = color.new(color.red, 70)
  3083. colorLowGlobal = color.new(color.orange, 70)
  3084. outPriorL = bosBreached ? priorL : globalL
  3085. outputPriorL = showHhLlLevels and priorTrend == -1
  3086. outputGlobalL = showHhLlLevels and globalTrendDirection == -1 and bosBreached
  3087. plot(outputPriorL ? outPriorL : na, "Prior Low", outputPriorL and outPriorL == outPriorL[1] ? colorLowPrior : na, offset = -ctfOffset,
  3088. editable = false, display = display.pane + display.data_window + display.status_line)
  3089. plot(outputGlobalL ? globalL : na, "Global Low", outputGlobalL and globalL == globalL[1] ? colorLowGlobal : na, offset = -ctfOffset,
  3090. editable = false, display = display.pane + display.data_window + display.status_line)
  3091. if showHhLlLevelsOrigin
  3092. if outputPriorL and globalTrendDirection[1] == 0
  3093. line.new(globalLB - ctfOffset, outPriorL, bar_index - ctfOffset, outPriorL, color = colorLowPrior, width = 1)
  3094. if outputGlobalL and not bosBreached[1]
  3095. line.new(globalLB - ctfOffset, globalL, bar_index - ctfOffset, globalL, color = colorLowGlobal, width = 1)
  3096.  
  3097. // -----------------------------
  3098. // 07.05 OUTPUT TREND BG / EDGE
  3099. // -----------------------------
  3100.  
  3101. displayTrendBgDirection = showTrendBG == 1 ? globalTrendDirection : showTrendBG == 2 ? htfglobalTrendDirection : 0
  3102. displayTrendBgWeak = showTrendBG == 1 ? bosBreached : showTrendBG == 2 ? htfbosBreached : 0
  3103. displayTrendBgOffset = showTrendBG == 1 ? ctfOffset : showTrendBG == 2 ? htfOffset : 0
  3104. displayTrendAboveDirection = showTrendAbove == 1 ? globalTrendDirection : showTrendAbove == 2 ? htfglobalTrendDirection : 0
  3105. displayTrendAboveWeak = showTrendAbove == 1 ? bosBreached : showTrendAbove == 2 ? htfbosBreached : 0
  3106. displayTrendAboveOffset = showTrendAbove == 1 ? ctfOffset : showTrendAbove == 2 ? htfOffset : 0
  3107. displayTrendBelowDirection = showTrendBelow == 1 ? globalTrendDirection : showTrendBelow == 2 ? htfglobalTrendDirection : 0
  3108. displayTrendBelowWeak = showTrendBelow == 1 ? bosBreached : showTrendBelow == 2 ? htfbosBreached : 0
  3109. displayTrendBelowOffset = showTrendBelow == 1 ? ctfOffset : showTrendBelow == 2 ? htfOffset : 0
  3110.  
  3111. bgcolor(displayTrendBgDirection == 0 ? na :
  3112. displayTrendBgDirection == 1 ?
  3113. (displayTrendBgWeak ? trendBgLongAux: trendBgLongMain) :
  3114. (displayTrendBgWeak ? trendBgShortAux : trendBgShortMain),
  3115. -displayTrendBgOffset)
  3116. plotshape(displayTrendAboveDirection == 0 ? na : displayTrendAboveDirection, "Trend Display: Above", shape.circle, location.top,
  3117. displayTrendAboveDirection == 1 ?
  3118. (displayTrendAboveWeak ? trendEdgeLongAux : trendEdgeLongMain) :
  3119. (displayTrendAboveWeak ? trendEdgeShortAux : trendEdgeShortMain), -displayTrendAboveOffset, size = size.tiny)
  3120. plotshape(displayTrendBelowDirection == 0 ? na : displayTrendBelowDirection, "Trend Display: Below", shape.circle, location.bottom,
  3121. displayTrendBelowDirection == 1 ?
  3122. (displayTrendBelowWeak ? trendEdgeLongAux : trendEdgeLongMain) :
  3123. (displayTrendBelowWeak ? trendEdgeShortAux : trendEdgeShortMain), -displayTrendBelowOffset, size = size.tiny)
  3124.  
  3125. // --------------------------------------------------------------
  3126. // 07.06 OUTPUT EVENTS
  3127. // --------------------------------------------------------------
  3128.  
  3129. if showTrendLabels
  3130. if showTrendLabelsCtf
  3131. if eventTrendBreak
  3132. printLabel(globalTrendDirection, bar_index - ctfOffset,"CTF\nTrend\nBreak!", globalTrendDirection != 1, false)
  3133. if eventTrendReversalConfirmed
  3134. printLabel(globalTrendDirection, bar_index - ctfOffset,"CTF\nTrend\nReversal\nConfirmed!", globalTrendDirection != 1, true)
  3135. if eventTrendResume
  3136. printLabel(globalTrendDirection, bar_index - ctfOffset,"CTF\nTrend\nResume!", globalTrendDirection == 1, true)
  3137. if showTrendLabelsHtf
  3138. if htfeventTrendBreak
  3139. printLabel(htfglobalTrendDirection, bar_index - htfOffset,"HTF\nTrend\nBreak!", htfglobalTrendDirection != 1, false)
  3140. if htfeventTrendReversalConfirmed
  3141. printLabel(htfglobalTrendDirection, bar_index - htfOffset,"HTF\nTrend\nReversal\nConfirmed!", htfglobalTrendDirection != 1, true)
  3142. if htfeventTrendResume
  3143. printLabel(htfglobalTrendDirection, bar_index - htfOffset,"HTF\nTrend\nResume!", htfglobalTrendDirection == 1, true)
  3144.  
  3145. // --------------------------------------------------------------
  3146. // 07.XX OUTPUT: DEBUG
  3147. // --------------------------------------------------------------
  3148.  
  3149. // plotshape(bosBreachCandles, "bosBreachCandles", color=na, offset = -ctfOffset)
  3150.  
  3151. if barstate.islast
  3152. tb = table.new(position.top_right, 1, 2, bgcolor = #111111, border_width = 1, border_color = color.gray, frame_width = 1, frame_color = color.gray)
  3153. table.cell(tb, 0, 0, str.tostring(0,format.mintick), text_color = color.white, text_size = size.small)
  3154. table.cell(tb, 0, 1, str.tostring(0/close*100,"#.##")+"%", text_color = color.white, text_size = size.small)
  3155.  
  3156. if barstate.islast and DBG_DebugEnabled
  3157. tb = table.new(position.top_right, 1, 2, bgcolor = #111111, border_width = 1, border_color = color.gray, frame_width = 1, frame_color = color.gray)
  3158. table.cell(tb, 0, 0, str.tostring(0,format.mintick), text_color = color.white, text_size = size.small)
  3159. table.cell(tb, 0, 1, str.tostring(0/close*100,"#.##")+"%", text_color = color.white, text_size = size.small)
  3160.  
  3161. if barstate.islast and DBG_FirstLimitOn
  3162. tb = table.new(position.top_right, 1, 2, bgcolor = #111111, border_width = 1, border_color = color.gray, frame_width = 1, frame_color = color.gray)
  3163. table.cell(tb, 0, 0, str.tostring(0,format.mintick), text_color = color.white, text_size = size.small)
  3164. table.cell(tb, 0, 1, str.tostring(0/close*100,"#.##")+"%", text_color = color.white, text_size = size.small)
  3165.  
  3166. if barstate.islast and DBG_ShowHTFDebugPlots
  3167. tb = table.new(position.top_right, 1, 2, bgcolor = #111111, border_width = 1, border_color = color.gray, frame_width = 1, frame_color = color.gray)
  3168. table.cell(tb, 0, 0, str.tostring(0,format.mintick), text_color = color.white, text_size = size.small)
  3169. table.cell(tb, 0, 1, str.tostring(0/close*100,"#.##")+"%", text_color = color.white, text_size = size.small)
  3170.  
  3171. if true
  3172. na
  3173. if true
  3174. na
  3175. if true
  3176. na
  3177. if true
  3178. na
  3179. if true
  3180. na
  3181. if true
  3182. na
  3183.  
Advertisement
Comments
  • hemal9022
    1 year
    # text 0.13 KB | 0 0
    1. i downloaded 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