如果只有多头头寸,Pine 编辑器止损或止盈

时间:2021-07-21 18:58:36

标签: pine-script

我正在尝试一个简单的突破系统,用于较短的时间段,例如 1m、5m、15。 当高价穿过最后一个分形的高点时,这是一个多头入场信号。 然后,如果高点超过高价的 0.1%,这就是获利。 但如果低点低于高点的 0.1%,这就是止损。

下面的代码放置多头入场并关闭头寸,例如止损。但我无法控制是否有任何未平仓的多头头寸。它应该检查最后一个多头入场是否以止损或止盈关闭。如您所见,即使没有打开多头入场,它也会设置止损。我尝试了 barsince() 函数,但没有成功。

提前致谢。

//@version=4
study("Williams Fractals Breakout",shorttitle="Fractals", overlay=true)

// Define "n" as the number of periods and keep a minimum value of 2 for error handling.
n = input(title="Periods", defval=2, minval=2)


topFractal = (high[n-2] < high[n]) and (high[n-1] < high[n]) and (high[n+1] < high[n]) and (high[n+2] < high[n]) //dnFractal
bottomFractal = (low[n-2] > low[n]) and (low[n-1] > low[n]) and (low[n+1] > low[n]) and (low[n+2] > low[n])


// Plot the fractals as shapes on the chart.
plotshape(topFractal, style=shape.triangleup, location=location.abovebar, offset=-2, color=color.green, transp=0)
plotshape(bottomFractal, style=shape.triangledown, location=location.belowbar, offset=-2, color=color.red, transp=0)  


enterLong_p = valuewhen(topFractal, high[n], 0)
exitLongTP_p = valuewhen(topFractal, enterLong_p * 1.001, 0)
exitLongSL_p = valuewhen(topFractal, enterLong_p * (1-0.001), 0)
enterLong= high == enterLong_p or crossover(high, enterLong_p)
exitLongTP= high == exitLongTP_p or crossover(high, exitLongTP_p)
exitLongSL= low == exitLongSL_p or crossunder(low, exitLongSL_p)
O1= barssince(enterLong)
O2= barssince(exitLongTP)
O3= barssince(exitLongSL)

    
plotshape(enterLong and O3<O1[1] ? high : na, title="enterLong", text="Long Entry", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
//plotshape(exitLongTP and (O3<O1[1] or O1<O2[1]) ? high+1 : na, title="exitLongTP", text="Exit Long TP", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.blue, textcolor=color.white, transp=0)
plotshape(exitLongSL and O1<O3[1] ? high+2 : na, title="exitLongSL", text="Exit Long SL", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

enter image description here

1 个答案:

答案 0 :(得分:1)

strategystudy 更适合您的任务:

//@version=4
strategy("Williams Fractals Breakout",shorttitle="Fractals", overlay=true, margin_long=100, margin_short=100)

n = input(title="Periods", defval=2, minval=2)
topFractal = (high[n-2] < high[n]) and (high[n-1] < high[n]) and (high[n+1] < high[n]) and (high[n+2] < high[n]) //dnFractal
bottomFractal = (low[n-2] > low[n]) and (low[n-1] > low[n]) and (low[n+1] > low[n]) and (low[n+2] > low[n])


// Plot the fractals as shapes on the chart.
plotshape(topFractal, style=shape.triangleup, location=location.abovebar, offset=-2, color=color.green, transp=0)
plotshape(bottomFractal, style=shape.triangledown, location=location.belowbar, offset=-2, color=color.red, transp=0)  

enterLong_p = valuewhen(topFractal, high[n], 0)

// added strategy logic here
strategy.entry("long", strategy.long, stop = enterLong_p)
percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)
strategy.exit("x", loss = percentAsPoints(0.1), profit = percentAsPoints(0.1))