Advertisement
xmd79

Trend Strength Over Time

Nov 24th, 2023
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. //@version=5
  2. indicator(" Trend Strength Over Time", shorttitle="TSOT", overlay=false)
  3.  
  4.  
  5. // Calculate 75th percentile of price for each length
  6. percentile_13H = ta.percentile_nearest_rank(high, 13, 75)
  7. percentile_21H = ta.percentile_nearest_rank(high, 21, 75)
  8. percentile_34H = ta.percentile_nearest_rank(high, 34, 75)
  9. percentile_55H = ta.percentile_nearest_rank(high, 55, 75)
  10. percentile_89H = ta.percentile_nearest_rank(high, 89, 75)
  11.  
  12. // Calculate 25th percentile of price for each length
  13. percentile_13L = ta.percentile_nearest_rank(low, 13, 25)
  14. percentile_21L = ta.percentile_nearest_rank(low, 21, 25)
  15. percentile_34L = ta.percentile_nearest_rank(low, 34, 25)
  16. percentile_55L = ta.percentile_nearest_rank(low, 55, 25)
  17. percentile_89L = ta.percentile_nearest_rank(low, 89, 25)
  18.  
  19. // Calculate 75th and 25th for length 144 (longest length)
  20. highest_high = ta.percentile_nearest_rank(high, 144, 75)
  21. lowest_low = ta.percentile_nearest_rank(low, 144, 25)
  22.  
  23. // Calculate trend strength conditions
  24. trendBull1 = percentile_13H > highest_high
  25. trendBull2 = percentile_21H > highest_high
  26. trendBull3 = percentile_34H > highest_high
  27. trendBull4 = percentile_55H > highest_high
  28. trendBull5 = percentile_89H > highest_high
  29. trendBull6 = percentile_13L > highest_high
  30. trendBull7 = percentile_21L > highest_high
  31. trendBull8 = percentile_34L > highest_high
  32. trendBull9 = percentile_55L > highest_high
  33. trendBull10 = percentile_89L > highest_high
  34.  
  35. trendBear1 = percentile_13H < lowest_low
  36. trendBear2 = percentile_21H < lowest_low
  37. trendBear3 = percentile_34H < lowest_low
  38. trendBear4 = percentile_55H < lowest_low
  39. trendBear5 = percentile_89H < lowest_low
  40. trendBear6 = percentile_13L < lowest_low
  41. trendBear7 = percentile_21L < lowest_low
  42. trendBear8 = percentile_34L < lowest_low
  43. trendBear9 = percentile_55L < lowest_low
  44. trendBear10 = percentile_89L < lowest_low
  45.  
  46. countBull =
  47. (trendBull1 ? 1 : 0) +
  48. (trendBull2 ? 1 : 0) +
  49. (trendBull3 ? 1 : 0) +
  50. (trendBull4 ? 1 : 0) +
  51. (trendBull5 ? 1 : 0) +
  52. (trendBull6 ? 1 : 0) +
  53. (trendBull7 ? 1 : 0) +
  54. (trendBull8 ? 1 : 0) +
  55. (trendBull9 ? 1 : 0) +
  56. (trendBull10 ? 1 : 0)
  57.  
  58. countBear =
  59. (trendBear1 ? 1 : 0) +
  60. (trendBear2 ? 1 : 0) +
  61. (trendBear3 ? 1 : 0) +
  62. (trendBear4 ? 1 : 0) +
  63. (trendBear5 ? 1 : 0) +
  64. (trendBear6 ? 1 : 0) +
  65. (trendBear7 ? 1 : 0) +
  66. (trendBear8 ? 1 : 0) +
  67. (trendBear9 ? 1 : 0) +
  68. (trendBear10 ? 1 : 0)
  69.  
  70. // Calculate weak bull count
  71. weakBullCount =
  72. (percentile_13L < highest_high and percentile_13L > lowest_low ? 1 : 0) +
  73. (percentile_21L < highest_high and percentile_21L > lowest_low ? 1 : 0) +
  74. (percentile_34L < highest_high and percentile_34L > lowest_low ? 1 : 0) +
  75. (percentile_55L < highest_high and percentile_55L > lowest_low ? 1 : 0) +
  76. (percentile_89L < highest_high and percentile_89L > lowest_low ? 1 : 0)
  77.  
  78. // Calculate weak bear count
  79. weakBearCount =
  80. (percentile_13H > lowest_low and percentile_13H < highest_high ? 1 : 0) +
  81. (percentile_21H > lowest_low and percentile_21H < highest_high ? 1 : 0) +
  82. (percentile_34H > lowest_low and percentile_34H < highest_high ? 1 : 0) +
  83. (percentile_55H > lowest_low and percentile_55H < highest_high ? 1 : 0) +
  84. (percentile_89H > lowest_low and percentile_89H < highest_high ? 1 : 0)
  85.  
  86. // Calculate bull strength and bear strength
  87. bullStrength = 10 * (countBull + 0.5*weakBullCount - 0.5*weakBearCount - countBear)
  88. bearStrength = 10 * (countBear + 0.5*weakBearCount - 0.5*weakBullCount - countBull)
  89.  
  90. // Calculate the current trend
  91. currentTrendValue = bullStrength - bearStrength
  92.  
  93. // Calulate volume strenght
  94. V13 = (ta.sma(volume,13)) /ta.sma(volume,144)
  95. V21 = (ta.sma(volume,21)) /ta.sma(volume,144)
  96. V34 = (ta.sma(volume,34)) /ta.sma(volume,144)
  97. V55 = (ta.sma(volume,55)) /ta.sma(volume,144)
  98. V89 = (ta.sma(volume,89)) /ta.sma(volume,144)
  99. VCF= (0.5*V13+0.25*V21+0.125*V34+0.08*V55+ 0.045*V89)
  100.  
  101. // Determine the color based on current trend value
  102. color_change = currentTrendValue> 0 ? color.green : (currentTrendValue < 0 ? color.red : color.blue)
  103. plot(bullStrength, color=color_change, title="Trend Strength")
  104. plot(VCF, color=color.white, title="Volume Strength")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement