Standard Percentage-based Stop Loss for long and short, with configurable extra exit conditions.
Just copy/paste into your script.
Uses "low" and "high" values for crossing the stop loss threshold, but can be changed to "close" if you prefer (change "low" and "high" on lines 31 and 32 to "close")