【问题标题】:Trailing STOPLOSS not triggering in tradingview (pine script)追踪止损未在交易视图中触发(松树脚本)
【发布时间】:2021-09-22 15:24:50
【问题描述】:

我是 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)

【问题讨论】:

  • 整个策略代码可以帮助找到问题的原因。
  • 添加完整代码

标签: pine-script algorithmic-trading tradingview-api


【解决方案1】:

试试这个版本:

//@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)

【讨论】:

  • 谢谢你太棒了,也只是想知道,如果初始止损达到它在 2 笔交易中退出所有 100% 头寸,有没有办法在一个订单中退出它。我尝试了下面的代码,但无法做到。 //添加止损和目标选项strategy.exit("x1", from_entry="BUY", qty = 50, profit = tar) strategy.exit("x2", from_entry="BUY", qty = 100, loss = sl) strategy.exit("x3", from_entry="SELL",qty = 50, profit = tar) strategy.exit("x4", from_entry="SELL",qty = 100, loss = sl)
猜你喜欢
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 2022-11-10
  • 2020-10-19
  • 2022-11-10
  • 2022-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多