Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //@author=LucF
- // TimesInLast [LucF]
- // v1.0, 2019.07.15 19:37 — Luc
- // This script illustrates 3 different ways of counting the number of occurrences in the last len bars.
- study("TimesInLast [LucF]")
- // ————— Allow different form-types to be specified as length value.
- // This part is only there to show the impact of using different form-types of length with the 3 functions.
- // In normal situation, we would just use the following:
- // len = input(100, "Length")
- deflen = 100 // Change this value when you want to use different lengths. Inputs cannot be change through Settings/Inputs; only the form-type.
- LT1 = "1. input int", LT2 = "2. input float", LT3="3. series int", LT4="4. series float"
- lt = input(LT1, "Type of 'length' argument to functions", options=[LT1, LT2, LT3, LT4])
- len1 = input(deflen, LT1, type=input.integer, minval=deflen, maxval=deflen)
- len2 = input(deflen, LT2, type=input.float, minval=deflen, maxval=deflen)
- var len3 = 0
- len3 := len3==deflen? len3:len3+1
- var len4 = 0.
- len4 := len4==deflen? len4:len4+1
- // Choose proper form-type of length.
- len = lt==LT1? len1 : lt==LT2? len2 : lt==LT3? len3 : lt==LT4? len4 : na
- // Condition on which all counts are done.
- condition = close>open
- // ————— Method 1. This function uses Pine's built-in function but only accepts a simple int for the length.
- Ideal_TimesInLast(_cond, _len) => sum(_cond?1:0, _len)
- // ————— Method 2. This function is equivalent to using sum() but works with a float and series value for _len.
- VerboseButEfficient_TimesInLast(_cond, _len) =>
- // For first _len bar we just add to cumulative count of occurrences.
- // After that we add count for current bar and make adjustment to count for the tail bar in our mini-series of length=_len.
- var _qtyBarsInCnt = 0
- var _cnt = 0
- if _cond
- // Add to count as per current bar's condition state.
- _cnt := _cnt+1
- if _qtyBarsInCnt<_len
- // We have not counted the first _len bars yet; keep adding to checked bars count.
- _qtyBarsInCnt := _qtyBarsInCnt+1
- else
- // We already have a _len bar total, so need to subtract last count at the tail of our _len length count.
- if _cond[_len]
- _cnt := _cnt-1
- _qtyBarsInCnt==_len?_cnt:na // Use this to return na until first _len bars have elapsed.
- // _cnt // Use this when you want the running count even if full _len bars haven't been examined yet.
- // ————— Method 3. Very inefficient way to go about the problem.
- VerboseAndInefficient_TimesInLast(_cond, _len) =>
- // 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.
- _cnt = 0
- for i=0 to _len-1
- if na(_cond[i])
- _cnt := na
- else
- if _cond[i]
- _cnt := _cnt+1
- _cnt
- plot(Ideal_TimesInLast(condition, int(len)), "1. Ideal_TimesInLast", color=color.fuchsia)
- plot(VerboseButEfficient_TimesInLast(condition, int(len)), "2. VerboseButEfficient_TimesInLast", color=color.orange)
- plot(VerboseAndInefficient_TimesInLast(condition, int(len)), "3. VerboseAndInefficient_TimesInLast")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement