Skip to content

Instantly share code, notes, and snippets.

@smyth64
Created March 23, 2022 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smyth64/2db4a07617725f1e20fe0c62a6d14697 to your computer and use it in GitHub Desktop.
Save smyth64/2db4a07617725f1e20fe0c62a6d14697 to your computer and use it in GitHub Desktop.
Pinescript Set Stop Loss to breakeven
breakEvenActivated = input(true, title = 'Break Even Activated', type = input.bool, group="Weedy")
breakEvenPercentage = input(1, title = 'At how many % will the break even stop loss be activated', step=0.1, type = input.float, group="Weedy") * 0.01
breakEvenProfitPercentage = input(0, title = 'Move Stop loss to x% profit', step=0.1, type = input.float, group="Weedy") * 0.01
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0
if (breakEvenActivated)
breakEvenLong = isLong and close > strategy.position_avg_price * (1 + breakEvenPercentage)
breakEvenPriceLong = strategy.position_avg_price * (1 + breakEvenProfitPercentage)
breakEvenShort = isShort and close < strategy.position_avg_price * (1 - breakEvenPercentage)
breakEvenPriceShort = strategy.position_avg_price * (1 - breakEvenProfitPercentage)
strategy.exit(id='Long', comment="Breakeven", stop=breakEvenPriceLong, when=breakEvenLong, qty_percent = 100)
strategy.exit(id='Short', comment="Breakeven", stop=breakEvenPriceShort, when=breakEvenShort, qty_percent = 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment