Strategy vault / Trend following
Moving Average Crossover Strategy 446560
This strategy uses two moving averages with different periods (fast moving average and slow moving average) to identify trading signals. When the fast moving average crosses above the slow moving average, it…

Overview
This strategy uses two moving averages with different periods (fast moving average and slow moving average) to identify trading signals. When the fast moving average crosses above the slow moving average, it generates a long signal; when the fast moving average crosses below the slow moving average, it generates a short signal. The strategy also sets stop-loss and take-profit levels to control risk and lock in profits.
Strategy Principle
The core principle of this strategy is to use the crossover relationship between moving averages of different periods to determine changes in market trends. The fast moving average is more sensitive to price changes, while the slow moving average reflects longer-term trends. When the fast moving average crosses the slow moving average, it indicates that the market trend may have changed, thus generating trading signals.
Specifically, when the fast moving average crosses above the slow moving average, it indicates that the market may be entering an uptrend, and a long position is opened; conversely, when the fast moving average crosses below the slow moving average, it indicates that the market may be entering a downtrend, and a short position is opened. At the same time, the strategy sets stop-loss and take-profit levels to control risk and lock in profits.
Strategy Advantages
Simple and easy to understand: The strategy uses the simple principle of moving average crossover, which is easy to understand and implement.
Trend tracking: By using the crossover relationship between moving averages of different periods, the strategy can effectively capture changes in market trends, suitable for trend-following trading.
Risk control: The strategy has built-in stop-loss and take-profit mechanisms, which help to control risk and lock in profits.
Strategy Risks
Market volatility: In highly volatile markets, frequent moving average crossovers may generate many false signals, leading to frequent trades and losses.
Parameter selection: The performance of the strategy depends on the selection of moving average periods, and different parameter settings may lead to different results.
Trend lag: Moving averages are lagging indicators, and crossover signals may appear after the trend has already formed, missing early entry opportunities.
Strategy Optimization Directions
Parameter optimization: Find the optimal moving average period parameters by backtesting and optimizing different period combinations.
Combining with other indicators: Consider combining moving average crossover signals with other technical indicators such as RSI and MACD to improve signal reliability.
Dynamic stop-loss: Dynamically adjust stop-loss levels based on market volatility conditions, rather than using fixed percentages, to better control risk.
Summary
The moving average crossover strategy is a simple, easy-to-understand trading strategy suitable for trend tracking. By using the crossover relationship between moving averages of different periods, the strategy can capture changes in market trends while having built-in stop-loss and take-profit mechanisms to control risk. However, the strategy may generate many false signals in highly volatile markets, and crossover signals have a lagging nature. Therefore, improvements such as parameter optimization, combining with other technical indicators, and dynamically adjusting stop-loss levels can be considered. Overall, the moving average crossover strategy is a basic strategy worth trying.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 10 | Periodo de la media rápida |
| v_input_2 | 30 | Periodo de la media lenta |
Source Pine Script
//@version=4
strategy("barreto es marica", overlay=true)
// Parámetros de entrada
fastLength = input(10, title="Periodo de la media rápida")
slowLength = input(30, title="Periodo de la media lenta")
// Cálculo de las medias móviles
fastMA = sma(close, fastLength)
slowMA = sma(close, slowLength)
// Condiciones de entrada
enterLong = crossover(fastMA, slowMA)
enterShort = crossunder(fastMA, slowMA)
// Condiciones de salida
exitLong = crossunder(fastMA, slowMA)
exitShort = crossover(fastMA, slowMA)
// Gestión de posiciones
if (enterLong)
strategy.entry("Long", strategy.long)
if (enterShort)
strategy.entry("Short", strategy.short)
if (exitLong)
strategy.close("Long")
if (exitShort)
strategy.close("Short")
// Stop loss y toma de ganancias
stopLossLevel = strategy.position_avg_price * (1 - 0.01)
takeProfitLevel = strategy.position_avg_price * (1 + 0.03)
strategy.exit("Stop Loss/Take Profit", "Long", stop=stopLossLevel, limit=takeProfitLevel)
strategy.exit("Stop Loss/Take Profit", "Short", stop=stopLossLevel, limit=takeProfitLevel)
// Plotting
plot(fastMA, color=color.blue, title="Media rápida")
plot(slowMA, color=color.red, title="Media lenta")Published by ChaoZhang on the FMZ strategy square · last modified 2024-03-29. 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-Crossover-Strategy-446560.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