MQL4自定义指标开发:如何隐藏指标线的部分线段?
Hey there! I get it—using DRAW_SECTION to draw horizontal breakout lines works, but those unwanted connected segments are definitely annoying. Let’s break down how to fix this, plus some better alternatives depending on your charting platform.
Why DRAW_SECTION Connects Everything
The core issue here is that DRAW_SECTION is designed to connect sequential data points by default. If you’re feeding it a series of prices where you only want segments between specific breakpoints, it’ll bridge the gaps between all points—even the ones you don’t want to show.
Solution 1: Use a Segment-Specific Drawing Function (Best Option)
Most charting platforms have a function that lets you draw individual line segments by specifying start/end bars and prices. This avoids automatic connections entirely.
For example, in Pine Script (TradingView), you’d use line.new() to create isolated horizontal segments:
//@version=5 indicator("Isolated Breakout Lines", overlay=true) // Define your breakout trigger condition breakoutTrigger = ta.crossover(close, ta.highest(close, 20)) // Capture breakout price and bar index when triggered breakoutPrice = valuewhen(breakoutTrigger, close, 0) breakoutBar = valuewhen(breakoutTrigger, bar_index, 0) // Define when the segment should end (e.g., next breakout or fixed period) segmentEndTrigger = ta.crossunder(close, ta.lowest(close, 10)) or bar_index == breakoutBar + 15 segmentEndBar = valuewhen(segmentEndTrigger, bar_index, 0) // Draw the segment only when we have valid start/end points if breakoutTrigger line.new( x1=breakoutBar, y1=breakoutPrice, x2=segmentEndBar, y2=breakoutPrice, color=color.new(color.blue, 0), width=2, style=line.style_solid )
This creates a standalone horizontal line between your breakout bar and the end bar—no extra connections to other segments.
Solution 2: Insert NaN Values to Break DRAW_SECTION
If you need to stick with DRAW_SECTION, you can force it to disconnect segments by inserting NaN (not a number) into your price series whenever you don’t want a line to appear.
Here’s how that might work:
//@version=5 indicator("DRAW_SECTION with Breaks", overlay=true) breakoutTrigger = ta.crossover(close, ta.sma(close, 20)) // Initialize a series to hold breakout prices (or NaN) displayPrice = na // Set displayPrice to breakout price only when active, else NaN if breakoutTrigger displayPrice := close else if bar_index > valuewhen(breakoutTrigger, bar_index, 0) + 10 displayPrice := na // Now DRAW_SECTION will only connect points where displayPrice is not NaN plot(displayPrice, title="Breakout Line", style=plot.style_linebr) // Or use DRAW_SECTION directly if your platform uses that function: // draw_section(bar_index, displayPrice, ...)
The NaN values act as gaps—DRAW_SECTION won’t draw lines between a valid price and a NaN, so your segments stay isolated.
Key Takeaways
- Opt for segment-specific functions (like
line.new()in Pine Script) whenever possible—they give you full control over each line’s start and end, eliminating unwanted connections. - Use
NaNto break lines if you’re stuck withDRAW_SECTION—it’s a simple way to force gaps in the series.
内容的提问来源于stack exchange,提问作者Lorry Laurence mcLarry

