Guest User

Untitled

a guest
Jul 26th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. # This source code is subject to the terms of the MIT License at https://opensource.org/licenses/MIT
  2. # /u/HurlTeaInTheSea v1.0
  3.  
  4. # Intraday Relative Volume (RVol) indicator:
  5. # https://stockbeep.com/how-to-calculate-relative-volume-rvol-for-intraday-trading
  6.  
  7. # still works on higher timeframe but it's not a "day" average anymore, so throw error to avoid confusion
  8. addlabel(GetAggregationPeriod() > AggregationPeriod.DAY, "RVol is only valid for daily timeframe or lower");
  9.  
  10. input _nDayAverage = 5;
  11. input _rVolHighlightThres = 0;
  12. input _colorBasedOnPrevClose = no;
  13.  
  14. def days = Max(_nDayAverage, 1);
  15. def rVolThres = Max(_rVolHighlightThres, 0);
  16.  
  17. # detect new session of day
  18. def isNewDay = GetYYYYMMDD() != GetYYYYMMDD()[1];
  19.  
  20. def cVol; # cumulative volume
  21. def beforeNewDayBars; # save bar number before new day
  22. def len; # count number of new days
  23. if isNewDay {
  24. cVol = volume;
  25. beforeNewDayBars = BarNumber() - 1;
  26. len = len[1] + 1;
  27. } else {
  28. cVol = cVol[1] + volume;
  29. beforeNewDayBars = beforeNewDayBars[1];
  30. len = len[1];
  31. }
  32.  
  33. # starting from last bar of previous session, go back in time and accumulate volume up to current time relative to trading day
  34. # stop after N day cumulative volume average collected
  35. def skip = BarNumber() - beforeNewDayBars;
  36. def aVol = fold i = skip to Max(skip, BarNumber())
  37. with v = 0
  38. while BarNumber() >= days + 1 && len >= days + 1 && len - 1 - GetValue(len, i) < days
  39. do If(GetTime() - RegularTradingStart(GetYYYYMMDD()) >= GetValue(GetTime(), i) - RegularTradingStart(GetValue(GetYYYYMMDD(), i)), v + GetValue(volume, i) / days, v);
  40.  
  41. def _rVol = if aVol > 0 then cVol / aVol else 0;
  42.  
  43. # visuals
  44. def upColorCondition = if _colorBasedOnPrevClose then close >= close[1] else close >= open;
  45.  
  46. Plot RVol = _rVol - 1;
  47. RVol.DefineColor("Up", Color.GREEN);
  48. RVol.DefineColor("Down", Color.RED);
  49. RVol.DefineColor("Up-Dim", Color.DARK_GREEN);
  50. RVol.DefineColor("Down-Dim", Color.DARK_RED);
  51. RVol.AssignValueColor(if _rVol >= rVolThres then if upColorCondition then RVol.Color("Up") else RVol.Color("Down") else if upColorCondition then RVol.Color("Up-Dim") else RVol.Color("Down-Dim"));
  52. RVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
  53.  
  54. # mark new sessions on intraday charts
  55. AddVerticalLine(isNewDay && GetAggregationPeriod() < AggregationPeriod.DAY, "", Color.GRAY, curve.SHORT_DASH);
  56.  
  57. #Plot Ref = 1;
  58. #Ref.DefineColor("Reference", Color.GRAY);
  59. #Ref.AssignValueColor(Ref.color("Reference"));
  60. #Ref.HideBubble();
  61. #Ref.HideTitle();
  62.  
  63. #Plot Zero = 0;
  64. #Zero.DefineColor("Zero", Color.GRAY);
  65. #Zero.AssignValueColor(Zero.color("Zero"));
  66. #Zero.HideBubble();
  67. #Zero.HideTitle();
Add Comment
Please, Sign In to add comment