0bps

Strategy vault / Machine learning

Golden Ratio Moving Average Trading Strategy

The golden ratio moving average trading strategy is a quantitative trading strategy that attempts to use the golden cross of short-term and long-term moving averages as trading signals. The strategy also incorporates…

Author
ChaoZhang
Complexity
Starter · 52 lines
Parameters
6
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

The golden ratio moving average trading strategy is a quantitative trading strategy that attempts to use the golden cross of short-term and long-term moving averages as trading signals. The strategy also incorporates the RSI indicator to avoid opening positions at local highs in order to control risks.

Strategy Logic

The strategy is mainly based on two moving averages: the 200-day MA as the long-term MA and the 10-day MA as the short-term MA. A buy signal is generated when the short-term MA crosses over the long-term MA; A sell signal is generated when the short-term MA crosses below the long-term MA. This is the famous "golden cross". The strategy also incorporates the RSI indicator so that the strategy only opens long positions in the oversold area when RSI is less than 30.

Specifically, a long position will be opened if the following conditions are met:

  1. 10-day MA crosses above 200-day MA
  2. Currently no position
  3. RSI less than 30

The closing position conditions are as follows:

  1. Stop loss: stop loss when the price falls below a certain percentage (adjustable) of the opening price
  2. Take profit: take profit when the price exceeds a certain percentage (adjustable)

Advantage Analysis

The strategy has the following advantages:

  1. It utilizes the golden cross signal of moving averages, which is a classic and effective technical indicator trading signal
  2. Incorporating RSI avoids buying at the highs, which can control risks to some extent
  3. With stop loss and take profit settings, it can lock in profits and avoid risks

Risk Analysis

The strategy also has some risks:

  1. Moving average strategies are prone to generating wrong signals and whipsaws
  2. RSI can fail in some strong trending markets
  3. If the stop loss is set too small, it may lead to ultra short-term trading and frequent stop loss activation

To reduce these risks, the following optimization measures can be considered:

  1. Adjust the MA parameters, or add more MAs
  2. Incorporate other indicators to confirm RSI signals
  3. Adjust stop loss and take profit parameter settings

Optimization Directions

There is room for further optimization of the strategy:

  1. Increase more indicator filters to avoid wrong signals
  2. Optimize moving average parameters
  3. Incorporate volatility indicators to set dynamic stops
  4. Add machine learning models to judge market conditions
  5. Use algorithms to automatically optimize parameters

Conclusion

In summary, the golden ratio moving average trading strategy is a simple and effective trend following strategy. It generates trading opportunities using classic MA crossover signals and has stops to control risks. The strategy can be further improved through multi-indicator combinations, parameter optimization, machine learning, etc. to obtain better strategy performance.

Parameters

NameDefaultWhat it controls
v_input_int_1200(?パラメータ)Long-term moving averageBASE200
v_input_int_210Long-term moving averageBASE10
v_input_int_320Stop loss ratio%
v_input_int_45Take profit ratio%
v_input_1timestamp(01 Jan 2018 13:30 +0000)(?(Period) Start date for backtesting
v_input_2timestamp(1 Jan 2099 19:30 +0000)バックテスをFinal day

Source Pine Script

/*backtest
start: 2022-12-29 00:00:00
end: 2024-01-04 00:00:00
period: 1d
basePeriod: 1h
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/
// © tsujimoto0403

//@version=5
strategy("Holy Grail", overlay=true,default_qty_type=strategy.percent_of_equity,
     default_qty_value=100)

//ユーザーインプットのPrepare
malongperiod=input.int(200,"Long-term moving averageBASE200",group = "パラメータ")
mashortperiod=input.int(10,"Long-term moving averageBASE10",group = "パラメータ")
stop=input.int(20,title = "Stop loss ratio%",group = "パラメータ")
profit=input.int(5,title = "Take profit ratio%",group = "パラメータ")
startday=input(title="バックテストをThe day to open", defval=timestamp("01 Jan 2018 13:30 +0000"), group="period")
endday=input(title="バックテスをBad day", defval=timestamp("1 Jan 2099 19:30 +0000"), group="period")

//UseうVariable
var float stopprice=0
var float takeprofit=0

//とりあえずUseうインジケーターをプロット
malong=ta.sma(close,malongperiod)
mashort=ta.sma(close,mashortperiod)


plot(malong,color=color.aqua,linewidth = 2)
plot(mashort,color=color.yellow,linewidth = 2)
bgcolor(ta.rsi(close,3)<30?color.rgb(229, 86, 86, 48):na)

//Period conditions
datefilter = true

//エントリーcondition
if close>malong and close<mashort and strategy.position_size == 0 and datefilter and ta.rsi(close,3)<30
    strategy.entry(id="long", direction=strategy.long)

if strategy.position_size>0 
    strategy.exit(id="long",stop=(1-0.01*stop)*strategy.position_avg_price)

//Sellり
if  strategy.position_size > 0 and close>mashort and close<low[1] 
    strategy.close(id ="long")


Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-05. 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/Golden-Ratio-Moving-Average-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