首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pine编辑器如果有多头头寸,就会失去或TakeProfit

Pine编辑器如果有多头头寸,就会失去或TakeProfit
EN

Stack Overflow用户
提问于 2021-07-22 02:58:37
回答 1查看 64关注 0票数 0

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

下面的代码放入长条目并关闭位置,例如止损。但我不能控制是否有空头多头头寸。它应该检查最后一个长条目是否以止损或获利了结。所以你可以看到,即使没有未平仓的长仓,它也会止损。我尝试使用barssince()函数,但没有成功。

提前谢谢你。

代码语言:javascript
复制
//@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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-23 19:50:13

study相比,strategy更适合您的任务:

代码语言:javascript
复制
//@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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68475123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档