Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study("barssince() Built-in Replacement with 2nd Occurrence Parameter", "_barssince(Condition, Occurrence)", false, format.price)
- _barssince(Condition, Occurrence) => // barssince() replicated with additional `Occurence` parameter
- n_thOccurrenceExpected = max(1, int(Occurrence))
- conditionHistory = false
- max_bars_back(conditionHistory, 5000)
- if(Condition==true)
- conditionHistory := true
- int return = na // Returns "na" near to bar_index == 0
- occurrenceCounter = 0
- for i=0 to min(bar_index, 4999)
- if(nz(conditionHistory[i], false))
- occurrenceCounter := occurrenceCounter + 1
- if(occurrenceCounter==n_thOccurrenceExpected)
- return := i
- break
- return
- ema7 = ema(close, 7)
- ema15 = ema(close, 15)
- EMAsCrossedBooleanCondition = cross(ema7, ema15)
- //===== Pine barssince() built-in used
- builtInBarsSince = barssince(EMAsCrossedBooleanCondition)
- // ##########################################################################################################################
- occurrenceExpected = input(1, "N-th occurence Expected", input.integer, minval=1)
- //===== _barssince() with `Occurence` parameter in use
- customBarsSince = _barssince(EMAsCrossedBooleanCondition, occurrenceExpected)
- f_barssince(condition, occurrence)=>
- _barssince = bar_index - valuewhen(condition, bar_index, occurrence)
- customBarsSince2 = f_barssince(EMAsCrossedBooleanCondition, occurrenceExpected - 1)
- plot(builtInBarsSince, "builtInBarsSince", color=color.orange, linewidth=18, transp = 70)
- plot(customBarsSince, "customBarsSince", color=#FF000080, linewidth = 8)
- plot(customBarsSince2, "customBarsSince2", color=#000000ff, linewidth = 2)
- plotchar(EMAsCrossedBooleanCondition, "EMAsCrossedBooleanCondition", "•", location.top)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement