ストップ損失追跡戦略を移動する

作者: リン・ハーンチャオチャン, 日付: 2024-01-03 16:15:29
タグ:

img

概要

この戦略は,エントリーシグナルを生成するためにストックインジケータを使用します.ポジションに入ると,ストップロスを動的に調整するためにリアルタイムで新しい高値または低値を追跡します.同時に,戦略は,実際の取引でポジションを調整するために,アラート機能を通じて,ストップロスの修正情報をMT4/MT5に送信します.

戦略原則

  1. この戦略は,ストック指標に基づいて購入・販売信号を生成する.ストックK線が下からD線の上を横切ると,購入信号が生成される.K線が上からD線下を横切ると,販売信号が生成される.

  2. ポジションに入力した後,戦略は,動的ストップ損失レベルとして,最低価格の最新の低値と最高価格の最新の高値をリアルタイムで追跡します.特に,ロングポジションでは,最低価格の最新の低値がストップ損失レベルとして追跡されます.ショートポジションでは,最高価格の最新の高値がストップ損失レベルとして追跡されます.

  3. ストップ・ロスのレベルの変化が検出されると,戦略はアラート機能を通じてストップ・ロスの変更オーダーを生成し,実際の取引のストップ・ロスのレベルをリアルタイムで調整するためにMT4 / MT5に送信します.ストップ・ロスの変化の直感的な表示のために,グラフィック・アノテーションもグラフ化されています.

  4. この戦略は,ダイナミックストップ・ロスのメカニズムを有効にするかどうかを手動で制御することをサポートします.有効であれば,ストップ・ロスは市場の変動に応じてリアルタイムで調整できます.

利点分析

  1. ダイナミック・トラッキング・ストップ・ロスのメカニズムは,市場の変動に応じてストップ・ロスのレベルを柔軟に調整し,リスクを効果的に制御することができます.

  2. 警告機能は,手動の介入なしに自動管理のために,ストップ損失調整情報をMT4/MT5にリアルタイムで送信することを可能にします.

  3. ストップ・ロスの調整のグラフ上の視覚的な注釈は,ストップ・ロスの後続効果の表示と検証を容易にする.

  4. ストップ・ロース・トラッキング・メカニズムを手動で制御するサポートにより,異なる市場状況に柔軟に適応できます.

  5. ストック指標と組み合わせて機会を特定することで 戦略は誤ったブレイクを効果的にフィルタリングし 安定性を向上させることができます

リスク分析

  1. ストック指標は頻繁にクロスオーバー信号を生成し,より非効率な操作のリスクを導入する.パラメータはフィルター信号に調整することができます.

  2. 極端な市場条件では,ストップ損失が突入し,巨大な損失を完全に回避することができません.ポジションリスクは,間に合う方法で監視する必要があります.

  3. 障害や遅延などのアラート接続の問題が発生し,調整のリアルタイムフィードバックを防止する.適切な故障容認対策が実施されなければならない.

  4. ダイナミック・トレリング・ストップ・ロスは,比較的頻繁な調整が必要で,取引コストが高くなる可能性があります.調整範囲はコストにバランスする必要があります.

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

  1. 異なるパラメータの組み合わせをテストすることで,よりよい信号品質と戦略性能のためにストック指標を最適化することができます.

  2. 他の指標は,シグナルをフィルタリングしたり,戦略の安定性を向上させる調整範囲を決定するために組み合わせることができます.

  3. ストップ損失効果を保証しながら調整頻度を減らすために,異なる追跡アルゴリズムを研究することができる.

  4. MT4/MT5との接続方法は,迅速かつ効率的なアラートを確保し,遅延を最小限に抑えるために強化できます.

  5. 自動および手動ストップ損失モードは,異なる市場条件下で異なるメカニズムを使用するために導入できます.

概要

この戦略は,まずストック指標に基づいて取引機会を決定し,その後ポジション中の価格変動を追跡し,ストップ損失を動的に調整し,アラートオーダーを通じて自動的に調整情報を発行する.そのような動的メカニズムは,手動的な介入が少なく市場変化に応じて積極的なポジションリスク管理を可能にします.一方,直感的なストップ損失注釈は監視も容易化します.信号フィルタリングとトレーリングアルゴリズムに対するさらなる最適化は収益性を向上させることができます.全体として,動的なトレーリングストップ損失戦略は,不安定な市場と自動化ポジションリスク管理を追跡するのに適しています.


