急速 遅い 二重移動平均 取引戦略

作者: リン・ハーンチャオチャン,日付: 2023年10月27日 16時41分24秒
タグ:

img

概要

双移動平均取引戦略は,高速移動平均と遅移動平均を計算し,クロスを観察することによって取引信号を生成する.高速移動平均がスロー移動平均を超えると,ロングポジションが取られる.高速移動平均がスロー移動平均を下回ると,ショートポジションが取られる.この戦略はトレンド取引とコントラントレンド取引の両方に使用できる.

戦略の論理

この戦略は,まず高速移動平均 maFastLength とスロー移動平均 maSlowLength の長さを設定する.その後,高速移動平均 fastMA とスロー移動平均 slowMA を計算する.高速移動平均は価格変化により迅速に反応し,現在の傾向を判断するために使用され,スロー移動平均はよりゆっくりと反応し,傾向の方向性を決定するために使用される.

急速移動平均がスロー移動平均を上回ると,ロングエントリー信号 goLong ((() が生成される.高速移動平均がスロー移動平均を下回ると,既存のロングポジションは killLong ((() 信号で閉鎖される.

ストラテジーは,ロングのみ,ショートのみに設定したり,ロングとショートの両方の取引を許可したりできます.

ロングのみモードでは,ロングポジションはgoLong() 信号で入力され,killLong() 信号で終了します.

ショートのみモードでは,ショートポジションは killLong ((() シグナルで入力され,goLong ((() シグナルで終了します.

スワップモードでは,goLongでロングポジションを入力し,killLongで閉鎖し,ショートに逆転します.

この戦略には,ストップ・ロスト,トライリング・ストップ,メッセージング,その他のオプション機能も含まれます.

利点

  1. シンプルで簡単に実行できます

  2. 長期,短期,または両方への柔軟性

  3. 選択的なストップ・ロストとトラッキング・ストップ機能

  4. 取引を警告するための メッセージの設定が可能です

  5. 市場の動向に敏感です

  6. 調整可能なパラメータは,異なる市場に対応します.

リスク

  1. 不安定な市場や 変動する市場で 過剰な取引を生む可能性があります

  2. 突然のニュースに反応するのが遅い.

  3. パラメータ選択は戦略のパフォーマンスに影響します

  4. シグナルを厳格にフォローし 裁量的な取引を避ける

  5. 取引コストは考慮されない場合 利益を損なう可能性があります

改良

  1. 偽信号を避けるため RSI のようなフィルターを追加します

  2. 最適な設定を見つけるためにパラメータ最適化を実行します.

  3. ダイナミックストップを使って 利益を固定して調整します

  4. 機械学習を組み込み トレンド予測を助けます

  5. 個々の取引習慣に合わせて メッセージングを最適化する

結論

ダブル移動平均戦略は,強いトレンドを捉えるのに比較的シンプルで有用である.しかし,低トレンド環境ではウィップソウを避けるように注意する必要があります.微調整パラメータと補助指標または強化を追加することで,強度と適応性がさらに向上します.


/*backtest
start: 2022-10-20 00:00:00
end: 2023-10-26 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4

strategy("SMA Strategy", shorttitle="SMA Strategy", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// === Inputs ===
// short ma
maFastSource = input(defval=close, title="Fast MA Source")
maFastLength = input(defval=3, title="Fast MA Period", minval=1)

// long ma
maSlowSource = input(defval=close, title="Slow MA Source")
maSlowLength = input(defval=9, title="Slow MA Period", minval=1)

// Trade direction
shorting = input(defval=false, title="Short only?")
longonly = input(defval=true, title="Long only?")
swapping = input(defval=false, title="Swap orders?")
// risk management
useStop = input(defval=false, title="Use Initial Stop Loss?")
slPoints = input(defval=25, title="Initial Stop Loss Points", minval=1)
useTS = input(defval=false, title="Use Trailing Stop?")
tslPoints = input(defval=120, title="Trail Points", minval=1)
useTSO = input(defval=false, title="Use Offset For Trailing Stop?")
tslOffset = input(defval=20, title="Trail Offset Points", minval=1)

// Messages for buy and sell
message_long_entry  = input("Long entry message", title="Long entry message")
message_long_exit   = input("Long exit message", title="Long exit message")
message_short_entry = input("Short entry message", title="Short entry message")
message_short_exit  = input("Short exit message", title="Short exit message")

// Calculate start/end date and time condition
startDate  = input(timestamp("2021-01-01T00:00:00"), type = input.time)
finishDate = input(timestamp("2021-12-31T00:00:00"), type = input.time)
 
time_cond  = true
// === Vars and Series ===
fastMA = sma(maFastSource, maFastLength)
slowMA = sma(maSlowSource, maSlowLength)

plot(fastMA, color=color.blue)
plot(slowMA, color=color.purple)

goLong() =>
    crossover(fastMA, slowMA)
killLong() =>
    crossunder(fastMA, slowMA)
    
// Long only
if longonly
    strategy.entry("Buy", strategy.long, when=goLong() and time_cond, alert_message = message_long_entry)
    strategy.close("Buy", when=killLong() and time_cond, alert_message = message_long_exit)

// Short only
if shorting
    strategy.entry("Sell", strategy.short, when=killLong() and time_cond, alert_message = message_short_entry)
    strategy.close("Sell", when=goLong() and time_cond, alert_message = message_short_exit)
    
// Order Swapping
if swapping
    strategy.entry("Buy", strategy.long, when=goLong() and time_cond, alert_message = message_long_entry)
    strategy.entry("Sell", strategy.short, when=killLong() and time_cond, alert_message = message_short_entry)

if useStop
    strategy.exit("XLS", from_entry="Buy", stop=strategy.position_avg_price / 1.08, alert_message = message_long_exit)
    strategy.exit("XSS", from_entry="Sell", stop=strategy.position_avg_price * 1.08, alert_message = message_short_exit)



もっと