TradingView
jason5480
3 מאי 2021 18:36

Trailing Stop Loss 

Ethereum / TetherUSBinance

תיאור

This script demonstrate how to make a Training Stop Loss to "ride the wave". In comparison to classic Stop Loss this strategy follows the price upwards (for long positions) and when price drops by a fixed percentage then you exit your position.

הערות שחרור

  • Minor typos

הערות שחרור

  • Add the ability to disable trailing for demonstration reasons
  • Close open orders on trend reversal
  • Add sell labels on cross-under

הערות שחרור

  • Fix a bug that made Stop Loss to not trigger appropriately when it was reached at the very first candle end enable trailing was un-checked

הערות שחרור

  • Refactor stopValue calculation to be more readable

הערות שחרור

  • Fix longIsActive logic

הערות שחרור

  • Check if time is within backtest period in order to set the price to prevent unnecessary calculations and drawing outside the area of interest
  • Replace deprecated trasp argument with color.new

הערות שחרור

  • Set calc on every tick to false by default, as @fgo943 suggested

הערות שחרור

  • Minor Update the chart for a better overview and understanding of the trailing stop loss

הערות שחרור

  • Fix a bug that were causing stop loss to be calculated wrong when there was new entry back to back with the end of the previous one
  • Use strategy.percent_of_equity

הערות שחרור

  • Draw lines with width 1

הערות שחרור

  • Update to pinescript V5
  • Stop Loss step is now 0.05

הערות שחרור

  • Let's face it a Green-Red color model is more intuitive!

הערות שחרור

  • Happy New Year!
  • Many code refactorings and optimizations to better use this script as a template and plug your own strategy at ease

הערות שחרור

  • Bug fix: (rare/minor) Remove 'strategy.position_size == 0' condition in stop loss calculation that made the very first stop loss price target to be calculated based on previous 'strategy.position_avg_price' instead of 'close' if the previous position closed exactly one bar before!

הערות שחרור

  • (minor) refactoring

הערות שחרור

  • Use time_filters library

הערות שחרור

  • Minor revision update

הערות שחרור

  • Replace deprecated "when=" with if statements in strategy calls

הערות שחרור

  • Change fastMA default color to yellow

הערות שחרור

  • Increase max labels to 300
  • Minor refactoring

הערות שחרור

  • Update time_filters library

הערות שחרור

  • Some minor label/color changes

הערות שחרור

  • small refactoring

הערות שחרור

  • Update time_filters library version

הערות שחרור

  • Update time_filters library version

הערות שחרור

  • Use the 'chrono_utils' library for the date-time window filter
תגובות
cryptopreet
ur script is awesome.... instead of "longTrailingStopLossPerc" , i want to use " StopLong= close <= open-ma_function(tr(true), length) " as a trailing stop loss. i need help in converting this series[bool] argument to type: series[float].
Soincoin
Thanks for sharing this code. Very clear for eduacational purposes and easy to implement in other strategies
THELEGENDSINGH
good one love that
jason5480
@THELEGENDSINGH, Thank you, enjoy!
KCBC3319
Hey Jason,

I am trying to use this code :

if bb_cross_test and no_position and current_bar_test
strategy.entry("Cross Short Entry Test", strategy.short, limit = upper_5 )

strategy.exit("Cross Short Exit Test 1/2","Cross Short Entry Test", limit = upper_6, qty_percent = 50, loss = std_loss)

strategy.exit("Cross Short Exit Test 2/2","Cross Short Entry Test", qty_percent = 100, trail_price = upper_6, trail_offset = + 50 )

So that the first exit sells 50% of the position to take profit at a certain level, and then at that same level a trailing stop for the other half of the position is implemented.

I am having difficulties making this work though it seems to me the logic makes sense. Is it necessary to use the code you have built to make something like this happen?

Thanks!
jason5480
@KCBC3319, Check the "Trailing Take Profit Trailing Stop Loss" script in my profile. I implemented what you try to achieve here.
pdreamy
Hello @jason5480 , thanks for this!

I would like to know how I can add a fixed spot loss in the code?
I already checked your other scripts this one seems to work the best :)
jason5480
@pdreamy, Hello if I understood correctly you want to set a fixed stop loss e.g. 10 USD. A good start would be to replace line 118 with something like "close - longTrailingStopLoss" where the longTrailingStopLoss variable is configurable from the user and its value is 10 for this example.
jeno_
Very nicely done, just what I need! However I remember I read somewhere some people saying tradingview build-in trailing function in buggy/flaws, would you kind enough to shed some light on this?
jason5480
@jeno_, Build-in trailing function? Maybe you are talking about this tradingview.com/script/W8nc1c3V-Trailing-Take-Profit/ Well as far as I know I does the job
עוד