標準偏差を考慮した取引戦略

作者: リン・ハーンチャオチャン, 日付: 2023-11-24 13:54:58
タグ:

img

概要

この戦略は,加密通貨のトレンド取引を実施するために,平衡標準偏差指標と移動平均を組み合わせて使用する.特定の期間の閉じる価格とボリュームに基づいて,平衡標準偏差の価格チャネルを計算する.価格が上または下チャネルを突破すると,ロングまたはショートポジションが取られる.ストップ損失と利益の条件も,取引ごとに損失を制限するために設定されている.

戦略の論理

このコードは,時間列と配列からの重度の標準偏差を計算するための2つのカスタム関数を定義している.主なステップは:

  1. 閉店価格と総額に基づいて,重度の平均価格を計算する
  2. 各キャンドルの正方形誤差と平均価格を計算します.
  3. サンプルサイズ,重量,調整された平均値に基づいて分散を計算する
  4. 標準偏差を導き出すために平方根を取ります.

これは,重度の平均価格を中心としたチャネルを与えます. 上限と下限は標準偏差の1つ離れた位置にあります. 価格がチャネルの下から突破すると,ロングに移動します. 上から上から突破すると,ショートに移動します.

利点分析

この戦略の最大の利点は,移動平均値と波動性分析の組み合わせである.MAは市場のトレンド方向を判断し,SD範囲は合理的な帯を定義する.両者はより高い信頼性のために互いに検証する.また,ボリューム重量は実際のブレイクでのより高い成功確率のために偽ブレイクをフィルタリングするのに役立ちます.

ストップ・ロストとテイク・プロフィートポイントは,トレンドに沿って取引し,逆転で過度の損失を避けるのにさらに役立ちます.これは多くの初心者トレーダーが実施できない微妙な方法です.

リスク分析

主なリスクは,激しい市場変動によるものです.これはSDチャネルも狂った振動を引き起こし,判断を困難にします.また,短い期間を選択すると,騒音やエラーによって誤導されるリスクがあります.

治療法は,パラメータと期間設定を適切に滑らかにすることです. ブレイクアウトの確認を改善するために,RSIのような他の指標を組み合わせることを検討してください.

オプティマイゼーションの方向性

  1. 最適なコンボのために5分,15分,30分などテスト
  2. ストップ・ロスト/テイク・プロフィートの比率を最適化して最大利益を得る
  3. フィルタを追加します.例えば,ボリュームは,誤ったブレイクを避けるために
  4. 精度を改善するために,閉じる価格,ウィックなどにキャンドルスタイクフィルターを追加

結論

この戦略は,暗号通貨のトレンドを追跡するためにMAと共に重度の標準偏差指標を成功裏に採用している.合理的なストップ・ロスト/テイク・プロフィートセットアップは,取引市場のリズムを助け,過度の逆転損失を回避する.パラメータチューニングとマルチインジケーター確認によるさらなる最適化は,堅牢なアルゴ取引戦略の信頼性を向上させる.


