0bps

Strategy vault / Trend following

Moving Average Trend Following Trading Strategy

This strategy calculates the moving average and price change rate to determine whether the current state is in an uptrend or a downtrend combined with K-lines over a certain period, and accordingly goes long or short.

Author
ChaoZhang
Complexity
Starter · 61 lines
Parameters
6
Backtest bar
1m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy calculates the moving average and price change rate to determine whether the current state is in an uptrend or a downtrend combined with K-lines over a certain period, and accordingly goes long or short.

Strategy Principle

This strategy first calculates the simple moving average a of length l and the price change rate r of length l. Then it calculates the difference k between the current K-line price and the moving average. Finally, it calculates the sum sum of k over the past s K-lines.

When sum>0, it indicates a current uptrend and the strategy will go long. When sum<0, it indicates a current downtrend and the strategy will go short.

After going long or short, the position will be held until the trend reverses (sum changes from positive to negative or vice versa), then the position will be closed.

Advantage Analysis

The biggest advantage of this strategy is that it can catch the trend and is suitable for trend trading. Specifically, it has the following advantages:

  1. Using the moving average to determine the overall trend direction can effectively filter market noise and lock in the major trend.

  2. Applying the price change rate indicator to measure momentum strength avoids missing strong momentum.

  3. Taking into account multiple K-lines over a period can more accurately determine the trend and avoid being misled by einzelne Ausreißer.

  4. As long as the trend remains unchanged, continue holding the position to maximize profits from the trending market.

Risk Analysis

The main risks of this strategy are:

  1. Unable to accurately determine the end time of the trend, may stop loss prematurely or miss some profits.

  2. Unable to effectively control the size of single loss, losses may be large in extreme market conditions.

  3. Improper strategy parameters may lead to over-frequent trading or missing some trading opportunities.

  4. Long-term holdings may face overnight interest and margin risks.

To control risks, we can set stop loss points, trade only highly liquid products, optimize parameters and reasonably use leverage.

Optimization Directions

The main aspects to optimize this strategy include:

  1. Test moving averages and price change rates of different lengths to find the best parameter combination.

  2. Try other indicators such as MACD to better determine the trend and further improve accuracy.

  3. Add position management mechanisms, such as taking profit after making some profits, to control single loss.

  4. Incorporate volatility indicators to set dynamic stops to reduce risks in extreme market conditions.

  5. Optimize entry and exit logic to filter false breakouts and improve trading efficiency.

Conclusion

The overall logic of this strategy is clear and easy to implement. By tracking trends for long-term holding trading, drawdown control is relatively reasonable. It is suitable for investors seeking stable returns. Further optimizing the stop loss and position management can expect good long-term steady returns.

Parameters

NameDefaultWhat it controls
v_input_1170Length for indicator
v_input_218Length of summation
v_input_3falseTake Profit
v_input_4falseStop Loss
v_input_5falseTrailing Stop Loss
v_input_6falseTrailing Stop Loss Offset

Source Pine Script

/*backtest
start: 2023-12-03 00:00:00
end: 2023-12-10 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
strategy("Indicator Integrator Strat",default_qty_type = strategy.percent_of_equity, default_qty_value = 100,currency="USD",initial_capital=662, overlay=false)

l = input(defval=170,title="Length for indicator")
s = input(title="Length of summation",defval=18)
a= sma(close,l)
r=roc(close,l)
k=close-a
sum = 0
for i = 0 to s
    sum := sum + k[i]
//plot(a,color=yellow,linewidth=2,transp=0)
//bc =  iff( sum > 0, white, teal)
//plot(sum,color=bc, transp=20, linewidth=3,style=columns)
//plot(sma(sum,3),color=white)
//hline(0)

inpTakeProfit = input(defval = 0, title = "Take Profit", minval = 0)
inpStopLoss = input(defval = 0, title = "Stop Loss", minval = 0)
inpTrailStop = input(defval = 0, title = "Trailing Stop Loss", minval = 0)
inpTrailOffset = input(defval = 0, title = "Trailing Stop Loss Offset", minval = 0)
useTakeProfit = inpTakeProfit >= 1 ? inpTakeProfit : na
useStopLoss = inpStopLoss >= 1 ? inpStopLoss : na
useTrailStop = inpTrailStop >= 1 ? inpTrailStop : na
useTrailOffset = inpTrailOffset >= 1 ? inpTrailOffset : na

////buyEntry = crossover(source, lower)
////sellEntry = crossunder(source, upper)
if sum>0
    strategy.entry("BBandLE", strategy.long, oca_name="BollingerBands",  comment="BBandLE")
else
    strategy.cancel(id="BBandLE")
if sum<0
    strategy.entry("BBandSE", strategy.short, oca_name="BollingerBands",  comment="BBandSE")
else
    strategy.cancel(id="BBandSE")

strategy.initial_capital = 50000
plot(strategy.equity-strategy.initial_capital-strategy.closedtrades*.25/2, title="equity", color=red, linewidth=2)
hline(0)
//longCondition = sum>0
//exitlong = sum<0

//shortCondition = sum<0
//exitshort = sum>0

//strategy.entry(id = "Long", long=true, when = longCondition)
//strategy.close(id = "Long", when = exitlong)
//strategy.exit("Exit Long", from_entry = "Long", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, when=exitlong)

//strategy.entry(id = "Short", long=false, when = shortCondition)
//strategy.close(id = "Short", when = exitshort)
//strategy.exit("Exit Short", from_entry = "Short", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, when=exitshort)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-11. 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-Trend-Following-Trading-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