0bps

Strategy vault / Machine learning

Spaced Out Trading Strategy

The Spaced Out Trading Strategy is a trend-following strategy based on moving averages. It utilizes a 30-day exponential moving average (EMA) to identify price trends and enters trades when prices break out…

Author
ChaoZhang
Complexity
Starter · 37 lines
Parameters
3
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The Spaced Out Trading Strategy is a trend-following strategy based on moving averages. It utilizes a 30-day exponential moving average (EMA) to identify price trends and enters trades when prices break out above/below the EMA. It exits trades when prices fall back below/above the EMA line. This strategy works well with 30-min to daily timeframes.

Strategy Logic

The core logic relies on the relationship between price and the 30-day EMA to generate entry and exit signals. Specifically:

  1. Calculate the 30-day EMA as the benchmark for the trend.
  2. Enter long trades when prices break out above the EMA.
  3. Exit trades when prices fall back below the EMA.

By capturing trend breakouts, it aims to capitalize on momentum moves and trend-following opportunities.

Advantage Analysis

The main advantages of this strategy include:

  1. Simple logic that is easy to understand and implement at low costs.
  2. Smoothens price fluctuations using EMA and focuses on the main trend.
  3. The 30-day EMA provides a medium-term lens to capture both swing and long-term trends.
  4. Customizable parameters adaptable across products and market regimes.

Risks and Mitigations

Some of the key risks are:

  1. Whipsaw risk from prices reversing after temporary breakout of EMAs. Can use longer EMA periods.
  2. Risk of accumulated losses from sustained trend reversal. Can set stop-loss limits.
  3. Suboptimal EMA period risk. Can ensemble adaptive EMA or multiple EMAs.

Enhancement Opportunities

Some ways the strategy can be upgraded:

  1. Add adaptive EMAs tailored to market volatility and asset characteristics.
  2. Build multi-EMA systems combining short and long-term EMAs.
  3. Incorporate stop-loss mechanisms e.g. moving average stop, range bound stop.
  4. Combine with other indicators e.g. momentum, volatility for signal filtering.
  5. Parameter optimization via machine learning algorithms.

Summary

The Spaced Out Trading Strategy aims to capture trends by trading price breakouts of EMA levels. It is a simple and practical quantitative strategy. With customizable loss limits and judicious optimizations, it can be a stable strategy providing sustainable returns across medium to long-term holding periods.

Parameters

NameDefaultWhat it controls
v_input_130EMA Period
v_input_22Stop Loss Percentage
v_input_33Take Profit Percentage

Source Pine Script

/*backtest
start: 2024-01-23 00:00:00
end: 2024-02-22 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Spaced Out Trading Strategy", overlay=true)

// Define strategy parameters
emaPeriod = input(30, title="EMA Period")  // Longer EMA period for more spaced-out trades
stopLossPct = input(2.0, title="Stop Loss Percentage")  // Stop loss percentage
takeProfitPct = input(3.0, title="Take Profit Percentage")  // Take profit percentage

// Calculate EMA
emaValue = ta.ema(close, emaPeriod)

// Define entry and exit conditions
enterLong = ta.crossover(close, emaValue)
exitLong = ta.crossunder(close, emaValue)

// Place orders
contractsQty = 5  // Number of contracts to buy
var float lastTradePrice = na  // Track the last trade price
if enterLong and strategy.position_size == 0
    strategy.entry("Buy Call", strategy.long, qty = contractsQty)
    lastTradePrice := close
else if exitLong and strategy.position_size > 0
    strategy.close("Buy Call")
    lastTradePrice := na

// Calculate stop loss and take profit
stopLossPrice = lastTradePrice * (1 - stopLossPct / 100)
takeProfitPrice = lastTradePrice * (1 + takeProfitPct / 100)
strategy.exit("Sell Call", "Buy Call", stop = stopLossPrice, limit = takeProfitPrice)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-23. 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/Spaced-Out-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