CBit Buffer Class - Data Serialization in MQ L5
Info
The CBit Buffer Class - Data Serialization in MQ L5 is a Library for MetaTrader 5 that cbitbuffer class - bit-level data serialization in mql5 the cbitbuffer class provides a robust foundation for bit-level data serialization in mql5, offering fine-grained control over data storage and retrieval. The class includes support for various data types, including variable-length integers (VLQ with ZigZag encoding), and serialization of strings and structures, which are excellent for optimizing space.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Libraries folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use CBit Buffer Class - Data Serialization in MQ L5
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
CBitBuffer Class - Bit-Level Data Serialization in MQL5 The CBitBuffer class provides a robust foundation for bit-level data serialization in MQL5, offering fine-grained control over data storage and retrieval.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.
Description & Settings
Related: The class to create the ring buffer - another powerful library for MetaTrader 5 traders.
CBitBuffer Class - Bit-Level Data Serialization in MQL5The CBitBuffer class provides a robust foundation for bit-level data serialization in MQL5, offering fine-grained control over data storage and retrieval. The class includes support for various data types, including variable-length integers (VLQ with ZigZag encoding), and serialization of strings and structures, which are excellent for optimizing space. The class employs optimizations like internal buffering and exponential array growth to enhance performance and provides a comprehensive error handling system. It's particularly useful for network communication or file storage where minimizing data size is crucial (e.g., compression of tick data).
Also recommended: The simplified class of the CArray Ring256 ring buffer - similar library with strong performance on MetaTrader 5.
Key Features:Bit-level Operations
: Allows writing and reading data bit by bit, or in specified bit lengths up to 64 bits.
Data Type Support
: Includes methods for bool, char, uchar, short, ushort, int, uint, long, ulong, datetime, float, double, string, and struct.
Variable-Length Integers (VLQ)
: Implements VLQ encoding for int, uint, long, and ulong values, which can significantly save space for frequently small integer values.
Error Handling
: Provides an ENUM_BIT_BUFFER_ERROR enum and GetLastError()/GetLastErrorString() methods for robust error management.
Buffer Management
: Offers functions to clear, finalize, set raw buffer content, save to file, and load from file.
Internal Buffering
: Uses internal 64-bit buffers (m_writeBufferInternal, m_readBufferInternal) to optimize bit-level operations by accumulating bits before writing to or reading from the main ulong[] array.
The complete test example "
CBitBuffer_Test.mq5"
provided above is the best way to detect errors and verify the class's capabilities.
The header file "
TicksShort.mqh
" provides an alternative implementation of '' tick compression library by fxsaber, utilizing VLQ encoding from 'CBitBuffer' class.
Updates:
2025.07.21 - v.1.01 :
The CBitBuffer class actively prevents illegal attempts to write after a read operation has begun, which helps in maintaining data integrity.
Users receive a specific error (BIT_BUFFER_MIXED_OPERATION_ERROR) when they attempt to mix operations.
2025.07.22 - v.1.02 :
Changed design of the class to allow the mixed read/write operations, by correct handling of partial flushes/refills, which helps in maintaining data integrity when switching between read and write modes.
Removed the variable (m_operationMode) and the error code (BIT_BUFFER_MIXED_OPERATION_ERROR).
Cleaned the code comments for better clarity of intent.
Updated examples in "CBitBuffer_Test.mq5" to cover more test cases.
You may also like: CPlot Manager (Auto Buffer and Plot Manager) - excellent alternative for library users on MetaTrader 5.
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.