Created
March 23, 2022 00:10
-
-
Save smyth64/2db4a07617725f1e20fe0c62a6d14697 to your computer and use it in GitHub Desktop.
Pinescript Set Stop Loss to breakeven
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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