0bps

Strategy vault / Trend following

Quantitative Golden Cross Strategy

This strategy calculates a custom net volume indicator to implement a trading strategy of buying on golden crossover and selling on death crossover. This strategy belongs to the trend following strategy.

Author
ChaoZhang
Complexity
Intermediate · 54 lines
Parameters
9
Backtest bar
1m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy calculates a custom net volume indicator to implement a trading strategy of buying on golden crossover and selling on death crossover. This strategy belongs to the trend following strategy.

Strategy Principle

The core logic of the strategy is to calculate a custom net volume (NV) indicator. The NV indicator judges the direction of price changes. If positive, it takes the daily volume. If negative, it takes the negative value of the daily volume. If unchanged, it takes 0. This can more clearly reflect the relationship between price changes and volume.

The strategy then calculates the 3-day simple moving average line of the NV indicator, respectively, as the golden cross line and death cross line. When the NV indicator breaks through the golden cross line from bottom to top, go long. When NV breaks through the death cross line from top to bottom, go short.

In addition, the strategy also sets parameterized start and end times to control trading hours.

Advantages of Strategy

The biggest advantage of this strategy is that the strategy is simple and clear, easy to understand, flexible parameter settings, customizable trading varieties, trading hours, etc. In addition, this strategy belongs to a trend-following strategy that can effectively capture price trends, reduce trading frequency, and achieve higher returns.

Risks of Strategy

The main risks of this strategy are:

  1. The daily following strategy cannot respond promptly to changes in price trends. It may miss some trading opportunities or fail to stop losses in time.

  2. The quantitative golden cross itself has certain hysteresis, which may lead to late entry and amplified losses.

  3. Unable to effectively filter out market noise and susceptible to traps.

Moving averages can be used dynamically, combined with other indicators to reduce risks.

Optimization Directions

The strategy can be optimized in the following aspects:

  1. Increase stop loss strategies to control single loss with moving stop loss, overnight stop loss methods.

  2. Increase filtering indicators and use MACD, KDJ and other indicators to filter false signals and improve strategy stability.

  3. Parameter optimization, iterative search for the optimal parameter combination through genetic algorithms, Markov chains and other methods.

  4. Strategy portfolio can be combined with other unrelated strategies to further diversify risks and increase overall returns.

Conclusion

This strategy implements simple and effective trend following through quantitative golden crosses. Although there is a certain degree of hysteresis, the parameter settings are flexible and easy to understand. It is a strategy suitable for beginners to practice. Through continuous optimization, the strategy effect can be gradually improved and risks reduced.

Parameters

NameDefaultWhat it controls
v_input_1_close0VA Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_250VHigh Amount
v_input_3-50VLow Amount
v_input_4trueFrom Month
v_input_5trueFrom Day
v_input_62018From Year
v_input_7trueTo Month
v_input_8trueTo Day
v_input_99999To Year

Source Pine Script

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

//@version=3
strategy(title="@DankCoins - Customized Net Volume")
src = input(defval = close, title = "VA Source")
nv = change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume



// Inputs //
VHigh = input(defval = 50, title = "VHigh Amount")
VLow = input(defval = -50, title = "VLow Amount")


// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 2012)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 2012)

MAV1 = sma(volume, 3)
MAV2 = -sma(volume, 3)

enterShort = crossunder(nv, MAV1)
exitShort = crossunder(nv, MAV2)
enterLong = crossover(nv, MAV2)
exitLong = crossover(nv, MAV1)

// Time Function 
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false // create function "within window of time"


strategy.entry(id="Long Entry", long=true, when=enterLong and window())
strategy.entry(id="Short Entry", long=false, when=enterShort and window())
strategy.exit("Exit Long", from_entry = "Long Entry",  when=exitLong and window())
strategy.exit("Exit Short", from_entry = "Short Entry",  when=exitShort and window())


// Plot
plot(nv, color=blue, title="NV")
plot(VHigh, color=red)
plot(VLow, color=red)
plot(MAV1, color=green)
plot(MAV2, color=green)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-22. 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/Quantitative-Golden-Cross-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