【问题标题】:Pine editor Stoploss or TakeProfit if only there is a Long PositionPine 编辑器止损或止盈(如果只有多头头寸)
【发布时间】:2021-07-23 19:50:12
【问题描述】:

我正在尝试一个简单的突破系统,用于较短的时间段,例如 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)

【问题讨论】:

    标签: pine-script


    【解决方案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))
    

    【讨论】:

    • 感谢您的帮助。正如您所建议的,也许将其更改为策略更合乎逻辑。但是,当使用止损或止盈退出头寸时,我想等待新的分形。这个策略和我的学习不等待新的。在 strategy.entry 之后我应该将 enterLong_p 的值更改为 0 吗?
    • 您可以为strategy.entry("long", strategy.long, stop = enterLong_p)添加when参数,并在适当的条件下进入。
    猜你喜欢
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 2022-07-21
    • 1970-01-01
    • 2022-10-25
    相关资源
    最近更新 更多