Strategy vault / Trend following
Fixed Percentage Stop Loss and Take Profit Strategy Based on Moving Averages
This strategy uses moving averages to generate trading signals and sets fixed percentage stop loss and take profit levels based on the entry price to control the risk and reward of each trade.

Overview
This strategy uses moving averages to generate trading signals and sets fixed percentage stop loss and take profit levels based on the entry price to control the risk and reward of each trade.
Strategy Logic
The strategy first uses the 5-day exponential moving average (EMA) and 32-day EMA to determine the trend direction. It goes long when the short-term moving average crosses above the long-term one and goes short on cross under.
After entering a trade, the strategy dynamically sets the stop loss and take profit for each trade based on the user-defined stop loss percentage and take profit percentage. Specifically, for long trades, the stop loss is set at the entry price × (1 - stop loss percentage) and take profit is set at entry price × (1 + take profit percentage). For short trades it is reversed - stop loss at entry price × (1 + stop loss percentage) and take profit at entry price × (1 - take profit percentage).
This allows ensuring fixed risk/reward ratio for each trade and controlling the risk and profit.
Advantage Analysis
This way of setting stop loss and take profit has several significant advantages:
It can limit the maximum loss per trade and effectively control trading risk.
It can lock in fixed profit ratio per trade and ensure return.
The stop loss and take profit points vary with the actual entry price instead of using fixed values.
Users can determine their risk appetite by adjusting the input parameters.
Simple and intuitive strategy logic, easy to understand and verify.
Risk Analysis
There are also some risks with this strategy:
Moving averages may generate excessive invalid signals, with high chance of being stopped out after entry.
Take profit ratio set too high may result insufficient profitability, too low may fail to win enough.
Stop loss too close may increase the chance of being stopped out and should give some buffer.
Choice of trading products and timeframes may affect the effectiveness.
Corresponding solutions:
Optimize moving average parameters to reduce false signals.
Test different take profit ratios to find optimum.
Adjust stop loss distance based on market volatility.
Evaluate strategy performance across different products and timeframes.
Optimization Directions
The strategy can be improved in the following aspects:
Add other indicators for trend validation to avoid excessive false signals from moving averages.
Optimize the stop loss and take profit percentages based on backtest data to find optimum parameters.
Change stop loss to trailing stop to lock in more running profit.
Add position sizing rules with pyramiding and stop loss to manage trade risk.
Assess performance variance across different trading instruments and timeframes.
Summary
This strategy identifies trend direction with moving averages, and sets fixed percentage stop loss and take profit based on entry price to control single trade risk and reward. Its advantage is effectively limiting losses, ensuring profit ratio, with simple and straightforward logic. Proper configuration of stop loss/take profit parameters, selection of trading products and timeframes, and various ways to further optimize the strategy need to be noted.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 5 | Stop Loss % |
| v_input_2 | 10 | Take Profit % |
Source Pine Script
/*backtest
start: 2022-12-11 00:00:00
end: 2023-12-17 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// © theCrypster 2020
//@version=4
strategy("Fixed Percent Stop Loss & Take Profit %", overlay=true)
// Moving Averages to get some example trades generated
eg1 = ema(close, 5)
eg2 = ema(close, 32)
long = crossover(eg1, eg2)
short = crossunder(eg1, eg2)
strategy.entry("LONG", strategy.long, when=long)
strategy.entry("SHORT", strategy.short, when=short)
//
// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input(5.0, title='Stop Loss %', type=input.float) / 100
takePer = input(10.0, title='Take Profit %', type=input.float) / 100
// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)
if strategy.position_size > 0
strategy.exit(id="Close Long", stop=longStop, limit=longTake)
if strategy.position_size < 0
strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)
//PLOT FIXED SLTP LINE
plot(strategy.position_size > 0 ? longStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Long Fixed SL")
plot(strategy.position_size < 0 ? shortStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Short Fixed SL")
plot(strategy.position_size > 0 ? longTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Long Take Profit")
plot(strategy.position_size < 0 ? shortTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Short Take Profit")
//
Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-18. 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.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Fixed-Percentage-Stop-Loss-and-Take-Profit-Strategy-Based-on-Moving-Averages.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- 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
- 200 EMA VWAP MFI Trend Following StrategyPine Script · Trend following · 1d
- 4 EMA Trend StrategyPine Script · Trend following · 1d
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR Dynamic Trend Following and EMA Crossover Trading StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- ATR and T3 Moving Average StrategyPine Script · Trend following · 5m