该策略采用Stoch指标进行入场信号判断,在进场后会实时追踪价格新高或者新低,从而动态调整止损位。同时,策略还会通过alert功能,将止损修改的信息发送到MT4/MT5,以实时调整真实交易中的头寸。
该策略基于Stoch指标产生买入和卖出信号,当Stoch的K线从下方向上突破D线时产生买入信号;当Stoch的K线从上方向下突破D线时产生卖出信号。
在进场后,策略会实时追踪最低价的最新低点和最高价的最新高点,作为动态的止损位。具体来说,对于做多单,会追踪最低价的最近低点作为止损位;对于做空单,会追踪最高价的最近高点作为止损位。
当检测到止损位发生变化时,策略会通过alert功能生成修改止损指令,发送到MT4/MT5以实时调整真实交易中的止损位。同时绘制图形标注以直观显示止损变化。
该策略支持手动控制是否启用动态止损机制。启用后,可以根据市场波动实时调整止损追踪价格。
采用动态追踪止损机制,可以根据市场波动灵活调整止损位,实现止损追踪,有效控制风险。
利用alert功能可将止损调整信息实时发送到MT4/MT5,实现自动化管理,无需人工干预。
直观地在图形上标注止损调整信息,便于查看和验证止损追踪效果。
支持手动控制是否启用止损追踪机制,灵活适应不同市场条件。
结合Stoch指标判断时机,可以有效过滤假突破,提高策略稳定性。
Stoch指标可能出现频繁交叉信号,带来更多无效操作的风险。可以适当调整参数以过滤信号。
在极端行情中,止损可能被突破,无法完全规避巨额亏损的风险。应适时监控头寸风险。
alert连接可能出现中断、延迟等问题,无法实时反馈调整结果,需要做好容错处理。
动态追踪止损需要相对密集的调整,可能带来更多交易成本。应平衡调整幅度与成本。
可以测试不同参数组合优化Stoch指标,获得更好的信号质量和策略效果。
可以结合其他指标过滤信号或确定调整幅度,优化止损机制改善策略稳定性。
可以研究不同的追踪算法,在降低调整频率的同时保证止损效果。
可以优化与MT4/MT5的连接方式,确保alert及时高效,减少延迟问题。
可以引入自动止损模式和手动模式切换,不同市场条件使用不同止损机制。
本策略首先基于Stoch指标判断买卖时机,然后在持仓期间实时追踪价格波动调整止损位,通过alert指令自动化下发调整信息。这种动态止损机制可以根据市场变化主动管理头寸风险,并减少人工干预提高效率。同时直观的止损调整标记也便于监控。该策略可以进一步优化信号过滤和止损算法提高盈利空间。总体而言,动态止损追踪策略适合用于跟踪多变的市场,自动化调整头寸风险。
/*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)