적응형 스마트 그리드 거래 전략


생성 날짜: 2024-01-16 14:51:48 마지막으로 수정됨: 2024-01-16 14:51:48
복사: 1 클릭수: 985
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

적응형 스마트 그리드 거래 전략

개요

이 전략은 TradingView 플랫폼을 기반으로 하는 적응형 스마트 그리드 거래 전략으로, Pine Script v4를 사용하여 작성되었다. 그것은 가격표에 덮여 있으며, 지정된 범위에서 구매 및 판매 신호를 생성하기 위해 그리드를 만듭니다.

전략 원칙

핵심 기능

  1. 피라미드와 자금 관리:

    • 이 피라미드에는 최대 14개의 동향이 허용됩니다.
    • 현금 기반의 전략으로 포지션 크기를 관리하고,
    • 시범적인 목적으로, 초기 자본은 100달러로 설정되어 있습니다.
    • 매 거래마다 0.1%의 수수료가 부과됩니다.
  2. 그리드 범위:

    • 사용자가 자동으로 계산된 범위를 사용하거나 수동으로 설정된 격자의 상하계를 선택할 수 있습니다.
    • 자동 범위는 최근 가격 고점과 낮은 점 또는 간단한 이동 평균 (SMA) 에서 추출할 수 있습니다.
    • 사용자는 계산 범위의 회귀 주기를 정의할 수 있으며, 오차를 조정하여 범위를 확장하거나 축소할 수 있다.
  3. 그리드 라인:

    • 이 정책은 범위 내에서 사용자 정의 가능한 격자 라인을 허용하며, 3에서 15 사이의 범위가 권장됩니다.
    • 그리드 라인은 상단과 하단 사이에 균일하게 니다.

전략 논리

  • 지분을 보유하는 것은:

    • 이 스크립트는 가격이 그리드 라인을 넘어갔을 때, 그 그리드 라인에는 관련되지 않은 미완성 주문이 없을 때, 구매합니다.
    • 각 구매의 수는 초기 자본을 그리드 라인의 수로 나누고 현재 가격에 따라 조정한다.
  • 포지션 탈퇴:

    • 가격 상승이 더 높은 그리드 라인을 초과하고, 다음 더 낮은 그리드 라인과 관련된 매각되지 않은 포지션 주문이 존재할 때, 판매 신호가 발생한다.
  • 적응망:

    • 자동 범위를 사용하는 경우, 격자는 상하한도를 재 계산하고 그에 따라 조정하여 변화하는 시장 조건에 적응합니다.

우위 분석

이 전략은 격자 거래의 체계적이고 효율적인 실행의 장점을 통합한다. 추가 및 재원 관리를 사용하여 위험을 효과적으로 제어 할 수 있습니다. 격자는 시장에 자동으로 적응하여 다른 상황에 적용됩니다. 매개 변수는 다른 거래 스타일에 적응할 수 있습니다.

위험 분석

가격의 격자 상부 하위 경계를 뚫는 것은 큰 손실을 초래할 수 있다. 적절한 변수를 조정하거나, 스톱로스를 결합하여 위험을 제어한다. 또한, 너무 자주 거래하면 거래 수수료가 증가한다.

최적화 방향

트렌드 지표 필터링 신호 또는 그리드 파라미터를 최적화 하는 것을 고려할 수 있으며, 또한 손실을 막아 극단적인 상황의 위험을 예방할 수 있다.

요약하다

이 전략은 체계적으로 매매점을 생성하고 포지션을 관리하며, 매개 변수를 조정하여 다양한 선호도에 맞게 조정할 수 있다. 그것은 격자 거래의 규칙성과 트렌드 거래의 유연성을 유기적으로 결합하여, 운영의 난이도를 줄이고, 약간의 오류를 허용한다.

