Chart Geometry Toolkit
Trend-line and support/resistance tools built on ad-hoc per-bar distance checks tend to get the edges wrong: "how close did price come to this trend line" measured as distance to the infinite line through its two endpoints overstates how close price actually came once price is past either end of the DRAWN segment, and "do these two trend lines cross" checked by solving for an intersection and trusting it ignores whether that crossing point falls inside the segments that were actually drawn, or only on their unlimited extensions. PointToSegmentDistance() projects the query point onto the line through the segment, clamps the projection parameter to [0,1] so it can never fall past either endpoint, and measures distance to that clamped point - correct right up to and past a segment's ends, not just along its middle. LineIntersection() solves for where two INFINITE lines cross (the natural operation for "where would these trend lines meet if extended"); SegmentIntersection() additionally checks, via the standard four-cross-product orientation test, that the crossing point actually lies on both DRAWN segments before reporting one - the demo shows a concrete case where the two disagree. ConvexHull() computes a genuine convex hull over a price-time point cloud using Andrew's monotone chain, DELIBERATELY chosen over the more commonly taught Graham scan: Graham scan sorts points by polar angle around a pivot, which needs an atan2-style two-argument arctangent, and MQL4 has no MathArctan2 at all (only the single-argument MathArctan, which cannot resolve the quadrant on its own - confirmed elsewhere in this catalogue). Monotone chain sorts by plain (x, y) lexicographic order instead, with no trigonometry anywhere in the algorithm, and produces an identical hull in the same O(n log n). The demo verifies the result is a genuine hull by confirming every original input point tests as inside-or-on it via PointInConvexPolygon() - not just printing the vertex list. Notes and limitations - SGeoPoint.x is deliberately generic - a bar index, or a time value cast to double - as long as it is used consistently across every point passed into a given call. The header does no unit conversion. - SegmentIntersection() reports collinear-overlapping segments (lying exactly on the same line and overlapping in range) as NOT intersecting, rather than trying to report an overlap range - a single point result would be the wrong answer for a genuine overlap, and this case is rare enough for trend-line tools that a clean "no" is preferable to an ambiguous partial answer. - ConvexHull() sorts its points[] argument IN PLACE. Pass a copy if the caller's original point order must be preserved afterward (the demo shows this pattern explicitly). - All comparisons use a small epsilon (1e-9 to 1e-12 depending on the function) rather than exact equality, to tolerate ordinary floating-point rounding in price/time arithmetic - genuinely coincident points or exactly parallel lines are still detected reliably at that tolerance.
Script · MT4 · v1.0 · Free
About Chart Geometry Toolkit
Chart Geometry Toolkit is a script for MetaTrader 4. MQL source code included.
Files in the download: ChartGeometryToolkit.mqh, ChartGeometryToolkit.txt, ChartGeometryToolkitDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Support & Resistance · Trend · MT4 Script · MQL4 Source Code