该策略是一个基于TTM指标的动态网格交易系统,通过对高低点的指数移动平均线(EMA)进行计算来判断市场趋势方向,并在动态更新的基准价格周围部署网格交易系统。网格的方向和价格水平会根据趋势动态调整,当价格穿越预定义的网格水平时执行交易,每笔交易风险敞口为账户权益的固定百分比。
策略的核心逻辑在于TTM状态计算,它通过以下步骤实现: 1. 基于ttmPeriod参数计算两个EMA:低点EMA(lowMA)和高点EMA(highMA) 2. 在highMA和lowMA之间定义两个阈值水平: - lowThird:底部1/3位置 - highThird:底部2/3位置 3. 根据收盘价相对于这些阈值的位置确定TTM状态: - 当收盘价高于highThird时,返回1(上升趋势) - 当收盘价低于lowThird时,返回0(下降趋势) - 当收盘价在lowThird和highThird之间时,返回-1(中性状态)
网格交易系统会根据TTM状态动态调整: 1. 当TTM状态发生变化时,更新网格基准价格和方向 2. 根据网格方向和间距计算买卖价格水平 3. 在价格突破网格水平时执行相应的买入或卖出操作
该策略通过将TTM趋势判断与动态网格交易相结合,实现了一个自适应性强、风险可控的交易系统。通过动态调整网格方向和价格水平,策略能够较好地适应不同市场环境。虽然存在一些固有风险,但通过合理的参数设置和优化措施,策略具有良好的实用价值和发展潜力。
/*backtest
start: 2024-12-04 00:00:00
end: 2024-12-11 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("TTM Grid Strategy", overlay=true)
// Input parameters
int ttmPeriod = input.int(6, minval=1, title="TTM Period")
int gridLevels = input.int(5, minval=2, title="Grid Levels")
float gridSpacing = input.float(0.01, minval=0.0001, title="Grid Spacing (%)")
// Calculate TTM State
ttmState() =>
lowMA = ta.ema(low, ttmPeriod)
highMA = ta.ema(high, ttmPeriod)
lowThird = (highMA - lowMA) / 3 + lowMA
highThird = 2 * (highMA - lowMA) / 3 + lowMA
if close > highThird
1
else if close < lowThird
0
else
-1
// State tracking variables
var float gridBasePrice = 0.0
var int gridDirection = -1
// Determine grid state
updateGridState(float currentClose, int currentState) =>
float newBasePrice = gridBasePrice
int newDirection = gridDirection
if currentState != -1 and currentState != gridDirection
newBasePrice := currentClose
newDirection := currentState
[newBasePrice, newDirection]
// Calculate grid levels
calcGridLevels(float basePrice, int direction, int levels) =>
float[] buyLevels = array.new_float(levels)
float[] sellLevels = array.new_float(levels)
for i = 1 to levels
multiplier = i * gridSpacing
if direction == 1 // Buy grid
array.set(buyLevels, i-1, basePrice * (1 - multiplier))
array.set(sellLevels, i-1, basePrice * (1 + multiplier))
else // Sell grid
array.set(buyLevels, i-1, basePrice * (1 + multiplier))
array.set(sellLevels, i-1, basePrice * (1 - multiplier))
[buyLevels, sellLevels]
// Execute grid trades
executeGridTrades(float basePrice, int direction, int levels) =>
[buyLevels, sellLevels] = calcGridLevels(basePrice, direction, levels)
for i = 0 to levels - 1
float buyLevel = array.get(buyLevels, i)
float sellLevel = array.get(sellLevels, i)
if direction == 1 // Buy grid
if low <= buyLevel
strategy.entry("GridBuy" + str.tostring(i), strategy.long, comment="Buy Level " + str.tostring(i))
if high >= sellLevel
strategy.entry("GridSell" + str.tostring(i), strategy.short, comment="Sell Level " + str.tostring(i))
else // Sell grid
if high >= buyLevel
strategy.entry("GridBuy" + str.tostring(i), strategy.long, comment="Buy Level " + str.tostring(i))
if low <= sellLevel
strategy.entry("GridSell" + str.tostring(i), strategy.short, comment="Sell Level " + str.tostring(i))
// Main strategy logic
currentState = ttmState()
[newGridBasePrice, newGridDirection] = updateGridState(close, currentState)
// Update global variables
if newGridBasePrice != gridBasePrice
gridBasePrice := newGridBasePrice
if newGridDirection != gridDirection
gridDirection := newGridDirection
// Execute grid trades
executeGridTrades(newGridBasePrice, newGridDirection, gridLevels)
// Visualization
plotColor = newGridDirection == 1 ? color.green : color.red
plot(newGridBasePrice, color=plotColor, style=plot.style_cross)