如何实现触及K线实体时停止绘制多头止盈折线?
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
bodyHighandbodyLowto 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 returnnato stop the line right there. - Style Compatibility: Since you're using
plot.style_linebr, thenavalue 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

