相対強度戦略のMACD

作者: リン・ハーンチャオチャン開催日:2023年12月21日 12:01:01
タグ:

img

概要

この戦略は,MACDと相対強度 (RS) という2つのよく知られた指標に基づいています.それらを組み合わせることで,強力な買い信号を得ることができます.実際には,この戦略の特性は,指標から指標を作成することです.したがって,我々はRSの値がソースであるMACDを構築します.戦略は,主に敗者であるため,売り信号を無視し,買い信号のみを取ります.また,利益の一部を再投資したり,実質的な損失の場合は注文のサイズを減らすことを可能にするマネー管理方法もあります.

戦略の論理

RSは,市場動力と市場効率の仮定との間の異常を測定する指標です.プロによって使用され,最も堅牢な指標の一つです.アイデアは,過去のパフォーマンスに基づいて平均よりも優れた資産を所有することです.我々はこの式を使用してRSを計算します:

RS = 現在の価格/RS 期間中の最高値 RS 期間中の最高値

ユーザーによって定義されたこの期間における現在の価格を最高価格と比較することができます.

MACDは,最も知られている指標の一つであり,二つの指数関数移動平均間の距離を測定する.一つは速い,もう一つは遅い.広い距離は速いモメントを示し,その逆である.この距離の値をプロットし,この線をmacdlineと呼びます.MACDは,最初の2つよりも低い周期を持つ第三の移動平均を使用します.この最後の移動平均は,macdlineを横切るときに信号を与えるでしょう.したがって,Macdlineの値をソースとして使用して構築されています.

最初の2つのMAsはRS値をソースとして構築されていることに注意することが重要です.したがって,我々は単に指標の指標を構築しました.この種の方法は非常に強力です.それはほとんど使用されず,戦略に価値をもたらします.

利点を分析する

この戦略は,MACDとRSという2つの個別に非常に強力な指標を組み合わせています.MACDは短期的なトレンドとモメンタムシフトを捉えることができ,RSは中期から長期的トレンドの強さを反映しています.それらを一緒に使用すると,短期的および長期的要因の両方を考慮し,購入信号をより信頼性있게します.

さらに,この戦略はRS指標からMACDを導き出し,戦略の効果を創造的に強化することで非常にユニークです.このような革新的なデザインは,このアプローチを使用する人がほとんどないため,アルファリターンにつながる可能性があります.

最後に,戦略にはリスク管理とストップ・ロスのメカニズムがあり,効果的にリスクを制御し,取引ごとに損失を制限します.

リスク分析

この戦略の最大のリスクは,RSおよびMACD指標が間違った信号を与える可能性である.両方の指標が強力であるにもかかわらず,技術指標は100%で未来を予測することができず,シグナルが時々失敗する可能性があります.また,RS自体は中長期トレンド判断に偏りがあり,短期的には誤解を招く信号を生成することがあります.

リスクを減らすために,RSとMACDのパラメータは,特定の取引ツールと市場環境により適するように調整することができる.また,より厳格なストップ損失範囲を課すことができる.一般的に,取引損失を制御するためにストップ損失を使用することは,この戦略のリスクに対処するための最良の方法である.

改善の方向性

まず,どの市場 (株,フォレックス,暗号等) がこの戦略の効果を最大限に発揮するか試し,その最適な資産に焦点を当てます.

2つ目に,機械学習アルゴリズムを使用して RS と MACD パラメータを手動で修正するのではなく自動最適化してみてください.これはパラメータの適応性を大幅に改善することができます.

第3に,取引シグナルを確立するために他の指標を組み込むことを検討し,信号の精度を向上させるための多要素モデルを形成します.例えば,ボリューム指標を追加するなどです.

結論

この戦略は,MACDとRSインジケーターをシネージ的に活用して強力な買い信号を提供する.その新しさは,MACDをRSインジケーターから導き,インジケーター間の結合を実現することで,有効性を向上させることです.この戦略には,リスクを効果的に制御する明確なエントリー,ストップ損失,マネーマネジメントメカニズムがあります.次のステップは,パラメータ最適化,信号生成の精製,他の要因を追加などを通じて戦略をさらに改善することです.