/*backtest
start: 2023-11-16 00:00:00
end: 2023-11-23 00:00:00
period: 45m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rumpypumpydumpy   © cache_that_pass

//@version=4
strategy("[cache_that_pass] 1m 15m Function - Weighted Standard Deviation", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.075)

f_weighted_sd_from_series(_src, _weight, _n) => //{
//  @function: Calculates weighted mean, variance, standard deviation, MSE and RMSE from time series variables
//  @parameters:
//      _src: time series variable of sample values
//      _weight: time series of corresponding weight values.
//      _n : number of samples
    _xw = _src * _weight
    _sum_weight = sum(_weight, _n)
    _mean = sum(_xw, _n) / _sum_weight
    float _sqerror_sum = 0
    int _nonzero_n = 0
    for _i = 0 to _n - 1
        _sqerror_sum := _sqerror_sum + pow(_mean - _src[_i], 2) * _weight[_i]
        _nonzero_n := _weight[_i] != 0 ? _nonzero_n + 1 : _nonzero_n
    _variance = _sqerror_sum / ((_nonzero_n - 1) * _sum_weight / _nonzero_n)
    _dev = sqrt(_variance)
    _mse = _sqerror_sum / _sum_weight
    _rmse = sqrt(_mse)
    [_mean, _variance, _dev, _mse, _rmse]
//}
// -----------------------------------------------------------------------------

f_weighted_sd_from_arrays(_a_src, _a_weight, _n) => //{
//  @function: Calculates weighted mean, variance, standard deviation, MSE and RMSE from arrays
//  Expects index 0 of the arrays to be the most recent sample and weight values!
//  @parameters:
//      _a_src: array of sample values
//      _a_weight: array of corresponding weight values.
//      _n : number of samples
    float _mean = na, float _variance = na, float _dev = na, float _mse = na
    float _rmse = na, float _sqerror_sum = na, float _sum_weight = na
    float[] _a_xw = array.new_float(_n)
    int _nonzero_n = 0
    if array.size(_a_src) >= _n
        _sum_weight := 0
        _sqerror_sum := 0
        for _i = 0 to _n - 1
            array.set(_a_xw, _i, array.get(_a_src, _i) * array.get(_a_weight, _i))
            _sum_weight := _sum_weight + array.get(_a_weight, _i)
            _nonzero_n := array.get(_a_weight, _i) != 0 ? _nonzero_n + 1 : _nonzero_n
        _mean := array.sum(_a_xw) / _sum_weight
        for _j = 0 to _n - 1
            _sqerror_sum := _sqerror_sum + pow(_mean - array.get(_a_src, _j), 2) * array.get(_a_weight, _j)
        _variance := _sqerror_sum / ((_nonzero_n - 1) * _sum_weight / _nonzero_n)
        _dev := sqrt(_variance)
        _mse := _sqerror_sum / _sum_weight
        _rmse := sqrt(_mse)
    [_mean, _variance, _dev, _mse, _rmse]
//}


// -----------------------------------------------------------------------------
// Example usage : 
// -----------------------------------------------------------------------------

len = input(20)

// -----------------------------------------------------------------------------
// From series :
// -----------------------------------------------------------------------------
[m, v, d, mse, rmse] = f_weighted_sd_from_series(close, volume, len)


plot(m, color = color.blue)
plot(m + d * 2, color = color.blue)
plot(m - d * 2, color = color.blue)
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
// From arrays : 
// -----------------------------------------------------------------------------
var float[] a_src = array.new_float()
var float[] a_weight = array.new_float()

if barstate.isfirst
    for i = 1 to len
        array.unshift(a_weight, i)

array.unshift(a_src, close)

if array.size(a_src) > len
    array.pop(a_src)

[a_m, a_v, a_d, a_mse, a_rmse] = f_weighted_sd_from_arrays(a_src, a_weight, len)

plot(a_m, color = color.orange)
plot(a_m + a_d * 2, color = color.orange)
plot(a_m - a_d * 2, color = color.orange)
// -----------------------------------------------------------------------------


series_text = "Mean : " + tostring(m) + "\nVariance : " + tostring(v) + "\nSD : " + tostring(d) + "\nMSE : " + tostring(mse) +  "\nRMSE : " + tostring(rmse)
array_text = "Mean : " + tostring(a_m) + "\nVariance : " + tostring(a_v) + "\nSD : " + tostring(a_d) + "\nMSE : " + tostring(a_mse) +  "\nRMSE : " + tostring(a_rmse)
debug_text = "Volume weighted from time series : \n" + series_text + "\n\nLinearly weighted from arrays : \n" + array_text

//debug = label.new(x = bar_index, y = close, text = debug_text, style = label.style_label_left)
//.delete(debug[1])

//test strategy
if low <= (m - d * 2)
    strategy.entry("LE", strategy.long)
if high >= (m + d * 2)
    strategy.entry("SE", strategy.short)

// User Options to Change Inputs (%)
stopPer = input(3.11, title='Stop Loss %', type=input.float) / 100
takePer = input(7.50, title='Take Profit %', type=input.float) / 100

// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)

if strategy.position_size > 0 
    strategy.exit(id="Close Long", stop=longStop, limit=longTake)
//    strategy.close("LE", when = (longStop) or (longTake), qty_percent = 100)
if strategy.position_size < 0 
    strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)
//    strategy.close("SE", when = (shortStop) or (shortTake), qty_percent = 100)

もっと