Advertisement
PineCoders

Optimisation: TimesInLast

Jul 24th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. //@version=4
  2. //@author=LucF
  3.  
  4. // TimesInLast [LucF]
  5. // v1.0, 2019.07.15 19:37 — Luc
  6.  
  7. // This script illustrates 3 different ways of counting the number of occurrences in the last len bars.
  8.  
  9. study("TimesInLast [LucF]")
  10.  
  11. // ————— Allow different form-types to be specified as length value.
  12. // This part is only there to show the impact of using different form-types of length with the 3 functions.
  13. // In normal situation, we would just use the following:
  14. // len = input(100, "Length")
  15. deflen = 100 // Change this value when you want to use different lengths. Inputs cannot be change through Settings/Inputs; only the form-type.
  16. LT1 = "1. input int", LT2 = "2. input float", LT3="3. series int", LT4="4. series float"
  17. lt = input(LT1, "Type of 'length' argument to functions", options=[LT1, LT2, LT3, LT4])
  18. len1 = input(deflen, LT1, type=input.integer, minval=deflen, maxval=deflen)
  19. len2 = input(deflen, LT2, type=input.float, minval=deflen, maxval=deflen)
  20. var len3 = 0
  21. len3 := len3==deflen? len3:len3+1
  22. var len4 = 0.
  23. len4 := len4==deflen? len4:len4+1
  24. // Choose proper form-type of length.
  25. len = lt==LT1? len1 : lt==LT2? len2 : lt==LT3? len3 : lt==LT4? len4 : na
  26.  
  27. // Condition on which all counts are done.
  28. condition = close>open
  29.  
  30. // ————— Method 1. This function uses Pine's built-in function but only accepts a simple int for the length.
  31. Ideal_TimesInLast(_cond, _len) => sum(_cond?1:0, _len)
  32.  
  33. // ————— Method 2. This function is equivalent to using sum() but works with a float and series value for _len.
  34. VerboseButEfficient_TimesInLast(_cond, _len) =>
  35. // For first _len bar we just add to cumulative count of occurrences.
  36. // After that we add count for current bar and make adjustment to count for the tail bar in our mini-series of length=_len.
  37. var _qtyBarsInCnt = 0
  38. var _cnt = 0
  39. if _cond
  40. // Add to count as per current bar's condition state.
  41. _cnt := _cnt+1
  42. if _qtyBarsInCnt<_len
  43. // We have not counted the first _len bars yet; keep adding to checked bars count.
  44. _qtyBarsInCnt := _qtyBarsInCnt+1
  45. else
  46. // We already have a _len bar total, so need to subtract last count at the tail of our _len length count.
  47. if _cond[_len]
  48. _cnt := _cnt-1
  49. _qtyBarsInCnt==_len?_cnt:na // Use this to return na until first _len bars have elapsed.
  50. // _cnt // Use this when you want the running count even if full _len bars haven't been examined yet.
  51.  
  52. // ————— Method 3. Very inefficient way to go about the problem.
  53. VerboseAndInefficient_TimesInLast(_cond, _len) =>
  54. // At each bar we loop back _len-1 bars to re-count conditions that were already counted in previous calls, except for the current bar's condition.
  55. _cnt = 0
  56. for i=0 to _len-1
  57. if na(_cond[i])
  58. _cnt := na
  59. else
  60. if _cond[i]
  61. _cnt := _cnt+1
  62. _cnt
  63.  
  64. plot(Ideal_TimesInLast(condition, int(len)), "1. Ideal_TimesInLast", color=color.fuchsia)
  65. plot(VerboseButEfficient_TimesInLast(condition, int(len)), "2. VerboseButEfficient_TimesInLast", color=color.orange)
  66. plot(VerboseAndInefficient_TimesInLast(condition, int(len)), "3. VerboseAndInefficient_TimesInLast")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement