0bps

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…

Author
ChaoZhang
Complexity
Starter · 41 lines
Parameters
3
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

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:

  1. Follow Uptrend Mode: Go long when close is above 200-day SMA; close position when stop loss or take profit is triggered.

  2. 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:

  1. Simple and clear logic that is easy to understand.

  2. Provides two optional modes to suit different market environments.

  3. Customizable stop loss and take profit allows tuning of risk-reward profile.

  4. Uses the well-known 200-day SMA indicator to determine trend direction.

  5. Generates automated trading signals without manual intervention.

Risks of The Strategy

The risks of this strategy include:

  1. Overly reliant on a single indicator, prone to false signals. Adding other indicators like MACD, KDJ for confirmation could help.

  2. 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.

  3. Using close price for signals has closing price biases. Consider using candle body or add signal confirmation.

  4. Does not account for trading costs. Need to reserve costs when going live.

Ways to Improve The Strategy

Some ways to improve the strategy:

  1. Add other indicators to confirm signals and avoid false signals, e.g. MACD.

  2. Optimize stop loss and take profit parameters to find optimal combination through backtesting.

  3. Add trend filter to only trade in well-defined trends, e.g. ADX.

  4. Improve entry method by considering candle body or adding confirmation.

  5. Consider trading volume to validate signal reliability.

  6. 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

NameDefaultWhat it controls
v_input_string_10mode: Buy Uptrend | Buy Downtrend
v_input_float_10.9Stop Loss (SL) level
v_input_float_21.1Take 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.

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/Cycle-Position-Trend-Following-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