0bps

Strategy vault / Trend following

Dynamic Trailing Stop Loss Strategy

This strategy is based on a dynamic trailing stop loss mechanism to set stop loss lines for long and short positions based on the highest and lowest prices of a stock. When the price hits the stop loss line, it will…

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

IMG

Overview

This strategy is based on a dynamic trailing stop loss mechanism to set stop loss lines for long and short positions based on the highest and lowest prices of a stock. When the price hits the stop loss line, it will close the current position and open a new position in the opposite direction. The strategy is simple and effective in controlling single transaction risk.

Principle

The main steps of this strategy are:

  1. Input parameters: choose to go long or short, set length for period, trailing stop slippage
  2. Calculate highest and lowest prices: get highest and lowest prices based on input length
  3. Calculate trailing stop loss lines: for long, lowest price minus slippage; for short, highest price plus slippage
  4. Open and close positions: when price hits stop loss line, close current direction position, and open opposite direction position

The above is the basic logic of the strategy. As price moves, the stop loss line keeps updating for dynamic tracking. By trailing stop loss, it can effectively control losses per trade.

Advantage Analysis

The main advantages of this strategy:

  1. Simple and clean logic, easy to understand and implement
  2. Dynamic trailing stop loss controls single trade loss
  3. Flexible to choose long or short, adaptable to different market environments
  4. Customization of period and slippage for optimization

In summary, by simple trailing stop loss mechanisms, this strategy can effectively manage positions and is a typical Risk Management strategy.

Risk Analysis

There are also some risks to note:

  1. Price volatility may trigger stop loss frequently, leading to over-trading
  2. Improper period settings may cause unsuitable stop loss lines
  3. Excessive slippage setting may result in loose stop loss, unable to stop loss in time

These risks can be optimized by adjusting the period, reducing slippage reasonably to make more sensible stop loss lines.

Optimization Directions

The strategy can be upgraded from the following aspects:

  1. Add optimization for dynamic stop loss line adjustment, avoid improper tight or loose stop loss lines
  2. Add open position conditions to avoid opening positions at inappropriate times
  3. Incorporate trend indicators for trend-following with more profit potential
  4. Add position sizing to dynamically adjust positions based on risk levels

Conclusion

The trading strategy realizes dynamic positions management through simple trailing stop loss methods. It is easy to understand and implement, and can effectively control single trade loss. We analyzed the advantages, potential risks and future optimization directions. In conclusion, this is a highly typical and practical Risk Management strategy.

Parameters

NameDefaultWhat it controls
v_input_1trueLong
v_input_2falseShort
v_input_320length
v_input_4falseTrailing Stop
v_input_5falsebackground

Source Pine Script

/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//Noro
//2019

//@version=4
strategy(title = "Noro's Trailing-Stop Strategy", shorttitle = "Trailing", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 0)

//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(false, defval = false, title = "Short")
length = input(20, minval = 1)
shift = input(0.0, minval = 0, title = "Trailing Stop")
background = input(false)

//Levels
max = highest(high, length)
min = lowest(low, length)

//Trailing
size = strategy.position_size
longtrailing = 0.0
shorttrailing = 0.0
longtrailing := size <= 0 ? min - ((min / 100) * shift) : max(min - ((min / 100) * shift), longtrailing[1])
shorttrailing := size >= 0 ? max + ((max / 100) * shift) : min(max + ((max / 100) * shift), shorttrailing[1])
trailing = size <= 0 ? shorttrailing : longtrailing
col = size == size[1] ? size > 0 ? color.red : color.lime : na
plot(trailing, color = col, linewidth = 2, transp = 0)

//Background
bgcol = background ? size > 0 ? color.lime : color.red : na
bgcolor(bgcol, transp = 80)

if trailing > 0 and size <= 0
    strategy.entry("Long", strategy.long, needlong ? na : 0, stop = trailing)
if trailing > 0 and size >= 0
    strategy.entry("Short", strategy.short, needshort ? na : 0, stop = trailing)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-31. 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/Dynamic-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