Advertisement
Guest User

552 local scopes

a guest
Oct 19th, 2022
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 192.42 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. array.push(correctionWickRelativeThresholdReachedOr ? succWR : failWR, wickRelative)
  1422. array.push(correctionCloseRelativeThresholdReachedOr ? succCR : failCR, closeRelative)
  1423. array.push(reverseRelativeThresholdReachedOr ? succRR : failRR, reverseRelative)
  1424. array.push(sidewaysThresholdReachedAnd ? succSW : failSW, sideways)
  1425. array.push(atrThresholdReachedAnd ? succWA : failWA, wickAbsolute)
  1426. globalChange
  1427.  
  1428. checkConditionBosBreak(tfSensitiveSettings, bosBreachCandles) =>
  1429. usebosBreakCandleThresholdOr = array.get(tfSensitiveSettings, 03)
  1430. bosBreakCandleThresholdReachedOr = bosBreachCandles >= array.get(tfSensitiveSettings, 02)
  1431. bosBreakCandleThresholdReachedAnd = bosBreachCandles >= array.get(tfSensitiveSettings, 04)
  1432. orUsed = usebosBreakCandleThresholdOr
  1433. or false //In case we make more conditions in the future
  1434. orConditions = not orUsed
  1435. or bosBreakCandleThresholdReachedOr and usebosBreakCandleThresholdOr
  1436. andConditions = (bosBreakCandleThresholdReachedAnd or not array.get(tfSensitiveSettings, 05))
  1437. and true //In case we make more conditions in the future
  1438. bosBreakCondition = orConditions and andConditions
  1439. bosBreakCondition
  1440.  
  1441. checkConditionNewBosBeyondOld(tfSensitiveSettings, int globalTrendDirection, float highLow, float openClose, float bosLH, float bosLL, float bosSH, float bosSL) =>
  1442. newBosThreshold = array.get(tfSensitiveSettings, 01)
  1443. newBosConfirmConditionLevel = array.get(tfSensitiveSettings, 00)
  1444. if newBosConfirmConditionLevel == 2
  1445. (globalTrendDirection == 1 and openClose > bosLH * (100 + newBosThreshold) / 100)
  1446. or (globalTrendDirection == -1 and openClose * (100 + newBosThreshold) / 100 < bosSL)
  1447. else
  1448. (globalTrendDirection == 1 and highLow > (newBosConfirmConditionLevel ? bosLH : bosLL) * (100 + newBosThreshold) / 100)
  1449. or (globalTrendDirection == -1 and highLow * (100 + newBosThreshold) / 100 < (newBosConfirmConditionLevel ? bosSL : bosSH))
  1450.  
  1451. checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) =>
  1452. array.get(tfSensitiveSettings, 12) and (tHigh - tClose) / (tHigh - tLow) >= array.get(tfSensitiveSettings, 11) / 100 and globalHB < barIndex - 1
  1453.  
  1454. checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) =>
  1455. array.get(tfSensitiveSettings, 12) and (tClose - tLow) / (tHigh - tLow) >= array.get(tfSensitiveSettings, 11) / 100 and globalLB < barIndex - 1
  1456.  
  1457. findGlobalLow(float globalL, int globalLB, float currentL, int barIndex, array<float> aFlt, array<int> aInt, bool checkNa = false, int ignoreAmount = 0) =>
  1458. g_l = globalL
  1459. g_lb = globalLB
  1460. if na(g_l) or g_l > currentL
  1461. g_l := currentL
  1462. g_lb := barIndex
  1463. aSize = arSizeFlt(aFlt)
  1464. if aSize > ignoreAmount
  1465. for i = ignoreAmount to aSize - 1
  1466. if checkNa
  1467. if na(arGetFlt(00, aFlt, i))
  1468. continue
  1469. lastLocalL = arGetFlt(01, aFlt, i)
  1470. if na(g_l) or g_l > lastLocalL
  1471. g_l := lastLocalL
  1472. g_lb := arGetInt(01, aInt, i)
  1473. [g_l, g_lb]
  1474.  
  1475. findGlobalHigh(float globalH, int globalHB, float currentH, int barIndex, array<float> aFlt, array<int> aInt, bool checkNa = false, int ignoreAmount = 0) =>
  1476. g_h = globalH
  1477. g_hb = globalHB
  1478. if na(g_h) or g_h < currentH
  1479. g_h := currentH
  1480. g_hb := barIndex
  1481. aSize = arSizeFlt(aFlt)
  1482. if aSize > ignoreAmount
  1483. for i = ignoreAmount to aSize - 1
  1484. if checkNa
  1485. if na(arGetFlt(01, aFlt, i))
  1486. continue
  1487. lastLocalH = arGetFlt(00, aFlt, i)
  1488. if na(g_h) or g_h < lastLocalH
  1489. g_h := lastLocalH
  1490. g_hb := arGetInt(00, aInt, i)
  1491. [g_h, g_hb]
  1492.  
  1493. // --------------------------------------------------------------
  1494. // 05 GET RISK SIZE
  1495. // --------------------------------------------------------------
  1496.  
  1497. // // Entry size will be = risk_size/close
  1498. // var start_price = 0.0
  1499. // var float risk_size = na
  1500. // if doStrategyBackTest and barstate.isfirst
  1501. // strategy.entry( "Get order size", strategy.long)
  1502. // start_price := close
  1503. // if doStrategyBackTest and ta.barssince(barstate.isfirst) >= 1 and strategy.position_entry_name == "Get order size"
  1504. // risk_size := strategy.position_size * start_price
  1505. // strategy.close("Get order size",comment="Got order size")
  1506.  
  1507. // --------------------------------------------------------------
  1508. // 06 MAIN CODE
  1509. // --------------------------------------------------------------
  1510.  
  1511. // --------------------------------------------------------------
  1512. // 06.01 BEGIN TREND & BOS DETERMINATION ALGORITHM
  1513. // --------------------------------------------------------------
  1514.  
  1515. 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) =>
  1516.  
  1517. // CONSTS
  1518.  
  1519. // Theese are used to feed annoying "must define na type" user function bug by TradingView
  1520. float _fna = na
  1521. int _ina = na
  1522.  
  1523. // ARRAYS
  1524.  
  1525. // Temporary "Split" arrays, to temporary hold split members
  1526. var sFlt = array.new<float>(0)
  1527. var sInt = array.new<int>(0)
  1528.  
  1529. // VARIABLES
  1530.  
  1531. // Variables for trend acquisition
  1532. var float acqLocalH = na
  1533. var int acqLocalHB = na
  1534. var float acqLocalL = na
  1535. var int acqLocalLB = na
  1536. var int firstCandleState = na
  1537.  
  1538. // Algo internal states
  1539. executeGlobalTrendChange = false
  1540. executeBosLevelSearch = false
  1541. executeBosLevelUpdate = false
  1542.  
  1543. // ATR
  1544. var float atr = na
  1545.  
  1546. // Classic BOS Bar
  1547. int classicB = na
  1548.  
  1549. [globalTrendDirection, bosTesting, bosBreached, bosBreachCandles] = arStateGet(aStateInt, aStateBool)
  1550. [priorH, priorHB, priorL, priorLB, globalH, globalHB, globalL, globalLB,
  1551. globalHC, globalLC, curNextHC, curNextLC,
  1552. curLNextClose, curLNextReverse, curSNextClose, curSNextReverse,
  1553. bosLH, bosLL, bosLB, bosSH, bosSL, bosSB,
  1554. cbosLH, cbosLL, cbosLB, cbosSH, cbosSL, cbosSB] = arValuesGet(aValuesFlt, aValuesInt)
  1555.  
  1556. tHigh = array.get(aHigh, 0)
  1557. tLow = array.get(aLow, 0)
  1558. tOpen = array.get(aOpen, 0)
  1559. tClose = array.get(aClose, 0)
  1560. tMaxOC = array.get(aMaxOC, 0)
  1561. tMinOC = array.get(aMinOC, 0)
  1562. tPivotH = array.get(aPivotH, 0)
  1563. tPivotL = array.get(aPivotL, 0)
  1564.  
  1565. // Reset algo state
  1566. bosTesting := false
  1567. prevBreachCandles = bosBreachCandles
  1568. bosBreachCandles := 0
  1569.  
  1570. // Globally used settings
  1571. bosIgnoreManipulationsAmount = int(array.get(tfSensitiveSettings, 12))
  1572. useAtrBreakThreshold = array.get(tfSensitiveSettings, 31)
  1573. bosSameCandleLegConsumptionDelay = int(array.get(tfSensitiveSettings, 13))
  1574. bosConsiderNextEnabled = array.get(tfSensitiveSettings, 06)
  1575. bosConsiderNextRecursive = array.get(tfSensitiveSettings, 10)
  1576. bosConsiderNextConsumptionThreshold = array.get(tfSensitiveSettings, 09)
  1577. bosConsiderNextDistanceThreshold = array.get(tfSensitiveSettings, 07)
  1578. bosConsiderNextConsumptionEnabled = array.get(tfSensitiveSettings, 08)
  1579.  
  1580.  
  1581.  
  1582. // --------------------------------------------------------------
  1583. // 06.01.01 TREND & BOS | Initial Calculations
  1584. // --------------------------------------------------------------
  1585.  
  1586. // ATR
  1587. atrLength = array.get(tfSensitiveSettings, 32)
  1588. if na(atr)
  1589. atr := tHigh - tLow
  1590. else
  1591. prevClose = array.get(aClose, 1)
  1592. curAtr = math.max(tHigh - tLow, math.abs(tHigh - prevClose), math.abs(tLow - prevClose))
  1593. atr := (atr * (atrLength - 1) + curAtr ) / atrLength
  1594.  
  1595. // --------------------------------------------------------------
  1596. // 06.01.02 TREND & BOS | Init & Acquire Trend
  1597. // --------------------------------------------------------------
  1598.  
  1599. // Initial trend forced Long
  1600. if globalTrendDirection == 2
  1601. priorH := tHigh
  1602. priorHB := barIndex
  1603. priorL := tLow
  1604. priorLB := barIndex
  1605. globalH := priorH
  1606. globalHB := priorHB
  1607. globalL := priorL
  1608. globalLB := priorLB
  1609. globalTrendDirection := -1
  1610. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1611. executeGlobalTrendChange := true
  1612. executeBosLevelUpdate := true
  1613. // Initial trend forced Short
  1614. else if globalTrendDirection == -2
  1615. priorH := tHigh
  1616. priorHB := barIndex
  1617. priorL := tLow
  1618. priorLB := barIndex
  1619. globalH := priorH
  1620. globalHB := priorHB
  1621. globalL := priorL
  1622. globalLB := priorLB
  1623. globalTrendDirection := 1
  1624. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1625. executeGlobalTrendChange := true
  1626. executeBosLevelUpdate := true
  1627. // Initial trend acquisition
  1628. else if globalTrendDirection == 0
  1629. MAX_INITIAL_CANDLES = array.get(tfIndependentSettings, 00)
  1630. globalHC := na(globalHC) or tMaxOC > globalHC ? tMaxOC : globalHC
  1631. globalLC := na(globalLC) or tMinOC < globalLC ? tMinOC : globalLC
  1632. // Step 1 : Record first available high/low once acquired, and rewrite them as long as both are pierced in same candle
  1633. if na(priorH) or (na(globalH) and tHigh > priorH and tLow < priorL)
  1634. priorH := tHigh
  1635. priorHB := barIndex
  1636. priorL := tLow
  1637. priorLB := barIndex
  1638. firstCandleState := tClose >= tOpen ? 1 : -1
  1639. // Step 2 : Record first price leg once price exceeds first available values
  1640. else if na(globalH) or barIndex - priorHB > MAX_INITIAL_CANDLES
  1641. // If price breached prior up (and only up)
  1642. // First leg is up
  1643. if tHigh > priorH
  1644. globalH := tHigh
  1645. globalHB := barIndex
  1646. globalL := priorL
  1647. globalLB := priorLB
  1648. // If price breached prior down (and only down)
  1649. // First leg is down
  1650. else if tLow < priorL
  1651. globalL := tLow
  1652. globalLB := barIndex
  1653. globalH := priorH
  1654. globalHB := priorHB
  1655. // Determine trend regardless if waited enough
  1656. if barIndex - priorHB > MAX_INITIAL_CANDLES
  1657. if na(globalH)
  1658. globalL := priorL
  1659. globalLB := priorLB
  1660. globalH := priorH
  1661. globalHB := priorHB
  1662. globalTrendDirection := -firstCandleState
  1663. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1664. executeGlobalTrendChange := true
  1665. executeBosLevelUpdate := true
  1666. // Step 3 : Record locals and determine trend when possible
  1667. else
  1668. // Record new local high as long as we don't have both pivots in a downtrend
  1669. if tPivotH and (na(acqLocalH) or tPivotH > acqLocalH) and (na(tPivotL) or globalLB < globalHB)
  1670. acqLocalH := tPivotH
  1671. acqLocalHB := barIndex
  1672. // Record new local low as we don't have both pivots in an uptrend
  1673. if tPivotL and (na(acqLocalL) or tPivotL < acqLocalL) and (na(tPivotH) or globalHB < globalLB)
  1674. acqLocalL := tPivotL
  1675. acqLocalLB := barIndex
  1676. // If breached global high or waited enough
  1677. if tHigh > globalH or barIndex - globalLB > MAX_INITIAL_CANDLES
  1678. // If with local low or first leg is down or enough candles passed
  1679. // We got a long trend!
  1680. if acqLocalL or globalHB < globalLB or barIndex - globalLB > MAX_INITIAL_CANDLES
  1681. globalL := na(acqLocalL) ? globalL : acqLocalL
  1682. globalLB := na(acqLocalL) ? globalLB : acqLocalLB
  1683. globalTrendDirection := -1
  1684. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1685. executeGlobalTrendChange := true
  1686. executeBosLevelUpdate := true
  1687. // Update global high for now
  1688. if tHigh > globalH
  1689. globalH := tHigh
  1690. globalHB := barIndex
  1691. // Breached global Low or waited enough
  1692. else if tLow < globalL or barIndex - globalHB > MAX_INITIAL_CANDLES
  1693. // If with local high or first leg is up
  1694. // We got a short trend!
  1695. if acqLocalH or globalLB < globalHB or barIndex - globalHB > MAX_INITIAL_CANDLES
  1696. globalH := na(acqLocalH) ? globalH : acqLocalH
  1697. globalHB := na(acqLocalH) ? globalHB : acqLocalHB
  1698. globalTrendDirection := 1
  1699. arInit(aLFlt, aLInt, aSFlt, aSInt)
  1700. executeGlobalTrendChange := true
  1701. executeBosLevelUpdate := true
  1702. // Update global low for now
  1703. if tLow < globalL
  1704. globalL := tLow
  1705. globalLB := barIndex
  1706.  
  1707. // --------------------------------------------------------------
  1708. // 06.01.03 TREND & BOS | Accumulated values
  1709. // --------------------------------------------------------------
  1710. if globalTrendDirection != 0
  1711. curNextHC := na(curNextHC) or curNextHC < tMaxOC ? tMaxOC : curNextHC
  1712. curNextLC := na(curNextLC) or curNextLC > tMinOC ? tMinOC : curNextLC
  1713. curLNextClose := na(curLNextClose) or curLNextClose < tMaxOC ? tMaxOC : curLNextClose
  1714. curLNextReverse := na(curLNextReverse) or curLNextReverse < tLow ? tLow : curLNextReverse
  1715. curSNextClose := na(curSNextClose) or curSNextClose > tMinOC ? tMinOC : curSNextClose
  1716. curSNextReverse := na(curSNextReverse) or curSNextReverse > tHigh ? tHigh : curSNextReverse
  1717. if globalTrendDirection == 1
  1718. globalHC := curNextHC
  1719. if globalTrendDirection == -1
  1720. globalLC := curNextLC
  1721.  
  1722.  
  1723. // --------------------------------------------------------------
  1724. // 06.01.04 TREND & BOS | Long Breaching
  1725. // --------------------------------------------------------------
  1726.  
  1727. if globalTrendDirection == 1
  1728. // If in bos breach mode
  1729. // Determine if resumed prior trend
  1730. if bosBreached
  1731. // If breached priorL
  1732. // Then resumed prior trend
  1733. if tLow < priorL - (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : priorL * array.get(tfSensitiveSettings, 30)) / 100
  1734. // When resuming prior short trend, global low should be like if this were a short trend leg
  1735. // When in short trend, we breach global low
  1736. // Here, we breach prior low, but in terms of price action, we have a certain low we fell from downwards
  1737. // That should be our global low for purpose of bos search
  1738. [curL, curLB] = findGlobalLow(_fna, _ina, _fna, barIndex, aSFlt, aSInt, true, 1)
  1739. globalL := na(curL) ? priorL : curL
  1740. globalLB := na(curL) ? priorLB : curLB
  1741. bosBreached := false
  1742. executeBosLevelSearch := true
  1743. executeGlobalTrendChange := true
  1744. executeBosLevelUpdate := true
  1745.  
  1746. // If not in bos breach mode
  1747. else
  1748. // Determine if testing bos
  1749. bosTesting := (tLow < bosSH or array.get(aLow, 1) < bosSH)
  1750. // Determine if breaching bos - when enough candles are observed confirm bos breach
  1751. if tHigh < bosSL * (100 - array.get(tfSensitiveSettings, 01)) / 100
  1752. bosBreachCandles := prevBreachCandles + 1
  1753. // BOS Break!
  1754. if checkConditionBosBreak(tfSensitiveSettings, bosBreachCandles)
  1755. priorH := globalH
  1756. priorHB := globalHB
  1757. bosBreached := true
  1758. executeBosLevelSearch := true
  1759. executeGlobalTrendChange := true
  1760.  
  1761. // --------------------------------------------------------------
  1762. // 06.01.05 TREND & BOS | SHORT Breaching
  1763. // --------------------------------------------------------------
  1764.  
  1765. else if globalTrendDirection == -1
  1766. // If in bos breach mode
  1767. // Determine if resumed prior trend
  1768. if bosBreached
  1769. // If breached priorH
  1770. // Then resumed prior trend
  1771. if tHigh > priorH + (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : priorH * array.get(tfSensitiveSettings, 30)) / 100
  1772. // When resuming prior long trend, global high should be like if this were a long trend leg
  1773. // When in long trend, we breach global high
  1774. // Here, we breach prior high, but in terms of price action, we have a certain high we pumped from upwards
  1775. // That should be our global high for purpose of bos search
  1776. [curH, curHB] = findGlobalHigh(_fna, _ina, _fna, barIndex, aLFlt, aLInt, true, 1)
  1777. globalH := na(curH) ? priorH : curH
  1778. globalHB := na(curH) ? priorHB : curHB
  1779. bosBreached := false
  1780. executeBosLevelSearch := true
  1781. executeGlobalTrendChange := true
  1782. executeBosLevelUpdate := true
  1783.  
  1784. // If not in bos breach mode
  1785. else
  1786. // Determine if testing bos
  1787. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  1788. // Determine if breaching bos - when enough candles are observed confirm bos breach
  1789. if tLow > bosLH * (100 + array.get(tfSensitiveSettings, 01)) / 100
  1790. bosBreachCandles := prevBreachCandles + 1
  1791. // BOS Break!
  1792. if checkConditionBosBreak(tfSensitiveSettings, bosBreachCandles)
  1793. priorL := globalL
  1794. priorLB := globalLB
  1795. bosBreached := true
  1796. executeBosLevelSearch := true
  1797. executeGlobalTrendChange := true
  1798.  
  1799. // --------------------------------------------------------------
  1800. // 06.01.06 TREND & BOS | New HH / LL
  1801. // --------------------------------------------------------------
  1802.  
  1803. if globalTrendDirection != 0
  1804. if globalTrendDirection == 1 and not executeGlobalTrendChange
  1805. // If broke global high
  1806. // Record new high, update BOS if possible
  1807. if tHigh > globalH + (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : globalH * array.get(tfSensitiveSettings, 30)) / 100
  1808. executeBosLevelSearch := true
  1809. else if globalTrendDirection == -1 and not executeGlobalTrendChange
  1810. // If broke global low
  1811. // Record new low, update BOS if possible
  1812. if tLow < globalL - (useAtrBreakThreshold ? atr * array.get(tfSensitiveSettings, 33) : globalL * array.get(tfSensitiveSettings, 30)) / 100
  1813. executeBosLevelSearch := true
  1814. nextTrend = globalTrendDirection == 0 ? 0 : executeGlobalTrendChange ? -globalTrendDirection : globalTrendDirection
  1815.  
  1816. // --------------------------------------------------------------
  1817. // 06.01.07 TREND & BOS | Price Legs
  1818. // --------------------------------------------------------------
  1819.  
  1820. // Possible BOS breaching means we are uncertain where the trend will go
  1821. // So we record price legs both ways always, the only exception is initial state with no trend
  1822.  
  1823. // --------------------------------------------------------------
  1824. // 06.01.07.01 TREND & BOS | Price Legs | Long New Candle Data
  1825. // --------------------------------------------------------------
  1826.  
  1827. if globalTrendDirection != 0
  1828.  
  1829. [curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF] = arPop(aLFlt, aLInt)
  1830.  
  1831. // If encountered pivot low
  1832. // If it's a lower or first local low
  1833. // Record it and all its data
  1834. // Otherwise it's ignored
  1835. if tPivotL and (curLLocalH or tPivotH) and (na(curLLocalL) or tPivotL < curLLocalL)
  1836. // A special case of high and low in one candle
  1837. if tPivotH and na(curLLocalH)
  1838. curLLocalH := tPivotH
  1839. curLLocalHB := barIndex
  1840. curLLocalL := tPivotL
  1841. curLLocalLB := barIndex
  1842. curLSidewaysL := na
  1843. // Reverse close low that has been calculated so far is now included and consumed
  1844. curLClose := na(curLClose) ? curLNextClose : math.max(curLClose, curLNextClose)
  1845. curLNextClose := na
  1846. curLReverse := na(curLReverse) ? curLNextReverse : math.max(curLReverse, curLNextReverse)
  1847. curLNextReverse := na
  1848. // If encountered a pivot high
  1849. if tPivotH
  1850. // If we have a local price leg (both high and low)
  1851. if curLLocalL
  1852. // Pack last price leg into array, begin a new one
  1853. arPush(aLFlt, aLInt, curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF)
  1854. curLLocalH := na
  1855. curLLocalL := na
  1856. curLClose := na
  1857. curLReverse := na
  1858. curLSidewaysL := na
  1859. if na(curLLocalH) or tPivotH > curLLocalH
  1860. curLLocalH := tPivotH
  1861. curLLocalHB := barIndex
  1862.  
  1863. arPush(aLFlt, aLInt, curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF)
  1864.  
  1865. // --------------------------------------------------------------
  1866. // 06.01.07.02 TREND & BOS | Price Legs | Short New Candle Data
  1867. // --------------------------------------------------------------
  1868.  
  1869. if globalTrendDirection != 0
  1870.  
  1871. [curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF] = arPop(aSFlt, aSInt)
  1872.  
  1873. // If encountered pivot high
  1874. // If it's a higher or first local high
  1875. // Record it and all its data
  1876. // Otherwise it's ignored
  1877. if tPivotH and (tPivotL or curSLocalL) and (na(curSLocalH) or tPivotH > curSLocalH)
  1878. // A special case of high and low in one candle
  1879. if tPivotL and na(curSLocalL)
  1880. curSLocalL := tPivotL
  1881. curSLocalLB := barIndex
  1882. curSLocalH := tPivotH
  1883. curSLocalHB := barIndex
  1884. curSSidewaysL := na
  1885. // Reverse high and close that has been calculated so far is now included and consumed
  1886. curSClose := na(curSClose) ? curSNextClose : math.min(curSClose, curSNextClose)
  1887. curSNextClose := na
  1888. curSReverse := na(curSReverse) ? curSNextReverse : math.min(curSReverse, curSNextReverse)
  1889. curSNextReverse := na
  1890. // If encountered a pivot low
  1891. if tPivotL
  1892. // If we have a local price leg (both high and low)
  1893. if curSLocalH
  1894. // Pack last price leg into array, begin a new one
  1895. arPush(aSFlt, aSInt, curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF)
  1896. curSLocalH := na
  1897. curSLocalL := na
  1898. curSClose := na
  1899. curSReverse := na
  1900. curSSidewaysL := na
  1901. if na(curSLocalL) or tPivotL < curSLocalL
  1902. curSLocalL := tPivotL
  1903. curSLocalLB := barIndex
  1904. arPush(aSFlt, aSInt, curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF)
  1905.  
  1906. // --------------------------------------------------------------
  1907. // 06.01.07.03 TREND & BOS | Price Legs | Long Merge
  1908. // --------------------------------------------------------------
  1909.  
  1910. if globalTrendDirection != 0
  1911.  
  1912. // Extra offset is nullified in case this candle is breaching BOS and isn't a manipulation
  1913. // In this case we are merging right before a bos level search to include candles that would otherwise be excluded
  1914. // Otherwise we don't merge bars within this offset, so they don't overwrite previous legs that could be used
  1915. splitOffset = 1 + (executeBosLevelSearch
  1916. and ((not checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) and nextTrend == 1)
  1917. or (not checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) and nextTrend == -1)) ? 0 :
  1918. bosIgnoreManipulationsAmount)
  1919. // Combine all price legs that get overwritten by price legs after the offset (in case of manipulation)
  1920. i = arSizeFlt(aLFlt)
  1921. // Go on while there are at least two legs left to compare
  1922. while i >= 2
  1923. i := i - 1
  1924. if splitOffset and arGetInt(01, aLInt, i) > barIndex - splitOffset
  1925. continue
  1926. // Attempt merger of current leg with the previous one
  1927. prevL = arGetFlt(01, aLFlt, i - 1)
  1928. curL = arGetFlt(01, aLFlt, i)
  1929. // Only legs with a lower low should be kept alive
  1930. if na(curL) or prevL < curL
  1931. continue
  1932. // Take its reverse low and close if higher
  1933. arSetFlt(02, aLFlt, i, math.max(arGetFlt(02, aLFlt, i), arGetFlt(02, aLFlt, i - 1)))
  1934. arSetFlt(03, aLFlt, i, math.max(arGetFlt(03, aLFlt, i), arGetFlt(03, aLFlt, i - 1)))
  1935. // Take its local high if it's higher
  1936. prevH = arGetFlt(00, aLFlt, i - 1)
  1937. if prevH > arGetFlt(00, aLFlt, i)
  1938. arSetFlt(00, aLFlt, i, prevH)
  1939. arSetInt(00, aLInt, i, arGetInt(00, aLInt, i - 1))
  1940. arSetInt(02, aLInt, i, _ina)
  1941. // Remove consumed leg
  1942. arRemove(aLFlt, aLInt, i - 1)
  1943.  
  1944. // --------------------------------------------------------------
  1945. // 06.01.07.04 TREND & BOS | Price Legs | Short Merge
  1946. // --------------------------------------------------------------
  1947.  
  1948. if globalTrendDirection != 0
  1949.  
  1950. // Extra offset is nullified in case this candle is breaching BOS and isn't a manipulation
  1951. // In this case we are merging right before a bos level search to include candles that would otherwise be excluded
  1952. // Otherwise we don't merge bars within this offset, so they don't overwrite previous legs that could be used
  1953. splitOffset = 1 + (executeBosLevelSearch
  1954. and ((not checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) and nextTrend == 1)
  1955. or (not checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) and nextTrend == -1)) ? 0 :
  1956. bosIgnoreManipulationsAmount)
  1957. // Combine all price legs that get overwritten by price legs after the offset (in case of manipulation)
  1958. i = arSizeFlt(aSFlt)
  1959. // Go on while there are at least two legs left to compare
  1960. while i >= 2
  1961. i := i - 1
  1962. if splitOffset and arGetInt(00, aSInt, i) > barIndex - splitOffset
  1963. continue
  1964. // Attempt merger of current leg with the previous one
  1965. prevH = arGetFlt(00, aSFlt, i - 1)
  1966. curH = arGetFlt(00, aSFlt, i)
  1967. // Only legs with a lower low should be kept alive
  1968. if na(curH) or prevH > curH
  1969. continue
  1970. // Take its reverse low and close if higher
  1971. arSetFlt(02, aSFlt, i, math.min(arGetFlt(02, aSFlt, i), arGetFlt(02, aSFlt, i - 1)))
  1972. arSetFlt(03, aSFlt, i, math.min(arGetFlt(03, aSFlt, i), arGetFlt(03, aSFlt, i - 1)))
  1973. // Take its local low if it's lower
  1974. prevL = arGetFlt(01, aSFlt, i - 1)
  1975. if prevL < arGetFlt(01, aSFlt, i)
  1976. arSetFlt(01, aSFlt, i, prevL)
  1977. arSetInt(01, aSInt, i, arGetInt(01, aSInt, i - 1))
  1978. arSetInt(02, aSInt, i, _ina)
  1979. // Remove consumed leg
  1980. arRemove(aSFlt, aSInt, i - 1)
  1981.  
  1982. // --------------------------------------------------------------
  1983. // 06.01.07.05 TREND & BOS | Price Legs | Long Sideways
  1984. // --------------------------------------------------------------
  1985.  
  1986. // What part of candle should be inside for it to be considered inside
  1987. candleTotal = 100
  1988. candlePercent = array.get(tfSensitiveSettings, 34)
  1989. candleRemainder = candleTotal - candlePercent
  1990. // What percent of candle should the body be for it to be considered a full body candle
  1991. candleFullBodyPercent = array.get(tfSensitiveSettings, 35)
  1992. // If candle is full body, at least candlePercent of candle's body must be inside
  1993. // If candle is not full body, at least candlePercent of whole candle must be inside
  1994.  
  1995. if globalTrendDirection != 0
  1996. maxI = arSizeFlt(aLFlt) - 1
  1997. maxI := maxI < 0 ? na : maxI
  1998. for i = 0 to maxI
  1999. // If localLow is filled then this is a complete price leg
  2000. localH = arGetFlt(00, aLFlt, i)
  2001. localL = arGetFlt(01, aLFlt, i)
  2002. if localL
  2003. // If sidewaysL is na then sideways L needs recalculation
  2004. if na(arGetInt(02, aLInt, i))
  2005. sidewaysL = 0
  2006. localClose = arGetFlt(02, aLFlt, i)
  2007. localReverse = arGetFlt(03, aLFlt, i)
  2008. startBar = arGetInt(01, aLInt, i)
  2009. lookbackStart = barIndex - startBar
  2010. lookbackEnd = barIndex - math.min(arGetInt(00, aLInt, i), math.max(nz(bosLB), nz(bosSB), nz(arGetLastBar(aLInt, i))))
  2011. lookbackEnd := lookbackEnd <= lookbackStart ? na : lookbackEnd
  2012. arSetInt(03, aLInt, i, lookbackStart)
  2013. arSetInt(04, aLInt, i, 0)
  2014. for j = lookbackStart to lookbackEnd
  2015. max = array.get(aHigh, j)
  2016. min = array.get(aLow, j)
  2017. maxOC = array.get(aMaxOC, j)
  2018. minOC = array.get(aMinOC, j)
  2019. // if full body candle compare body
  2020. if (maxOC - minOC) / (max - min) > candleFullBodyPercent / candleTotal
  2021. max := maxOC
  2022. min := minOC
  2023. bodyLow = (max * candleRemainder + min * candlePercent) / candleTotal
  2024. bodyHigh = (max * candlePercent + min * candleRemainder) / candleTotal
  2025. if bodyLow > localH or bodyHigh < localL
  2026. break
  2027. curReverse = array.get(aLow, j)
  2028. curClose = array.get(aMaxOC, j)
  2029. if na(localClose) or curClose > localClose
  2030. localClose := curClose
  2031. if na(localReverse) or curReverse > localReverse
  2032. localReverse := curReverse
  2033. sidewaysL := j - lookbackStart
  2034. arSetInt(02, aLInt, i, sidewaysL)
  2035. arSetFlt(02, aLFlt, i, math.min(localH, localClose))
  2036. arSetFlt(03, aLFlt, i, localReverse)
  2037. // If still moving sideways R
  2038. if arGetInt(04, aLInt, i) == 0
  2039. max = tHigh
  2040. min = tLow
  2041. // if full body candle compare body
  2042. if (tMaxOC - tMinOC) / (max - min) > candleFullBodyPercent / candleTotal
  2043. max := tMaxOC
  2044. min := tMinOC
  2045. bodyHigh = (max * candlePercent + min * candleRemainder) / candleTotal
  2046. if bodyHigh > localH
  2047. arSetInt(04, aLInt, i, 1)
  2048. else
  2049. arSetInt(03, aLInt, i, arGetInt(03, aLInt, i) + 1)
  2050.  
  2051. // --------------------------------------------------------------
  2052. // 06.01.07.06 TREND & BOS | Price Legs | Short Sideways
  2053. // --------------------------------------------------------------
  2054.  
  2055. if globalTrendDirection != 0
  2056. // Sideways update for short legs
  2057. maxI = arSizeFlt(aSFlt) - 1
  2058. maxI := maxI < 0 ? na : maxI
  2059. for i = 0 to maxI
  2060. // If localHigh is filled then this is a complete price leg
  2061. localH = arGetFlt(00, aSFlt, i)
  2062. localL = arGetFlt(01, aSFlt, i)
  2063. if localH
  2064. // If sidewaysL = na then sideways L needs recalculation
  2065. if na(arGetInt(02, aSInt, i))
  2066. sidewaysL = 0
  2067. localClose = arGetFlt(02, aSFlt, i)
  2068. localReverse = arGetFlt(03, aSFlt, i)
  2069. startBar = arGetInt(00, aSInt, i)
  2070. lookbackStart = barIndex - startBar
  2071. lookbackEnd = barIndex - math.min(arGetInt(01, aSInt, i), math.max(nz(bosLB), nz(bosSB), nz(arGetLastBar(aSInt, i))))
  2072. lookbackEnd := lookbackEnd <= lookbackStart ? na : lookbackEnd
  2073. arSetInt(03, aSInt, i, lookbackStart)
  2074. arSetInt(04, aSInt, i, 0)
  2075. for j = lookbackStart to lookbackEnd
  2076. max = array.get(aHigh, j)
  2077. min = array.get(aLow, j)
  2078. maxOC = array.get(aMaxOC, j)
  2079. minOC = array.get(aMinOC, j)
  2080. // if full body candle compare body
  2081. if (maxOC - minOC) / (max - min) > candleFullBodyPercent / candleTotal
  2082. max := maxOC
  2083. min := minOC
  2084. bodyLow = (max * candleRemainder + min * candlePercent) / candleTotal
  2085. bodyHigh = (max * candlePercent + min * candleRemainder) / candleTotal
  2086. if bodyHigh > localH or bodyLow < localL
  2087. break
  2088. curReverse = array.get(aHigh, j)
  2089. curClose = array.get(aMinOC, j)
  2090. if na(localClose) or curClose < localClose
  2091. localClose := curClose
  2092. if na(localReverse) or curReverse < localReverse
  2093. localReverse := curReverse
  2094. sidewaysL := j - lookbackStart
  2095. arSetInt(02, aSInt, i, sidewaysL)
  2096. arSetFlt(02, aSFlt, i, math.max(localL, localClose))
  2097. arSetFlt(03, aSFlt, i, localReverse)
  2098. // If still moving sideways R
  2099. if arGetInt(04, aSInt, i) == 0
  2100. max = tHigh
  2101. min = tLow
  2102. // if full body candle compare body
  2103. if (tMaxOC - tMinOC) / (max - min) > candleFullBodyPercent / candleTotal
  2104. max := tMaxOC
  2105. min := tMinOC
  2106. bodyLow = (max * candleRemainder + min * candlePercent) / candleTotal
  2107. if bodyLow < localL
  2108. arSetInt(04, aSInt, i, 1)
  2109. else
  2110. arSetInt(03, aSInt, i, arGetInt(03, aSInt, i) + 1)
  2111.  
  2112. // --------------------------------------------------------------
  2113. // 06.01.08 TREND & BOS | BOS Search
  2114. // --------------------------------------------------------------
  2115.  
  2116. // First check if we have to consume price leg we left behind
  2117. // If that leg was overwritten we instead remove it completely
  2118. breakoutPriceLegConsumption = false
  2119. // Long check:
  2120. if nextTrend == 1 and not executeBosLevelSearch
  2121. // a) we have long price legs
  2122. if array.size(aLFlt) > 0
  2123. // Ensure price leg is full
  2124. // b) [0] price leg low bar is on or before global high bar
  2125. // c) [0] price leg low bar is far away from barIndex (user input value)
  2126. localLowB = arGetInt(01, aLInt, 0)
  2127. if arGetFlt(01, aLFlt, 0) and localLowB <= globalHB and barIndex - localLowB >= bosSameCandleLegConsumptionDelay
  2128. localLow = array.get(aMinOC, barIndex - localLowB)
  2129. consumed = false
  2130. // d) price low never went below that price leg's body low (meaning, potential BOS was not consumed)
  2131. for i = 0 to bosSameCandleLegConsumptionDelay - 1
  2132. if array.get(aLow, i) <= localLow
  2133. arRemove(aLFlt, aLInt, 0)
  2134. if array.size(aLFlt) == 0
  2135. arPush(aLFlt, aLInt)
  2136. consumed := true
  2137. break
  2138. // Then do intermittent bos search to consume that leg
  2139. if not consumed
  2140. breakoutPriceLegConsumption := true
  2141. // Short check:
  2142. if nextTrend == -1 and not executeBosLevelSearch
  2143. // a) we have short price legs
  2144. if array.size(aSFlt) > 0
  2145. // Ensure price leg is full
  2146. // b) [0] price leg high bar is on or before global low bar
  2147. // c) [0] price leg high bar is far away from barIndex (user input value)
  2148. localHighB = arGetInt(00, aSInt, 0)
  2149. if arGetFlt(00, aSFlt, 0) and localHighB <= globalLB and barIndex - localHighB >= bosSameCandleLegConsumptionDelay
  2150. localHigh = array.get(aMaxOC, barIndex - localHighB)
  2151. consumed = false
  2152. // d) price high never went above that price leg's body high (meaning, potential BOS was not consumed)
  2153. for i = 0 to bosSameCandleLegConsumptionDelay
  2154. if array.get(aHigh, i) >= localHigh
  2155. arRemove(aSFlt, aSInt, 0)
  2156. if array.size(aSFlt) == 0
  2157. arPush(aSFlt, aSInt)
  2158. consumed := true
  2159. break
  2160. // Then do intermittent bos search to consume that leg
  2161. if not consumed
  2162. breakoutPriceLegConsumption := true
  2163.  
  2164. // --------------------------------------------------------------
  2165. // 06.01.08.01 TREND & BOS | Long BOS Search
  2166. // --------------------------------------------------------------
  2167.  
  2168. if nextTrend == 1 and (executeBosLevelSearch or breakoutPriceLegConsumption)
  2169.  
  2170. // If we are doing bos search after trend break or to consume breakout price leg then this is a special case
  2171. intermediarySearch = breakoutPriceLegConsumption or (bosBreached and executeGlobalTrendChange)
  2172.  
  2173. // If intermittent search after bos break:
  2174. // Split away legs that were achieved above current global high
  2175. if intermediarySearch
  2176. [_void, splitBar] = findGlobalHigh(globalH, globalHB, tHigh, barIndex, aSFlt, aSInt)
  2177. // If consuming breakout legs, we must offset by user's input value, because we could have made closer global highs afterwards,
  2178. // but still need to consume only the original leg
  2179. if breakoutPriceLegConsumption
  2180. splitBar := barIndex - bosSameCandleLegConsumptionDelay
  2181. arSplit(aLFlt, aLInt, sFlt, sInt, true, splitBar)
  2182. // If normal search:
  2183. // Split away entry added in the last bars in case this candle is a manipulation
  2184. else
  2185. splitOffset = 1 + (checkConditionManipulationLong(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalHB) ? bosIgnoreManipulationsAmount : 0)
  2186. arSplit(aLFlt, aLInt, sFlt, sInt, true, barIndex - splitOffset)
  2187.  
  2188. // Output where we're checking Long BOS
  2189. if array.get(tfIndependentSettings, 06) and array.get(tfSensitiveSettings, 60)
  2190. debugText("BL\nS" + (breakoutPriceLegConsumption ? "B" : intermediarySearch ? "I" : "") +
  2191. "\nS"+str.tostring(arSizeFlt(sFlt))+"\nO"+str.tostring(arSizeFlt(aLFlt)), true)
  2192.  
  2193. // Remember first and last that passed our conditions
  2194. float firstPassedL = na
  2195. int firstPassedLB = na
  2196. float lastPassedL = na
  2197. int lastPassedLB = na
  2198. bool found = false
  2199. // Iterate until we find suitable LocalH or exhaust all options
  2200. while not found and array.size(aLFlt) > 0
  2201. // Get next leg to examine
  2202. [curLLocalH, curLLocalHB, curLLocalL, curLLocalLB, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, curLSidewaysF] = arPop(aLFlt, aLInt)
  2203. found := checkConditionGlobalChange(tfSensitiveSettings, tfIndependentSettings, aBarIndex, curLLocalLB,
  2204. globalH, globalLC, curLLocalH, curLLocalL, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, atr, true)
  2205. if array.get(tfSensitiveSettings, 61)
  2206. printPriceLeg(tfSensitiveSettings, tfIndependentSettings, aBarIndex,
  2207. curLLocalHB, curLLocalH, curLLocalLB, curLLocalL, curLClose, curLReverse, curLSidewaysL, curLSidewaysR, true, found)
  2208. // Record last passed High
  2209. if found
  2210. lastPassedL := curLLocalL
  2211. lastPassedLB := curLLocalLB
  2212. // Record first passed High
  2213. if na(firstPassedL)
  2214. firstPassedL := curLLocalL
  2215. firstPassedLB := curLLocalLB
  2216. // If we can look further, have price legs remaining, and have something to compare to
  2217. aSize = arSizeFlt(aLFlt)
  2218. if bosConsiderNextEnabled and aSize > 0 and firstPassedL
  2219. // If next point is within range - clear found flag to allow while loop to continue
  2220. // Also ensure next point isn't within old BOS in case we're breaching
  2221. float nextH = na
  2222. nextL = arGetFlt(01, aLFlt, aSize - 1)
  2223. nextMinOC = array.get(aMinOC, barIndex - arGetInt(01, aLInt, aSize - 1))
  2224. nextDiff = (globalH - nextL) / (globalH - (bosConsiderNextRecursive ? lastPassedL : firstPassedL)) - 1
  2225. nextConsumed = false
  2226. if bosConsiderNextConsumptionEnabled
  2227. nextConsumed := (nextMinOC - lastPassedL) / (nextMinOC - nextL) > bosConsiderNextConsumptionThreshold / 100
  2228. if not nextConsumed and nextDiff < bosConsiderNextDistanceThreshold / 100
  2229. and (not bosBreached or checkConditionNewBosBeyondOld(tfSensitiveSettings, nextTrend, nextL, nextMinOC, bosLH, bosLL, bosSH, bosSL))
  2230. found := false
  2231. // If next point is not within range - break
  2232. else
  2233. break
  2234. // If we still have a localL value here that means we found a suitable local low to use as a new BOS
  2235. if lastPassedL
  2236. // Relocate global low and BOS on it
  2237. executeBosLevelUpdate := true
  2238. globalL := lastPassedL
  2239. globalLB := lastPassedLB
  2240. globalLC := math.max(curNextLC, globalL)
  2241. // Set classic BOS bar in case we have an unused price leg, and that BOS is outside selected BOS by at least the threshold
  2242. if array.size(aLInt) > 0
  2243. // First we pick the last price leg, which is by definition the highest
  2244. classicB := arGetInt(01, aLInt, 0)
  2245. classicMinOC = array.get(aMinOC, barIndex - classicB)
  2246. classicDiff = 1 - classicMinOC / lastPassedL
  2247. // Classic BOS' closer boundary must be away from algorithm-chosen BOS by at least this much, otherwise it's discarded
  2248. if classicDiff <= array.get(tfSensitiveSettings, 01) / 100
  2249. classicB := na
  2250. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, true)
  2251. arSplit(sFlt, sInt, aLFlt, aLInt)
  2252. if array.size(aLFlt) == 0
  2253. arPush(aLFlt, aLInt)
  2254. // If not doing intermittent search
  2255. if not intermediarySearch
  2256. // Update global high
  2257. globalH := tHigh
  2258. globalHB := barIndex
  2259. // Waste short accumulated values
  2260. curSNextClose := na
  2261. curSNextReverse := na
  2262. // If not in bos breached mode the next lowest close is reset here
  2263. // Otherwise it can also be reset if we confirm trend change and establish new BOS later
  2264. if not bosBreached
  2265. curNextLC := tMinOC
  2266.  
  2267. // --------------------------------------------------------------
  2268. // 06.01.08.02 TREND & BOS | Short BOS Search
  2269. // --------------------------------------------------------------
  2270.  
  2271. else if nextTrend == -1 and (executeBosLevelSearch or breakoutPriceLegConsumption)
  2272.  
  2273. // If we are doing bos search after trend break or to consume breakout price leg then this is a special case
  2274. intermediarySearch = breakoutPriceLegConsumption or (bosBreached and executeGlobalTrendChange)
  2275.  
  2276. // Split away legs that were achieved below current global low
  2277. if intermediarySearch
  2278. [_void, splitBar] = findGlobalLow(globalL, globalLB, tLow, barIndex, aLFlt, aLInt)
  2279. // If consuming breakout legs, we must offset by user's input value, because we could have made closer global lows afterwards,
  2280. // but still need to consume only the original leg
  2281. if breakoutPriceLegConsumption
  2282. splitBar := barIndex - bosSameCandleLegConsumptionDelay
  2283. arSplit(aSFlt, aSInt, sFlt, sInt, false, splitBar)
  2284. // If normal search:
  2285. // split away entry added in the last bars in case this candle is a manipulation
  2286. else
  2287. splitOffset = 1 + (checkConditionManipulationShort(tfSensitiveSettings, tHigh, tLow, tClose, barIndex, globalLB) ? bosIgnoreManipulationsAmount : 0)
  2288. arSplit(aSFlt, aSInt, sFlt, sInt, false, barIndex - splitOffset)
  2289.  
  2290. // Output where we're checking Short BOS
  2291. if array.get(tfIndependentSettings, 06) and array.get(tfSensitiveSettings, 60)
  2292. debugText("BS\nS" + (breakoutPriceLegConsumption ? "B" : intermediarySearch ? "I" : "") +
  2293. "\nS"+str.tostring(arSizeFlt(sFlt))+"\nO"+str.tostring(arSizeFlt(aSFlt)), true)
  2294.  
  2295. // Remember first and last that passed our conditions
  2296. float firstPassedH = na
  2297. int firstPassedHB = na
  2298. float lastPassedH = na
  2299. int lastPassedHB = na
  2300. bool found = false
  2301. // Iterate until we find suitable LocalH or exhaust all options
  2302. while not found and array.size(aSFlt) > 0
  2303. // Get next leg to examine
  2304. [curSLocalH, curSLocalHB, curSLocalL, curSLocalLB, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, curSSidewaysF] = arPop(aSFlt, aSInt)
  2305. found := checkConditionGlobalChange(tfSensitiveSettings, tfIndependentSettings, aBarIndex, curSLocalHB,
  2306. globalHC, globalL, curSLocalH, curSLocalL, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, atr, false)
  2307. if array.get(tfSensitiveSettings, 61)
  2308. printPriceLeg(tfSensitiveSettings, tfIndependentSettings, aBarIndex,
  2309. curSLocalHB, curSLocalH, curSLocalLB, curSLocalL, curSClose, curSReverse, curSSidewaysL, curSSidewaysR, false, found)
  2310. // Record last passed High
  2311. if found
  2312. lastPassedH := curSLocalH
  2313. lastPassedHB := curSLocalHB
  2314. // Record first passed High
  2315. if na(firstPassedH)
  2316. firstPassedH := curSLocalH
  2317. firstPassedHB := curSLocalHB
  2318. // If we can look further, have price legs remaining, and have something to compare to
  2319. aSize = arSizeFlt(aSFlt)
  2320. if bosConsiderNextEnabled and aSize > 0 and firstPassedH
  2321. // If next point is within range - clear found flag to allow while loop to continue
  2322. // Also ensure next point isn't within old BOS in case we're breaching
  2323. float nextL = na
  2324. nextH = arGetFlt(00, aSFlt, aSize - 1)
  2325. nextMaxOC = array.get(aMaxOC, barIndex - arGetInt(00, aSInt, aSize - 1))
  2326. nextDiff = (nextH - globalL) / ((bosConsiderNextRecursive ? lastPassedH : firstPassedH) - globalL) - 1
  2327. nextConsumed = false
  2328. if bosConsiderNextConsumptionEnabled
  2329. nextConsumed := (lastPassedH - nextMaxOC) / (nextH - nextMaxOC) > bosConsiderNextConsumptionThreshold / 100
  2330. if not nextConsumed and nextDiff < bosConsiderNextDistanceThreshold / 100
  2331. and (not bosBreached or checkConditionNewBosBeyondOld(tfSensitiveSettings, nextTrend, nextH, nextMaxOC, bosLH, bosLL, bosSH, bosSL))
  2332. found := false
  2333. // If next point is not within range - break
  2334. else
  2335. break
  2336. // If we still have a localL value here that means we found a suitable local high to use as a new BOS
  2337. if lastPassedH
  2338. // Relocate global high and place BOS on it
  2339. executeBosLevelUpdate := true
  2340. globalH := lastPassedH
  2341. globalHB := lastPassedHB
  2342. globalHC := math.min(curNextHC, globalH)
  2343. // Set classic BOS bar in case we have an unused price leg, and that BOS is outside selected BOS by at least the threshold
  2344. if array.size(aSInt) > 0
  2345. // First we pick the last price leg, which is by definition the highest
  2346. classicB := arGetInt(00, aSInt, 0)
  2347. classicMaxOC = array.get(aMaxOC, barIndex - classicB)
  2348. classicDiff = classicMaxOC / lastPassedH - 1
  2349. // Classic BOS' closer boundary must be away from algorithm-chosen BOS by at least this much, otherwise it's discarded
  2350. if classicDiff <= array.get(tfSensitiveSettings, 01) / 100
  2351. classicB := na
  2352. arClear(aSFlt, aSInt, false, tfSensitiveSettings, tfIndependentSettings, aBarIndex, true)
  2353. arSplit(sFlt, sInt, aSFlt, aSInt)
  2354. if array.size(aSFlt) == 0
  2355. arPush(aSFlt, aSInt)
  2356. // If not doing intermittent search
  2357. if not intermediarySearch
  2358. // Update global low
  2359. globalL := tLow
  2360. globalLB := barIndex
  2361. // Waste long accumulated values
  2362. curLNextClose := na
  2363. curLNextReverse := na
  2364. // If not in bos breached mode the next highest close is reset here
  2365. // Otherwise it can also be reset if we confirm trend change and establish new BOS later
  2366. if not bosBreached
  2367. curNextHC := tMaxOC
  2368.  
  2369. // --------------------------------------------------------------
  2370. // 06.01.09 TREND & BOS | TREND CHANGE
  2371. // --------------------------------------------------------------
  2372.  
  2373. if executeGlobalTrendChange
  2374. // Global trend changed to SHORT
  2375. if globalTrendDirection == 1
  2376. globalTrendDirection := -1
  2377. [g_l, g_lb] = findGlobalLow(globalL, globalLB, tLow, barIndex, aLFlt, aLInt)
  2378. globalL := g_l
  2379. globalLB := g_lb
  2380. // Global trend changed to LONG
  2381. else
  2382. globalTrendDirection := 1
  2383. [g_h, g_hb] = findGlobalHigh(globalH, globalHB, tHigh, barIndex, aSFlt, aSInt)
  2384. globalH := g_h
  2385. globalHB := g_hb
  2386.  
  2387. // --------------------------------------------------------------
  2388. // 06.01.10 BOS LEVEL UPDATE
  2389. // --------------------------------------------------------------
  2390.  
  2391. // First confirm BOS breach if breached, otherwise no BOS Update occurs
  2392. if executeBosLevelUpdate
  2393. if bosBreached
  2394. highLow = globalTrendDirection == 1 ? globalL : globalH
  2395. openClose = globalTrendDirection == 1 ? array.get(aMinOC, barIndex - globalLB) : array.get(aMaxOC, barIndex - globalHB)
  2396. if checkConditionNewBosBeyondOld(tfSensitiveSettings, globalTrendDirection, highLow, openClose, bosLH, bosLL, bosSH, bosSL)
  2397. bosBreached := false
  2398.  
  2399. // --------------------------------------------------------------
  2400. // 06.01.10.01 BOS LEVEL UPDATE | LONG
  2401. // --------------------------------------------------------------
  2402.  
  2403. if globalTrendDirection == 1 and not bosBreached
  2404. // Waste short entries
  2405. arClear(aSFlt, aSInt, false, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2406. arPush(aSFlt, aSInt)
  2407. // Waste accumulated next lowest close
  2408. curNextLC := tMinOC
  2409. // Change BOS
  2410. if true
  2411. bosSB := globalLB
  2412. lookbackBos = barIndex - bosSB
  2413. bosSH := array.get(aMinOC, lookbackBos)
  2414. bosSL := array.get(aLow, lookbackBos)
  2415. // If current candle completely consumed BOS then ignore it
  2416. beginI = tMinOC <= bosSL ? 1 : 0
  2417. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2418. // Look ahead to narrow down the volume level range
  2419. for i = beginI to lookbackBos - 1
  2420. bosSH := math.min(bosSH, array.get(aMinOC, i))
  2421. // Immediately check for testing
  2422. bosTesting := (tLow < bosSH or array.get(aLow, 1) < bosSH)
  2423. // Change CBOS
  2424. if not na(classicB)
  2425. cbosSB := classicB
  2426. lookbackBos = barIndex - cbosSB
  2427. cbosSH := array.get(aMinOC, lookbackBos)
  2428. cbosSL := array.get(aLow, lookbackBos)
  2429. // If current candle completely consumed BOS then ignore it
  2430. beginI = tMinOC <= cbosSL ? 1 : 0
  2431. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2432. // Look ahead to narrow down the volume level range
  2433. for i = beginI to lookbackBos - 1
  2434. cbosSH := math.min(cbosSH, array.get(aMinOC, i))
  2435. bosLH := na
  2436. bosLL := na
  2437. cbosLH := na
  2438. cbosLL := na
  2439.  
  2440. // --------------------------------------------------------------
  2441. // 06.01.10.02 BOS LEVEL UPDATE | SHORT
  2442. // --------------------------------------------------------------
  2443.  
  2444. if globalTrendDirection == -1 and not bosBreached
  2445. // Waste long entries
  2446. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2447. arPush(aLFlt, aLInt)
  2448. // Waste accumulated next highest close
  2449. curNextHC := tMaxOC
  2450. // Change BOS
  2451. if true
  2452. bosLB := globalHB
  2453. lookbackBos = barIndex - bosLB
  2454. bosLL := array.get(aMaxOC, lookbackBos)
  2455. bosLH := array.get(aHigh, lookbackBos)
  2456. // If current candle completely consumed BOS then ignore it
  2457. beginI = tMaxOC >= bosLH ? 1 : 0
  2458. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2459. // Look ahead to narrow down the volume level range
  2460. for i = beginI to lookbackBos - 1
  2461. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2462. // Immediately check for testing
  2463. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2464. // Change CBOS
  2465. if not na(classicB)
  2466. cbosLB := classicB
  2467. lookbackBos = barIndex - cbosLB
  2468. cbosLL := array.get(aMaxOC, lookbackBos)
  2469. cbosLH := array.get(aHigh, lookbackBos)
  2470. // If current candle completely consumed BOS then ignore it
  2471. beginI = tMaxOC >= cbosLH ? 1 : 0
  2472. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2473. // Look ahead to narrow down the volume level range
  2474. for i = beginI to lookbackBos - 1
  2475. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2476. bosSH := na
  2477. bosSL := na
  2478. cbosSH := na
  2479. cbosSL := na
  2480.  
  2481. // --------------------------------------------------------------
  2482. // 06.01.11 TRIM ARRAYS
  2483. // --------------------------------------------------------------
  2484.  
  2485. if not array.get(tfIndependentSettings, 01)
  2486. maxLookback = math.max(2, barIndex - math.min(bosLB, bosSB)) + 1
  2487. while array.size(aHigh) > maxLookback
  2488. array.pop(aHigh)
  2489. array.pop(aLow)
  2490. array.pop(aOpen)
  2491. array.pop(aClose)
  2492. array.pop(aMaxOC)
  2493. array.pop(aMinOC)
  2494. array.pop(aPivotH)
  2495. array.pop(aPivotL)
  2496.  
  2497. // --------------------------------------------------------------
  2498. // 06.01.12 LAST BAR CLEANING
  2499. // --------------------------------------------------------------
  2500.  
  2501. // Cleaning house on last bar
  2502. if finalize
  2503. // Clear arrays so we can see their contents
  2504. arInit(aLFlt, aLInt, aSFlt, aSInt, tfSensitiveSettings, tfIndependentSettings, aBarIndex, true)
  2505.  
  2506. // --------------------------------------------------------------
  2507. // 06.01.13 FINISHED, PACK DATA
  2508. // --------------------------------------------------------------
  2509.  
  2510. arStateSet(aStateInt, aStateBool, globalTrendDirection, bosTesting, bosBreached, bosBreachCandles)
  2511. arValuesSet(aValuesFlt, aValuesInt, priorH, priorHB, priorL, priorLB, globalH, globalHB, globalL, globalLB,
  2512. globalHC, globalLC, curNextHC, curNextLC,
  2513. curLNextClose, curLNextReverse, curSNextClose, curSNextReverse,
  2514. bosLH, bosLL, bosLB, bosSH, bosSL, bosSB,
  2515. cbosLH, cbosLL, cbosLB, cbosSH, cbosSL, cbosSB)
  2516.  
  2517. // --------------------------------------------------------------
  2518. // 06.01 END TREND & BOS DETERMINATION ALGORITHM
  2519. // --------------------------------------------------------------
  2520. if globalTrendDirection != 0
  2521. if globalTrendDirection == -1 and not bosBreached
  2522. // Waste long entries
  2523. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2524. arPush(aLFlt, aLInt)
  2525. // Waste accumulated next highest close
  2526. curNextHC := tMaxOC
  2527. // Change BOS
  2528. if true
  2529. bosLB := globalHB
  2530. lookbackBos = barIndex - bosLB
  2531. bosLL := array.get(aMaxOC, lookbackBos)
  2532. bosLH := array.get(aHigh, lookbackBos)
  2533. // If current candle completely consumed BOS then ignore it
  2534. beginI = tMaxOC >= bosLH ? 1 : 0
  2535. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2536. // Look ahead to narrow down the volume level range
  2537. for i = beginI to lookbackBos - 1
  2538. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2539. // Immediately check for testing
  2540. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2541. // Change CBOS
  2542. if not na(classicB)
  2543. cbosLB := classicB
  2544. lookbackBos = barIndex - cbosLB
  2545. cbosLL := array.get(aMaxOC, lookbackBos)
  2546. cbosLH := array.get(aHigh, lookbackBos)
  2547. // If current candle completely consumed BOS then ignore it
  2548. beginI = tMaxOC >= cbosLH ? 1 : 0
  2549. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2550. // Look ahead to narrow down the volume level range
  2551. for i = beginI to lookbackBos - 1
  2552. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2553. bosSH := na
  2554. bosSL := na
  2555. cbosSH := na
  2556. cbosSL := na
  2557.  
  2558. if globalTrendDirection != 0
  2559. if globalTrendDirection == -1 and not bosBreached
  2560. // Waste long entries
  2561. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2562. arPush(aLFlt, aLInt)
  2563. // Waste accumulated next highest close
  2564. curNextHC := tMaxOC
  2565. // Change BOS
  2566. if true
  2567. bosLB := globalHB
  2568. lookbackBos = barIndex - bosLB
  2569. bosLL := array.get(aMaxOC, lookbackBos)
  2570. bosLH := array.get(aHigh, lookbackBos)
  2571. // If current candle completely consumed BOS then ignore it
  2572. beginI = tMaxOC >= bosLH ? 1 : 0
  2573. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2574. // Look ahead to narrow down the volume level range
  2575. for i = beginI to lookbackBos - 1
  2576. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2577. // Immediately check for testing
  2578. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2579. // Change CBOS
  2580. if not na(classicB)
  2581. cbosLB := classicB
  2582. lookbackBos = barIndex - cbosLB
  2583. cbosLL := array.get(aMaxOC, lookbackBos)
  2584. cbosLH := array.get(aHigh, lookbackBos)
  2585. // If current candle completely consumed BOS then ignore it
  2586. beginI = tMaxOC >= cbosLH ? 1 : 0
  2587. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2588. // Look ahead to narrow down the volume level range
  2589. for i = beginI to lookbackBos - 1
  2590. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2591. bosSH := na
  2592. bosSL := na
  2593. cbosSH := na
  2594. cbosSL := na
  2595.  
  2596. if globalTrendDirection != 0
  2597. if globalTrendDirection == -1 and not bosBreached
  2598. // Waste long entries
  2599. arClear(aLFlt, aLInt, true, tfSensitiveSettings, tfIndependentSettings, aBarIndex, not array.get(tfIndependentSettings, 05))
  2600. arPush(aLFlt, aLInt)
  2601. // Waste accumulated next highest close
  2602. curNextHC := tMaxOC
  2603. // Change BOS
  2604. if true
  2605. bosLB := globalHB
  2606. lookbackBos = barIndex - bosLB
  2607. bosLL := array.get(aMaxOC, lookbackBos)
  2608. bosLH := array.get(aHigh, lookbackBos)
  2609. // If current candle completely consumed BOS then ignore it
  2610. beginI = tMaxOC >= bosLH ? 1 : 0
  2611. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2612. // Look ahead to narrow down the volume level range
  2613. for i = beginI to lookbackBos - 1
  2614. bosLL:= math.max(bosLL, array.get(aMaxOC, i))
  2615. // Immediately check for testing
  2616. bosTesting := tHigh > bosLL or array.get(aHigh, 1) > bosLL
  2617. // Change CBOS
  2618. if not na(classicB)
  2619. cbosLB := classicB
  2620. lookbackBos = barIndex - cbosLB
  2621. cbosLL := array.get(aMaxOC, lookbackBos)
  2622. cbosLH := array.get(aHigh, lookbackBos)
  2623. // If current candle completely consumed BOS then ignore it
  2624. beginI = tMaxOC >= cbosLH ? 1 : 0
  2625. lookbackBos := lookbackBos <= beginI ? na : lookbackBos
  2626. // Look ahead to narrow down the volume level range
  2627. for i = beginI to lookbackBos - 1
  2628. cbosLL:= math.max(cbosLL, array.get(aMaxOC, i))
  2629. bosSH := na
  2630. bosSL := na
  2631. cbosSH := na
  2632. cbosSL := na
  2633.  
  2634. // --------------------------------------------------------------
  2635. // 06.02 ALGORITHM EXECUTION
  2636. // --------------------------------------------------------------
  2637.  
  2638. // This condition only processes selected bars (for debug purposes) an doesn't try to acquire any longer
  2639. inRange = not (bar_index < DBG_FirstBar or bar_index > DBG_LastBar)
  2640. if inRange
  2641. if initialCtfTrendDirection != 0
  2642. array.set(carStateInt, 0, initialCtfTrendDirection)
  2643. initialCtfTrendDirection := 0
  2644. algoTrendAndBosTick(bar_index, cBarIndex,
  2645. carHigh, carLow, carOpen, carClose, carMaxOC, carMinOC, carPivotH, carPivotL,
  2646. carLFlt, carLInt, carSFlt, carSInt,
  2647. ctfSensitiveSettings, tfIndependentSettings, carStateInt, carStateBool, carValuesFlt, carValuesInt,
  2648. barstate.islastconfirmedhistory or bar_index == DBG_LastBar)
  2649. // HTF Trend, Called every tick there was new data, and once to finalize
  2650. if inRange and calculateHtfTrend and htfBarIndex >=0 and (newHtfDataThisTick or barstate.islastconfirmedhistory)
  2651. if initialHtfTrendDirection != 0
  2652. array.set(harStateInt, 0, initialHtfTrendDirection)
  2653. initialHtfTrendDirection := 0
  2654. algoTrendAndBosTick(htfBarIndex, harBarIndex,
  2655. harHigh, harLow, harOpen, harClose, harMaxOC, harMinOC, harPivotH, harPivotL,
  2656. harLFlt, harLInt, harSFlt, harSInt,
  2657. htfSensitiveSettings, tfIndependentSettings, harStateInt, harStateBool, harValuesFlt, harValuesInt,
  2658. barstate.islastconfirmedhistory or bar_index == DBG_LastBar)
  2659. // Discard trend on last bar if processing select bars
  2660. if bar_index == DBG_LastBar + 1
  2661. array.set(carStateInt, 0, 0)
  2662. array.set(harStateInt, 0, 0)
  2663.  
  2664. // --------------------------------------------------------------
  2665. // 06.03 POST-PROCESSING
  2666. // --------------------------------------------------------------
  2667.  
  2668. // --------------------------------------------------------------
  2669. // 06.03.01 GET DATA
  2670. // --------------------------------------------------------------
  2671.  
  2672. [globalTrendDirection, bosTesting, bosBreached, bosBreachCandles] = arStateGet(carStateInt, carStateBool)
  2673. [priorH, priorHB, priorL, priorLB, globalH, globalHB, globalL, globalLB,
  2674. globalHC, globalLC, curNextHC, curNextLC,
  2675. curLNextClose, curLNextReverse, curSNextClose, curSNextReverse,
  2676. bosLH, bosLL, bosLB, bosSH, bosSL, bosSB,
  2677. cbosLH, cbosLL, cbosLB, cbosSH, cbosSL, cbosSB] = arValuesGet(carValuesFlt, carValuesInt)
  2678.  
  2679. [htfglobalTrendDirection, htfbosTesting, htfbosBreached, htfbosBreachCandles] = arStateGet(harStateInt, harStateBool)
  2680. [htfpriorH, _htfpriorHB, htfpriorL, _htfpriorLB, htfglobalH, _htfglobalHB, htfglobalL, _htfglobalLB,
  2681. _htfGlobalHC, _htfGlobalLC, _htfCurNextHC, _htfCurNextLC,
  2682. htfcurLNextClose, htfcurLNextReverse, htfcurSNextClose, htfcurSNextReverse,
  2683. htfbosLH, htfbosLL, _htfbosLB, htfbosSH, htfbosSL, _htfbosSB,
  2684. htfcbosLH, htfcbosLL, _htfcbosLB, htfcbosSH, htfcbosSL, _htfcbosSB] = arValuesGet(harValuesFlt, harValuesInt)
  2685.  
  2686. htfglobalHB = array.get(harBarIndex, _htfglobalHB)
  2687. htfglobalLB = array.get(harBarIndex, _htfglobalLB)
  2688. htfbosLB = array.get(harBarIndex, _htfbosLB)
  2689. htfbosSB = array.get(harBarIndex, _htfbosSB)
  2690. htfcbosLB = array.get(harBarIndex, _htfcbosLB)
  2691. htfcbosSB = array.get(harBarIndex, _htfcbosSB)
  2692.  
  2693. priorTrend = globalTrendDirection == 0 ? 0 : bosBreached ? -globalTrendDirection : globalTrendDirection
  2694. htfpriorTrend = htfglobalTrendDirection == 0 ? 0 : htfbosBreached ? -htfglobalTrendDirection : htfglobalTrendDirection
  2695.  
  2696. // --------------------------------------------------------------
  2697. // 06.03.02 TRIGGERS, ALERTS, EVENTS
  2698. // --------------------------------------------------------------
  2699.  
  2700. eventTrendBreak = bosBreached == true and bosBreached[1] == false
  2701. eventTrendReversalConfirmed = bosBreached == false and bosBreached[1] == true and globalTrendDirection == globalTrendDirection[1]
  2702. eventTrendResume = bosBreached == false and bosBreached[1] == true and globalTrendDirection != globalTrendDirection[1]
  2703. htfeventTrendBreak = htfbosBreached == true and htfbosBreached[1] == false
  2704. htfeventTrendReversalConfirmed = htfbosBreached == false and htfbosBreached[1] == true and htfglobalTrendDirection == htfglobalTrendDirection[1]
  2705. htfeventTrendResume = htfbosBreached == false and htfbosBreached[1] == true and htfglobalTrendDirection != htfglobalTrendDirection[1]
  2706.  
  2707. // --------------------------------------------------------------
  2708. // 07 OUTPUT
  2709. // --------------------------------------------------------------
  2710.  
  2711. // --------------------------------------------------------------
  2712. // 07.01 OUTPUT: DEBUG OUTPUT PLOTS
  2713. // --------------------------------------------------------------
  2714.  
  2715. float outhtfOpen = na
  2716. float outhtfHigh = na
  2717. float outhtfLow = na
  2718. float outhtfClose = na
  2719. float outhtfPivotHigh = na
  2720. float outhtfPivotLow = na
  2721. if array.size(harHigh) > 0 and newHtfDataThisTick
  2722. //debugText(bar_index)
  2723. if DBG_ShowHTFDebugPlots and DBG_DebugEnabled
  2724. outhtfOpen := array.get(harOpen, 0)
  2725. outhtfHigh := array.get(harHigh, 0)
  2726. outhtfLow := array.get(harLow, 0)
  2727. outhtfClose := array.get(harClose, 0)
  2728. if DBG_ShowHTFDebugPlots and DBG_DebugEnabled or showPPEnabled and showPPHtf
  2729. outhtfPivotHigh := na(array.get(harPivotH, 0)) ? array.get(harPivotH, 1) : na
  2730. outhtfPivotLow := na(array.get(harPivotL, 0)) ? array.get(harPivotL, 1) : na
  2731. // HTF Plots
  2732. plot(outhtfOpen, "HTF Open", color.purple, 2, offset = -htfOffset)
  2733. plot(outhtfHigh, "HTF High", color.fuchsia, 2, offset = -htfOffset)
  2734. plot(outhtfLow, "HTF Low", color.fuchsia, 2, offset = -htfOffset)
  2735. plot(outhtfClose, "HTF Close", color.purple, 2, offset = -htfOffset)
  2736. plotshape(DBG_ShowHTFDebugPlots ? outhtfPivotHigh : na, "HTF Pivot H", shape.xcross, location.absolute, color.fuchsia,
  2737. -htfOffset - htfSidewaysMultiplier, size = size.small)
  2738. plotshape(DBG_ShowHTFDebugPlots ? outhtfPivotLow : na, "HTF Pivot L", shape.xcross, location.absolute, color.fuchsia,
  2739. -htfOffset - htfSidewaysMultiplier, size = size.small)
  2740. // HLC / NHLC
  2741. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots and globalTrendDirection == 1 ? globalHC : na, "globalHC", color.red, linewidth = 3)
  2742. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots and globalTrendDirection == -1 ? globalLC : na, "globalLC", color.green, linewidth = 3)
  2743. // Maybe this was better? But uses x2 plots since color not const
  2744. // plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? globalHC : na, "globalHC", globalTrendDirection == 1 ? na : color.red, linewidth = 3)
  2745. // plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? globalLC : na, "globalLC", globalTrendDirection == -1 ? na : color.green, linewidth = 3)
  2746. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? curNextHC : na, "curNextHC", color.new(color.red, 60), linewidth = 2)
  2747. plot(DBG_DebugEnabled and DBG_ShowHLCDebugPlots ? curNextLC : na, "curNextLC", color.new(color.green, 60), linewidth = 2)
  2748.  
  2749. // --------------------------------------------------------------
  2750. // 07.02 OUTPUT: PIVOT POINTS
  2751. // --------------------------------------------------------------
  2752.  
  2753. plotshape(showPPEnabled and showPPCtf and not cPivotH ? cPivotH[1] : na, 'CTF High', shape.triangledown,
  2754. location.abovebar, color.new(color.green,50), -ctfOffset - 1, "H", color.new(color.green,50))
  2755. plotshape(showPPEnabled and showPPCtf and not cPivotL ? cPivotL[1] : na, 'CTF Low', shape.triangleup,
  2756. location.belowbar, color.new(color.red,50), -ctfOffset - 1, "L", color.new(color.red,50))
  2757.  
  2758. plotshape(showPPEnabled and showPPHtf ? outhtfPivotHigh : na, 'HTF High', shape.triangledown,
  2759. location.abovebar, color.new(color.green,30), -htfOffset - htfSidewaysMultiplier, '[H]', color.new(color.green,30))
  2760. plotshape(showPPEnabled and showPPHtf ? outhtfPivotLow : na, 'HTF Low', shape.triangleup,
  2761. location.belowbar, color.new(color.red,30), -htfOffset - htfSidewaysMultiplier, '[L]', color.new(color.red,30))
  2762.  
  2763. // --------------------------------------------------------------
  2764. // 07.03 OUTPUT: BOS
  2765. // --------------------------------------------------------------
  2766.  
  2767. // --------------------------------------------------------------
  2768. // 07.03.01 OUTPUT: CTF BOS
  2769. // --------------------------------------------------------------
  2770.  
  2771. // --------------------------------------------------------------
  2772. // 07.03.01.01 OUTPUT: CTF BOS | LONG
  2773. // --------------------------------------------------------------
  2774.  
  2775. // CTF Long BOS Colors
  2776. colorBosLongLineMainNormal = color.new(colorBosLongMain, ctfBosT3)
  2777. colorBosLongLineMainTesting = color.new(colorBosLongMain, ctfBosT4)
  2778. colorBosLongLineMainBreached = color.new(colorBosLongMain, ctfBosT5)
  2779. colorBosLongLineAuxNormal = color.new(colorBosLongAux, ctfBosT2)
  2780. colorBosLongLineAuxTesting = color.new(colorBosLongAux, ctfBosT3)
  2781. colorBosLongLineAuxBreached = color.new(colorBosLongMain, ctfBosT3)
  2782. colorBosLongBgNormal = color.new(colorBosLongAux, ctfBosT1)
  2783. colorBosLongBgTesting = color.new(colorBosLongAux, ctfBosT2)
  2784. colorBosLongBgBreached = color.new(colorBosLongMain, ctfBosT2)
  2785.  
  2786. // CTF Long BOS Draw
  2787. drawLongBos = showBosEnabled and priorTrend == -1
  2788. longBosUnchanged = bosLH == bosLH[1] and bosLL == bosLL[1]
  2789. drawLongBosRegular = drawLongBos and longBosUnchanged
  2790. drawLongBosOrigin = drawLongBos and not longBosUnchanged
  2791. colorBosLongLineMain = not drawLongBosRegular or not showBosCtfMainLine ? na :
  2792. bosTesting ? colorBosLongLineMainTesting : bosBreached ? colorBosLongLineMainBreached : colorBosLongLineMainNormal
  2793. colorBosLongLineAux = not drawLongBosRegular or not showBosCtfAuxLine? na :
  2794. bosTesting ? colorBosLongLineAuxTesting : bosBreached ? colorBosLongLineAuxBreached : colorBosLongLineAuxNormal
  2795. colorBosLongFill = not drawLongBosRegular or not showBosCtfFill ? na :
  2796. bosTesting ? colorBosLongBgTesting : bosBreached ? colorBosLongBgBreached : colorBosLongBgNormal
  2797. if drawLongBosOrigin and bosLH
  2798. if showBosCtfOriginMainLine
  2799. line.new(bosLB - ctfOffset, bosLH, bar_index - ctfOffset, bosLH, color = colorBosLongLineMainNormal, width = showBosCtfWidthMain)
  2800. if showBosCtfOriginAuxLine
  2801. line.new(bosLB - ctfOffset, bosLL, bar_index - ctfOffset, bosLL, color = colorBosLongLineAuxNormal, width = showBosCtfWidthAux)
  2802. if showBosCtfOriginFill
  2803. box.new(bosLB - ctfOffset, bosLH, bar_index - ctfOffset, bosLL, na, bgcolor = colorBosLongBgNormal)
  2804.  
  2805. // --------------------------------------------------------------
  2806. // 07.03.01.02 OUTPUT: CTF BOS | SHORT
  2807. // --------------------------------------------------------------
  2808.  
  2809. // CTF Short BOS Colors
  2810. colorBosShortLineMainNormal = color.new(colorBosShortMain, ctfBosT3)
  2811. colorBosShortLineMainTesting = color.new(colorBosShortMain, ctfBosT4)
  2812. colorBosShortLineMainBreached = color.new(colorBosShortMain, ctfBosT5)
  2813. colorBosShortLineAuxNormal = color.new(colorBosShortAux, ctfBosT2)
  2814. colorBosShortLineAuxTesting = color.new(colorBosShortAux, ctfBosT3)
  2815. colorBosShortLineAuxBreached = color.new(colorBosShortMain, ctfBosT3)
  2816. colorBosShortBgNormal = color.new(colorBosShortAux, ctfBosT1)
  2817. colorBosShortBgTesting = color.new(colorBosShortAux, ctfBosT2)
  2818. colorBosShortBgBreached = color.new(colorBosShortMain, ctfBosT2)
  2819.  
  2820. // CTF Short BOS Draw
  2821. drawShortBos = showBosEnabled and priorTrend == 1
  2822. shortBosUnchanged = bosSH == bosSH[1] and bosSL == bosSL[1]
  2823. drawShortBosRegular = drawShortBos and shortBosUnchanged
  2824. drawShortBosOrigin = drawShortBos and not shortBosUnchanged
  2825. colorBosShortLineAux = not drawShortBosRegular or not showBosCtfAuxLine? na :
  2826. bosTesting ? colorBosShortLineAuxTesting : bosBreached ? colorBosShortLineAuxBreached : colorBosShortLineAuxNormal
  2827. colorBosShortLineMain = not drawShortBosRegular or not showBosCtfMainLine? na :
  2828. bosTesting ? colorBosShortLineMainTesting : bosBreached ? colorBosShortLineMainBreached : colorBosShortLineMainNormal
  2829. colorBosShortFill = not drawShortBosRegular or not showBosCtfFill? na :
  2830. bosTesting ? colorBosShortBgTesting : bosBreached ? colorBosShortBgBreached : colorBosShortBgNormal
  2831. if drawShortBosOrigin and bosSH
  2832. if showBosCtfOriginAuxLine
  2833. line.new(bosSB - ctfOffset, bosSH, bar_index - ctfOffset, bosSH, color = colorBosShortLineAuxNormal, width = showBosCtfWidthAux)
  2834. if showBosCtfOriginMainLine
  2835. line.new(bosSB - ctfOffset, bosSL, bar_index - ctfOffset, bosSL, color = colorBosShortLineMainNormal, width = showBosCtfWidthMain)
  2836. if showBosCtfOriginFill
  2837. box.new(bosSB - ctfOffset, bosSH, bar_index - ctfOffset, bosSL, na, bgcolor = colorBosShortBgNormal)
  2838.  
  2839. // --------------------------------------------------------------
  2840. // 07.03.01.03 OUTPUT: CTF BOS | PLOT
  2841. // --------------------------------------------------------------
  2842.  
  2843. ctfblh = plot(priorTrend == 1 ? bosSL : bosLH, "CTF BOS Main", priorTrend == 1 ? colorBosShortLineMain : colorBosLongLineMain, showBosCtfWidthMain,
  2844. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2845. ctfbll = plot(priorTrend == 1 ? bosSH : bosLL, "CTF BOS Aux", priorTrend == 1 ? colorBosShortLineAux : colorBosLongLineAux, showBosCtfWidthAux,
  2846. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2847. fill(ctfblh, ctfbll, priorTrend == 1 ? colorBosShortFill : colorBosLongFill , "CTF BOS Fill", editable = false)
  2848.  
  2849. // --------------------------------------------------------------
  2850. // 07.03.02 OUTPUT: CTF CLASSIC BOS
  2851. // --------------------------------------------------------------
  2852.  
  2853. // --------------------------------------------------------------
  2854. // 07.03.02.01 OUTPUT: CTF CLASSIC BOS | LONG
  2855. // --------------------------------------------------------------
  2856.  
  2857. // CTF Long Classic BOS Colors
  2858. colorCBosLongLineMainNormal = color.new(colorCBosLongMain, ctfCBosT3)
  2859. colorCBosLongLineAuxNormal = color.new(colorCBosLongAux, ctfCBosT2)
  2860. colorCBosLongBgNormal = color.new(colorCBosLongAux, ctfCBosT1)
  2861.  
  2862. // CTF Long Classic BOS Draw
  2863. var float hiddenLH = na
  2864. if showCBosHideCollision and cbosLH == htfbosLH
  2865. hiddenLH := htfbosLH
  2866. drawLongCBos = showBosEnabled and showCBosEnabled and priorTrend == -1 and (na(hiddenLH) or cbosLH != hiddenLH)
  2867. longCBosUnchanged = cbosLH == cbosLH[1] and cbosLL == cbosLL[1]
  2868. drawLongCBosRegular = drawLongCBos and longCBosUnchanged
  2869. drawLongCBosOrigin = drawLongCBos and not longCBosUnchanged
  2870. colorCBosLongLineMain = not drawLongCBosRegular or not showCBosCtfAuxLine ? na : colorCBosLongLineMainNormal
  2871. colorCBosLongLineAux = not drawLongCBosRegular or not showCBosCtfAuxLine ? na : colorCBosLongLineAuxNormal
  2872. colorCBosLongFill = not drawLongCBosRegular or not showCBosCtfFill ? na : colorCBosLongBgNormal
  2873. if drawLongCBosOrigin and cbosLH
  2874. if showCBosCtfOriginMainLine
  2875. line.new(cbosLB - ctfOffset, cbosLH, bar_index - ctfOffset, cbosLH, color = colorCBosLongLineMainNormal, width = showCBosCtfWidthMain)
  2876. if showCBosCtfOriginAuxLine
  2877. line.new(cbosLB - ctfOffset, cbosLL, bar_index - ctfOffset, cbosLL, color = colorCBosLongLineAuxNormal, width = showCBosCtfWidthAux)
  2878. if showCBosCtfOriginFill
  2879. box.new(cbosLB - ctfOffset, cbosLH, bar_index - ctfOffset, cbosLL, na, bgcolor = colorCBosLongBgNormal)
  2880.  
  2881. // --------------------------------------------------------------
  2882. // 07.03.02.02 OUTPUT: CTF CLASSIC BOS | SHORT
  2883. // --------------------------------------------------------------
  2884.  
  2885. // CTF Short Classic BOS Colors
  2886. colorCBosShortLineMainNormal = color.new(colorCBosShortMain, ctfCBosT3)
  2887. colorCBosShortLineAuxNormal = color.new(colorCBosShortAux, ctfCBosT2)
  2888. colorCBosShortBgNormal = color.new(colorCBosShortAux, ctfCBosT1)
  2889.  
  2890. // CTF Short Classic BOS Draw
  2891. var float hiddenSL = na
  2892. if showCBosHideCollision and cbosSL == htfbosSL
  2893. hiddenSL := htfbosSL
  2894. drawShortCBos = showBosEnabled and showCBosEnabled and priorTrend == 1 and (na(hiddenSL) or cbosSL != hiddenSL)
  2895. shortCBosUnchanged = cbosSH == cbosSH[1] and cbosSL == cbosSL[1]
  2896. drawShortCBosRegular = drawShortCBos and shortCBosUnchanged
  2897. drawShortCBosOrigin = drawShortCBos and not shortCBosUnchanged
  2898. colorCBosShortLineAux = not drawShortCBosRegular or not showCBosCtfAuxLine ? na : colorCBosShortLineAuxNormal
  2899. colorCBosShortLineMain = not drawShortCBosRegular or not showCBosCtfAuxLine ? na : colorCBosShortLineMainNormal
  2900. colorCBosShortFill = not drawShortCBosRegular or not showCBosCtfFill? na : colorCBosShortBgNormal
  2901. if drawShortCBosOrigin and cbosSH
  2902. if showCBosCtfOriginAuxLine
  2903. line.new(cbosSB - ctfOffset, cbosSH, bar_index - ctfOffset, cbosSH, color = colorCBosShortLineAuxNormal, width = showCBosCtfWidthAux)
  2904. if showCBosCtfOriginMainLine
  2905. line.new(cbosSB - ctfOffset, cbosSL, bar_index - ctfOffset, cbosSL, color = colorCBosShortLineMainNormal, width = showCBosCtfWidthMain)
  2906. if showCBosCtfOriginFill
  2907. box.new(cbosSB - ctfOffset, cbosSH, bar_index - ctfOffset, cbosSL, na, bgcolor = colorCBosShortBgNormal)
  2908.  
  2909. // --------------------------------------------------------------
  2910. // 07.03.02.03 OUTPUT: CTF CLASSIC BOS | PLOT
  2911. // --------------------------------------------------------------
  2912.  
  2913. ctfcblh = plot(priorTrend == 1 ? cbosSL : cbosLH, "CTF CBOS Main", priorTrend == 1 ? colorCBosShortLineMain : colorCBosLongLineMain, showCBosCtfWidthMain,
  2914. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2915. ctfcbll = plot(priorTrend == 1 ? cbosSH : cbosLL, "CTF CBOS Aux", priorTrend == 1 ? colorCBosShortLineAux : colorCBosLongLineAux, showCBosCtfWidthAux,
  2916. plot.style_line, offset = -ctfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2917. fill(ctfcblh, ctfcbll, priorTrend == 1 ? colorCBosShortFill : colorCBosLongFill, "CTF CBOS Fill", editable = false)
  2918.  
  2919. // --------------------------------------------------------------
  2920. // 07.03.03 OUTPUT: HTF BOS
  2921. // --------------------------------------------------------------
  2922.  
  2923. // --------------------------------------------------------------
  2924. // 07.03.03.01 OUTPUT: HTF BOS | LONG
  2925. // --------------------------------------------------------------
  2926.  
  2927. // HTF Long BOS Colors
  2928. htfcolorBosLongLineMainNormal = color.new(htfcolorBosLongMain, htfBosT3)
  2929. htfcolorBosLongLineMainTesting = color.new(htfcolorBosLongMain, htfBosT4)
  2930. htfcolorBosLongLineMainBreached = color.new(htfcolorBosLongMain, htfBosT5)
  2931. htfcolorBosLongLineAuxNormal = color.new(htfcolorBosLongAux, htfBosT2)
  2932. htfcolorBosLongLineAuxTesting = color.new(htfcolorBosLongAux, htfBosT3)
  2933. htfcolorBosLongLineAuxBreached = color.new(htfcolorBosLongMain, htfBosT2)
  2934. htfcolorBosLongBgNormal = color.new(htfcolorBosLongAux, htfBosT1)
  2935. htfcolorBosLongBgTesting = color.new(htfcolorBosLongAux, htfBosT2)
  2936. htfcolorBosLongBgBreached = color.new(htfcolorBosLongMain, htfBosT2)
  2937.  
  2938. // HTF Long BOS Draw
  2939. htfdrawLongBos = showBosEnabled and htfpriorTrend == -1
  2940. htflongBosUnchanged = htfbosLH == htfbosLH[1] and htfbosLL == htfbosLL[1]
  2941. htfdrawLongBosRegular = htfdrawLongBos and htflongBosUnchanged
  2942. htfdrawLongBosOrigin = htfdrawLongBos and not htflongBosUnchanged
  2943. htfcolorBosLongLineMain = not htfdrawLongBosRegular or not showBosHtfMainLine ? na :
  2944. htfbosTesting ? htfcolorBosLongLineMainTesting : htfbosBreached ? htfcolorBosLongLineMainBreached : htfcolorBosLongLineMainNormal
  2945. htfcolorBosLongLineAux = not htfdrawLongBosRegular or not showBosHtfAuxLine ? na :
  2946. htfbosTesting ? htfcolorBosLongLineAuxTesting : htfbosBreached ? htfcolorBosLongLineAuxBreached : htfcolorBosLongLineAuxNormal
  2947. htfcolorBosLongFill = not htfdrawLongBosRegular or not showBosHtfFill ? na :
  2948. htfbosTesting ? htfcolorBosLongBgTesting : htfbosBreached ? htfcolorBosLongBgBreached : htfcolorBosLongBgNormal
  2949. if htfdrawLongBosOrigin and htfbosLH
  2950. if showBosHtfOriginMainLine
  2951. line.new(htfbosLB - htfOffset, htfbosLH, bar_index - htfOffset, htfbosLH, color = htfcolorBosLongLineMainNormal, width = showBosHtfWidthMain)
  2952. if showBosHtfOriginAuxLine
  2953. line.new(htfbosLB - htfOffset, htfbosLL, bar_index - htfOffset, htfbosLL, color = htfcolorBosLongLineAuxNormal, width = showBosHtfWidthAux)
  2954. if showBosHtfOriginFill
  2955. box.new(htfbosLB - htfOffset, htfbosLH, bar_index - htfOffset, htfbosLL, na, bgcolor = htfcolorBosLongBgNormal)
  2956.  
  2957. // --------------------------------------------------------------
  2958. // 07.03.03.02 OUTPUT: HTF BOS | SHORT
  2959. // --------------------------------------------------------------
  2960.  
  2961. // HTF Short BOS Colors
  2962. htfcolorBosShortLineMainNormal = color.new(htfcolorBosShortMain, htfBosT3)
  2963. htfcolorBosShortLineMainTesting = color.new(htfcolorBosShortMain, htfBosT4)
  2964. htfcolorBosShortLineMainBreached = color.new(htfcolorBosShortMain, htfBosT5)
  2965. htfcolorBosShortLineAuxNormal = color.new(htfcolorBosShortAux, htfBosT2)
  2966. htfcolorBosShortLineAuxTesting = color.new(htfcolorBosShortAux, htfBosT3)
  2967. htfcolorBosShortLineAuxBreached = color.new(htfcolorBosShortMain, htfBosT3)
  2968. htfcolorBosShortBgNormal = color.new(htfcolorBosShortAux, htfBosT1)
  2969. htfcolorBosShortBgTesting = color.new(htfcolorBosShortAux, htfBosT2)
  2970. htfcolorBosShortBgBreached = color.new(htfcolorBosShortMain, htfBosT2)
  2971.  
  2972. // HTF Short BOS Draw
  2973. htfdrawShortBos = showBosEnabled and htfpriorTrend == 1
  2974. htfshortBosUnchanged = htfbosSH == htfbosSH[1] and htfbosSL == htfbosSL[1]
  2975. htfdrawShortBosRegular = htfdrawShortBos and htfshortBosUnchanged
  2976. htfdrawShortBosOrigin = htfdrawShortBos and not htfshortBosUnchanged
  2977. htfcolorBosShortLineAux = not htfdrawShortBosRegular or not showBosHtfAuxLine ? na :
  2978. htfbosTesting ? htfcolorBosShortLineAuxTesting : htfbosBreached ? htfcolorBosShortLineAuxBreached : htfcolorBosShortLineAuxNormal
  2979. htfcolorBosShortLineMain = not htfdrawShortBosRegular or not showBosHtfMainLine? na :
  2980. htfbosTesting ? htfcolorBosShortLineMainTesting : htfbosBreached ? htfcolorBosShortLineMainBreached : htfcolorBosShortLineMainNormal
  2981. htfcolorBosShortFill = not htfdrawShortBosRegular or not showBosHtfFill ? na :
  2982. htfbosTesting ? htfcolorBosShortBgTesting : htfbosBreached ? htfcolorBosShortBgBreached : htfcolorBosShortBgNormal
  2983. if htfdrawShortBosOrigin and htfbosSH
  2984. if showBosHtfOriginAuxLine
  2985. line.new(htfbosSB - htfOffset, htfbosSH, bar_index - htfOffset, htfbosSH, color = htfcolorBosShortLineAuxNormal, width = showBosHtfWidthAux)
  2986. if showBosHtfOriginMainLine
  2987. line.new(htfbosSB - htfOffset, htfbosSL, bar_index - htfOffset, htfbosSL, color = htfcolorBosShortLineMainNormal, width = showBosHtfWidthMain)
  2988. if showBosHtfOriginFill
  2989. box.new(htfbosSB - htfOffset, htfbosSH, bar_index - htfOffset, htfbosSL, na, bgcolor = htfcolorBosShortBgNormal)
  2990.  
  2991. // --------------------------------------------------------------
  2992. // 07.03.03.03 OUTPUT: HTF BOS | PLOT
  2993. // --------------------------------------------------------------
  2994.  
  2995. htfblh = plot(htfpriorTrend == 1 ? htfbosSL : htfbosLH, "HTF BOS Main", htfpriorTrend == 1 ? htfcolorBosShortLineMain : htfcolorBosLongLineMain, showBosHtfWidthMain,
  2996. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2997. htfbll = plot(htfpriorTrend == 1 ? htfbosSH : htfbosLL, "HTF BOS Aux", htfpriorTrend == 1 ? htfcolorBosShortLineAux : htfcolorBosLongLineAux, showBosHtfWidthAux,
  2998. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  2999. fill(htfblh, htfbll, htfpriorTrend == 1 ? htfcolorBosShortFill : htfcolorBosLongFill , "HTF BOS Fill", editable = false)
  3000.  
  3001. // --------------------------------------------------------------
  3002. // 07.03.04 OUTPUT: HTF CLASSIC BOS
  3003. // --------------------------------------------------------------
  3004.  
  3005. // --------------------------------------------------------------
  3006. // 07.03.04.01 OUTPUT: HTF CLASSIC BOS | LONG
  3007. // --------------------------------------------------------------
  3008.  
  3009. // HTF Long Classic BOS Colors
  3010. htfcolorCBosLongLineMainNormal = color.new(htfcolorCBosLongMain, htfCBosT3)
  3011. htfcolorCBosLongLineAuxNormal = color.new(htfcolorCBosLongAux, htfCBosT2)
  3012. htfcolorCBosLongBgNormal = color.new(htfcolorCBosLongAux, htfCBosT1)
  3013.  
  3014. // HTF Long Classic BOS Draw
  3015. htfdrawLongCBos = showBosEnabled and showCBosEnabled and htfpriorTrend == -1
  3016. htflongCBosUnchanged = htfcbosLH == htfcbosLH[1] and htfcbosLL == htfcbosLL[1]
  3017. htfdrawLongCBosRegular = htfdrawLongCBos and htflongCBosUnchanged
  3018. htfdrawLongCBosOrigin = htfdrawLongCBos and not htflongCBosUnchanged
  3019. htfcolorCBosLongLineMain = not htfdrawLongCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosLongLineMainNormal
  3020. htfcolorCBosLongLineAux = not htfdrawLongCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosLongLineAuxNormal
  3021. htfcolorCBosLongFill = not htfdrawLongCBosRegular or not showCBosHtfFill ? na : htfcolorCBosLongBgNormal
  3022. if htfdrawLongCBosOrigin and htfcbosLH
  3023. if showCBosHtfOriginMainLine
  3024. line.new(htfcbosLB - htfOffset, htfcbosLH, bar_index - htfOffset, htfcbosLH, color = htfcolorCBosLongLineMainNormal, width = showCBosHtfWidthMain)
  3025. if showCBosHtfOriginAuxLine
  3026. line.new(htfcbosLB - htfOffset, htfcbosLL, bar_index - htfOffset, htfcbosLL, color = htfcolorCBosLongLineAuxNormal, width = showCBosHtfWidthAux)
  3027. if showCBosHtfOriginFill
  3028. box.new(htfcbosLB - htfOffset, htfcbosLH, bar_index - htfOffset, htfcbosLL, na, bgcolor = htfcolorCBosLongBgNormal)
  3029.  
  3030. // --------------------------------------------------------------
  3031. // 07.03.04.02 OUTPUT: HTF CLASSIC BOS | SHORT
  3032. // --------------------------------------------------------------
  3033.  
  3034. // HTF Short Classic BOS Colors
  3035. htfcolorCBosShortLineMainNormal = color.new(htfcolorCBosShortMain, htfCBosT3)
  3036. htfcolorCBosShortLineAuxNormal = color.new(htfcolorCBosShortAux, htfCBosT2)
  3037. htfcolorCBosShortBgNormal = color.new(htfcolorCBosShortAux, htfCBosT1)
  3038.  
  3039. // HTF Short Classic BOS Draw
  3040. htfdrawShortCBos = showBosEnabled and showCBosEnabled and htfpriorTrend == 1
  3041. htfshortCBosUnchanged = htfcbosSH == htfcbosSH[1] and htfcbosSL == htfcbosSL[1]
  3042. htfdrawShortCBosRegular = htfdrawShortCBos and htfshortCBosUnchanged
  3043. htfdrawShortCBosOrigin = htfdrawShortCBos and not htfshortCBosUnchanged
  3044. htfcolorCBosShortLineAux = not htfdrawShortCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosShortLineAuxNormal
  3045. htfcolorCBosShortLineMain = not htfdrawShortCBosRegular or not showCBosHtfAuxLine ? na : htfcolorCBosShortLineMainNormal
  3046. htfcolorCBosShortFill = not htfdrawShortCBosRegular or not showCBosHtfFill ? na : htfcolorCBosShortBgNormal
  3047. if htfdrawShortCBosOrigin and htfcbosSH
  3048. if showCBosHtfOriginAuxLine
  3049. line.new(htfcbosSB - htfOffset, htfcbosSH, bar_index - htfOffset, htfcbosSH, color = htfcolorCBosShortLineAuxNormal, width = showCBosHtfWidthAux)
  3050. if showCBosHtfOriginMainLine
  3051. line.new(htfcbosSB - htfOffset, htfcbosSL, bar_index - htfOffset, htfcbosSL, color = htfcolorCBosShortLineMainNormal, width = showCBosHtfWidthMain)
  3052. if showCBosHtfOriginFill
  3053. box.new(htfcbosSB - htfOffset, htfcbosSH, bar_index - ctfOffset, htfcbosSL, na, bgcolor = htfcolorCBosShortBgNormal)
  3054.  
  3055. // --------------------------------------------------------------
  3056. // 07.03.04.03 OUTPUT: HTF CLASSIC BOS | PLOT
  3057. // --------------------------------------------------------------
  3058.  
  3059. htfcblh = plot(htfpriorTrend == 1 ? htfcbosSL : htfcbosLH, "HTF CBOS Main", htfpriorTrend == 1 ? htfcolorCBosShortLineMain : htfcolorCBosLongLineMain, showCBosHtfWidthMain,
  3060. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  3061. htfcbll = plot(htfpriorTrend == 1 ? htfcbosSH : htfcbosLL, "HTF CBOS Aux", htfpriorTrend == 1 ? htfcolorCBosShortLineAux : htfcolorCBosLongLineAux, showCBosHtfWidthAux,
  3062. plot.style_line, offset = -htfOffset, editable = false, display = display.pane + display.data_window + display.status_line)
  3063. fill(htfcblh, htfcbll, htfpriorTrend == 1 ? htfcolorCBosShortFill : htfcolorCBosLongFill, "HTF CBOS Fill", editable = false)
  3064.  
  3065. // --------------------------------------------------------------
  3066. // 07.04 OUTPUT: HH/LL LEVELS
  3067. // --------------------------------------------------------------
  3068.  
  3069. // 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
  3070.  
  3071. // Highs
  3072. colorHighPrior = color.new(color.green, 70)
  3073. colorHighGlobal = color.new(color.yellow, 70)
  3074. outPriorH = bosBreached ? priorH : globalH
  3075. outputPriorH = showHhLlLevels and priorTrend == 1
  3076. outputGlobalH = showHhLlLevels and globalTrendDirection == 1 and bosBreached
  3077. plot(outputPriorH ? outPriorH : na, "Prior High", outputPriorH and outPriorH == outPriorH[1] ? colorHighPrior : na, offset = -ctfOffset,
  3078. editable = false, display = display.pane + display.data_window + display.status_line)
  3079. plot(outputGlobalH ? globalH : na, "Global High", outputGlobalH and globalH == globalH[1] ? colorHighGlobal : na, offset = -ctfOffset,
  3080. editable = false, display = display.pane + display.data_window + display.status_line)
  3081. if showHhLlLevelsOrigin
  3082. if outputPriorH and globalTrendDirection[1] == 0
  3083. line.new(globalHB - ctfOffset, outPriorH, bar_index - ctfOffset, outPriorH, color = colorHighPrior, width = 1)
  3084. if outputGlobalH and not bosBreached[1]
  3085. line.new(globalHB - ctfOffset, globalH, bar_index - ctfOffset, globalH, color = colorHighGlobal, width = 1)
  3086. // Lows
  3087. colorLowPrior = color.new(color.red, 70)
  3088. colorLowGlobal = color.new(color.orange, 70)
  3089. outPriorL = bosBreached ? priorL : globalL
  3090. outputPriorL = showHhLlLevels and priorTrend == -1
  3091. outputGlobalL = showHhLlLevels and globalTrendDirection == -1 and bosBreached
  3092. plot(outputPriorL ? outPriorL : na, "Prior Low", outputPriorL and outPriorL == outPriorL[1] ? colorLowPrior : na, offset = -ctfOffset,
  3093. editable = false, display = display.pane + display.data_window + display.status_line)
  3094. plot(outputGlobalL ? globalL : na, "Global Low", outputGlobalL and globalL == globalL[1] ? colorLowGlobal : na, offset = -ctfOffset,
  3095. editable = false, display = display.pane + display.data_window + display.status_line)
  3096. if showHhLlLevelsOrigin
  3097. if outputPriorL and globalTrendDirection[1] == 0
  3098. line.new(globalLB - ctfOffset, outPriorL, bar_index - ctfOffset, outPriorL, color = colorLowPrior, width = 1)
  3099. if outputGlobalL and not bosBreached[1]
  3100. line.new(globalLB - ctfOffset, globalL, bar_index - ctfOffset, globalL, color = colorLowGlobal, width = 1)
  3101.  
  3102. // -----------------------------
  3103. // 07.05 OUTPUT TREND BG / EDGE
  3104. // -----------------------------
  3105.  
  3106. displayTrendBgDirection = showTrendBG == 1 ? globalTrendDirection : showTrendBG == 2 ? htfglobalTrendDirection : 0
  3107. displayTrendBgWeak = showTrendBG == 1 ? bosBreached : showTrendBG == 2 ? htfbosBreached : 0
  3108. displayTrendBgOffset = showTrendBG == 1 ? ctfOffset : showTrendBG == 2 ? htfOffset : 0
  3109. displayTrendAboveDirection = showTrendAbove == 1 ? globalTrendDirection : showTrendAbove == 2 ? htfglobalTrendDirection : 0
  3110. displayTrendAboveWeak = showTrendAbove == 1 ? bosBreached : showTrendAbove == 2 ? htfbosBreached : 0
  3111. displayTrendAboveOffset = showTrendAbove == 1 ? ctfOffset : showTrendAbove == 2 ? htfOffset : 0
  3112. displayTrendBelowDirection = showTrendBelow == 1 ? globalTrendDirection : showTrendBelow == 2 ? htfglobalTrendDirection : 0
  3113. displayTrendBelowWeak = showTrendBelow == 1 ? bosBreached : showTrendBelow == 2 ? htfbosBreached : 0
  3114. displayTrendBelowOffset = showTrendBelow == 1 ? ctfOffset : showTrendBelow == 2 ? htfOffset : 0
  3115.  
  3116. bgcolor(displayTrendBgDirection == 0 ? na :
  3117. displayTrendBgDirection == 1 ?
  3118. (displayTrendBgWeak ? trendBgLongAux: trendBgLongMain) :
  3119. (displayTrendBgWeak ? trendBgShortAux : trendBgShortMain),
  3120. -displayTrendBgOffset)
  3121. plotshape(displayTrendAboveDirection == 0 ? na : displayTrendAboveDirection, "Trend Display: Above", shape.circle, location.top,
  3122. displayTrendAboveDirection == 1 ?
  3123. (displayTrendAboveWeak ? trendEdgeLongAux : trendEdgeLongMain) :
  3124. (displayTrendAboveWeak ? trendEdgeShortAux : trendEdgeShortMain), -displayTrendAboveOffset, size = size.tiny)
  3125. plotshape(displayTrendBelowDirection == 0 ? na : displayTrendBelowDirection, "Trend Display: Below", shape.circle, location.bottom,
  3126. displayTrendBelowDirection == 1 ?
  3127. (displayTrendBelowWeak ? trendEdgeLongAux : trendEdgeLongMain) :
  3128. (displayTrendBelowWeak ? trendEdgeShortAux : trendEdgeShortMain), -displayTrendBelowOffset, size = size.tiny)
  3129.  
  3130. // --------------------------------------------------------------
  3131. // 07.06 OUTPUT EVENTS
  3132. // --------------------------------------------------------------
  3133.  
  3134. if showTrendLabels
  3135. if showTrendLabelsCtf
  3136. if eventTrendBreak
  3137. printLabel(globalTrendDirection, bar_index - ctfOffset,"CTF\nTrend\nBreak!", globalTrendDirection != 1, false)
  3138. if eventTrendReversalConfirmed
  3139. printLabel(globalTrendDirection, bar_index - ctfOffset,"CTF\nTrend\nReversal\nConfirmed!", globalTrendDirection != 1, true)
  3140. if eventTrendResume
  3141. printLabel(globalTrendDirection, bar_index - ctfOffset,"CTF\nTrend\nResume!", globalTrendDirection == 1, true)
  3142. if showTrendLabelsHtf
  3143. if htfeventTrendBreak
  3144. printLabel(htfglobalTrendDirection, bar_index - htfOffset,"HTF\nTrend\nBreak!", htfglobalTrendDirection != 1, false)
  3145. if htfeventTrendReversalConfirmed
  3146. printLabel(htfglobalTrendDirection, bar_index - htfOffset,"HTF\nTrend\nReversal\nConfirmed!", htfglobalTrendDirection != 1, true)
  3147. if htfeventTrendResume
  3148. printLabel(htfglobalTrendDirection, bar_index - htfOffset,"HTF\nTrend\nResume!", htfglobalTrendDirection == 1, true)
  3149.  
  3150. // --------------------------------------------------------------
  3151. // 07.XX OUTPUT: DEBUG
  3152. // --------------------------------------------------------------
  3153.  
  3154. // plotshape(bosBreachCandles, "bosBreachCandles", color=na, offset = -ctfOffset)
  3155.  
  3156. if barstate.islast
  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_DebugEnabled
  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_FirstLimitOn
  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 barstate.islast and DBG_ShowHTFDebugPlots
  3172. tb = table.new(position.top_right, 1, 2, bgcolor = #111111, border_width = 1, border_color = color.gray, frame_width = 1, frame_color = color.gray)
  3173. table.cell(tb, 0, 0, str.tostring(0,format.mintick), text_color = color.white, text_size = size.small)
  3174. table.cell(tb, 0, 1, str.tostring(0/close*100,"#.##")+"%", text_color = color.white, text_size = size.small)
  3175.  
  3176. if true
  3177. na
  3178. if true
  3179. na
  3180. if true
  3181. na
  3182. if true
  3183. na
  3184. if true
  3185. na
  3186. if true
  3187. na
  3188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement