Advertisement IC Markets

Logging that slows the thing being logged

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 7533
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Logging that slows the thing being logged

Post by Fairman »

Logging that slows the thing being logged

Good practice says to log everything. In a trading program, logging has a price.

Writing to disk takes time. Formatting strings, especially with timestamps and numbers, takes CPU. Under a burst of ticks, a program that writes a line per tick can fall behind, and the delay lands in the middle of the decision loop.

Synchronous logging blocks the program until the write finishes. If the disk is slow or busy, the program waits.

Better design moves logging off the critical path. Messages are placed in a queue and written by a separate thread. The main loop only pays the cost of adding to memory.

Log volume matters too. Recording every tick of every pair generates gigabytes a day. Logging only events of interest, such as signals, orders, fills, and errors, keeps the files manageable.

Log levels allow detail when debugging and brevity in production.

A test is easy: run the program with logging at full verbosity and at minimal, and compare the decision latency distribution.

Many unexplained latency spikes in home-made bots trace back to a log call.

With verbose logging on, how much does order-sending time grow?
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
FXS HongKong Scalper
Posts: 720
Joined: Sat Oct 24, 2026 10:15 pm

Re: Logging that slows the thing being logged

Post by FXS HongKong Scalper »

Fairman wrote: Under a burst of ticks, a program that writes a line per tick can fall behind, and the delay lands in the middle of the decision loop.
Buffering log messages and writing them in batches, or logging only key events, reduces the impact. For high-frequency systems, asynchronous logging is a common solution. In MQL, writing to a file only on trade events rather than on every tick, and flushing in batches, is usually enough to remove the delay.
FXS Official Team | Hong Kong Session Desk
Fairman
Posts: 7533
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Logging that slows the thing being logged

Post by Fairman »

FXS HongKong Scalper wrote: Thu Nov 19, 2026 11:07 am
Fairman wrote: Under a burst of ticks, a program that writes a line per tick can fall behind, and the delay lands in the middle of the decision loop.
Buffering log messages and writing them in batches, or logging only key events, reduces the impact. For high-frequency systems, asynchronous logging is a common solution. In MQL, writing to a file only on trade events rather than on every tick, and flushing in batches, is usually enough to remove the delay.
Batching helps, but there is one trade-off I would keep in mind: a buffer that has not been written yet is lost if the terminal or the machine dies. Those last few lines before a crash are usually the ones you want most. So I would flush straight away on the events that matter, like an order sent, a fill, a rejection or an error, and only batch the routine detail. That keeps the useful record safe without paying a disk write on every tick.

I also agree that writing only on trade events is usually enough, but it does make one question harder to answer later, which is what the program saw in the seconds before it decided. A small rolling buffer in memory that gets dumped only when something goes wrong is a decent middle ground. You get the context when you need it, and the normal path stays light. Whatever you choose, I would still run the quiet and verbose comparison first, so the choice rests on measured timing rather than a guess.
It’s Fairman :geek:
Amanhasnoname
Posts: 2584
Joined: Fri Oct 30, 2026 9:45 am

Re: Logging that slows the thing being logged

Post by Amanhasnoname »

The rolling in-memory buffer that only gets dumped when something goes wrong is a nice compromise, and it pairs well with flushing immediately on orders, fills and errors. One more detail worth considering is string formatting. Building a long log line with timestamps, prices and account values on every tick costs CPU even if the line is never written, so wrapping detailed log calls in a check of the current log level avoids paying for messages that will be discarded.

In MQL specifically, Print statements also go to the terminal journal, which can become a bottleneck if called heavily during fast markets, so a dedicated file with controlled writes is usually lighter. For Python bots, the standard logging module supports queue-based handlers that move the actual writing to a separate thread, which is a simple way to keep the decision loop clean.

On measuring it, the comparison between quiet and verbose modes is most useful during busy periods rather than across a whole day. Running both configurations around similar news releases and comparing the slowest decision times, not just the averages, shows whether logging is contributing to the occasional spike.
Post Reply