尾随止损未在交易视图中触发(pine 脚本)
Trailing STOPLOSS not triggering in tradingview (pine script)
我是 pinescript 的新手,正在尝试编写代码,在 longcondtion(ema 交叉)后进入,如果初始止损触发,策略退出 100% 数量。并在第一个目标处预定 50% 的数量,剩余的 50% 数量应该跟随超级趋势。当超级趋势改变方向时,它应该以剩余的 50% 退出,但即使超级趋势交叉,它也不会退出剩余的 50% 数量。短条件反之亦然
如果有人做过类似的代码,请推荐我,在此先感谢。
if (longCond )
strategy.entry("BUY",strategy.long)
if (shortCond )
strategy.entry("SELL",strategy.short)
tar=input(defval=30.0,title="TARGET")
sl=input(defval=30.0,title="STOP LOSS")
tar:=tar/syminfo.mintick
sl:=sl/syminfo.mintick
//adding stoploss and target option
strategy.exit("x1", from_entry="BUY", qty = 50, profit = tar,loss = sl)
strategy.exit("x1", from_entry="SELL",qty = 50, profit = tar,loss = sl)
if strategy.position_size>0 and abs(strategy.position_size)==50 and crossunder(close,atrStop)
strategy.exit("x2", from_entry="BUY", qty_percent=100)
if strategy.position_size<0 and abs(strategy.position_size)==50 and crossover(close,atrStop)
strategy.exit("x2", from_entry="SELL",qty_percent=100)
**完整代码如下
//complete code//@version=4
strategy("SLtrail", overlay = true,calc_on_every_tick=true,default_qty_value=100)
quant=input(title="Trade Quantity",defval=50)
// === LOGIC ===
length = input(type=input.integer,defval=20,minval=1,title="Length")
ratio = input(type=input.integer,defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])
longOnly = input(type=input.bool,defval=false,title="Long Only")
fast = ema(hl2,length)
slow = ema(hl2,length * ratio)
plot(fast,linewidth=2,color=color.orange,title="Fast")
plot(slow,linewidth=2,color=color.blue,title="Slow")
longCond = crossover(fast,slow)
shortCond = crossunder(fast,slow)
//length = input(10, "Length", minval = 2)
src = input(close, "Source")
factor = input(3, "Multiplier", minval = 0.25, step = 0.25)
st(src, atrlen, atrfactor) =>
var max = src
var min = src
var uptrend = true
var stop = 0.0
atrM = nz(atr(atrlen) * atrfactor, tr)
max := max(max, src)
min := min(min, src)
stop := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)
uptrend := src - stop >= 0.0
if uptrend != nz(uptrend[1], true)
max := src
min := src
stop := uptrend ? max - atrM : min + atrM
[stop, uptrend]
[atrStop, uptrend] = st(src, length, factor)
plot(atrStop, "Volatility Stop", style=plot.style_stepline, color= uptrend ? #009688 : #F44336)
bool downtrend = (uptrend != true)
if (longCond )
strategy.entry("BUY",strategy.long)
if (shortCond )
strategy.entry("SELL",strategy.short)
tar=input(defval=30.0,title="TARGET IN POINTS")
sl=input(defval=30.0,title="STOP LOSS IN POINTS")
tar:=tar/syminfo.mintick
sl:=sl/syminfo.mintick
//adding stoploss and target option
strategy.exit("x1", from_entry="BUY", qty = 50, profit = tar,loss = sl)
strategy.exit("x1", from_entry="SELL",qty = 50, profit = tar,loss = sl)
if strategy.position_size>0 and abs(strategy.position_size)==50 and crossunder(close,atrStop)
strategy.exit("x2", from_entry="BUY", qty_percent=100)
if strategy.position_size<0 and abs(strategy.position_size)==50 and crossover(close,atrStop)
strategy.exit("x2", from_entry="SELL",qty_percent=100)
strategy.exit(id="LX4",from_entry="BUY",qty_percent=100,loss=sl)
strategy.exit(id="LX4",from_entry="SELL",qty_percent=100,loss=sl)
试试这个版本:
//@version=4
strategy("SLtrail", overlay = true,calc_on_every_tick=true,default_qty_value=100)
quant=input(title="Trade Quantity",defval=50)
// === LOGIC ===
length = input(type=input.integer,defval=20,minval=1,title="Length")
ratio = input(type=input.integer,defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])
longOnly = input(type=input.bool,defval=false,title="Long Only")
fast = ema(hl2,length)
slow = ema(hl2,length * ratio)
plot(fast,linewidth=2,color=color.orange,title="Fast")
plot(slow,linewidth=2,color=color.blue,title="Slow")
longCond = crossover(fast,slow)
shortCond = crossunder(fast,slow)
//length = input(10, "Length", minval = 2)
src = input(close, "Source")
factor = input(3, "Multiplier", minval = 0.25, step = 0.25)
st(src, atrlen, atrfactor) =>
var max = src
var min = src
var uptrend = true
var stop = 0.0
atrM = nz(atr(atrlen) * atrfactor, tr)
max := max(max, src)
min := min(min, src)
stop := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)
uptrend := src - stop >= 0.0
if uptrend != nz(uptrend[1], true)
max := src
min := src
stop := uptrend ? max - atrM : min + atrM
[stop, uptrend]
[atrStop, uptrend] = st(src, length, factor)
plot(atrStop, "Volatility Stop", style=plot.style_stepline, color= uptrend ? #009688 : #F44336)
bool downtrend = (uptrend != true)
if (longCond )
strategy.entry("BUY",strategy.long)
if (shortCond )
strategy.entry("SELL",strategy.short)
tar=input(defval=30.0,title="TARGET IN POINTS")
sl=input(defval=30.0,title="STOP LOSS IN POINTS")
tar:=tar/syminfo.mintick
sl:=sl/syminfo.mintick
//adding stoploss and target option
strategy.exit("x1", from_entry="BUY", qty = 50, profit = tar,loss = sl)
strategy.exit("x1", from_entry="SELL",qty = 50, profit = tar,loss = sl)
if crossunder(close,atrStop)
strategy.close("BUY", qty_percent=100)
if crossover(close,atrStop)
strategy.close("SELL",qty_percent=100)
strategy.exit(id="LX4",from_entry="BUY",qty_percent=100,loss=sl)
strategy.exit(id="LX4",from_entry="SELL",qty_percent=100,loss=sl)