/*backtest
start: 2022-12-14 00:00:00
end: 2023-12-20 00:00:00
period: 1d
basePeriod: 1h
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/
// © gsanson66


//This strategy calculates the Relative Strength and plot the MACD of this Relative Strenght
//We take only buy signals send by MACD
//@version=5
strategy("MACD OF RELATIVE STRENGHT STRATEGY", shorttitle="MACD RS STRATEGY", precision=4, overlay=false, initial_capital=1000, default_qty_type=strategy.cash, default_qty_value=950, commission_type=strategy.commission.percent, commission_value=0.18, slippage=3)


//------------------------------TOOL TIPS--------------------------------//

t1 = "Relative Strength length i.e. number of candles back to find the highest high and compare the current price with this high."
t2 = "Relative Strength fast EMA length used to plot the MACD."
t3 = "Relative Strength slow EMA length used to plot the MACD."
t4 = "Macdline SMA length used to plot the MACD."
t5 = "The maximum loss a trade can incur (in percentage of the trade value)"
t6 = "Each gain or losse (relative to the previous reference) in an amount equal to this fixed ratio will change quantity of orders."
t7 = "The amount of money to be added to or subtracted from orders once the fixed ratio has been reached."


//----------------------------------------FUNCTIONS---------------------------------------//

//@function Displays text passed to `txt` when called.
debugLabel(txt, color, loc) =>
    label.new(bar_index, loc, text=txt, color=color, style=label.style_label_lower_right, textcolor=color.black, size=size.small)

//@function which looks if the close date of the current bar falls inside the date range
inBacktestPeriod(start, end) => (time >= start) and (time <= end)


//---------------------------------------USER INPUTS--------------------------------------//

//Technical parameters
rs_lenght = input.int(defval=300, minval=1, title="RS Length", group="Technical parameters", tooltip=t1)
fast_length = input(title="MACD Fast Length", defval=14, group="Technical parameters", tooltip=t2)
slow_length = input(title="MACD Slow Length", defval=26, group="Technical parameters", tooltip=t3)
signal_length = input.int(title="MACD Signal Smoothing",  minval=1, maxval=50, defval=10, group="Technical parameters", tooltip=t4)
//Risk Management
slMax = input.float(8, "Max risk per trade (in %)", minval=0, group="Risk Management", tooltip=t5)
//Money Management
fixedRatio = input.int(defval=400, minval=1, title="Fixed Ratio Value ($)", group="Money Management", tooltip=t6)
increasingOrderAmount = input.int(defval=200, minval=1, title="Increasing Order Amount ($)", group="Money Management", tooltip=t7)
//Backtesting period
startDate = input(title="Start Date", defval=timestamp("1 Jan 2020 00:00:00"), group="Backtesting Period")
endDate = input(title="End Date", defval=timestamp("1 July 2024 00:00:00"), group="Backtesting Period")


//----------------------------------VARIABLES INITIALISATION-----------------------------//
strategy.initial_capital = 50000
//Relative Strenght Calculation
rs = close/ta.highest(high, rs_lenght)
//MACD of RS Calculation
[macdLine, signalLine, histLine] = ta.macd(rs, fast_length, slow_length, signal_length)
//Money management
equity = math.abs(strategy.equity - strategy.openprofit)
var float capital_ref = strategy.initial_capital
var float cashOrder = strategy.initial_capital * 0.95
//Backtesting period
bool inRange = na


//------------------------------CHECKING SOME CONDITIONS ON EACH SCRIPT EXECUTION-------------------------------//

//Checking if the date belong to the range
inRange := true

//Checking performances of the strategy
if equity > capital_ref + fixedRatio
    spread = (equity - capital_ref)/fixedRatio
    nb_level = int(spread)
    increasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder + increasingOrder
    capital_ref := capital_ref + nb_level*fixedRatio
if equity < capital_ref - fixedRatio
    spread = (capital_ref - equity)/fixedRatio
    nb_level = int(spread)
    decreasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder - decreasingOrder
    capital_ref := capital_ref - nb_level*fixedRatio

//Checking if we close all trades in case where we exit the backtesting period
if strategy.position_size!=0 and not inRange
    strategy.close_all()
    debugLabel("END OF BACKTESTING PERIOD : we close the trade", color=color.rgb(116, 116, 116), loc=macdLine)


//-----------------------------------EXIT SIGNAL------------------------------//

if strategy.position_size>0 and histLine<0
    strategy.close("Long")


//-------------------------------BUY CONDITION-------------------------------------//

if histLine>0 and not (strategy.position_size>0) and inRange
    qty = cashOrder/close
    stopLoss = close*(1-slMax/100)
    strategy.entry("Long", strategy.long, qty)
    strategy.exit("Exit Long", "Long", stop=stopLoss)


//---------------------------------PLOTTING ELEMENT----------------------------------//

hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(macdLine, title="MACD", color=color.blue)
plot(signalLine, title="Signal", color=color.orange)
plot(histLine, title="Histogram", style=plot.style_columns, color=(histLine>=0 ? (histLine[1] < histLine ? #26A69A : #B2DFDB) : (histLine[1] < histLine ? #FFCDD2 : #FF5252)))
plotchar(rs, "Relative Strenght", "", location.top, color=color.yellow)

もっと