
This strategy is a trend-following trading system based on dual moving average crossovers. By comparing the relative positions of short-term and long-term moving averages (9-day and 21-day respectively), it captures market trend reversal opportunities. The strategy combines classical technical analysis theory with modern quantitative trading methods to achieve fully automated trading decisions.
The core logic relies on crossover signals between two moving averages of different periods. When the short-term MA (9-day) crosses above the long-term MA (21-day), the system identifies upward momentum and triggers a long position; when the short-term MA crosses below the long-term MA, the system recognizes downward momentum and closes the position. Additionally, the strategy includes trade statistics functionality that tracks total trades, winning trades, and losing trades in real-time to help traders evaluate strategy performance.
This is a classic and practical trend-following strategy that captures market momentum changes through dual moving average crossovers. While it has certain limitations in terms of lag and false signals, its simplicity and robustness make it an important tool in quantitative trading. Through the proposed optimization directions, the strategy’s stability and profitability can be further enhanced.
/*backtest
start: 2024-05-20 00:00:00
end: 2024-12-13 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=5
strategy("Simple MA Crossover Strategy", overlay=true)
// Input parameters
shortMA = ta.sma(close, 9)
longMA = ta.sma(close, 21)
// Buy/Sell conditions
buyCondition = ta.crossover(shortMA, longMA)
sellCondition = ta.crossunder(shortMA, longMA)
// Plot moving averages
plot(shortMA, color=color.blue, title="Short MA")
plot(longMA, color=color.red, title="Long MA")
// Execute trades
if (buyCondition)
strategy.entry("Buy", strategy.long)
if (sellCondition)
strategy.close("Buy")
// Track trades, wins, and losses
var int totalTrades = 0
var int totalWins = 0
var int totalLosses = 0
if (strategy.opentrades > 0)
totalTrades := totalTrades + 1
if (strategy.opentrades == 0 and strategy.opentrades[1] > 0)
if (strategy.netprofit > 0)
totalWins := totalWins + 1
else
totalLosses := totalLosses + 1
// Plot trade statistics
var label tradeStats = na
if (not na(tradeStats))
label.delete(tradeStats)
tradeStats := label.new(bar_index, high, text="Trades: " + str.tostring(totalTrades) + "\nWins: " + str.tostring(totalWins) + "\nLosses: " + str.tostring(totalLosses), style=label.style_label_down, color=color.white, textcolor=color.black)