Strategy vault / Trend following
CCI and EMA Trend Following Trading Strategy
The core idea of this strategy is to use the CCI indicator to determine the market trend direction and use the EMA indicator to smooth the CCI to implement trend following trading. Go long when the CCI crosses above…

Overview
The core idea of this strategy is to use the CCI indicator to determine the market trend direction and use the EMA indicator to smooth the CCI to implement trend following trading. Go long when the CCI crosses above the buy point and go short when the CCI crosses below the sell point to follow the market trend.
Strategy Logic
Calculate the CCI indicator. The CCI indicator judges whether the current stock price is overbought or oversold based on the degree of deviation from the 20-day moving average. The formula is: (typical price - 20D SMA) / (0.015 * 20D TP standard deviation).
Smooth the CCI indicator with an EMA to get a CCI-EMA curve, which reduces the fluctuation of the CCI and makes the signal clearer.
Set the buy and sell points for CCI. Go long when CCI-EMA crosses above the buy point, and go short when CCI-EMA crosses below the sell point.
Hold the position until CCI-EMA touches the buy or sell point again to close the position.
Advantage Analysis
Using CCI to determine market trend direction combined with EMA to filter false signals can effectively follow market trends.
CCI is sensitive to price anomalies and can quickly capture trend reversals. EMA reduces false signals. Used together, they can seize opportunities at the beginning of trends.
Trend following strategies minimize transactions, reduce trading costs and slippage.
The backtest results are decent, giving the strategy some practical viability.
Risk Analysis
CCI can be overly sensitive to curves and EMA cannot completely filter all false signals, some false signals remain.
Pure trend following strategies are prone to losses when trends consolidate or reverse. Trend judgment indicators should be used.
Mechanical trading systems cannot flexibly adjust parameters based on markets. Over optimization is a risk.
Limited backtest data cannot fully reflect live performance. Parameters should be adjusted carefully and stops managed strictly.
Optimization Directions
Optimize CCI parameters by testing different length periods.
Optimize EMA parameters to find the optimal EMA period.
Test different buy/sell point combinations to find the optimal parameters.
Incorporate other indicators to determine trend reversal and set stop losses.
Add auto parameter optimization to find the optimal parameters for different products.
Summary
Overall this is a relatively simple trend following trading strategy. It uses CCI to determine trend direction and is sensitive to price changes, combined with EMA filtering to generate signals. The strategy has some advantages but also risks to note. Through parameter optimization and using other indicators, the stability and live performance can be further improved. Overall it provides a simple and reliable trend following template for quant trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 20 | length |
| v_input_2_close | 0 | Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4 |
| v_input_3 | false | CCI Sell Point |
| v_input_4 | false | CCI Buy Buy Point |
| v_input_5 | 12 | length cci ema |
Source Pine Script
/*backtest
start: 2023-10-02 00:00:00
end: 2023-11-01 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("CCI with EMA Strategy", overlay=false, pyramiding=1, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.07)
length = input(20, minval=1)
src = input(close, title="Source")
cciSellPoint = input(0, title = 'CCI Sell Point', type = input.integer)
cciBuyPoint = input(0, title = 'CCI Buy Buy Point', type = input.integer)
lengthcci = input(12,"length cci ema", minval=1)
ma = sma(src, length)
cci = (src - ma) / (0.015 * dev(src, length))
cciema=ema(cci,lengthcci)
plot(cci, "CCI", color=#996A15)
plot(cciSellPoint, title = 'CCI Sell Point', color = color.red, linewidth = 1, style = plot.style_cross, transp = 35)
plot(cciBuyPoint, title = 'CCI Buy Point', color = color.green, linewidth = 1, style = plot.style_cross, transp = 35)
plot(cciema, title = 'CCI EMA', color = color.green, linewidth = 1, transp = 35)
band1 = hline(100, "Upper Band", color=#C0C0C0, linestyle=hline.style_dashed)
band0 = hline(-100, "Lower Band", color=#C0C0C0, linestyle=hline.style_dashed)
fill(band1, band0, color=#9C6E1B, title="Background")
startLongTrade= cciema >cciBuyPoint
startShortTrade= cciema <cciSellPoint
//exitLong = cciema <cciSellPoint
//exitShort = cciema >cciBuyPoint
strategy.entry("long",strategy.long, when = startLongTrade )
//strategy.close( "long", when=exitLong)
strategy.entry("short",strategy.short,when=startShortTrade )
//strategy.close("short", when=exitShort)Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-02. 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/CCI-and-EMA-Trend-Following-Trading-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
- EMA SMA Multi Indicator Comprehensive Trend Following StrategyPine Script · Trend following · 1h
- Supertrend Advance StrategyPine Script · Trend following · 5m
- SuperTrend Trailing Stop Strategy Based on Heikin AshiPine Script · Trend following · 4h
- CCIRSIKC Trend Filter Bi Directional Trading StrategyPine Script · Trend following · 2h
- MOST Indicator Dual Position Adaptive StrategyPine Script · Trend following · 4h
- Patient Trend Following StrategyPine Script · Trend following · 1m