Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Libraries | JSON Library for LLMs | MQL5 Code

New free code from MQL5: indicators, EAs, and scripts for traders.

High-Performance JSON (v3.5.0)

A JSON library designed for LLMs, Autonomous Trading and Ultra-Low Latency.

The Problem

When integrating AI models (GPT-4o, Claude 3.5, DeepSeek) into MetaTrader 5, the community's standard libraries failed on two critical points: Memory Allocation and Serialisation Latency. The excessive use of recursion and temporary strings turned the processing of AI responses (Function Calling) into bottlenecks that froze the terminal. The "pain" of losing ticks while the Garbage Collector cleared strings was what motivated this architecture.

Grid trading done right – try the robust Grid Expert Advisor for controlled risk. Details here.

fast_json architecture

Rewritten from scratch with an obsessive focus on performance:

  1. Zero-Allocation Architecture: Parsing via Tape (contiguous buffer long[] ) and Direct Serialisation in buffer uchar[] . We eliminate intermediaries: the payload goes from the socket straight to the data structure without creating thousands of objects.
  2. Hybrid Numeric Parsing: In v3.4.0, we introduced integer accumulation via long (native ALU) and static Exp10 lookup tables for maximum precision in floating-points.
  3. Iterative State Machine: Goodbye recursion. A linear parser prevents Stack Overflow even in deeply nested JSONs.
  4. SWAR Scanning: SIMD reading (8 bytes per cycle) to skip whitespace and long strings.

Performance

Tests carried out on standard hardware (x64) with a complex payload of 50,000 nodes:

Operation (fast_json) Legacy Lib (JAson) Advantage
Parse 137 ms 1540 ms 11.2x faster
Serialisation 264 ms 568 ms 2.1x faster
Total (Roundtrip) 401 ms 2129 ms 5.3x faster

> Results verifiable via TestJsonBenchmark.mq5 script included in the package.

Image for JSON Library for LLMs


Image for JSON Library for LLMs

Key Features

  • HFT resilience: Deterministic memory allocation. Garbage Collector does not intervene during parse.
  • O(1) Introspection: Check HasKey() keys or Size() arrays instantly, without linear scanning.
  • Type Safety: Strict type access ( GetInt , GetDouble , GetString ). No magic variants that cause silent bugs.
  • Accurate Error Reporting: In case of failure, returns exact Row and Column.

Usage Example: Reading OpenAI Response

#include <fast_json.mqh>

void OnStart() {
    string payload = GetOpenAIResponse(); // JSON massivo
    
    CJson json;
    if(json.Parse(payload)) {
        // Acesso direto performance-critical (Zero-Copy)
        string content = json["choices"][0]["message"]["content"].ToString();
        
        // Exemplo: Extraindo uso de tokens
        if(json.HasKey("usage")) {
            long tokens = json["usage"]["total_tokens"].ToInt();
            Print("Consumo: ", tokens);
        }
    } else {
        // Debug preciso
        int l, c;
        json.GetErrorPos(l, c);
        PrintFormat("Erro JSON na Linha %d, Coluna %d", l, c);
    }
}

Example: Building Request (Optimised Builder)

CJsonBuilder b;
b.Obj()
    .Key("model").Val("gpt-4-turbo")
    .Key("messages").Arr()
        .Obj()
            .Key("role").Val("user")
            .Key("content").Val("Analyze EURUSD H1 trend")
        .EndObj()
    .EndArr()
    .Key("temperature").Val(0.7)
.EndObj();

string body = b.Serialization(); // Serialização ultra-rápida

Developed by Jonathan Pereira as the core infrastructure of the AI-Toolkit framework.

Thanks for reading. Discover premium MetaTrader solutions at RobotFX.

68596

Best MetaTrader Indicators + Profitable Expert Advisors