Advertisement
xmd79

Price Swing Detection - Smart Money Concept

Feb 12th, 2023
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 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. // © MirNader_
  3.  
  4. //@version=5
  5. indicator("Price Swing Detection - Smart Money Concept" , overlay = true)
  6.  
  7. //Input & Swing Detection
  8. // Define the input "swingLength" for the number of candles to look back for Swing detection
  9. int swingLength = input.int(20, 'Lookback Period' , minval = 5 , tooltip = "Determines the length of bullish and bearish swings in the market")
  10.  
  11. // Set the line color for bullish trend
  12. color swingcolorBullish = input.color(color.green, 'Bullish Line' , inline = '01' , tooltip = "This line represents the trend direction. Green color indicates a bullish trend and red color indicates a bearish trend")
  13.  
  14. // Set the line color for bearish trend
  15. color swingcolorBearish = input.color(color.red , 'Bearish Line' , inline = '01' , tooltip = "This line represents the trend direction. Green color indicates a bullish trend and red color indicates a bearish trend")
  16.  
  17. //End of Input & Swing Detection
  18. //------------------------------\\
  19. //Swing Detection Function
  20. // The "detectSwing" function will calculate the swing highs and lows for the given number of candles
  21. detectSwing(swingPeriod) =>
  22. // Initialize the "currentSwingState" variable, which will be used to determine the swing state (0 for swing high, 1 for swing low)
  23. var currentSwingState = 0
  24.  
  25. // Calculate the highest high and lowest low for the given number of candles
  26. highestSwing = ta.highest(swingPeriod)
  27. lowestSwing = ta.lowest(swingPeriod)
  28.  
  29. // Determine the swing state based on the current candle high and low compared to the highest high and lowest low
  30. currentSwingState := high[swingPeriod] > highestSwing ? 0 : low[swingPeriod] < lowestSwing ? 1 : currentSwingState[1]
  31.  
  32. // If the current candle is a swing high and the previous candle was not a swing high, set "currentSwingTop" to the current candle high
  33. currentSwingTop = currentSwingState == 0 and currentSwingState[1] != 0 ? high[swingPeriod] : 0
  34. // If the current candle is a swing low and the previous candle was not a swing low, set "currentSwingBottom" to the current candle low
  35. currentSwingBottom = currentSwingState == 1 and currentSwingState[1] != 1 ? low[swingPeriod] : 0
  36.  
  37. // Return the calculated swing high and swing low values
  38. [currentSwingTop, currentSwingBottom]
  39.  
  40. //End of Swing Detection
  41. //------------------------------\\
  42. //Parameters
  43. // Initialize the "trendDirection" variable, which will keep track of the trend state (0 for downtrend, 1 for uptrend)
  44. var trendDirection = 0
  45.  
  46. // Initialize variables for storing pivot high values
  47. var swingTopValue = 0., var swingTopTimestamp = 0
  48.  
  49. // Initialize variables for storing pivot low values
  50. var swingBottomValue = 0., var swingBottomTimestamp = 0
  51.  
  52. // Initialize variables for keeping track of the trailing maximum
  53. var trailingSwingHigh = high, var trailingSwingLow = low
  54. var trailingSwingHighTimestamp = 0, var trailingSwingLowTimestamp = 0
  55.  
  56. // Initialize variables for keeping track of pivot crosses
  57. var topCrossCheck = true, var bottomCrossCheck = true
  58.  
  59. // Call the "detectSwing" function to calculate the swing highs and lows
  60. [currentSwingTop, currentSwingBottom] = detectSwing(swingLength)
  61.  
  62. //Line Extend
  63. var line extendingTopLine = na // Initialize a line object for extending the recent top
  64. var line extendingBottomLine = na // Initialize a line object for extending the recent bottom
  65.  
  66. //End of Parameters
  67. //------------------------------\\
  68. //Pivot High Function
  69. //The calculation of pivot high is an important step in detecting bullish structures in the market
  70. if currentSwingTop
  71. // If a new top is found, set "topCrossCheck" to true
  72. topCrossCheck := true
  73.  
  74. // Delete the previous extendingTopLine line
  75. line.delete(extendingTopLine[1])
  76. // Create a new extendingTopLine line from the recent top to the last bar
  77. extendingTopLine := line.new(bar_index-swingLength, currentSwingTop, bar_index, currentSwingTop, color = swingcolorBullish)
  78.  
  79. // Store the pivot high values
  80. swingTopValue := currentSwingTop
  81. swingTopTimestamp := bar_index - swingLength
  82.  
  83. // Update the trailing maximum values
  84. trailingSwingHigh := currentSwingTop
  85. trailingSwingHighTimestamp := bar_index - swingLength
  86. //Update the trailing maximum values
  87. trailingSwingHigh := math.max(high, trailingSwingHigh)
  88. trailingSwingHighTimestamp := trailingSwingHigh == high ? bar_index : trailingSwingHighTimestamp
  89.  
  90. //Set the top extension line properties
  91. if barstate.islast
  92. line.set_xy1(extendingTopLine, trailingSwingHighTimestamp, trailingSwingHigh)
  93. line.set_xy2(extendingTopLine, bar_index + 20, trailingSwingHigh)
  94. //End of Pivot High Function
  95. //------------------------------\\
  96. //Pivot Low Function
  97. //the Pivot low is used to detect the presence of a bearish structure in the price movement and update the trend accordingly.
  98. if currentSwingBottom
  99. // If a new low is found, set "bottomCrossCheck" to true
  100. bottomCrossCheck := true
  101. //Extend recent bottom to last bar
  102. line.delete(extendingBottomLine[1])
  103. extendingBottomLine := line.new(bar_index - swingLength, currentSwingBottom, bar_index, currentSwingBottom, color = swingcolorBearish)
  104.  
  105. //Store the pivot low values
  106. swingBottomValue := currentSwingBottom
  107. swingBottomTimestamp := bar_index - swingLength
  108.  
  109. //Update the trailing minimum values
  110. trailingSwingLow := currentSwingBottom
  111. trailingSwingLowTimestamp := bar_index - swingLength
  112. //Update the trailing minimum values
  113. trailingSwingLow := math.min(low, trailingSwingLow)
  114. trailingSwingLowTimestamp := trailingSwingLow == low ? bar_index : trailingSwingLowTimestamp
  115.  
  116. //Set the bottom extension line properties
  117. if barstate.islast
  118. line.set_xy1(extendingBottomLine, trailingSwingLowTimestamp, trailingSwingLow)
  119. line.set_xy2(extendingBottomLine, bar_index + 20, trailingSwingLow)
  120.  
  121. // End of Pivot Low Function
  122. //------------------------------\\
  123. // You can use 'trendDirection' for your strategy to identify the uptrend or downtrend
  124. // This section of code checks if the close price crosses over the trailing maximum value (top_y).
  125. // If it does, the top_cross boolean is set to false and the trend is set to 1, indicating a bullish trend.
  126. //Detect bullish Structure
  127. if ta.crossover(close, swingTopValue) and topCrossCheck
  128. topCrossCheck := false
  129. trendDirection := 1
  130.  
  131. // This section of code checks if the close price crosses under the trailing minimum value (btm_y).
  132. // If it does, the btm_cross boolean is set to false and the trend is set to -1, indicating a bearish trend.
  133. // Detect bearish Structure
  134. if ta.crossunder(close, swingBottomValue) and bottomCrossCheck
  135. bottomCrossCheck := false
  136. trendDirection := -1
  137.  
  138. // End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement