本策略基于Stochastic oscillator指标判断市场的超买超卖状态,结合弹性止损原理开展短期交易。在Stochastic指标上金叉时做多,死叉时做空,同时设置基于前期枢轴点的弹性止损,在保证盈利的同时控制风险。
Stochastic oscillator指标包含%K线和%D线。当%K线从下向上突破%D线时,为金叉信号,做多;当%K线从上向下突破%D线时,为死叉信号,做空。本策略就是根据Stochastic指标的金叉死叉信号判断入场。
具体来说,在Stochastic指标金叉时,如果%K线值小于80(未超买),则做多;在Stochastic指标死叉时,如果%K线值大于20(未超卖),则做空。
GoLong=crossover(k,d) and k<80
GoShort=crossunder(k,d) and k>20
本策略使用弹性止损的方法,根据前期的枢轴点设置止损价位,代码如下:
piv_high = pivothigh(high,1,1)
piv_low = pivotlow(low,1,1)
stoploss_long=valuewhen(piv_low,piv_low,0)
stoploss_short=valuewhen(piv_high,piv_high,0)
枢轴点代表了重要的支撑阻力,如果价格突破枢轴点,则退出仓位,使止损价位“弹性”跟随枢轴点变化。
此外,止损价位也会考虑当前期间内最低价和最高价,进一步优化止损位置,如下代码所示:
if GoLong
stoploss_long := low<pl ? low : pl
if GoShort
stoploss_short := high>ph ? high : ph
使用Stochastic指标判断市场超买超卖状态,避免追高杀跌;
应用弹性止损原理,可以根据市场变化优化止损位置;
结合枢轴点突破实现止损,使止损更有效;
考虑当期最高最低价进行止损优化,使止损更精准。
Stochastic指标发出假信号的风险
止损被突破造成损失扩大的风险
交易频繁造成交易费用增加的风险
优化止损策略,如使用 Chandelier Exit、移动止损、振荡止损等方式
优化入场条件,结合其他指标避免 Stochastic 指标的假信号
优化止盈方式,如使用移动止盈、振荡止盈等,实现更高的盈利率
添加位置管理,如固定每单数量、固定投资比例等,控制单笔风险
优化参数设定,如 K、D 期数、平滑周期等,针对不同市场调整参数
本策略通过 Stochastic 指标判断超买超卖状态入场,并使用弹性止损方式进行风险管理。策略具有避免追高杀跌、止损有效等优势,但也存在一定的假信号风险。未来可通过优化入场条件、止损策略、止盈方式、风险管理等方面进一步完善该策略。
/*backtest
start: 2023-08-28 00:00:00
end: 2023-09-27 00:00:00
period: 2h
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/
// © Peter_O
//@version=4
//strategy(title="TradingView Alerts to MT4 MT5 example with cancelling pending orders", commission_type=strategy.commission.cash_per_order, commission_value=0.00003, overlay=true, default_qty_value=100000, initial_capital=1000)
// This script was created for educational purposes only.
// It is showing how to create pending orders and cancel them
// Together with syntax to send these events through TradingView alerts system
// All the way to brokers for execution
TakeProfitLevel=input(400)
// **** Entries logic **** {
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)
// plot(k, title="%K", color=color.blue)
// plot(d, title="%D", color=color.orange)
// h0 = hline(80)
// h1 = hline(20)
// fill(h0, h1, color=color.purple, transp=75)
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.lime, title="stoploss_long")
plot(stoploss_short, color=color.red, title="stoploss_short")
// } End of Pivot-points and stop-loss logic
CancelLong=crossunder(low,stoploss_long) and strategy.position_size[1]<=0 and strategy.position_size<=0
CancelShort=crossover(high,stoploss_short) and strategy.position_size[1]>=0 and strategy.position_size>=0
entry_distance=input(10, title="Entry distance for stop orders")
plotshape(CancelLong ? stoploss_long[1]-10*syminfo.mintick : na, location=location.absolute, style=shape.labelup, color=color.gray, textcolor=color.white, text="cancel\nlong", size=size.tiny)
plotshape(CancelShort ? stoploss_short[1]+10*syminfo.mintick : na, location=location.absolute, style=shape.labeldown, color=color.gray, textcolor=color.white, text="cancel\nshort", size=size.tiny)
strategy.entry("Long", strategy.long, when=GoLong, stop=close+entry_distance*syminfo.mintick)
strategy.exit("XLong", from_entry="Long", stop=stoploss_long, profit=TakeProfitLevel)
strategy.cancel("Long", when = CancelLong)
strategy.entry("Short", strategy.short, when=GoShort, stop=close-entry_distance*syminfo.mintick)
strategy.exit("XShort", from_entry="Short", stop=stoploss_short, profit=TakeProfitLevel)
strategy.cancel("Short", when = CancelShort)
if GoLong
alertsyntax_golong='long offset=' + tostring(entry_distance) + ' slprice=' + tostring(stoploss_long) + ' tp=' + tostring(TakeProfitLevel)
alert(message=alertsyntax_golong, freq=alert.freq_once_per_bar_close)
if GoShort
alertsyntax_goshort='short offset=' + tostring(-entry_distance) + ' slprice=' + tostring(stoploss_short) + ' tp=' + tostring(TakeProfitLevel)
alert(message=alertsyntax_goshort, freq=alert.freq_once_per_bar_close)
if CancelLong
alertsyntax_cancellong='cancel long'
alert(message=alertsyntax_cancellong, freq=alert.freq_once_per_bar_close)
if CancelShort
alertsyntax_cancelshort='cancel short'
alert(message=alertsyntax_cancelshort, freq=alert.freq_once_per_bar_close)