Advertisement
PineCoders

Jamie's barssince occ

Feb 12th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. //@version=4
  2. study("barssince() Built-in Replacement with 2nd Occurrence Parameter", "_barssince(Condition, Occurrence)", false, format.price)
  3.  
  4. _barssince(Condition, Occurrence) => // barssince() replicated with additional `Occurence` parameter
  5. n_thOccurrenceExpected = max(1, int(Occurrence))
  6. conditionHistory = false
  7. max_bars_back(conditionHistory, 5000)
  8. if(Condition==true)
  9. conditionHistory := true
  10. int return = na // Returns "na" near to bar_index == 0
  11. occurrenceCounter = 0
  12. for i=0 to min(bar_index, 4999)
  13. if(nz(conditionHistory[i], false))
  14. occurrenceCounter := occurrenceCounter + 1
  15. if(occurrenceCounter==n_thOccurrenceExpected)
  16. return := i
  17. break
  18. return
  19.  
  20. ema7 = ema(close, 7)
  21. ema15 = ema(close, 15)
  22. EMAsCrossedBooleanCondition = cross(ema7, ema15)
  23.  
  24. //===== Pine barssince() built-in used
  25. builtInBarsSince = barssince(EMAsCrossedBooleanCondition)
  26.  
  27.  
  28. // ##########################################################################################################################
  29. occurrenceExpected = input(1, "N-th occurence Expected", input.integer, minval=1)
  30.  
  31. //===== _barssince() with `Occurence` parameter in use
  32. customBarsSince = _barssince(EMAsCrossedBooleanCondition, occurrenceExpected)
  33. f_barssince(condition, occurrence)=>
  34. _barssince = bar_index - valuewhen(condition, bar_index, occurrence)
  35. customBarsSince2 = f_barssince(EMAsCrossedBooleanCondition, occurrenceExpected - 1)
  36.  
  37. plot(builtInBarsSince, "builtInBarsSince", color=color.orange, linewidth=18, transp = 70)
  38. plot(customBarsSince, "customBarsSince", color=#FF000080, linewidth = 8)
  39. plot(customBarsSince2, "customBarsSince2", color=#000000ff, linewidth = 2)
  40. plotchar(EMAsCrossedBooleanCondition, "EMAsCrossedBooleanCondition", "•", location.top)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement