日中戦略をフォローするMACDトレンド

作者: リン・ハーンチャオチャン,日付: 2023-12-19 11:16:44
タグ:

img

概要

MACDトレンドフォロー・イントラデイ戦略は,移動平均値,MACD指標,ウィリアムズ指標を組み合わせるイントラデイ取引戦略である.長期および短期間のトレンド特性を把握することを目的としたロングおよびショートポジションのエントリーおよびアウトジット基準を形成するために3つの指標の異なる組み合わせを使用する.

戦略の論理

この戦略の主要な取引論理は,いくつかの側面に基づいています.

  1. 価格が指数関数移動平均線 (EMA) を突破するとロング,突破するとショート.

  2. MACDの速い線がスローラインの上にあるときロングで,下にあるときショートする.

  3. ウィリアム・インディケーターの速いMA線が遅いMA線よりも上にあるとき,ロングに移動し,逆です.

  4. この3つのシナリオの組み合わせを 入国条件として使用する.

  5. 逆転信号で出口

この戦略は,全体的なトレンド方向とMACDを短期的なモメンタムに組み合わせることで,利益のための適正なエントリーポイントで価格トレンドの動きを把握することができます. ウィリアムズ指標は,過剰購入/過剰販売レベルを測定することによって,偽のブレイクを避けるのにさらに役立ちます.

利点

この多指標のコンボ構造は,以下の戦略を伴う典型的な短期トレンドを作り出します.

  1. 誤った信号を減らすために三重クロス検証

  2. 主なトレンドのEMA,短期モメントのMACD

  3. ウィリアムズ・インディケーターは,不安定な動き中に上部や下部を追うのを避けます.

  4. リバースコンボは リスクコントロールが出口と一致することを保証します

リスク

この戦略には重大なリスクもあります.

  1. 複雑な構造によりパラメータ調整が困難です

  2. 短期取引が頻繁に行われる場合,取引コストが上昇する可能性があります.

  3. 真のトレンド逆転点を検出できない場合,損失が発生する可能性があります.

主な緩和策はパラメータの最適化とストップロストで 利益のコンボを最大化し 単一の取引損失を最大化します

増進 の 機会

戦略の強化のための主な側面:

  1. 最適なセットのためにより多くのパラメータの組み合わせをテストする.

  2. 入力検証のためのボリュームなどのより多くのデータフィードを追加します.

  3. リスク管理を強化するために動的または後続ストップ損失を使用する.

  4. 機械学習を組み込み 真の逆転を検知します

結論

このMACDトレンドは,日中戦略をフォローして,短期的トレンドを特定し,リスクを管理するための指標を効果的に組み合わせています.調整パラメータのさらなる改善,ストップ損失レベルの設定,より多くのデータフィードを組み込むことは,戦略の勝利率と収益性を高めることができます.戦略の進歩のために研究する価値があります.


/*backtest
start: 2023-11-18 00:00:00
end: 2023-12-18 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/
// © platsn

//@version=5
strategy("MACD Willy Strategy", overlay=true, pyramiding=1, initial_capital=10000) 

// ******************** Trade Period **************************************
startY = input(title='Start Year', defval=2011, group = "Trading window")
startM = input.int(title='Start Month', defval=1, minval=1, maxval=12, group = "Trading window")
startD = input.int(title='Start Day', defval=1, minval=1, maxval=31, group = "Trading window")
finishY = input(title='Finish Year', defval=2050, group = "Trading window")
finishM = input.int(title='Finish Month', defval=12, minval=1, maxval=12, group = "Trading window")
finishD = input.int(title='Finish Day', defval=31, minval=1, maxval=31, group = "Trading window")
timestart = timestamp(startY, startM, startD, 00, 00)
timefinish = timestamp(finishY, finishM, finishD, 23, 59)
// t1 = time(timeframe.period, "0945-1545:23456") 
// window = time >= timestart and time <= timefinish and t1 ? true : false 
// t2 = time(timeframe.period, "0930-1555:23456")
// window2 = time >= timestart and time <= timefinish and t2 ? true : false 

leverage = input.float(1, title="Leverage (if applicable)", step=0.1, group = "Trading Options")
reinvest = input.bool(defval=false,title="Reinvest profit", group = "Trading Options")
reinvest_percent = input.float(defval=20, title = "Reinvest percentage", group="Trading Options")
// entry_lookback = input.int(defval=10, title="Lookback period for entry condition", group = "Trading Options")

// -------------------------------------------- Data Source --------------------------------------------

src = input(title="Source", defval=close)

// ***************************************************************************************************** Daily ATR *****************************************************
atrlen = input.int(14, minval=1, title="ATR period", group = "Daily ATR")
iPercent = input.float(5, minval=1, maxval=100, step=0.1, title="% ATR to use for SL / PT", group = "Daily ATR")
 
percentage = iPercent * 0.01
datr = request.security(syminfo.tickerid, "1D", ta.rma(ta.tr, atrlen))
datrp = datr * percentage

// plot(datr,"Daily ATR")
// plot(datrp, "Daily % ATR")

//*********************************************************** VIX volatility index ****************************************

VIX = request.security("BTC_USDT:swap", timeframe.period, close)
vix_thres = input.float(20.0, "VIX Threshold for entry", step=0.5, group="VIX Volatility Index")

// ************************************************ Volume ******************************************************

vol_len = input(50, 'Volume MA Period')
avg_vol = ta.sma(volume, vol_len)

//-------------------------------------------------------- Moving Average ------------------------------------

emalen1 = input.int(200, minval=1, title='EMA', group= "Moving Averages")
ema1 = ta.ema(src, emalen1)

// ------------------------------------------ MACD ------------------------------------------
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

// ---------------------------------------- William %R --------------------------------------
w_length = input.int(defval=34, minval=1)
w_upper = ta.highest(w_length)
w_lower = ta.lowest(w_length)

w_output = 100 * (close - w_upper) / (w_upper - w_lower)

fast_period = input(defval=5, title='Smoothed %R Length')
slow_period = input(defval=13, title='Slow EMA Length')

w_fast_ma = ta.wma(w_output,fast_period)
w_slow_ma = ta.ema(w_output,slow_period)



// ------------------------------------------------ Entry Conditions ----------------------------------------

L_entry1 = close > ema1 and hist > 0 and w_fast_ma > w_slow_ma 
S_entry1 = close < ema1 and hist < 0 and w_fast_ma < w_slow_ma 

// -------------------------------------------------- Entry -----------------------------------------------
strategy.initial_capital = 50000
profit = strategy.netprofit
trade_amount = math.floor(strategy.initial_capital*leverage / close) 

if strategy.netprofit > 0 and reinvest
    trade_amount := math.floor((strategy.initial_capital+(profit*reinvest_percent*0.01))*leverage / close) 
else
    trade_amount := math.floor(strategy.initial_capital*leverage/ close) 


if L_entry1 //and window
    strategy.entry("Long", strategy.long, trade_amount)

if S_entry1 //and window
    strategy.entry("Short", strategy.short, trade_amount)

// --------------------------------------------------- Exit Conditions -------------------------------------

L_exit1 = hist < 0 and w_fast_ma < w_slow_ma and w_fast_ma < -20
S_exit1 = hist > 0 and w_fast_ma > w_slow_ma and w_fast_ma > -80

// ----------------------------------------------------- Exit ---------------------------------------------

if L_exit1 //and window2
    strategy.close("Long")
    
if S_exit1 //and window2
    strategy.close("Short")

// if time(timeframe.period, "1530-1600:23456")
//     strategy.close_all()

もっと