You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现触及K线实体时停止绘制多头止盈折线?

Fixing Take Profit Line to Stop at K烛 Entity

Got it, let's get that take profit line to stop right when it touches the K烛 body instead of cutting straight through it. Here's how to adjust your existing code:

Core Idea

The key is to add a check that stops drawing the line whenever the longExitPrice falls within the current candle's body range. We'll use na to break the line exactly at that point—this works perfectly with plot.style_linebr since it automatically stops lines at na values.

Modified Code

// Define the upper and lower bounds of the candle body
bodyHigh = ta.max(open, close)
bodyLow = ta.min(open, close)

// Updated plot statement with the new stop condition
plot(enableTP and dir == 1 and not (longExitPrice >= bodyLow and longExitPrice <= bodyHigh) ? longExitPrice : na, "Long Take Profit", linewidth=2, color=TPcolor, style=plot.style_linebr)

Breakdown of Changes

  • Candle Body Bounds: First we calculate bodyHigh and bodyLow to get the exact range of the current candle's body, regardless of whether it's a bullish or bearish candle.
  • Stop Condition: The new not (longExitPrice >= bodyLow and longExitPrice <= bodyHigh) part ensures we only draw the line when the take profit price is outside the current candle's body. If it falls inside (meaning the candle body has touched the take profit price), we return na to stop the line right there.
  • Style Compatibility: Since you're using plot.style_linebr, the na value will automatically break the line, so it won't extend into or through the candle body.

Quick Tweak Option

If you want the line to stop exactly at the edge of the candle body (instead of just inside it), you can adjust the condition based on your take profit direction. For example, if your long take profit sits above the entry price, you could use longExitPrice > bodyHigh instead of the range check—tweak it to match your specific strategy!

内容的提问来源于stack exchange,提问作者Hannes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 10:47:37