xmd79

MarketStructure

Dec 31st, 2022
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.24 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. // Β© SimpleCryptoLife
  3.  
  4. //@version=5
  5.  
  6. // @description This library contains functions for identifying Lows and Highs in a rule-based way, and deriving useful information from them.
  7.  
  8. library("MarketStructure", overlay=true)
  9.  
  10. string in_showFunction = input.string(title="Show Function", defval="f_localLowHigh", options=["f_simpleLowHigh","f_localLowHigh","f_enhancedSimpleLowHigh", "f_trueLowHigh"])
  11.  
  12. // Variables derived entirely from inputs
  13. var bool showSimple = in_showFunction == "f_simpleLowHigh"
  14. var bool showLocal = in_showFunction == "f_localLowHigh"
  15. var bool showEnhanced = in_showFunction == "f_enhancedSimpleLowHigh"
  16. var bool showTrue = in_showFunction == "f_trueLowHigh"
  17.  
  18.  
  19. import SimpleCryptoLife/Labels/2 as SCL_Label
  20. color labelColour = color.new(color.white,50)
  21.  
  22.  
  23.  
  24. // 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦 FUNCTION 1 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦
  25.  
  26. // @function _simpleLowHigh() // This function finds Local Lows and Highs, but NOT in order. A Local High is any candle that has its Low taken out on close by a subsequent candle (and vice-versa for Local Lows).
  27. // The Local High does NOT have to be the candle with the highest High out of recent candles. It does NOT have to be a Williams High. It is not necessarily a swing high or a reversal or anything else.
  28. // It doesn't have to be "the" high, so don't be confused.
  29. // By the rules, Local Lows and Highs must alternate. In this function they do not, so I'm calling them Simple Lows and Highs.
  30. // Simple Highs and Lows, by the above definition, can be useful for entries and stops. Because I intend to use them for stops, I want them all, not just the ones that alternate in strict order.
  31. // @param - there are no parameters. The function uses the chart OHLC.
  32. // @returns boolean values for whether this bar confirms a Simple Low/High, and ints for the bar_index of that Low/High.
  33.  
  34. export f_simpleLowHigh() =>
  35. var float _trailingLow = na, var float _trailingHigh = na // Trail for the low (which is used to confirm Highs), and the high (used to confirm Lows).
  36. var int _simpleHighIndex = na, var int _simpleLowIndex = na // The bar index of the Simple High/Low when confirmed.
  37. var int _trailingLowIndex = na, var int _trailingHighIndex = na // The bar index of the last low that changed the trailing low, or high for trailing high.
  38. // Setup for Highs
  39. if low > _trailingLow or (na(_trailingLow) and (low > low[1]))
  40. _trailingLow := low // Trail the lows up. If higher, reset, otherwise persist.
  41. _trailingLowIndex := bar_index // When you change the trailing low, store the bar index.
  42. // Setup for Lows
  43. if high < _trailingHigh or (na(_trailingHigh) and (high < high[1]))
  44. _trailingHigh := high // Trail the highs down. If lower, reset, otherwise persist.
  45. _trailingHighIndex := bar_index // When you change the trailing high, store the bar index.
  46. // Confirm Highs, Lows
  47. bool _isSimpleHigh = close < _trailingLow // Confirm a High on close below the trailing low.
  48. bool _isSimpleLow = close > _trailingHigh // Confirm a Low on close above the trailing high.
  49. // Cleanup
  50. if _isSimpleHigh
  51. _trailingLow := na // When a High is confirmed, unset the trailing low line.
  52. _simpleHighIndex := _trailingLowIndex // And store the bar index of the High.
  53. if _isSimpleLow
  54. _trailingHigh := na // When a Low is confirmed, unset the trailing high line.
  55. _simpleLowIndex := _trailingHighIndex // And store the bar index of the Low.
  56. [_isSimpleHigh, _isSimpleLow, _simpleHighIndex, _simpleLowIndex]
  57.  
  58.  
  59. // Example usage
  60.  
  61. [isSimpleHigh, isSimpleLow, simpleHighIndex, simpleLowIndex] = f_simpleLowHigh()
  62.  
  63. plotshape(showSimple ? isSimpleHigh : na, title="Simple High Confirmed", style=shape.arrowdown, color=color.red, location=location.abovebar, size=size.small)
  64. plotshape(showSimple ? isSimpleLow : na, title="Simple Low Confirmed", style=shape.arrowup, color=color.green, location=location.belowbar, size=size.small)
  65.  
  66. int offsetHighSimple = simpleHighIndex - bar_index, int offsetLowSimple = simpleLowIndex - bar_index // Offset for the label must be negative to offset to the left.
  67. int historyHighSimple = bar_index - simpleHighIndex, int historyLowSimple = bar_index - simpleLowIndex // Offset for the historical operator must be the other way round.
  68.  
  69. // We have to use a label because plotshape and plotchar don't work for some reason with series offsets even though they're supposed to.
  70. if showSimple and isSimpleLow
  71. SCL_Label.labelLast(_text="Simple\nLow", _keepLast = false, _offset=offsetLowSimple, _y=low[historyLowSimple], _textAlign=text.align_left, _style=label.style_label_up, _color=labelColour)
  72. if showSimple and isSimpleHigh
  73. SCL_Label.labelLast(_text="Simple\nHigh", _keepLast = false, _offset=offsetHighSimple, _y=high[historyHighSimple], _textAlign=text.align_left, _style=label.style_label_down, _color=labelColour)
  74.  
  75.  
  76.  
  77. // 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦 FUNCTION 2 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦
  78.  
  79. // @function f_localLowHigh() // This function finds Local Lows and Highs, in order. A Local High is any candle that has its Low taken out on close by a subsequent candle (and vice-versa for Local Lows).
  80. // The Local High does NOT have to be the candle with the highest High out of recent candles. It does NOT have to be a Williams High. It is not necessarily a swing high or a reversal or anything else.
  81. // By the rules, Local Lows and Highs must alternate, and in this function they do.
  82. // @param - there are no parameters. The function uses the chart OHLC.
  83. // @returns boolean values for whether this bar confirms a Local Low/High, and ints for the bar_index of that Low/High.
  84.  
  85. export f_localLowHigh() =>
  86. var float _trailingLow = na, var float _trailingHigh = na // Trail for the low (which is used to confirm Highs), and the high (used to confirm Lows).
  87. var int _simpleHighIndex = na, var int _simpleLowIndex = na // The bar index of the Simple High/Low when confirmed.
  88. var int _trailingLowIndex = na, var int _trailingHighIndex = na // The bar index of the last low that changed the trailing low, or high for trailing high.
  89. var string _lastPivot = na // Store whether the most recent pivot was a High or Low.
  90. var bool _isStrictLocalHigh = na, var bool _isStrictLocalLow = na // Whether the Local High/Low counts, because it's in order.
  91. // Setup for Highs
  92. if low > _trailingLow or (na(_trailingLow) and (low > low[1]))
  93. _trailingLow := low // Trail the lows up. If higher, reset, otherwise persist.
  94. _trailingLowIndex := bar_index // When you change the trailing low, store the bar index.
  95. // Setup for Lows
  96. if high < _trailingHigh or (na(_trailingHigh) and (high < high[1]))
  97. _trailingHigh := high // Trail the highs down. If lower, reset, otherwise persist.
  98. _trailingHighIndex := bar_index // When you change the trailing high, store the bar index.
  99. // Confirm Highs, Lows
  100. bool _isLocalHigh = (close < _trailingLow) // Confirm a High on close below the trailing low.
  101. bool _isLocalLow = (close > _trailingHigh) // Confirm a Low on close above the trailing high.
  102. // Cleanup
  103. if _isLocalHigh
  104. _trailingLow := na // When a High is confirmed, unset the trailing low line.
  105. _simpleHighIndex := _trailingLowIndex // And store the bar index of the High.
  106. if _isLocalLow
  107. _trailingHigh := na // When a Low is confirmed, unset the trailing high line.
  108. _simpleLowIndex := _trailingHighIndex // And store the bar index of the Low.
  109. // Promote Highs, Lows only in strict order
  110. if _isLocalHigh and _lastPivot != "High"
  111. _isStrictLocalHigh := true
  112. _lastPivot := "High"
  113. else
  114. _isStrictLocalHigh := false
  115. if _isLocalLow and _lastPivot != "Low"
  116. _isStrictLocalLow := true
  117. _lastPivot := "Low"
  118. else
  119. _isStrictLocalLow := false
  120.  
  121. [_isStrictLocalHigh, _isStrictLocalLow, _simpleHighIndex, _simpleLowIndex, _trailingHigh, _trailingLow]
  122.  
  123.  
  124. // Example usage
  125. [isLocalHigh, isLocalLow, localHighIndex, localLowIndex, trailingHigh, trailingLow] = f_localLowHigh()
  126.  
  127. plotshape(showLocal ? isLocalHigh : na, title="Local High Confirmed", style=shape.arrowdown, color=color.red, location=location.abovebar, size=size.small)
  128. plotshape(showLocal ? isLocalLow : na, title="Local Low Confirmed", style=shape.arrowup, color=color.green, location=location.belowbar, size=size.small)
  129.  
  130. int offsetHighLocal = localHighIndex - bar_index, int offsetLowLocal = localLowIndex - bar_index // Offset for the label must be negative to offset to the left.
  131. int historyHighLocal = bar_index - localHighIndex, int historyLowLocal = bar_index - localLowIndex // Offset for the historical operator must be the other way round.
  132.  
  133. if showLocal and isLocalLow
  134. SCL_Label.labelLast(_text="Local\nLow", _keepLast = false, _offset=offsetLowLocal, _y=low[historyLowLocal], _textAlign=text.align_left, _style=label.style_label_up, _color=labelColour)
  135. if showLocal and isLocalHigh
  136. SCL_Label.labelLast(_text="Local\nHigh", _keepLast = false, _offset=offsetHighLocal, _y=high[historyHighLocal], _textAlign=text.align_left, _style=label.style_label_down, _color=labelColour)
  137.  
  138.  
  139.  
  140. // 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦 FUNCTION 3 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦
  141.  
  142. // @function f_enhancedSimpleLowHigh() // This function finds Local Lows and Highs, but NOT in order. A Local High is any candle that has its Low taken out on close by a subsequent candle (and vice-versa for Local Lows).
  143. // The Local High does NOT have to be the candle with the highest High out of recent candles. It does NOT have to be a Williams High. It is not necessarily a swing high or a reversal or anything else.
  144. // By the rules, Local Lows and Highs must alternate. In this function they do not, so I'm calling them Simple Lows and Highs.
  145. // Simple Highs and Lows, by the above definition, can be useful for entries and stops. Because I intend to use them for trailing stops, I want them all, not just the ones that alternate in strict order.
  146. // The difference between this function and f_simpleLowHigh() is that it also tracks the lowest/highest recent level. This level can be useful for trailing stops.
  147. // In effect, these are like more "normal" highs and lows that you would pick by eye, but confirmed faster in many cases than by waiting for the low/high of that particular candle to be taken out on close,
  148. // because they are instead confirmed by ANY subsequent candle having its low/high exceeded. Hence, I call these Enhanced Simple Lows/Highs.
  149. // The levels are taken from the extreme highs/lows, but the bar indexes are given for the candles that were actually used to confirm the Low/High.
  150. // This is by design, because it might be misleading to label the extreme, since we didn't use that candle to confirm the Low/High..
  151. // @param - there are no parameters. The function uses the chart OHLC.
  152. // @returns - boolean values for whether this bar confirms an Enhanced Simple Low/High
  153. // ints for the bar_index of that Low/High
  154. // floats for the values of the recent high/low levels
  155. // floats for the trailing high/low levels (for debug/post-processing)
  156. // bools for market structure bias
  157.  
  158.  
  159. export f_enhancedSimpleLowHigh() =>
  160. var float _trailingLow = na, var float _trailingHigh = na // Trail for the low (which is used to confirm Highs), and the high (used to confirm Lows).
  161. var int _enhancedHighIndex = na, var int _enhancedLowIndex = na // The bar index of the Simple High/Low when confirmed.
  162. var int _trailingLowIndex = na, var int _trailingHighIndex = na // The bar index of the last low that changed the trailing low, or high for trailing high.
  163. var bool _pivotHighModeUp = na, var bool _pivotLowModeDown = na // Whether we are trailing the pivot high line up/low line down or not.
  164. var float _pivotHighTrail = na, var float _pivotLowTrail = na // Levels used only to track recent highest/lowest levels to plot. They're NOT used to confirm highs/lows.
  165. var float _pivotHigh = na, var float _pivotLow = na // The levels of the most recent confirmed Enhanced High/Low.
  166.  
  167. // Setup for calculating Highs
  168. if low > _trailingLow or (na(_trailingLow) and (low > low[1]))
  169. _trailingLow := low // Trail the lows up. If higher, reset, otherwise persist.
  170. _trailingLowIndex := bar_index // When you change the trailing low, store the bar index.
  171. // Setup for calculating Lows
  172. if high < _trailingHigh or (na(_trailingHigh) and (high < high[1]))
  173. _trailingHigh := high // Trail the highs down. If lower, reset, otherwise persist.
  174. _trailingHighIndex := bar_index // When you change the trailing high, store the bar index.
  175. // Confirm Highs, Lows
  176. bool _isEnhancedHigh = close < _trailingLow // Confirm a High on close below the trailing low.
  177. bool _isEnhancedLow = close > _trailingHigh // Confirm a Low on close above the trailing high.
  178.  
  179. // Track pivot levels
  180. _pivotHighModeUp := na(_pivotHighTrail) and high > high[1] ? true : _isEnhancedHigh ? false : _pivotHighModeUp
  181. _pivotHighTrail := _pivotHighModeUp and high > _pivotHighTrail ? high : _pivotHighModeUp and na(_pivotHighTrail) ? high : _pivotHighTrail
  182. _pivotLowModeDown := na(_pivotLowTrail) and low < low[1] ? true : _isEnhancedLow ? false : _pivotLowModeDown
  183. _pivotLowTrail := _pivotLowModeDown and low < _pivotLowTrail ? low : _pivotLowModeDown and na(_pivotLowTrail) ? low : _pivotLowTrail
  184.  
  185. // Cleanup
  186. if _isEnhancedHigh
  187. _trailingLow := na // When a High is confirmed, unset the trailing low line.
  188. _enhancedHighIndex := _trailingLowIndex // And store the bar index of the High.
  189. _pivotHigh := na(_pivotHighTrail) ? math.max(high, high[1]) : math.max(_pivotHighTrail,high) // Reset the pivot high line
  190. _pivotHighTrail := na // Unset the pivot high trail
  191. if _isEnhancedLow
  192. _trailingHigh := na // When a Low is confirmed, unset the trailing high line.
  193. _enhancedLowIndex := _trailingHighIndex // And store the bar index of the Low.
  194. _pivotLow := na(_pivotLowTrail) ? math.min(low, low[1]) : math.min(_pivotLowTrail, low) // Reset the pivot low line
  195. _pivotLowTrail := na // Unset the pivot high trail
  196.  
  197. // Market Structure bias
  198. var bool _isBullish = na, var bool _isBearish = na // Market structure bias
  199. _isBullish := close > _pivotHigh ? true : _isBearish ? false : _isBullish
  200. _isBearish := close < _pivotLow ? true : _isBullish ? false : _isBearish
  201. if na(_isBullish) and na(_isBearish) // Approximate bias on first pivot
  202. _isBullish := _isEnhancedLow ? true : _isBullish
  203. _isBearish := _isEnhancedHigh ? true : _isBearish
  204.  
  205. [_isEnhancedHigh, _isEnhancedLow, _enhancedHighIndex, _enhancedLowIndex, _pivotHighTrail, _pivotHigh, _pivotLowTrail, _pivotLow, _isBullish, _isBearish]
  206.  
  207.  
  208. // Example usage
  209.  
  210. [isEnhancedHigh, isEnhancedLow, enhancedHighIndex, enhancedLowIndex, pivotHighTrail, pivotHigh, pivotLowTrail, pivotLow, enhancedBullish, enhancedBearish] = f_enhancedSimpleLowHigh()
  211.  
  212. plotshape(showEnhanced ? isEnhancedHigh : na, title="Enhanced Simple High Confirmed", style=shape.arrowdown, color=color.red, location=location.abovebar, size=size.small)
  213. plotshape(showEnhanced ? isEnhancedLow : na, title="Enhanced Simple Low Confirmed", style=shape.arrowup, color=color.green, location=location.belowbar, size=size.small)
  214.  
  215. int offsetHighEnhanced = enhancedHighIndex - bar_index, int offsetLowEnhanced = enhancedLowIndex - bar_index // Offset for the label must be negative to offset to the left.
  216. int historyHighEnhanced = bar_index - enhancedHighIndex, int historyLowEnhanced = bar_index - enhancedLowIndex // Offset for the historical operator must be the other way round.
  217.  
  218. // We have to use a label because plotshape and plotchar don't work for some reason with series offsets even though they're supposed to.
  219. if showEnhanced and isEnhancedLow
  220. SCL_Label.labelLast(_text="Enhanced\nLow", _keepLast = false, _offset=offsetLowEnhanced, _y=low[historyLowEnhanced], _textAlign=text.align_left, _style=label.style_label_up, _color=labelColour)
  221. if showEnhanced and isEnhancedHigh
  222. SCL_Label.labelLast(_text="Enhanced\nHigh", _keepLast = false, _offset=offsetHighEnhanced, _y=high[historyHighEnhanced], _textAlign=text.align_left, _style=label.style_label_down, _color=labelColour)
  223.  
  224. plot(showEnhanced ? pivotHighTrail : na, title="Recent High Level", linewidth=1, color=color.green, style=plot.style_linebr)
  225. plot(showEnhanced ? pivotHigh : na, title="Enhanced High Level", linewidth=4, color=color.new(color.green,50), style=plot.style_linebr)
  226.  
  227. plot(showEnhanced ? pivotLowTrail : na, title="Recent Low Level", linewidth=1, color=color.red, style=plot.style_linebr)
  228. plot(showEnhanced ? pivotLow : na, title="Enhanced Low Level", linewidth=4, color=color.new(color.red,50), style=plot.style_linebr)
  229.  
  230. enhancedBackGroundColour = not showEnhanced ? na : enhancedBullish ? color.new(color.green,80) : enhancedBearish ? color.new(color.red,80) : na
  231. bgcolor(enhancedBackGroundColour)
  232.  
  233.  
  234.  
  235. // 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦 FUNCTION 4 🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦
  236.  
  237. // @function f_trueLowHigh() // This function finds True Lows and Highs.
  238. // A True High is the candle with the highest recent high, which then has its low taken out on close by a subsequent candle (and vice-versa for True Lows).
  239. // The difference between this and an Enhanced High is that confirmation requires not just any Simple High, but confirmation of the very candle that has the highest high.
  240. // Because of this, confirmation is often later, and multiple Simple Highs and Lows can develop within ranges formed by a single big candle without any of them being confirmed. This is by design.
  241. // A True High looks like the intuitive "real high" when you look at the chart. True Lows and Highs must alternate.
  242. // @param - there are no parameters. The function uses the chart OHLC.
  243. // @returns - boolean values for whether this bar confirms an Enhanced Simple Low/High
  244. // ints for the bar_index of that Low/High
  245. // floats for the values of the recent high/low levels
  246. // floats for the trailing high/low levels (for debug/post-processing)
  247. // bools for market structure bias
  248.  
  249.  
  250. export f_trueLowHigh() =>
  251. var float _trailingLow = na, var float _trailingHigh = na // Trail for the low (which is used to confirm Highs), and the high (used to confirm Lows).
  252. var int _trueHighIndex = na, var int _trueLowIndex = na // The bar index of the True High/Low when confirmed.
  253. var int _trailingLowIndex = na, var int _trailingHighIndex = na // The bar index of the last low that changed the trailing low, or high for trailing high.
  254. var string _lastPivot = na // Store whether the most recent pivot was a High or Low.
  255. var bool _isTrueHigh = na, var bool _isTrueLow = na // Whether the Local High/Low counts, because it's in order.
  256. var bool _pivotHighModeUp = na, var bool _pivotLowModeDown = na // Whether we are trailing the pivot high line up/low line down or not.
  257. var float _pivotHighTrail = na, var float _pivotLowTrail = na // Levels used only to track recent highest/lowest levels to plot. Here, they ARE used to identify the candle from which to confirm highs/lows.
  258. var float _pivotHigh = na, var float _pivotLow = na // The levels of the most recent confirmed Enhanced High/Low.
  259.  
  260. // Track pivot levels
  261. _pivotHighModeUp := na(_pivotHighTrail) and high > high[1] ? true : _isTrueHigh ? false : _pivotHighModeUp
  262. _pivotHighTrail := _pivotHighModeUp and high > _pivotHighTrail ? high : _pivotHighModeUp and na(_pivotHighTrail) ? high : _pivotHighTrail
  263. _pivotLowModeDown := na(_pivotLowTrail) and low < low[1] ? true : _isTrueLow ? false : _pivotLowModeDown
  264. _pivotLowTrail := _pivotLowModeDown and low < _pivotLowTrail ? low : _pivotLowModeDown and na(_pivotLowTrail) ? low : _pivotLowTrail
  265.  
  266. // Setup for Highs
  267. if high > _pivotHighTrail[1] or na(_pivotHighTrail[1]) and not na(_pivotHighTrail)
  268. _trailingLow := low // Trail the lows up. If higher, reset, otherwise persist.
  269. _trailingLowIndex := bar_index // When you change the trailing low, store the bar index.
  270.  
  271. // Setup for Lows
  272. var bool _debug1 = na
  273. if low < _pivotLowTrail[1] or na(_pivotLowTrail[1]) and not na(_pivotLowTrail)
  274. _trailingHigh := high // Trail the highs down and stop when we reach a low. If lower, reset, otherwise persist.
  275. _trailingHighIndex := bar_index // When you change the trailing high, store the bar index.
  276.  
  277. // Confirm Highs, Lows
  278. bool _isLocalHigh = (close < _trailingLow) // Confirm a High on close below the trailing low.
  279. bool _isLocalLow = (close > _trailingHigh) // Confirm a Low on close above the trailing high.
  280.  
  281. // Cleanup
  282. if _isLocalHigh
  283. _trailingLow := na // When a High is confirmed, unset the trailing low line.
  284. _trueHighIndex := _trailingLowIndex // And store the bar index of the High.
  285. if _isLocalLow
  286. _trailingHigh := na // When a Low is confirmed, unset the trailing high line.
  287. _trueLowIndex := _trailingHighIndex // And store the bar index of the Low.
  288.  
  289. // Promote Highs, Lows only in strict order
  290. if _isLocalHigh and _lastPivot != "High"
  291. _isTrueHigh := true
  292. _lastPivot := "High"
  293. else
  294. _isTrueHigh := false
  295. if _isLocalLow and _lastPivot != "Low"
  296. _isTrueLow := true
  297. _lastPivot := "Low"
  298. else
  299. _isTrueLow := false
  300.  
  301. // Moar cleanup
  302. if _isTrueHigh
  303. _pivotHigh := na(_pivotHighTrail) ? math.max(high, high[1]) : math.max(_pivotHighTrail,high) // Reset the pivot high line
  304. _pivotHighTrail := na // Unset the pivot high trail
  305. _pivotLowTrail := low
  306. _trailingHigh := high
  307. _trailingHighIndex := bar_index // Reset the trailing high index, which will reset the True Low Index in due course
  308. if _isTrueLow
  309. _pivotLow := na(_pivotLowTrail) ? math.min(low, low[1]) : math.min(_pivotLowTrail, low) // Reset the pivot low line
  310. _pivotLowTrail := na // Unset the pivot high trail
  311. _pivotHighTrail := high
  312. _trailingLow := low
  313. _trailingLowIndex := bar_index
  314.  
  315. // Market Structure bias
  316. var bool _isBullish = na, var bool _isBearish = na // Market structure bias
  317. _isBullish := close > _pivotHigh ? true : _isBearish ? false : _isBullish // Flip bullish when we make a higher high
  318. _isBearish := close < _pivotLow ? true : _isBullish ? false : _isBearish
  319. if na(_isBullish) and na(_isBearish) // Approximate bias on first pivot
  320. _isBullish := _isTrueLow ? true : _isBullish
  321. _isBearish := _isTrueHigh ? true : _isBearish
  322.  
  323. [_isTrueHigh, _isTrueLow, _trueHighIndex, _trueLowIndex, _pivotHighTrail, _pivotHigh, _pivotLowTrail, _pivotLow, _isBullish, _isBearish]
  324.  
  325.  
  326. // Example usage
  327.  
  328. [isTrueHigh, isTrueLow, trueHighIndex, trueLowIndex, trueHighTrail, trueHigh, trueLowTrail, trueLow, trueBullish, trueBearish] = f_trueLowHigh()
  329.  
  330. plotshape(showTrue ? isTrueHigh : na, title="True High Confirmed", style=shape.arrowdown, color=color.red, location=location.abovebar, size=size.small)
  331. plotshape(showTrue ? isTrueLow : na, title="True Low Confirmed", style=shape.arrowup, color=color.green, location=location.belowbar, size=size.small)
  332.  
  333. int offsetHighTrue = trueHighIndex - bar_index, int offsetLowTrue = trueLowIndex - bar_index // Offset for the label must be negative to offset to the left.
  334. int historyHighTrue = bar_index - trueHighIndex, int historyLowTrue = bar_index - trueLowIndex // Offset for the historical operator must be the other way round.
  335.  
  336. if showTrue and isTrueLow
  337. SCL_Label.labelLast(_text="True\nLow", _keepLast = false, _offset=offsetLowTrue, _y=low[historyLowTrue], _textAlign=text.align_left, _style=label.style_label_up, _color=labelColour)
  338. if showTrue and isTrueHigh
  339. SCL_Label.labelLast(_text="True\nHigh", _keepLast = false, _offset=offsetHighTrue, _y=high[historyHighTrue], _textAlign=text.align_left, _style=label.style_label_down, _color=labelColour)
  340.  
  341. plot(showTrue ? trueHighTrail : na, title="Recent High Level", linewidth=1, color=color.green, style=plot.style_linebr)
  342. plot(showTrue ? trueHigh : na, title="True High Level", linewidth=4, color=color.new(color.green,50), style=plot.style_linebr)
  343.  
  344. plot(showTrue ? trueLowTrail : na, title="Recent Low Level", linewidth=1, color=color.red, style=plot.style_linebr)
  345. plot(showTrue ? trueLow : na, title="True Low Level", linewidth=4, color=color.new(color.red,50), style=plot.style_linebr)
  346.  
  347. trueBackGroundColour = not showTrue ? na : trueBullish ? color.new(color.green,80) : trueBearish ? color.new(color.red,80) : na
  348. bgcolor(trueBackGroundColour)
  349.  
  350.  
  351. // ================================================= //
  352. // //
  353. // (β•―Β°β–‘Β°οΌ‰β•―οΈ΅ Β‘sʎɐʍlɐ `Κ‡sΙΉα΄‰ΙŸ ǝɹnΚ‡Ι”nΙΉΚ‡s Κ‡ΗΚžΙΉΙW //
  354. // //
  355. // ================================================= //
Advertisement
Add Comment
Please, Sign In to add comment