缠论笔结构简洁版
分型连成笔线,标顶底箭头,只给最近几个转折画短支撑阻力段
MT5 真机运行截图 · 服务器把编译好的 EX5 挂到黄金 H1 图表上截的,你装上去看到的就是这个样子
#property copyright "XAUOF 指标工坊"
#property version "2.0"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
//--- 笔线:DRAW_SECTION 只占 1 个缓冲区,会把相邻的非空值直接连成折线
#property indicator_type1 DRAW_SECTION
#property indicator_color1 clrCyan
#property indicator_width1 2
#property indicator_label1 "笔"
//--- 顶分型箭头
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrOrangeRed
#property indicator_width2 2
#property indicator_label2 "顶分型"
//--- 底分型箭头
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrDeepSkyBlue
#property indicator_width3 2
#property indicator_label3 "底分型"
//============================ 可调参数 ============================
input int FractalBars = 5; // 分型识别宽度:左右各比较几根K线。越大转折越少越干净,日线推荐5,M15推荐3
input int MinMovePoints = 300; // 最小折返点数:小于这个幅度的反向转折直接忽略。黄金推荐300-800,外汇推荐300
input int CalcBars = 600; // 只计算最近多少根K线,0=全部。设小一点图更清爽、速度更快
input bool ShowArrows = true; // 是否显示顶底分型箭头
input int ArrowGapPoints = 0; // 箭头与K线的间距(点)。0=自动按近期波幅计算
input int KeyLevelCount = 3; // 只给最近几个转折点画水平线。0=完全不画(这是上一版满屏横线的开关)
input int SegmentBars = 24; // 水平线长度(横跨几根K线)。不再用贯穿全图的HLINE
input bool ShowPriceLabel = true; // 是否在水平线右端标价格数字
已显示前 35 行,共 327 行。完整源码仅对 xauusd.team VIP 开放查看与复制。
开通 VIP 查看完整源码作品说明
The user's right that it's cluttered — the horizontal lines are from OBJ_HLINE being drawn at every fractal point, and since OBJ_HLINE spans the full width of the chart infinitely, dozens of fractals means dozens of full-width lines crossing everything. I need to rework the code to fix this clutter issue.
I should switch to OBJ_TREND with short segments matching the reference chart, only draw for the most recent N pivots, limit labels too, and stop redrawing objects inefficiently every calculation. I also spot a real bug: the extremePoints array keeps growing on every recalculation instead of resetting, and there's a buffer mismatch since DRAW_ZIGZAG actually requires 2 indicator buffers but my SetIndexBuffer assignments didn't account for that offset correctly.
A cleaner design: use DRAW_SECTION for the zigzag line since it only needs a single buffer and connects non-empty values naturally, then separate plots for top and bottom fractal arrows — three plots total instead of four. I'll draw short OBJ_TREND segments and price labels only at the last N pivots, and just recompute the whole zigzag from scratch each tick rather than trying to do incremental updates, since that's simpler and safer.
Now I'm limiting the recalculation to a configurable window instead of the full chart history to keep it fast, sweeping through that window to find fractals and building a pivot list that enforces alternating tops and bottoms, applying an amplitude filter, and keeping only the most extreme pivot when duplicates occur.
I plan to draw the zigzag buffer and arrows only at pivot bars, limit horizontal segment drawing to the last few pivots with price labels, and clean up old chart objects before redrawing to avoid clutter, only triggering recalculation on a new bar to reduce churn. For the fractal test itself I'm weighing strict inequality versus allowing ties on one side to avoid missing valid pivots.
I need to compute the future bar's time when the segment extends past the available bars, extrapolating using the period length if the target index falls beyond rates_total. I also need to make sure the fractal detection loop only considers bars that have enough neighbors on both sides, so it can't start too close to the array edges.
Now I'm setting up the indicator's properties — buffers for the pen (bi) lines, top fractal arrows, and bottom fractal arrows with their respective colors and labels.
Then I'm defining the input parameters: fractal comparison bars, minimum reversal points for gold, calculation window, whether to show arrows, and settings for drawing key level horizontal lines.
I'm setting up global arrays to track pivot points and writing functions to detect pivots, draw key levels, and clean up chart objects. I'm handling the empty-value plotting so segments skip gaps properly, using distinct arrow codes for top and bottom fractals, and deciding when the indicator needs to recalculate versus reuse cached results.
I'm also working out the arrow offset distance from price, considering a point-based gap input that scales reasonably for gold but might need adjustment for other instruments like stocks.
For the auto-offset behavior, I'm planning to default to computing an average high-low range over the last 20 bars when the input is set to zero. On a full recalculation I'll reset the entire buffer to EMPTY_VALUE to avoid stale drawings, and I'll clear all chart objects using a common name prefix so cleanup stays simple.
Now I'm structuring the response: a short explanation of why the chart got cluttered with horizontal lines and labels (each fractal point was drawing a full-width line plus a text label), followed by the corrected code, then usage notes.
I also realized the earlier version had a buffer misalignment bug—DRAW_ZIGZAG needs two buffers but I'd only allocated one, so I'm switching to DRAW_SECTION which only needs a single buffer. Writing the pivot detection function...
Writing pivot-merging logic...
I'll skip that extra edge-case guard for simplicity, since it risks deleting legitimate pivots — better to keep the logic simple and documented. Now I'm writing the loop that fills the indicator buffers from the pivot arrays.
Filling arrow buffers...
For key levels, I'm writing a function that clears old "CLN_" objects and draws horizontal lines for the most recent pivots up to KeyLevelCount.
Still writing trend line creation...
Adding price label text objects...
Now I'm writing a function to compute an automatic arrow gap based on recent price range, falling back to a fixed point value if a manual gap setting is provided.
Then I'm handling initialization validation, cleanup of chart objects on deinit, and figuring out the starting bar index for calculations when limiting to a fixed number of bars.
For the recalculation approach, doing a full recalc each new bar is justified for zigzag structures despite the extra cost, since it keeps the logic simple and correct without needing prev_calculated tricks. Now I need to draft the title (缠论笔结构自动画线, staying within character limits) and a concise Chinese description under 200 words explaining what the horizontal lines represent.
那些横线是我上一版给每一个顶/底分型都画了一条 OBJ_HLINE。MT5 的 HLINE 是贯穿整张图的无限长水平线,几十个分型就是几十条铺满屏幕的横线,再加上每个分型都挂中文标签,必然糊成一片。
顺带修掉一个真实缺陷:上一版 plot1 用 DRAW_ZIGZAG,它要占 2 个缓冲区,而我按 1 个分配,导致缓冲区和 plot 序号整体错位。这版改用 DRAW_SECTION(只占 1 个缓冲区)。
这一版的做法:只留青色笔线 + 顶底箭头,水平线改成短线段、且只给最近 3 个转折点画,其余全部不画。
使用说明
安装:存为 ChanLun_Pen_Clean.mq5 放进 MQL5/Indicators,F7 编译,拖到图上。上一版留下的横线用图表右键「对象列表」全选删除,或直接把旧指标从图上移除。
嫌乱就调这三个参数
KeyLevelCount:满屏横线的总开关。设0就一条水平线都不画,只剩笔线和箭头,最清爽;默认3只给最近三个转折画短段。MinMovePoints:笔线锯齿多就往大调。黄金日线 500-800、M15 200-300,改完立即生效。FractalBars:转折点密度。调到 7-8 只留大级别转折,调到 3 抓小波段。
CalcBars 控制往左算多少根,默认 600。往前拉历史看不到线就把它调大或设 0。
怎么看
青色折线是笔,连接已确认的顶底;橙红下箭头是顶分型,天蓝上箭头是底分型;短虚线段是最近几个转折的支撑阻力位,右端标着价格。
注意
分型天生滞后 FractalBars 根K线才能确认,所以最右侧那段笔是未完成的,箭头不会出现在当前K线上,这不是 bug。指标只在每根新K线收盘后重算一次,盘中不会跳动。这版只画结构,不判断买卖点,也不下单。
AI 解读根据源码逐行分析写成,供参考,以实际运行为准
这是什么
这是一个 MT5 图表指标,把缠论里的「笔」画出来。它自动找顶分型和底分型,按顶底交替连成青色折线,转折处标箭头,并只给最近几个转折画一小段支撑阻力线。解决的问题是:手动数分型、画笔太累,而很多同类指标满屏横线看不清——这一版把横线改成了短线段,图面干净。
逻辑原理
计算分三步。第一步找分型:某根K线的最高价同时高于左右各 FractalBars 根K线,就是顶分型;最低价同理是底分型。第二步整理成笔:连续同向的分型只留最极端那个(顶取最高、底取最低);反向转折必须达到 MinMovePoints 点数才被承认,幅度不够就当噪音丢掉——这是横盘不出小锯齿的关键。第三步画图:转折点写进缓冲区,DRAW_SECTION 自动把相邻转折连成折线;顶画橙红向下箭头,底画天蓝向上箭头;最近 KeyLevelCount 个转折处画一条虚线短线段(不向右延伸),右端可标价格数字。
注意重算机制:每根新K线出现时,整个计算窗口从头重算一遍。所以最近一笔的转折位置可能随新K线变化,这是笔线算法的固有特性,不是 bug。
参数说明
- FractalBars(5):分型识别宽度,左右各比较几根K线。越大转折越少越干净。注释建议日线 5、M15 用 3。
- MinMovePoints(300):最小折返点数,小于这个幅度的反向转折直接忽略。注释建议黄金 300-800、外汇 300。
- CalcBars(600):只计算最近多少根K线,0 为全部。设小一点图更清爽、速度更快。
- ShowArrows(true):是否显示顶底分型箭头。
- ArrowGapPoints(0):箭头与K线的间距(点)。0 表示自动按近 20 根平均振幅的 0.6 倍计算。
- KeyLevelCount(3):只给最近几个转折点画水平线段。0 为完全不画。
- SegmentBars(24):水平线段长度,横跨几根K线。
- ShowPriceLabel(true):是否在线段右端标价格数字。
怎么用
把 .ex5 文件放进 MT5 的 MQL5/Indicators 目录,刷新导航器,拖到图表上即可。建议挂黄金或主流外汇品种,先在当前周期看默认参数效果。看图方式:青色折线就是笔,箭头是分型确认点,虚线短线段是最近转折附近的参考支撑阻力位。卸载指标时会自动清掉自己画的对象,不留垃圾。
适用场景
适合看结构、找转折、配合支撑阻力做计划的交易者,黄金和外汇都行,周期上注释里给了日线和 M15 的参数建议。震荡行情里 MinMovePoints 能滤掉小锯齿,但参数太小仍会出很多碎笔。不适合想要固定不重绘信号、或直接拿它当进出场指令的人——它只是结构标注工具,不产生交易信号。
风险提示
一,会重绘:新K线可能改写最近一笔,箭头和线段位置会变,别把刚出现的箭头当确认信号,分型要等右侧 FractalBars 根K线走完才成立。二,滞后:分型确认本身就需要右侧若干根K线,转折确认时价格已经走了一段。三,参数敏感:MinMovePoints 换品种、换周期必须重调,否则要么满屏碎笔要么漏掉大转折。四,CalcBars 设太小可能截断笔结构,窗口边缘的笔不完整。
常见问题
- 这个指标会重绘吗
- 会。代码里每根新K线出现时会在计算窗口内整体重算笔结构,所以最近一笔的转折点可能随新K线变化,属于这类笔线算法的固有特性。
- 可以用在 XAUUSD 以外的品种吗
- 可以,代码没有限制品种。但 MinMovePoints 是按点数过滤噪音的,换品种后需要自行调整,注释建议黄金 300-800、外汇 300。
- MT4 能用吗
- 不能,这是 MQL5 指标,用了 MT5 特有的绘图缓冲区机制,需要 MT5 平台。
- 参数默认值适合什么周期
- 代码注释建议 FractalBars 日线用 5、M15 用 3,MinMovePoints 黄金 300-800。其他周期需自行测试。
- 为什么图上没有水平线
- 检查 KeyLevelCount 是否为 0,为 0 时完全不画水平线段;另外它只给最近 KeyLevelCount 个转折画短线段,不是全图横线。
Chan Pen Structure Indicator for MT5
A lightweight MT5 indicator that detects top and bottom fractals, connects them into Chan-theory pen (stroke) lines, and marks pivots with arrows. It draws short support/resistance segments only at the most recent pivots to keep the chart clean. All parameters are adjustable.
A short source preview is shown above for visitors; full source, copy and compiled EX5 files are available to VIP members.
评论
还没有人评论,来抢个沙发。