Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study("Consecutive Up/Down Study", overlay=true)
- consecutiveBarsUp = input(3)
- consecutiveBarsDown = input(3)
- price = close
- ups = 0.0
- dns = 0.0
- ups := price > price[1] ? nz(ups[1]) + 1 : 0
- dns := price < price[1] ? nz(dns[1]) + 1 : 0
- goLong = ups >= consecutiveBarsUp
- goShort = dns >= consecutiveBarsDown
- // Use your own exit conditions here.
- outLong = dns >= consecutiveBarsDown - 1
- outShort = ups >= consecutiveBarsUp - 1
- inLongPosition = na
- inShortPosition = na
- inLongPosition := (goLong and not inShortPosition[1]) or (inLongPosition[1] and not outLong)
- inShortPosition := (goShort and not inLongPosition[1]) or (inShortPosition[1] and not outShort)
- enterLong = inLongPosition and not inLongPosition[1]
- exitLong = inLongPosition[1] and not inLongPosition
- enterShort = inShortPosition and not inShortPosition[1]
- exitShort = inShortPosition[1] and not inShortPosition
- alertcondition(enterLong)
- alertcondition(exitLong)
- alertcondition(enterShort)
- alertcondition(exitShort)
- plotshape(enterLong, location=location.belowbar, style=shape.arrowup, color=lime)
- plotshape(exitLong, location=location.abovebar, style=shape.arrowdown, color=green)
- plotshape(enterShort, location=location.abovebar, style=shape.arrowdown, color=red)
- plotshape(exitShort, location=location.belowbar, style=shape.arrowup, color=maroon)
- // Debugging.
- plotchar(goLong, "goLong", "▲", location.top, size = size.tiny)
- plotchar(goShort, "goShort", "▼", location.bottom, size = size.tiny)
- plotchar(inLongPosition, "inLongPosition", "—", location.top, size = size.tiny)
- plotchar(inShortPosition, "inShortPosition", "—", location.bottom, size = size.tiny)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement