適応型スマートグリッド取引戦略


作成日: 2024-01-16 14:51:48 最終変更日: 2024-01-16 14:51:48
コピー: 1 クリック数: 985
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)