0bps

Strategy vault / Machine learning

Swing Trading Strategy

This is a trend-following strategy based on moving average crossover, combined with stop loss/take profit management and leverage effect, aiming to identify trends across multiple markets and maximize profits.

Author
ChaoZhang
Complexity
Intermediate · 80 lines
Parameters
14
Backtest bar
2h · intraday
Tested on
BTC/USDT · futures

Overview

This is a trend-following strategy based on moving average crossover, combined with stop loss/take profit management and leverage effect, aiming to identify trends across multiple markets and maximize profits.

Strategy Logic

The strategy uses crossover of fast and slow moving averages as trading signals. It goes long when the fast MA crosses above the slow MA, and goes short when the fast MA crosses below the slow MA.

To filter out noise trades from minor trends, it also uses a 200-day MA as a trend filter. Trade signals are only generated when the price is above or below the 200-day MA.

The strategy uses range trading stops. After entry, fixed percentage stop loss and take profit levels are set, e.g. 1% stop loss and 1% take profit. Positions will be closed when price hits the stop loss or take profit.

Leverage effect is employed to amplify trading profits. Based on different market characteristics, appropriate leverage ratios can be selected, e.g. 10x.

Advantage Analysis

  • One advantage is that it can identify trends across multiple markets including crypto, stocks and futures, making the strategy widely applicable.

  • Using fast/slow MA crossover and trend filter can better identify trend direction and achieve good win rate in trending markets.

  • Range trading stops help control single trade loss within bearable range, allowing stable running of the strategy.

  • Leverage effect amplifies trading profits, making full use of the strategy edge.

  • Visual interface design with different background colors for bull/bear markets offers intuitive market insight.

Risk Analysis

  • The strategy is trend-following so may underperform in choppy, range-bound markets. Position sizing should be controlled.

  • Fixed percentage stop loss/take profit carries risk of getting stopped out. Levels should be adjusted based on specific market volatility.

  • Leverage amplifies position size as well as risks. Leverage ratio should be controlled to avoid oversized losses.

  • Lagging nature of moving averages may cause delayed trade signals.

Optimization Directions

  • Test different parameter combinations and select optimal fast/slow MA lengths.

  • Incorporate other indicators or models as filter signals to improve accuracy, e.g. ATR stops, RSI etc.

  • Research other trend identification tools like ADX to further enhance trend capturing ability.

  • Use machine learning models to optimize strategy signals and find more effective entry/exit points.

  • Consider dynamic stop loss/take profit based on volatility and market conditions for more sensible stops.

Summary

The strategy employs a systematic trend-following approach and uses stops/take profit and leverage to control risk and magnify profits. It is widely applicable across markets with potential for steady alpha. Attention should still be paid to parameter optimization, risk control and strategy iteration for long-term success.

Parameters

NameDefaultWhat it controls
v_input_10Source MA Type: EMA | SMA
v_input_250Source MA Length
v_input_320Fast MA Length
v_input_450Slow MA Length
v_input_5trueTrend Filter
v_input_60Trend Filter MA Type: EMA | SMA
v_input_7200Trend Filter MA Period
v_input_8trueShow MAs
v_input_9falseSwing Trading
v_input_10true(?Backtest Control)Stop Loss (in %)
v_input_11trueTake Profit (in %)
v_input_1210Leverage
v_input_13timestamp(2021 01 01)Backtest Start Time
v_input_14timestamp(2021 12 31)Backtest End Time

Source Pine Script

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

////////////////////////////////////////////////////////////////////////////////
// Bozz Strategy
// Developed for Godstime
// Version 1.1
// 11/28/2021
////////////////////////////////////////////////////////////////////////////////

//@version=4
// strategy("Bozz Strategy", "", true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, margin_long=0, margin_short=0)

// ----------------------------- Inputs ------------------------------------- //
source_ma_type = input("EMA", "Source MA Type", options=["SMA", "EMA"])
source_ma_length = input(50, "Source MA Length")
fast_ma_length = input(20, "Fast MA Length")
slow_ma_length = input(50, "Slow MA Length")
use_trend_filter = input(true, "Trend Filter")
trend_filter_ma_type = input("EMA", "Trend Filter MA Type", options=["SMA", "EMA"])
trend_filter_ma_length = input(200, "Trend Filter MA Period")
show_mas = input(true, "Show MAs")
swing_trading_mode = input(false, "Swing Trading")

// -------------------------- Calculations ---------------------------------- //
fast_ma = ema(close, fast_ma_length)
slow_ma = ema(close, slow_ma_length)
source_ma = source_ma_type == "EMA"? ema(close, source_ma_length): 
                                     sma(close, source_ma_length)
trend_filter_ma = trend_filter_ma_type == "EMA"? ema(close, trend_filter_ma_length): 
                                                 sma(close, trend_filter_ma_length)

// --------------------------- Conditions ----------------------------------- //
uptrend = not use_trend_filter or close > trend_filter_ma
buy_cond = crossover(fast_ma, slow_ma) and uptrend

downtrend = not use_trend_filter or close < trend_filter_ma
sell_cond = crossunder(fast_ma, slow_ma) and downtrend

// ---------------------------- Plotting ------------------------------------ //
bgcolor(use_trend_filter and downtrend? color.red: use_trend_filter? color.green: na)
plot(show_mas? fast_ma: na, "Fast MA", color.green)
plot(show_mas? slow_ma: na, "Slow MA", color.red)
plot(show_mas? source_ma: na, "Source MA", color.purple)
plot(show_mas? trend_filter_ma: na, "Trend Filter MA", color.blue)


// ---------------------------- Trading  ------------------------------------ //
// Inputs
sl_perc = input(1.0, "Stop Loss (in %)", group="Backtest Control")/100
tp_perc = input(1.0, "Take Profit (in %)", group="Backtest Control")/100
leverage = input(10, "Leverage", maxval=100, group="Backtest Control")
bt_start_time = input(timestamp("2021 01 01"), "Backtest Start Time", input.time, group="Backtest Control")
bt_end_time = input(timestamp("2021 12 31"), "Backtest End Time", input.time, group="Backtest Control")

// Trading Window
in_trading_window = true
trade_qty = (strategy.equity * leverage) / close 

// Long Side
strategy.entry("Long Entry", strategy.long,  when=buy_cond and in_trading_window)
long_tp = strategy.position_avg_price * (1 + tp_perc)
long_sl = strategy.position_avg_price * (1 - sl_perc)
if not swing_trading_mode
    strategy.exit("Long Exit", "Long Entry", limit=long_tp, stop=long_sl)

// Short Side
strategy.entry("Short Entry", strategy.short, when=sell_cond and in_trading_window)
short_tp = strategy.position_avg_price * (1 - tp_perc)
short_sl = strategy.position_avg_price * (1 + sl_perc)
if not swing_trading_mode
    strategy.exit("Short Exit", "Short Entry", limit=short_tp, stop=short_sl)

// End of trading window close
strategy.close_all(when=not in_trading_window)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-11. 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/Swing-Trading-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