Skip to content

MtApi5: OnChartEvent forwarding and EventChartCustom#306

Open
biohazardxxx wants to merge 6 commits into
vdemydiuk:devfrom
biohazardxxx:feat/mql5-chart-event
Open

MtApi5: OnChartEvent forwarding and EventChartCustom#306
biohazardxxx wants to merge 6 commits into
vdemydiuk:devfrom
biohazardxxx:feat/mql5-chart-event

Conversation

@biohazardxxx

Copy link
Copy Markdown

Summary

Forwards the MQL5 OnChartEvent handler to the .NET client and exposes the native EventChartCustom() function. Chart interaction events (clicks, key presses, object edits) and custom events were previously invisible to MtApi5 applications; this adds both directions end-to-end, so a .NET client can send a custom event to a chart and receive it (or any built-in chart event) back.

New API surface on MtApi5Client:

  • public event EventHandler<Mt5ChartEventArgs>? OnChartEvent — raised for every chart event forwarded by the EA. Mt5ChartEventArgs carries int ExpertHandle, int EventId, long Lparam, double Dparam, string Sparam.
  • public bool EventChartCustom(long chartId, ushort customEventId, long lparam, double dparam, string sparam) — wraps the native MQL5 function; the event is delivered back through OnChartEvent with EventId = CHARTEVENT_CUSTOM (1000) + customEventId (documented in the XML doc and the mq5 comments).

Implementation notes

  • Command id 385 (Mt5CommandType.EventChartCustom) with matching ADD_EXECUTOR(385, EventChartCustom) in the EA; event type 6 (Mt5EventTypes.OnChartEvent / ON_CHART_EVENT).
  • The event DTO is MtApi5/MtProtocol/OnChartEvent.cs (internal class OnChartEvent), following the existing OnBookEvent naming pattern — the internal DTO and the same-named public event coexist exactly like the existing OnBookEvent pair.
  • The event payload carries exactly {EventId, Lparam, Dparam, Sparam}. ExpertHandle reaches C# via the event transport parameter and is set on Mt5ChartEventArgs from there (OnLastTimeBarEvent duplicates it in the payload but never reads it; that redundancy was not replicated).
  • Added input bool Enable_OnChartEvent = true to the EA's "Disable Events" input group, mirroring every other forwarded event's toggle, so chatty chart events can be muted.
  • CustomEventId is sent as int in JSON and cast to ushort in the EA; the C# signature uses ushort to match the native function.
  • JSONString::toString() in mq5/json.mqh performs no escaping (while the parser does unescape on read), so sparam is escaped manually at the OnChartEvent call site (backslash first, then quote, \n, \r, \t) before serialization — mirroring the existing mq4 call-site workaround pattern. Without this, chart events whose Sparam contains a quote would be silently dropped and backslash sequences would corrupt.
  • No existing behavior is modified (two adjacent EA input lines lost trailing whitespace as edit collateral).
  • TestClient — an EventChartCustom test action plus OnChartEvent logging for manual exercising.
  • A post-implementation review traced the feature end-to-end (command/event id match between C# and the EA, case-sensitive JSON key match in both directions, ushort/int/long marshaling, handler signatures) and found no wiring defects; the Sparam escaping fix above was the one finding it produced.

Testing

  • MtApi5 and MtApi5TestClient build clean (Release).
  • mq5/MtApi5.mq5 compiles via MetaEditor with "Result: 0 errors"; the recompiled mq5/MtApi5.ex5 is committed.
  • Not runtime-tested against a live MT5 terminal — verification was build/compile-level plus the code trace above; no live round-trip was run.

🤖 Generated with Claude Code

biohazardxxx and others added 6 commits July 3, 2026 23:57
… 385

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ogging

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Escape sparam in OnChartEvent before serialization: JSONString::toString()
in mq5/json.mqh performs no escaping, so an sparam containing a double
quote produced malformed JSON (event silently dropped in the C# client)
and backslashes were corrupted on deserialization. Escape backslash first,
then quote and control characters, mirroring the mq4 call-site pattern.
Recompiled MtApi5.ex5.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant