Search code examples
pine-script

How to buy sell on the pivot high & low signals


I am new to pine script & would like to ask you the simple code where it should buy at the pivotHigh & sell at the pivotLow. Thanks in advance for your help...

`//@version=5
strategy("pivot High & Low Trade", overlay = true)

leftbars = 5
rightbars = 5
pivLow = ta.pivotlow(leftbars, rightbars)
pivHigh = ta.pivothigh(leftbars, rightbars)
plotshape(pivHigh, style = shape.labelup, location = location.abovebar, offset = - rightbars)
plotshape(pivLow, style = shape.labelup, location = location.belowbar, offset = - rightbars)`
`


Please suggest the code for the same.

Solution

  • Life would be perfect if we could always buy at the bottom and sell at the top, right?

    Unfortunately, that is not possible. You see that offset = - rightbars in your plotshape()? That plots the shape with an offset.

    In your example, it will verify the price action and then plot it with a 5 bar offset. So, you will always be delayed by 5 bars.