Strategy vault / Trend following
Cycle Position Trend Following Strategy
The Cycle Position Trend Following Strategy is a quantitative trading strategy that determines the trend direction based on the 200-day Simple Moving Average (SMA). It provides two modes - "Follow Uptrend" and…

Overview
The Cycle Position Trend Following Strategy is a quantitative trading strategy that determines the trend direction based on the 200-day Simple Moving Average (SMA). It provides two modes - "Follow Uptrend" and "Follow Downtrend" for traders to choose from according to their preferences. The strategy allows traders to customize stop loss and take profit levels for greater flexibility.
How The Strategy Works
The core indicator of this strategy is the 200-day SMA. The strategy has two modes:
Follow Uptrend Mode: Go long when close is above 200-day SMA; close position when stop loss or take profit is triggered.
Follow Downtrend Mode: Go long when close is below 200-day SMA; close position when stop loss or take profit is triggered.
The long condition is defined in longCondition variable based on the close price's relation to the 200-day SMA. The close condition is defined in closeCondition variable based on stop loss, take profit and SMA.
Specifically, strategy.entry is used to open long positions when the long condition is met. strategy.exit is used to close positions when the close condition is triggered.
Advantages of The Strategy
The advantages of this strategy include:
Simple and clear logic that is easy to understand.
Provides two optional modes to suit different market environments.
Customizable stop loss and take profit allows tuning of risk-reward profile.
Uses the well-known 200-day SMA indicator to determine trend direction.
Generates automated trading signals without manual intervention.
Risks of The Strategy
The risks of this strategy include:
Overly reliant on a single indicator, prone to false signals. Adding other indicators like MACD, KDJ for confirmation could help.
Stop loss and take profit levels too tight or too wide could lead to premature stop out or missing ideal exit points. Parameters need proper testing and optimization.
Using close price for signals has closing price biases. Consider using candle body or add signal confirmation.
Does not account for trading costs. Need to reserve costs when going live.
Ways to Improve The Strategy
Some ways to improve the strategy:
Add other indicators to confirm signals and avoid false signals, e.g. MACD.
Optimize stop loss and take profit parameters to find optimal combination through backtesting.
Add trend filter to only trade in well-defined trends, e.g. ADX.
Improve entry method by considering candle body or adding confirmation.
Consider trading volume to validate signal reliability.
Test different SMA periods to find the optimal parameter.
Conclusion
In conclusion, the strategy has clear and understandable logic with practical value. But reliance on a single indicator has limitations. More conditions should be added for confirmation. Parameters also need testing and optimization for better live performance. Furthermore, trading costs like slippage and commissions require consideration in live trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_string_1 | 0 | mode: Buy Uptrend | Buy Downtrend |
| v_input_float_1 | 0.9 | Stop Loss (SL) level |
| v_input_float_2 | 1.1 | Take Profit (TP) level |
Source Pine Script
/*backtest
start: 2022-11-10 00:00:00
end: 2023-11-16 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © I11L
//@version=5
strategy("Cycle Position Trading", overlay=true, pyramiding=1, default_qty_value=100000, initial_capital=100000, default_qty_type=strategy.cash, process_orders_on_close=false, calc_on_every_tick=false)
// Input for selecting the mode
mode = input.string("Buy Uptrend", options = ["Buy Uptrend", "Buy Downtrend"])
// Input for customizing stop loss and take profit levels
stopLoss = input.float(0.9, title="Stop Loss (SL) level", step=0.01)
takeProfit = input.float(1.1, title="Take Profit (TP) level", step=0.01)
// Calculate the 200-day Simple Moving Average (SMA)
sma = ta.sma(close, 200)
// Plot the SMA on the chart
plot(sma)
// Define the conditions for entering a long position based on the selected mode
longCondition = mode == "Buy Uptrend" ? close > sma and close[5] > sma : close < sma
// Define the conditions for closing a position based on the selected mode
closeCondition = mode == "Buy Uptrend" ? (strategy.position_avg_price * stopLoss > close or strategy.position_avg_price * takeProfit < close or close < sma * 0.95) : (strategy.position_avg_price * stopLoss > close or strategy.position_avg_price * takeProfit < close or close > sma * 1.05)
// Execute a long position if the longCondition is met
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
// Close the position if the closeCondition is met
if (closeCondition)
strategy.exit("Exit", limit = close)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-17. 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/Cycle-Position-Trend-Following-Strategy.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
- EMA SMA Multi Indicator Comprehensive Trend Following StrategyPine Script · Trend following · 1h
- Multi Dimensional Short Term Trend Predictor Technical Analysis and Volatility Adaptive Trading StrategyPine Script · Trend following · 1h
- Multi Indicator Dynamic Trend Following Strategy Based on EMA and SMAPine Script · Trend following · 1d
- Advanced Quantitative Trading Strategy Multi Dimensional Super Trend ATR Dynamic Tracking SystemPine Script · Trend following · 1h
- Dual EMA Crossover Trend Following Strategy with ATR and ADX FiltersPine Script · Trend following · 1d
- Trend Confirmation Multi Indicator Dynamic Stop Loss StrategyPine Script · Trend following · 1d