/*backtest
start: 2022-12-27 00:00:00
end: 2024-01-02 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/
// © Peter_O

//@version=4
strategy(title="Moving Stop-Loss mechanism", overlay=true)

// This script was created for educational purposes only and it is a spin-off of my previous script:
// https://www.tradingview.com/script/9MJO3AgE-TradingView-Alerts-to-MT4-MT5-dynamic-variables-NON-REPAINTING/
// This spin-off adds very often requested Moving Stop-Loss Mechanism - the logic here moves the stop-loss each time 
// a new pivot is detected.
//
// Last lines of the script include alert() function calls, with a syntax compatible with TradingConnector
// for execution in Forex/indices/commodities/crypto markets via MetaTrader.
// Please note that "tradeid=" variable must be passed with each alert, so that MetaTrader knows which
// trade to modify.

TakeProfitLevel=input(400)

// **** Entries logic, based on Stoch **** {
periodK = 13 //input(13, title="K", minval=1)
periodD = 3 //input(3, title="D", minval=1)
smoothK = 4 //input(4, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)

GoLong=crossover(k,d) and k<80
GoShort=crossunder(k,d) and k>20
// } End of entries logic

// **** Pivot-points and stop-loss logic **** {
piv_high = pivothigh(high,1,1)
piv_low = pivotlow(low,1,1)
var float stoploss_long=low
var float stoploss_short=high

pl=valuewhen(piv_low,piv_low,0)
ph=valuewhen(piv_high,piv_high,0)

if GoLong 
    stoploss_long := low<pl ? low : pl
if GoShort 
    stoploss_short := high>ph ? high : ph

plot(stoploss_long, color=color.red, title="stoploss_long")
plot(stoploss_short, color=color.lime, title="stoploss_short")

// Stop-Loss Updating mechanism
enable_stoploss_mechanism=input(true, title="Enable Stoploss Modification Mechanism")
UpdateLongStopLoss = strategy.position_size>0 and strategy.position_size[1]>0 and piv_low and pl!=stoploss_long and not GoLong and enable_stoploss_mechanism
UpdateShortStopLoss = strategy.position_size<0 and strategy.position_size[1]<0 and piv_high and ph!=stoploss_short and not GoShort and enable_stoploss_mechanism
if UpdateLongStopLoss
    stoploss_long := pl
if UpdateShortStopLoss
    stoploss_short := ph

plotshape(UpdateLongStopLoss ? stoploss_long[1]-300*syminfo.mintick : na, location=location.absolute, style=shape.labelup, color=color.lime, textcolor=color.white, text="SL\nmove")
plotshape(UpdateShortStopLoss ? stoploss_short[1]+300*syminfo.mintick : na, location=location.absolute, style=shape.labeldown, color=color.red, textcolor=color.black, text="SL\nmove")
// } End of Pivot-points and stop-loss logic

// **** Trade counter **** {
var int trade_id=0
if GoLong or GoShort
    trade_id:=trade_id+1
// } End of Trade counter

strategy.entry("Long", strategy.long, when=GoLong)
strategy.exit("XLong", from_entry="Long", stop=stoploss_long, profit=TakeProfitLevel)
strategy.entry("Short", strategy.short, when=GoShort)
strategy.exit("XShort", from_entry="Short", stop=stoploss_short, profit=TakeProfitLevel)

if GoLong
    alertsyntax_golong='long slprice=' + tostring(stoploss_long) + ' tradeid=' + tostring(trade_id) + ' tp=' + tostring(TakeProfitLevel)
    alert(message=alertsyntax_golong, freq=alert.freq_once_per_bar_close)
if GoShort
    alertsyntax_goshort='short slprice=' + tostring(stoploss_short) + ' tradeid=' + tostring(trade_id) + ' tp=' + tostring(TakeProfitLevel)
    alert(message=alertsyntax_goshort, freq=alert.freq_once_per_bar_close)
if UpdateLongStopLoss
    alertsyntax_updatelongstoploss='slmod slprice=' + tostring(stoploss_long) + ' tradeid=' + tostring(trade_id)
    alert(message=alertsyntax_updatelongstoploss, freq=alert.freq_once_per_bar_close)
if UpdateShortStopLoss
    alertsyntax_updateshortstoploss='slmod slprice=' + tostring(stoploss_short) + ' tradeid=' + tostring(trade_id)
    alert(message=alertsyntax_updateshortstoploss, freq=alert.freq_once_per_bar_close)


もっと