为什么要策略。
我正在使用版本5,并尝试显示一个标签,该标签显示当前的实时未实现的损失/利润。我做到了,以便以前的同一标签被删除,只有最后一个保留。问题在于,它没有显示当前的活蜡烛,而是在关闭后打印了前一个。我提供了一个用于预览标签打印位置的图像,这是代码的一个示例:
//Colors
ColorLongSL = color.purple
ColorLongTP = color.blue
ColorShortSL = color.orange
ColorShortTP = #ff79b8
//Is there a long or short position open?
longopen = strategy.position_size>0
shortopen = strategy.position_size<0
//Convert current loss or profit to string text
unrealizedL = ('Unrealized Loss -$' + str.tostring(math.round(strategy.openprofit*-1)))
unrealizedP = ('Unrealized Profit $' + str.tostring(math.round(strategy.openprofit)))
//Print Labels for live unrealized loss/profit
//For Long
currentLLoss = label.new(strategy.openprofit<0 and longopen ? bar_index : na, na, unrealizedL, style=label.style_label_up, color=ColorLongSL, textcolor=color.white, size=size.small, yloc=yloc.belowbar)
currentLProfit = label.new(strategy.openprofit>0 and longopen ? bar_index : na, na, unrealizedP, style=label.style_label_down, color=ColorLongTP, textcolor=color.white, size=size.small, yloc=yloc.abovebar)
label.delete(currentLLoss[1])
label.delete(currentLProfit[1])
//For Short
currentSProfit = label.new(strategy.openprofit>0 and shortopen ? bar_index : na, na, unrealizedP, style=label.style_label_up, color=ColorShortTP, textcolor=color.white, size=size.small, yloc=yloc.belowbar)
currentSLoss = label.new(strategy.openprofit<0 and shortopen ? bar_index : na, na, unrealizedL, style=label.style_label_down, color=ColorShortSL, textcolor=color.white, size=size.small, yloc=yloc.abovebar)
label.delete(currentSLoss[1])
label.delete(currentSProfit[1])
I'm using version 5 and trying to display a label that shows the current live unrealized loss/profit. I made it so that previous labels of the same get deleted and only the last one remains printed. The problem is that instead of showing the current live candle, it prints the previous one after close. I provided an image for previewing where the label prints and this is an example of the code:
TradingView Chart Label Preview
//Colors
ColorLongSL = color.purple
ColorLongTP = color.blue
ColorShortSL = color.orange
ColorShortTP = #ff79b8
//Is there a long or short position open?
longopen = strategy.position_size>0
shortopen = strategy.position_size<0
//Convert current loss or profit to string text
unrealizedL = ('Unrealized Loss -
+ str.tostring(math.round(strategy.openprofit*-1)))
unrealizedP = ('Unrealized Profit
+ str.tostring(math.round(strategy.openprofit)))
//Print Labels for live unrealized loss/profit
//For Long
currentLLoss = label.new(strategy.openprofit<0 and longopen ? bar_index : na, na, unrealizedL, style=label.style_label_up, color=ColorLongSL, textcolor=color.white, size=size.small, yloc=yloc.belowbar)
currentLProfit = label.new(strategy.openprofit>0 and longopen ? bar_index : na, na, unrealizedP, style=label.style_label_down, color=ColorLongTP, textcolor=color.white, size=size.small, yloc=yloc.abovebar)
label.delete(currentLLoss[1])
label.delete(currentLProfit[1])
//For Short
currentSProfit = label.new(strategy.openprofit>0 and shortopen ? bar_index : na, na, unrealizedP, style=label.style_label_up, color=ColorShortTP, textcolor=color.white, size=size.small, yloc=yloc.belowbar)
currentSLoss = label.new(strategy.openprofit<0 and shortopen ? bar_index : na, na, unrealizedL, style=label.style_label_down, color=ColorShortSL, textcolor=color.white, size=size.small, yloc=yloc.abovebar)
label.delete(currentSLoss[1])
label.delete(currentSProfit[1])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,在Bars关闭中计算出策略。如果您真的想要实时数据,则应设置
calc_on_every_tick
Strategy()
true
的属性。注意:,而不是一直创建和删除标签,您可以为每个标签都有一个标签,只需使用
label.set_x
,标签更新其位置和文本.set_y
和label.set_text
函数。这将改善您的脚本的性能。By default strategies are calculated on bars' closes. If you really want real-time data, you should set
calc_on_every_tick
property of yourstrategy()
totrue
.Note: Instead of creating and deleting labels all the time, you can have one label for each and just update their position and text using the
label.set_x
,label.set_y
andlabel.set_text
functions. This will improve your script's performance.