移動平均のブレイクアウト戦略

作者: リン・ハーンチャオチャン, 日時: 2023-09-26 16:18:37
タグ:

概要

移動平均ブレイクアウト戦略は,移動平均を活用して入口と出口を決定する短期取引戦略である. シンプルで使いやすい.

戦略の論理

基本論理は,価格の傾向を測定するために,2つの移動平均線,高速線と遅い線に依存する.高速線は短い期間があり,より敏感である.遅い線は長い期間があり,より安定している.

このコードは,ユーザが入力パラメータを使用して,高速ライン期間の shortPeriodとスローライン期間の longPeriodを設定できるようにします.この2つの移動平均値の値は,shortSMAとlongSMAとして計算されます.

急速移動平均線がスロー移動平均線を超えると,上向きのブレイクとロングエントリーが示される.高速移動平均線がスロー移動平均線を下向きのブレイクとショートエントリーが示される.

長い入口条件:

Fast MA crosses above slow MA
Fast MA > Slow MA

短いエントリー条件:

Fast MA crosses below slow MA
Fast MA < Slow MA 

ストップ・ロスの設定や 収益の設定, ポジションのサイズ設定なども リスク管理に含まれています

利点

  • 使いやすくて,初心者でも理解しやすい
  • 移動平均値はノイズをフィルタリングします
  • 異なる時間枠における精細調整のMA期間における柔軟性
  • 既定のストップ・ロストと得益

リスク

  • 偽の脱出や鞭打ちに敏感です
  • 範囲に限定された不安定な市場には理想的ではありません
  • 遅刻の兆候,エントリが遅れる可能性があります
  • トレンド逆転を効果的にフィルタリングできない

リスク管理

  • 偽信号を避けるためにフィルターを追加する
  • 傾向 が 明らか に なる とき に 戦略 を 適用 する
  • より良いエントリのために MA パラメータを最適化
  • 早期の停止を避けるため,より広い停止を許可します.

増進 の 機会

  • 最適な組み合わせを見つけるために MA パラメータを最適化
  • BOLLチャンネルやKDなどの追加指標を追加します
  • 利益を最大化するために出口ルールを改善する
  • 異なる機器で耐久性を試験する
  • ビッグデータを使った機械学習を組み込む

結論

移動平均ブレイクアウト戦略は,迅速かつ遅いMAで信号を生成し,理解しやすい.しかし,偽ブレイクや遅れの問題などの欠点もあります.パラメータチューニング,追加のフィルターおよびその他の強化により,戦略は改善することができます.全体として,アルゴリズム取引への初心者向けの最初のステップとして機能し,コアコンセプトを理解した後,より高度な戦略への道を開きます.


/*backtest
start: 2023-08-26 00:00:00
end: 2023-09-25 00:00:00
period: 1h
basePeriod: 15m
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/
// © YohanNaftali

//@version=5

///////////////////////////////////////////////////////////////////////////////
// Heikin Ashi Candle Startegy
// ver 2021.12.29
// © YohanNaftali
// This script composed by Yohan Naftali for educational purpose only 
// Reader who will use this signal must do own research
///////////////////////////////////////////////////////////////////////////////
strategy(
     title = 'Heikin Ashi Candle Startegy Long',  
     shorttitle = 'HA Strategy Long',  
     format = format.price,
     precision = 0,
     overlay = true)

// Input
validationPeriod = input.int( 
     defval = 3, 
     title = 'Validation Period', 
     group = 'Candle')

qtyOrder = input.float(
     defval = 1.0,
     title = 'Qty', 
     group = 'Order')

maxActive = input.float(
     defval = 1.0,
     title = 'Maximum Active Open Position', 
     group = 'Order')

// Long Strategy
tpLong = input.float(
     defval = 1,
     title = "Take Profit (%)",
     minval = 0.0, 
     step = 0.1, 
     group = "Long") * 0.01

slLong = input.float(
     defval = 25,
     title = "Stop Loss (%)", 
     minval=0.0, 
     step=0.1,
     group="Long") * 0.01

trailingStopLong = input.float(
     defval = 0.2,
     title = "Trailing Stop (%)",
     minval = 0.0, 
     step = 0.1,
     group = 'Long') * 0.01

// Calculation
haTicker = ticker.heikinashi(syminfo.tickerid)
haClose = request.security(haTicker, timeframe.period, close)
haOpen = request.security(haTicker, timeframe.period, open)

// Long
limitLong = tpLong > 0.0 ? strategy.position_avg_price * (1 + tpLong) : na
stopLong = slLong > 0.0 ? strategy.position_avg_price * (1 - slLong) : na
float trailLong = 0.0
trailLong := if strategy.position_size > 0
    trailClose = close * (1 - trailLong)
    math.max(trailClose, trailLong[1])
else
    0

isGreen = true
for i = 0 to validationPeriod-1
    isGreen := isGreen and haClose[i] > haOpen[i]        
isLong = isGreen and haClose[validationPeriod] < haOpen[validationPeriod]



plot(
     limitLong,
     title = 'Limit', 
     color = color.rgb(0, 0, 255, 0), 
     style = plot.style_stepline,
     linewidth = 1)

plot(
     trailLong,
     title = 'Trailing', 
     color = color.rgb(255, 255, 0, 0), 
     style = plot.style_stepline,
     linewidth = 1)

plot(
     stopLong,
     title = 'Stop', 
     style = plot.style_stepline,
     color = color.rgb(255, 0, 0, 0), 
     linewidth = 1)

// plotshape(
//      isLong, 
//      title = 'Entry', 
//      style = shape.arrowup, 
//      location = location.belowbar, 
//      offset = 1, 
//      color = color.new(color.green, 0), 
//      text = 'Long Entry',
//      size = size.small)

// Strategy
strategy.risk.max_position_size(maxActive)
strategy.risk.allow_entry_in(strategy.direction.long)

strategy.entry(
     id = "Long", 
     direction = strategy.long, 
     qty = qtyOrder,  
     when = isLong,       
     alert_message = "LN")
if (strategy.position_size > 0)
    strategy.exit(
         id = "Long Exit",
         from_entry = "Long",
         limit = limitLong,
         stop = stopLong,
         trail_price = trailLong,
         alert_message = "LX")      

もっと