
이 전략은 TTM 지표에 기반한 동적 격자 거래 시스템으로, 높은 낮은 지수 이동 평균 (EMA) 의 계산을 통해 시장의 경향 방향을 판단하고, 동적으로 업데이트 된 기준 가격을 중심으로 격자 거래 시스템을 배포한다. 격자의 방향과 가격 수준은 동적으로 트렌드에 따라 조정되며, 가격이 미리 정의된 격자 수준을 넘어서면 거래가 수행되며, 각 거래의 리스크 은 계좌 권익에 대한 고정된 비율이다.
이 전략의 핵심 논리는 TTM 상태 계산에 있다.
그리드 거래 시스템은 TTM 상태에 따라 동적으로 조정됩니다:
이 전략은 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)