0bps

Strategy vault / Trend following

Moving Average Entry Optimization Strategy

This strategy optimizes the entry points after signals from a basic moving average system. The main logic is: 1. Calculate moving average over a period (e.g. 20-day) 2. Crossovers generate long/short signals 3. After…

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

Strategy Logic

This strategy optimizes the entry points after signals from a basic moving average system.

The main logic is:

  1. Calculate moving average over a period (e.g. 20-day)

  2. Crossovers generate long/short signals

  3. After signals, don't enter immediately but wait for better levels

  4. If better levels occur within specified days (e.g. 3 days), enter trades

  5. If not, enter at closing price on 5th day to avoid missing out

This seeks to capitalize on resumption of trends after consolidations rather than entering signals immediately. Allows establishing positions at improved levels.

Advantages

  • Entry optimization for better entry levels

  • Maximum wait days avoids completely missing trades

  • Simple and clear rules easy to implement

Risks

  • Waiting time and thresholds require optimization

  • Could miss some short-term trend opportunities

  • Needs to monitor both time and price conditions

Summary

This strategy aims to get better entry levels through simple entry optimization while ensuring trends are not missed. But optimizing wait time and entry criteria is crucial.

Parameters

NameDefaultWhat it controls
v_input_120period
v_input_23maxwait
v_input_30.01threshold

Source Pine Script

/*backtest
start: 2023-08-14 00:00:00
end: 2023-09-13 00:00:00
period: 1h
basePeriod: 15m
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/
// © dongyun

//@version=4
strategy("Wait for a better entry opportunity", overlay=true)

period = input(20,'')
maxwait = input(3,'')
threshold = input(0.01,'')

signal = 0
trend = 0.0
newtrend = 0.0
wait = 0.0
initialentry = 0.0

trend := sma(close,period)
signal := nz(signal[1])
if trend > nz(trend[1])
	signal := 1
else
	if trend < nz(trend[1])
		signal := -1

wait := nz(wait[1])
initialentry := nz(initialentry[1])

if signal != signal[1]
	if strategy.position_size > 0
		strategy.close('long',comment='trend sell')
		signal := -1
	else
		if strategy.position_size < 0
    		strategy.close('short',comment='trend buy')
    		signal := 1
	wait := 0
	initialentry := close
else
	if signal != 0 and strategy.position_size == 0
		wait := wait + 1

// test for better entry
if strategy.position_size == 0
	if wait >= maxwait
		if signal > 0
			strategy.entry('long',strategy.long, comment='maxtime Long')
		else
			if signal < 0
				strategy.entry('short',strategy.short, comment='maxtime Short')
	else
		if signal > 0 and close < initialentry - threshold
			strategy.entry('long',strategy.long, comment='delayed Long')
		else
			if signal < 0 and close > initialentry + threshold
				strategy.entry('short',strategy.short, comment='delayed short')

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-14. 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/Moving-Average-Entry-Optimization-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