Advertisement
Guest User

EWMA Directional Volatility

a guest
Feb 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 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. // © Motgench
  3.  
  4. //@version=4
  5. study("Pig Downside Upside Volatility",precision=2)
  6. bgcolor(#000000c0)
  7.  
  8. Annual = input( 365, "Annual")
  9. n = input( 30, "Length")
  10. lambda = input(0.94, "lambda (EWMA)", step=0.01)
  11. oneMinusLambda = 1.0 - lambda
  12.  
  13. priorClose = nz(close[1], close) // nz() replaces na with 0.0, added ", close)" Divide by zero situation combined with #2
  14. xPrice = close - priorClose
  15. logCloseDividedPriorCLose = log(close / priorClose) // #2
  16.  
  17. d = xPrice<0.0 ? logCloseDividedPriorCLose : 0.0 // Less than: xPrice < ...
  18. v = 0.0
  19. v := lambda * nz(v[1]) + oneMinusLambda * pow(d, 2)
  20. dhv = sqrt(v) * sqrt(Annual) * 100.0
  21.  
  22. u = xPrice>0.0 ? logCloseDividedPriorCLose : 0.0 // Greater than: xPrice > ...
  23. v2 = 0.0
  24. v2 := lambda * nz(v2[1]) + oneMinusLambda * pow(u, 2)
  25. uhv = sqrt(v2) * sqrt(Annual) * 100.0
  26.  
  27. plot(dhv, color=color.red)
  28. plot(uhv, color=color.green) // Was causing na for eternity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement