Class FixOrderEncoder

java.lang.Object
com.quantfinlib.fix.FixOrderEncoder

public final class FixOrderEncoder extends Object
Garbage-free FIX 4.4 NewOrderSingle encoder — the hot-lane counterpart of the String-based NewOrderSingle/FixMessage codec, for venues that only speak FIX (where the sbe binary adapters aren't an option, order entry IS the FIX edge, and per-order String building would put allocation back on the measured path).

Techniques, all standard in commercial garbage-free FIX engines:

  • One reusable byte buffer — the body is written at a fixed offset, then the 8=FIX.4.4|9=len| prefix is written backwards in front of it (no copy, no second pass), and 10=checksum| appended;
  • ASCII digit writers — longs and scaled decimal prices are rendered digit-by-digit; no Long.toString, no format calls;
  • Prices as scaled longsmantissa × 10^-decimals (e.g. 1.08505 = mantissa 108505, decimals 5), the venue-grade representation; doubles never touch the encoder;
  • Cached timestamp date part — the yyyyMMdd- prefix is recomputed only when the UTC day changes (once per day, off the per-order path); intraday time renders from millis by division;
  • Symbols pre-registered — dense symbol id → ASCII bytes at setup; the hot path never encodes a String.

Correctness is pinned by round-trip tests: every encoded message is parsed back by the validated FixMessage.parse(byte[]) (which checks BodyLength and CheckSum) and field-compared. Zero allocation per encode is asserted with the allocation-counter test, like every hot-path claim in this library. Single-threaded: one encoder per session, like the session itself.

  • Constructor Summary

    Constructors
    Constructor
    Description
    FixOrderEncoder(String senderCompId, String targetCompId, int maxSymbols, int bufferSize)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    The reusable buffer holding the last encoded message.
    int
    encodeLimit(long msgSeqNum, long clOrdId, int symbolId, Side side, long quantity, long priceMantissa, int priceDecimals, long epochMillis)
    Encodes a limit NewOrderSingle into the reusable buffer.
    int
    encodeMarket(long msgSeqNum, long clOrdId, int symbolId, Side side, long quantity, long epochMillis)
    Market NewOrderSingle (40=1, no price tag).
    int
    Length of the last message.
    int
    Start offset of the last message within buffer().
    registerSymbol(int symbolId, String symbol)
    Registers a tradeable symbol (cold path, before trading).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FixOrderEncoder

      public FixOrderEncoder(String senderCompId, String targetCompId, int maxSymbols, int bufferSize)
      Parameters:
      senderCompId - session sender (tag 49)
      targetCompId - session target (tag 56)
      maxSymbols - dense symbol-id capacity
      bufferSize - message buffer (512 is ample for a NewOrderSingle)
  • Method Details

    • registerSymbol

      public FixOrderEncoder registerSymbol(int symbolId, String symbol)
      Registers a tradeable symbol (cold path, before trading).
    • encodeLimit

      public int encodeLimit(long msgSeqNum, long clOrdId, int symbolId, Side side, long quantity, long priceMantissa, int priceDecimals, long epochMillis)
      Encodes a limit NewOrderSingle into the reusable buffer. Zero allocation. The encoded message occupies [offset(), offset() + length()) of buffer() until the next encode.
      Parameters:
      msgSeqNum - session sequence number (tag 34)
      clOrdId - client order id, numeric (tag 11)
      priceMantissa - price × 10^priceDecimals as a long
      priceDecimals - decimal places (5 for EURUSD, 3 for USDJPY)
      epochMillis - UTC time for tags 52/60
      Returns:
      message length in bytes
    • encodeMarket

      public int encodeMarket(long msgSeqNum, long clOrdId, int symbolId, Side side, long quantity, long epochMillis)
      Market NewOrderSingle (40=1, no price tag). Zero allocation.
    • buffer

      public byte[] buffer()
      The reusable buffer holding the last encoded message.
    • offset

      public int offset()
      Start offset of the last message within buffer().
    • length

      public int length()
      Length of the last message.