Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("Swings", overlay=true)
- import andyepartridge/utils/3 as utils
- pivotLength = input.int(5)
- pivotHigh = ta.pivothigh(close, pivotLength, pivotLength)
- pivotLow = ta.pivotlow(close, pivotLength, pivotLength)
- var float lastPivotHigh = na
- var float lastPivotLow = na
- var line stopLossLine = na
- var line takeProfitLine = na
- // Add stop loss and take profit to keep track of
- var float stopLoss = na
- var float takeProfit = na
- // If no position, reset the stop loss and take profit
- if strategy.position_size == 0
- stopLoss := na
- takeProfit := na
- // Update last pivot high and low
- if not na(pivotHigh)
- lastPivotHigh := pivotHigh
- if not na(pivotLow)
- lastPivotLow := pivotLow
- // Enter and set the exit for short positions
- if strategy.position_size == 0 and not na(lastPivotHigh) and not na(lastPivotLow)
- if close < lastPivotLow
- strategy.entry("Short", strategy.short)
- stopLoss := lastPivotHigh
- takeProfit := lastPivotLow
- strategy.exit("TP / SL", "Short", stop=stopLoss, limit=takeProfit)
- // Move the trailing stop for short positions
- if strategy.position_size < 0 and not na(lastPivotHigh) and pivotHigh < lastPivotHigh
- stopLoss := pivotHigh
- strategy.exit("TP / SL", "Short", stop=stopLoss, limit=takeProfit)
- // Enter and set the exit for long positions
- if strategy.position_size == 0 and not na(lastPivotHigh) and not na(lastPivotLow)
- if close > lastPivotHigh
- strategy.entry("Long", strategy.long)
- stopLoss := lastPivotLow
- takeProfit := lastPivotHigh
- strategy.exit("TP / SL", "Long", stop=stopLoss, limit=takeProfit)
- // Move the trailing stop for long positions
- if strategy.position_size > 0 and not na(lastPivotLow) and pivotLow > lastPivotLow
- stopLoss := pivotLow
- strategy.exit("TP / SL", "Long", stop=stopLoss, limit=takeProfit)
- // Plot the stop loss and take profit
- plot(strategy.position_size != 0 ? stopLoss : na, title="SL", style=plot.style_linebr, color=color.red)
- plot(strategy.position_size != 0 ? takeProfit : na, title="TP", style=plot.style_linebr, color=color.green)
- utils.DrawDailyTable()
Advertisement
Add Comment
Please, Sign In to add comment