전략 소스 코드
/*backtest
start: 2024-01-08 00:00:00
end: 2024-01-15 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("(IK) Grid Script", overlay=true, pyramiding=14, close_entries_rule="ANY", default_qty_type=strategy.cash, initial_capital=100.0, currency="USD", commission_type=strategy.commission.percent, commission_value=0.1)
i_autoBounds    = input(group="Grid Bounds", title="Use Auto Bounds?", defval=true, type=input.bool)                             // calculate upper and lower bound of the grid automatically? This will theorhetically be less profitable, but will certainly require less attention
i_boundSrc      = input(group="Grid Bounds", title="(Auto) Bound Source", defval="Hi & Low", options=["Hi & Low", "Average"])     // should bounds of the auto grid be calculated from recent High & Low, or from a Simple Moving Average
i_boundLookback = input(group="Grid Bounds", title="(Auto) Bound Lookback", defval=250, type=input.integer, maxval=500, minval=0) // when calculating auto grid bounds, how far back should we look for a High & Low, or what should the length be of our sma
i_boundDev      = input(group="Grid Bounds", title="(Auto) Bound Deviation", defval=0.10, type=input.float, maxval=1, minval=-1)  // if sourcing auto bounds from High & Low, this percentage will (positive) widen or (negative) narrow the bound limits. If sourcing from Average, this is the deviation (up and down) from the sma, and CANNOT be negative.
i_upperBound    = input(group="Grid Bounds", title="(Manual) Upper Boundry", defval=0.285, type=input.float)                      // for manual grid bounds only. The upperbound price of your grid
i_lowerBound    = input(group="Grid Bounds", title="(Manual) Lower Boundry", defval=0.225, type=input.float)                      // for manual grid bounds only. The lowerbound price of your grid.
i_gridQty       = input(group="Grid Lines",  title="Grid Line Quantity", defval=8, maxval=15, minval=3, type=input.integer)       // how many grid lines are in your grid

f_getGridBounds(_bs, _bl, _bd, _up) =>
    if _bs == "Hi & Low"
        _up ? highest(close, _bl) * (1 + _bd) : lowest(close, _bl)  * (1 - _bd)
    else
        avg = sma(close, _bl)
        _up ? avg * (1 + _bd) : avg * (1 - _bd)

f_buildGrid(_lb, _gw, _gq) =>
    gridArr = array.new_float(0)
    for i=0 to _gq-1
        array.push(gridArr, _lb+(_gw*i))
    gridArr

f_getNearGridLines(_gridArr, _price) =>
    arr = array.new_int(3)
    for i = 0 to array.size(_gridArr)-1
        if array.get(_gridArr, i) > _price
            array.set(arr, 0, i == array.size(_gridArr)-1 ? i : i+1)
            array.set(arr, 1, i == 0 ? i : i-1)
            break
    arr

var upperBound      = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true) : i_upperBound  // upperbound of our grid
var lowerBound      = i_autoBounds ? f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false) : i_lowerBound // lowerbound of our grid
var gridWidth       = (upperBound - lowerBound)/(i_gridQty-1)                                                       // space between lines in our grid
var gridLineArr     = f_buildGrid(lowerBound, gridWidth, i_gridQty)                                                 // an array of prices that correspond to our grid lines
var orderArr        = array.new_bool(i_gridQty, false)                                                              // a boolean array that indicates if there is an open order corresponding to each grid line

var closeLineArr    = f_getNearGridLines(gridLineArr, close)                                                        // for plotting purposes - an array of 2 indices that correspond to grid lines near price
var nearTopGridLine = array.get(closeLineArr, 0)                                                                    // for plotting purposes - the index (in our grid line array) of the closest grid line above current price
var nearBotGridLine = array.get(closeLineArr, 1)                                                                    // for plotting purposes - the index (in our grid line array) of the closest grid line below current price
strategy.initial_capital = 50000
for i = 0 to (array.size(gridLineArr) - 1)
    if close < array.get(gridLineArr, i) and not array.get(orderArr, i) and i < (array.size(gridLineArr) - 1)
        buyId = i
        array.set(orderArr, buyId, true)
        strategy.entry(id=tostring(buyId), long=true, qty=(strategy.initial_capital/(i_gridQty-1))/close, comment="#"+tostring(buyId))
    if close > array.get(gridLineArr, i) and i != 0
        if array.get(orderArr, i-1)
            sellId = i-1
            array.set(orderArr, sellId, false)
            strategy.close(id=tostring(sellId), comment="#"+tostring(sellId))

if i_autoBounds
    upperBound  := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, true)
    lowerBound  := f_getGridBounds(i_boundSrc, i_boundLookback, i_boundDev, false)
    gridWidth   := (upperBound - lowerBound)/(i_gridQty-1)
    gridLineArr := f_buildGrid(lowerBound, gridWidth, i_gridQty)

closeLineArr    := f_getNearGridLines(gridLineArr, close)
nearTopGridLine := array.get(closeLineArr, 0)
nearBotGridLine := array.get(closeLineArr, 1)