Advertisement
xmd79

Volume Entropy

Sep 11th, 2023
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 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. // © Uldisbebris
  3.  
  4. //@version=5
  5. indicator("Volume Entropy", overlay = false, format=format.price, precision=2, timeframe="", timeframe_gaps=true)
  6.  
  7. //Custom function for natural logarithm using Taylor Series
  8. naturalLog(x, terms) =>
  9. a = x - 1.0
  10. sum = 0.0
  11. for i = 1 to terms
  12. term = math.pow(a, i) / i
  13. sum := sum + (i % 2 != 0 ? term : -term)
  14. sum
  15.  
  16. // Entropy
  17. len = input.int(2, 'micro_pattern_len' )
  18. micro_pattern_len = len
  19. entropy(data) =>
  20. p = ta.valuewhen(data, 1, 0) / math.sum(data, micro_pattern_len)
  21. -p * naturalLog(p, 10)
  22.  
  23. LowEntropy = entropy(volume)
  24.  
  25. // Z-score normalization
  26. lookback = input.int(20, 'Z-score normalization lookback')
  27. mean_entropy = ta.sma(LowEntropy, lookback)
  28. std_entropy = ta.stdev(LowEntropy, lookback)
  29. z_score_entropy = (LowEntropy - mean_entropy) / std_entropy
  30.  
  31. // hline
  32. hline(1.3)
  33. hline(-1.3)
  34. hline(0)
  35.  
  36. /// direction change for color
  37. Dir = ta.change(-z_score_entropy) > 0 ? 1 : -1
  38. Col = Dir == 1 ? color.new(#06a02c, 0) : Dir == -1 ? color.new(#94060d, 0) : color.gray
  39. volplot = plot(-z_score_entropy, title='Smoothed and Scaled EMA', color = Col, linewidth = 2)
  40.  
  41. midLinePlot = plot(0, color = na, editable = false, display = display.none)
  42. fill(volplot, midLinePlot, 2.5, 1, top_color = color.new(color.green, 0), bottom_color = color.new(color.green, 100), title = "Overbought Gradient Fill")
  43. fill(volplot, midLinePlot, -1.0,-4.0, top_color = color.new(color.red, 100), bottom_color = color.new(color.red, 0), title = "Oversold Gradient Fill")
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement