
この戦略は,動的に調整された取引網を確立することによって,変動状況で安定した利益を達成します. この戦略は,設定された網の数に応じて,格子間隔と上下限価格を自動的に計算します. 価格が各格子線を突破すると,順次多額のポジションを建設したり空白したりします. 価格が元の格子線に再び触れたときに,徐々にポジションを平準化します. この戦略は,手動と自動の格子パラメータの調整を同時にサポートし,市場環境の変化に適応できます.
格子界と格子線価格の配列を入力パラメータに基づいて計算する.
価格が特定の格子線より低く,その格子線に相応の掛札がないとき,その格子線価格点で多項を確立する.価格が上記の格子線 ((最初のものを除く) よりも高く,上記の格子線に相応の保有項があるとき,上記の格子線に相応の多項を平らにする.
格子パラメータの自動調整を有効にすると,最近ある数のK線データに基づいて,格子上の上下限価格,格子間隔,および格子配列を定期的に再計算する.
波動的な状況で利益を得るという目標を達成した. 変動する状況で,異なる価格ポイントで持仓と止まり平準ポジションを批次に構築することができ,その結果,全体的に利益を達成した.
格子パラメータを手動または自動で調整する選択があります. 手動調整は人工介入が必要ですが,より制御可能です. 自動調整は操作の作業量を軽減し,戦略が市場環境の変化に適応できるようにします.
最大の格子数を制限することで,一方的リスクを制御できます. 価格がすべての格子線を突破した後に,この方向のリスクは制御されます.
格子間隔を調整することで,各単位の損益率を制御できます.格子間隔を小さくすることで単位の損益を減らすことができます.
大幅に波動的な状況下では,ブートレイトされるリスクがある.価格が複数の格子間で急速に波動しあう場合,ブートレイトされるリスクがある.
合理的な初期資金の設定が必要です.初期資金が不足すると,十分な数のグリットラインを支える事ができません.
格子数が大きすぎても小さすぎても,戦略的利益には不利である.格子数が少なくても,波動を十分に利用できない.格子数が多すぎても,単一の損失が小さすぎます.最適なパラメータを決定するためのテストが必要です.
格子パラメータの自動調整は,操作されるリスクがある. 格子パラメータの計算は,一定のK線数に依存し,短期的な操作の影響を受ける可能性がある.
ストップロジックを追加する.例えば,浮動ストップまたはストップを追跡する.一方的な損失リスクをさらに制御する.
アルゴリズムにグリッドパラメータを最適化する.異なる市場段階のパラメータ設定をテストし,機械学習の方法を使用してモデルを訓練し,パラメータの自動最適化を可能にします.
MACD,KDなどの判断は,現在上昇傾向にあるか下降傾向にあるかを判断し,格子数またはパラメータを調整する.
撤回制御を最適化する.例えば,最大撤回比率を設定し,値に達したときに戦略を終了させ,損失をさらに拡大しないようにする.
この戦略は波動的な状況の特徴を充分に活用し,ダイナミック・グリッド取引によって安定した収益を達成する目的である.この戦略は,パラメータの設定の柔軟性を考慮するとともに,操作の作業の強さを軽減している.波動的な状況では,この戦略は理想的な収益の選択肢であると言えます.将来,さらなる最適化により,戦略の適用シナリオをより広く,引き戻し制御をより優しくし,その結果,より持続的な安定した収益を生むことができる.
/*backtest
start: 2024-01-02 00:00:00
end: 2024-02-01 00:00:00
period: 1h
basePeriod: 15m
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)