为什么要策略。

发布于 2025-02-12 14:54:51 字数 2139 浏览 2 评论 0原文

我正在使用版本5,并尝试显示一个标签,该标签显示当前的实时未实现的损失/利润。我做到了,以便以前的同一标签被删除,只有最后一个保留。问题在于,它没有显示当前的活蜡烛,而是在关闭后打印了前一个。我提供了一个用于预览标签打印位置的图像,这是代码的一个示例:

nofollow noreferrer“> tradingview targel图表标签preview <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /a>

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉鱼一梦 2025-02-19 14:54:51

默认情况下,在Bars关闭中计算出策略。如果您真的想要实时数据,则应设置calc_on_every_tick Strategy() true的属性。

注意:,而不是一直创建和删除标签,您可以为每个标签都有一个标签,只需使用label.set_x标签更新其位置和文本.set_ylabel.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 your strategy() to true.

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 and label.set_text functions. This will improve your script's performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文