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…
Strategy Logic
This strategy optimizes the entry points after signals from a basic moving average system.
The main logic is:
Calculate moving average over a period (e.g. 20-day)
Crossovers generate long/short signals
After signals, don't enter immediately but wait for better levels
If better levels occur within specified days (e.g. 3 days), enter trades
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
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 20 | period |
| v_input_2 | 3 | maxwait |
| v_input_3 | 0.01 | threshold |
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.
/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- 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
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Adaptive FVG Detection and MA Trend Trading Strategy with Dynamic ResistancePine Script · Trend following · 1d
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m