Moving Average Breakout Strategy
The moving average breakout strategy is a short-term trading strategy that utilizes moving averages to determine entries and exits. It is characterized by its simplicity and ease of use.
Overview
The moving average breakout strategy is a short-term trading strategy that utilizes moving averages to determine entries and exits. It is characterized by its simplicity and ease of use.
Strategy Logic
The core logic relies on two moving averages, a fast line and a slow line, to gauge the trend of prices. The fast line has a shorter period and is more sensitive. The slow line has a longer period and is more stable.
The code allows users to set the fast line period shortPeriod and the slow line period longPeriod via input parameters. The values of the two moving averages are calculated as shortSMA and longSMA.
When the fast moving average crosses above the slow moving average, it signals an upside breakout and long entry. When the fast MA crosses below the slow MA, it signals a downside breakout and short entry.
Long entry condition:
Fast MA crosses above slow MA
Fast MA > Slow MA
Short entry condition:
Fast MA crosses below slow MA
Fast MA < Slow MA
The strategy also incorporates stop loss, take profit and position sizing settings to control risks.
Advantages
- Simple to use, easy for beginners to grasp
- Moving averages filter out some noise
- Flexibility in fine tuning MA periods for different timeframes
- Predefined stop loss and take profit
Risks
- Susceptible to false breakouts and whipsaws
- Not ideal for range-bound choppy markets
- Lagging indication, entries could be late
- Unable to filter trend reversals effectively
Risk Management:
- Add filters to avoid false signals
- Apply strategy when trend is obvious
- Optimize MA parameters for better entries
- Allow wider stops to avoid premature stops
Enhancement Opportunities
- Optimize MA parameters to find best combinations
- Add additional indicators like BOLL channels or KD
- Improve exit rules to maximize profits
- Test robustness across different instruments
- Incorporate machine learning using big data
Conclusion
The moving average breakout strategy is easy to understand, generating signals with fast and slow MAs. But it also has some flaws like false breaks and lagging issues. With parameter tuning, additional filters and other enhancements, the strategy can be improved. Overall it serves as a beginner-friendly first step into algorithmic trading, and paves the way for more advanced strategies after grasping the core concepts.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_int_1 | 3 | (?Candle)Validation Period |
| v_input_float_1 | true | (?Order)Qty |
| v_input_float_2 | true | Maximum Active Open Position |
| v_input_float_3 | true | (?Long)Take Profit (%) |
| v_input_float_4 | 25 | Stop Loss (%) |
| v_input_float_5 | 0.2 | Trailing Stop (%) |
Source Pine Script
/*backtest
start: 2023-08-26 00:00:00
end: 2023-09-25 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/
// © YohanNaftali
//@version=5
///////////////////////////////////////////////////////////////////////////////
// Heikin Ashi Candle Startegy
// ver 2021.12.29
// © YohanNaftali
// This script composed by Yohan Naftali for educational purpose only
// Reader who will use this signal must do own research
///////////////////////////////////////////////////////////////////////////////
strategy(
title = 'Heikin Ashi Candle Startegy Long',
shorttitle = 'HA Strategy Long',
format = format.price,
precision = 0,
overlay = true)
// Input
validationPeriod = input.int(
defval = 3,
title = 'Validation Period',
group = 'Candle')
qtyOrder = input.float(
defval = 1.0,
title = 'Qty',
group = 'Order')
maxActive = input.float(
defval = 1.0,
title = 'Maximum Active Open Position',
group = 'Order')
// Long Strategy
tpLong = input.float(
defval = 1,
title = "Take Profit (%)",
minval = 0.0,
step = 0.1,
group = "Long") * 0.01
slLong = input.float(
defval = 25,
title = "Stop Loss (%)",
minval=0.0,
step=0.1,
group="Long") * 0.01
trailingStopLong = input.float(
defval = 0.2,
title = "Trailing Stop (%)",
minval = 0.0,
step = 0.1,
group = 'Long') * 0.01
// Calculation
haTicker = ticker.heikinashi(syminfo.tickerid)
haClose = request.security(haTicker, timeframe.period, close)
haOpen = request.security(haTicker, timeframe.period, open)
// Long
limitLong = tpLong > 0.0 ? strategy.position_avg_price * (1 + tpLong) : na
stopLong = slLong > 0.0 ? strategy.position_avg_price * (1 - slLong) : na
float trailLong = 0.0
trailLong := if strategy.position_size > 0
trailClose = close * (1 - trailLong)
math.max(trailClose, trailLong[1])
else
0
isGreen = true
for i = 0 to validationPeriod-1
isGreen := isGreen and haClose[i] > haOpen[i]
isLong = isGreen and haClose[validationPeriod] < haOpen[validationPeriod]
plot(
limitLong,
title = 'Limit',
color = color.rgb(0, 0, 255, 0),
style = plot.style_stepline,
linewidth = 1)
plot(
trailLong,
title = 'Trailing',
color = color.rgb(255, 255, 0, 0),
style = plot.style_stepline,
linewidth = 1)
plot(
stopLong,
title = 'Stop',
style = plot.style_stepline,
color = color.rgb(255, 0, 0, 0),
linewidth = 1)
// plotshape(
// isLong,
// title = 'Entry',
// style = shape.arrowup,
// location = location.belowbar,
// offset = 1,
// color = color.new(color.green, 0),
// text = 'Long Entry',
// size = size.small)
// Strategy
strategy.risk.max_position_size(maxActive)
strategy.risk.allow_entry_in(strategy.direction.long)
strategy.entry(
id = "Long",
direction = strategy.long,
qty = qtyOrder,
when = isLong,
alert_message = "LN")
if (strategy.position_size > 0)
strategy.exit(
id = "Long Exit",
from_entry = "Long",
limit = limitLong,
stop = stopLong,
trail_price = trailLong,
alert_message = "LX") Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-26. 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-Breakout-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
- Dynamic ATR based Trailing Stop Trading StrategyPine Script · Breakout · 1d
- Kana Candle Breakout Strategy Based on Moving Average and Support ResistancePine Script · Breakout · 1h
- Momentum Breakout Trading Strategy 440964Pine Script · Breakout · 5m
- Moving Average Bollinger Bands Breakout StrategyPine Script · Breakout · 1h
- Multi Indicator Momentum Breakout with Smoothed Candlestick Trading StrategyPine Script · Breakout · 6h
- Quantitative Trading Strategy Based on Ichimoku with Multiple SignalsPine Script · Breakout · 1h