blackcat1402

魔改高灵敏止盈止损策略

SZSE:002235   XIAMEN ANNE CORPOR
**社区反馈**
之前发不过一个脚本“说个高灵敏度跟踪止盈止损算法”,没想到过去了一段时间,无论是国内,还是海外社区多有很多反馈,其中@hedan1提出了一个建议:“这个指标可以再优化开发一条迟钝的线配合过滤整体方向,再用灵敏的线做确认信号进入”。事实上,这是典型的双层思路,也就是说内部是快速趋势或者振荡器指标,外层是个趋势指标,两者结合就能保证比较好的性能。也就是通过快慢结合,让策略获得“大局观”。在2019年和一位国外的量化交易者NILX交流时候,他说就是简单一个内圈埃勒斯振荡器,外圈一个超级趋势,就可以在他的冰糖橙合约策略中稳定发挥,他实盘了半年多,一路向东北。后面我自己也做了一个类似的跑起来,但是还是不够稳定,我想是细节上还做的不够好。无论如何,都说明了双层策略思想是有搞头的。


**双层策略思路**

根据@hedan1的建议,他提出了一个建议:这个指标可以进一步优化,制定一条慢趋势线来过滤整体方向,然后用敏感线作为确认信号进场。如前作所说,我最喜欢的查看快速趋势的工具之一就是BCL,它简单但功能强大。 我决定用它做为内圈快线。外圈就用大名鼎鼎的超级趋势。买卖进出条件通过内外圈的状态综合决定。所以,这个策略是一个两层结构:内层敏感部分是最好的成本线指标(BCL),外层是控制整体趋势方向的超趋势指标。

具体逻辑细节不说了,请参考如下代码。只是提下一些标识的基本定义:

标签含义和颜色:
L:开多仓,黄色
XL:平多仓,紫红色
S:开空仓,绿色
XS:平空仓,红色

内部看涨:黄色
内部看跌:紫红色
外部看涨:绿色
外部看跌:红色

内部指标:最佳成本线BCL
外部指标:超趋势Supertrend

此外,我改进了显示功能,使用 linebr 功能删除了一些模糊线条, 让视觉效果更加清楚。

这个脚本开源发布在TradingView社区,感兴趣的朋友可以用以下链接查看:


**双层策略源代码**
```c
// This source code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// © blackcat1402
//@version=4

study(" L3 Super Best Cost Line", overlay=true, max_bars_back=5000, max_labels_count=500)

//functions
xrf(values, length) =>
r_val = float(na)
if length >= 1
for i = 0 to length by 1
if na(r_val) or not na(values)
r_val := values
r_val
r_val

//定义典型价格wcx
wcx = (2*high + low + high + open/2) / 4.5
//3根K线中最高点
hh = highest(high,3)
//3根K线中最低点
ll = lowest(low,3)
//高点创新高并且典型价格上涨
ch1 = (hh > xrf(hh,1) and wcx>xrf(wcx,1))
//低点创新低并且典型价格下跌
ch2 = (ll < xrf(ll,1) and wcx<xrf(wcx,1))
//向上突破K线回溯索引值
k3 = barssince(ch1)
//向下突破K线回溯索引值
k4 = barssince(ch2)
//最近有向上突破或者创新高并且不能有创新低
k5 = (k3<k4 or ch1) and not(ch2)
//最近有向下突破或者创新低并且不能有创新高
k6 = (k3>k4 or ch2) and not(ch1)
//如果向上突破则取最低价作为退出准则(跟踪止损放飞利润),否则取开始向前期低点(做多止损位)
zc = iff(hh>xrf(hh,1) and wcx>xrf(wcx,1),ll,xrf(ll,barssince(hh>xrf(hh,1) and wcx>xrf(wcx,1))))
//如果向下突破则取最高价作为推出准则(跟踪止损放飞利润),否则取开始向前期高点 (做空止损位,A股不需要)
yl = iff(ll<xrf(ll,1) and wcx<xrf(wcx,1),hh,xrf(hh,barssince(ll<xrf(ll,1) and wcx<xrf(wcx,1))))
//如果向上突破,则按算法生成止损退出价格
bcl = iff(k5,zc,yl)

plot(wcx>bcl ? bcl : na, color=wcx>bcl?color.yellow:na, linewidth =1, style=plot.style_linebr)
plot(wcx<bcl ? bcl : na, color=wcx<bcl?color.fuchsia:na, linewidth =1, style=plot.style_linebr)


atrPeriod = input(8, "ATR Length")
factor = input(4.0, "Factor", step = 0.01)

= supertrend(factor, atrPeriod)

bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = direction < 0 ? color.green : na, style=plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, "Down Trend", color = direction > 0 ? color.red : na, style=plot.style_linebr)

fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)

long = (crossover(wcx,bcl) and direction < 0) or (direction>0 and direction<0)
xlong = crossunder(wcx,bcl) and direction < 0 or (direction<0 and direction>0)
short = (crossover(wcx,bcl) and direction > 0) or (direction<0 and direction>0)
xshort = crossunder(wcx,bcl) and direction > 0 and not xlong and not long

l_bs = xlong ?
label.new (bar_index, high, "XL", color=color.fuchsia, textcolor=color.white, style=label.style_labeldown, yloc=yloc.price, size=size.small) :
long ?
label.new (bar_index, low, "L", color=color.yellow, textcolor=color.white, style=label.style_labelup, yloc=yloc.price, size=size.small) :
na

s_bs = short ?
label.new (bar_index, high, "S", color=color.red, textcolor=color.white, style=label.style_labeldown, yloc=yloc.price, size=size.small) :
xshort ?
label.new (bar_index, low, "XS", color=color.green, textcolor=color.white, style=label.style_labelup, yloc=yloc.price, size=size.small) :
na
```

一些图说明下:

BTCUSDT行情来自TradingView
黄金行情来自TradingView
同为股份行情来自TradingView
恒久科技行情来自TradingView

Avoid losing contact!Don't miss out! The first and most important thing to do is to join my Discord chat now! Click here to start your adventure: discord.com/invite/ZTGpQJq 防止失联,请立即行动,加入本猫聊天群: discord.com/invite/ZTGpQJq
Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.