
The Adaptive Moving Average Grid Quantitative Trading Strategy is a quantitative approach that combines moving average and grid trading concepts. This strategy calculates a Simple Moving Average (SMA) as the central line representing market trend, then establishes grid lines at specific percentage intervals above and below this center line. When price fluctuates between these grid lines, the strategy buys when price touches lower grid lines and sells when price touches higher grid lines. This trading method is particularly suitable for market environments where prices fluctuate within a range while generally oscillating around the moving average.
The core principle of the Adaptive Moving Average Grid Quantitative Trading Strategy is based on the mean reversion property of market prices. The strategy is implemented through the following steps:
The essence of this strategy is to capture high-frequency fluctuations within a certain price range, achieving “buy low, sell high.” The strategy allows holding multiple positions simultaneously (up to 15), with each position corresponding to different grid lines. This design enables the strategy to more fully utilize price fluctuations.
The Adaptive Moving Average Grid Quantitative Trading Strategy offers the following significant advantages:
Despite its reasonable design, the strategy still has the following potential risks:
Based on the analysis of the code, the strategy can be optimized in the following directions:
The Adaptive Moving Average Grid Quantitative Trading Strategy is a grid trading system based on the principle of mean reversion, capturing trading opportunities from price fluctuations by setting grids around moving averages. The strategy design is concise and clear, with few parameters that are easy to adjust, and is particularly suitable for application in oscillating markets. The main advantages of the strategy lie in its adaptability and risk diversification characteristics, automatically adapting to different price levels and spreading risk through multiple grid positions.
However, the strategy may face risks in strong trending markets and needs to add trend filtering and stop-loss mechanisms for optimization. In addition, dynamically adjusting grid width, improving capital management, and adding multi-timeframe confirmation are also worth exploring as optimization directions. Through these optimizations, the strategy is expected to achieve more stable and excellent performance in different market environments.
For experienced quantitative traders, this strategy provides a good foundation framework that can be further customized and optimized according to individual trading styles and risk preferences, leveraging the advantages of grid trading in capturing market volatility.
/*backtest
start: 2025-04-01 00:00:00
end: 2025-06-22 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
//@version=5
strategy('Grid Trading Strategy', overlay=true, pyramiding=15)
// 输入参数设置
ma_length = input.int(300, '移动平均线长度', group='移动平均线条件', step=10)
std = input.float(0.03, title='网格上下偏差率', group='网格条件', step=0.01)
grid = input.int(15, maxval=15, title='网格线数量', group='网格条件')
// 计算移动平均线及网格边界
ma = ta.sma(close, ma_length)
upper_bound = ma * (1 + std)
lower_bound = ma * (1 - std)
grid_width = (upper_bound - lower_bound) / (grid - 1)
// 创建网格价格数组
grid_array = array.new_float(0)
for i = 0 to grid - 1 by 1
array.push(grid_array, lower_bound + grid_width * i)
// 创建订单状态布尔数组(只初始化一次)
var order_array = array.new_bool(grid, false)
// 执行交易逻辑
for i = 0 to grid - 1 by 1
// 买入逻辑:价格低于网格线且该位置未持仓
if close < array.get(grid_array, i) and not array.get(order_array, i)
buy_id = i
array.set(order_array, buy_id, true)
strategy.entry(id=str.tostring(buy_id), direction=strategy.long, comment='#Long ' + str.tostring(buy_id))
// 卖出逻辑:价格高于网格线且下一个网格位置持仓
if close > array.get(grid_array, i) and i != 0
if array.get(order_array, i - 1)
sell_id = i - 1
array.set(order_array, sell_id, false)
strategy.close(id=str.tostring(sell_id), comment='#Close ' + str.tostring(sell_id))
// 可视化网格线
plot(grid > 0 ? array.get(grid_array, 0) : na, color=color.yellow, transp=10)
plot(grid > 1 ? array.get(grid_array, 1) : na, color=color.yellow, transp=10)
plot(grid > 2 ? array.get(grid_array, 2) : na, color=color.yellow, transp=10)
plot(grid > 3 ? array.get(grid_array, 3) : na, color=color.yellow, transp=10)
plot(grid > 4 ? array.get(grid_array, 4) : na, color=color.yellow, transp=10)
plot(grid > 5 ? array.get(grid_array, 5) : na, color=color.yellow, transp=10)
plot(grid > 6 ? array.get(grid_array, 6) : na, color=color.yellow, transp=10)
plot(grid > 7 ? array.get(grid_array, 7) : na, color=color.yellow, transp=10)
plot(grid > 8 ? array.get(grid_array, 8) : na, color=color.yellow, transp=10)
plot(grid > 9 ? array.get(grid_array, 9) : na, color=color.yellow, transp=10)
plot(grid > 10 ? array.get(grid_array, 10) : na, color=color.yellow, transp=10)
plot(grid > 11 ? array.get(grid_array, 11) : na, color=color.yellow, transp=10)
plot(grid > 12 ? array.get(grid_array, 12) : na, color=color.yellow, transp=10)
plot(grid > 13 ? array.get(grid_array, 13) : na, color=color.yellow, transp=10)
plot(grid > 14 ? array.get(grid_array, 14) : na, color=color.yellow, transp=10)