0bps

Strategy vault / Machine learning

Percent Trailing Stop Loss Strategy

This strategy implements a configurable percentage trailing stop loss to manage trade risk. It allows setting long and short stop loss percentage from entry price for dynamic stop loss tracking.

Author
ChaoZhang
Complexity
Starter · 50 lines
Parameters
1
Backtest bar
2h · intraday
Tested on
BTC/USDT · futures

Overview

This strategy implements a configurable percentage trailing stop loss to manage trade risk. It allows setting long and short stop loss percentage from entry price for dynamic stop loss tracking.

Strategy Logic

The main logic is:

  1. Input long and short stop loss percentage
  2. For longs: continually track lows and calculate stop loss line
  3. For shorts: continually track highs and calculate stop loss line
  4. Exit positions when price touches stop loss line

The strategy allows customizing stop percentage, e.g. 10%. For longs, it dynamically calculates 10% above the low as the stop line. For shorts, 10% below the high.

This way, the stop line keeps moving favorably to maximize profit protection while controlling risk.

Advantages

  • Automates trailing stop loss without manual intervention
  • Dynamic stop line protects profits as much as possible
  • Customizable stop loss percentage for different instruments
  • Helps control risk and reduce outsized losses
  • Easy to integrate into other strategies

Risks and Mitigation

  • Slow trailing risks inability to stop out
  • Stop loss too loose can increase losses
  • Stop loss too tight risks over-frequent stops

Mitigations:

  1. Optimize stop percentage to balance effectiveness
  2. Incorporate other stop types like time-based stops
  3. Tune stop based on market volatility
  4. Maintain stop consistency, avoid arbitrary changes

Enhancement Opportunities

Enhancement opportunities:

  1. Machine learning to dynamically optimize stop
  2. Auto-adjust based on max drawdown metrics
  3. Incorporate indicators like moving averages for stop placement
  4. Use different configurations based on volatility regime
  5. Set profit stops after partial stops to lock in profits

Conclusion

This strategy provides an effective percentage trailing stop method to dynamically adjust stop loss. It maximizes profit protection while controlling risk. Enhancements through parameter optimization, indicator integration can make the stops more intelligent.

Parameters

NameDefaultWhat it controls
v_input_110Long Trailing Stop (%)

Source Pine Script

/*backtest
start: 2023-08-19 00:00:00
end: 2023-09-18 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// © theCrypster

//@version=4
strategy("Percent Trailing Stop %", overlay=true)

//ENTER SOME SETUP TRADES FOR TSL EXAMPLE
longCondition = crossover(sma(close, 10), sma(close, 20))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 10), sma(close, 20))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)
    

//TRAILING STOP CODE
trailStop = input(title="Long Trailing Stop (%)", type=input.float, minval=0.0, step=0.1, defval=10) * 0.01

longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - trailStop)
    max(stopValue, longStopPrice[1])
else
    0
shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + trailStop)
    min(stopValue, shortStopPrice[1])
else
    999999

//PLOT TSL LINES
plot(series=strategy.position_size > 0 ? longStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title="Long Trail Stop", offset=1, title="Long Trail Stop")
plot(series=strategy.position_size < 0 ? shortStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title="Short Trail Stop", offset=1, title="Short Trail Stop")


//EXIT TRADE @ TSL
if strategy.position_size > 0
    strategy.exit(id="Close Long", stop=longStopPrice)
if strategy.position_size < 0
    strategy.exit(id="Close Short", stop=shortStopPrice)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-19. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Percent-Trailing-Stop-Loss-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open