Convert String FIX Message

Convert String FIX Message

If you are using FIX protocol to send and receive messages where you need to build FIX message from String then you could use below sample program. I have used API from QuickFIX/J which is open source and very famous in the insustry.

Note: You could directly downlaed QuickFix jar from its web site here. To make this example work right away I have used quickfixj-all 1.6.0 version where you could convert your string message to any FIX version and for this example we are using FIX 4.2.

  • Project structure:

Convert String FIX Message

As you see above this is complete class where ENUM has been used for constant where you could add remove message type easily.

  • FixMessageTypeFields.java:
package com.javahonk;

public enum FixMessageTypeFields {

	MSGTYPE(35),
	QTY(32),
	PRICE(31),
	EXECTRANSTYPE(20),
	EXECTYPE(150),
	EXECID(17),
	ORDERID(37),
	SIDE(54),
	UNDERLIERSTOCKSYMBOL(55),
	TRANSACTTIME(60),
	SENDINGTIME(52),
	FUTSETTDATE(64),
	SECURIRYTYPE(167),
	SEND(10335),
	TRADERID(11107),
	LEGALENTITIY(111320),
	ACCOUNTNAME(111321),
	
	//Assigned values	
	EXECTYPEVALUE(0),
	EXECTRANSTYPEVALUE(0),	
	MSGTYPEVALUE("D"),
	SECURITYTYPEVALUE("CS"),
	BUY("BUY"),
	SELL("SELL"),
	SENDVALUE("Y");
	
	private int value = 0;
	private String assignedValue = null;
	
	FixMessageTypeFields(int value) {
		this.value = value;
	}
	
	FixMessageTypeFields(String messageValue) {
		this.assignedValue = messageValue;
	}

	public int getValue() {
		return value;
	}

	public String getAssignedValue() {
		return assignedValue;
	}
	
}
  • ConvertStringToFIXMessage.java:
package com.javahonk;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import quickfix.DoubleField;
import quickfix.IntField;
import quickfix.StringField;
import quickfix.fix42.Message;



public class ConvertStringToFIXMessage {

	public static void main(String[] args) {
		
		Message message = new Message();
		
		StringField msgType = new StringField(FixMessageTypeFields.MSGTYPE.getValue(), FixMessageTypeFields.MSGTYPEVALUE.getAssignedValue());
		DoubleField qty = new DoubleField(FixMessageTypeFields.QTY.getValue(), 100.00);
		DoubleField price = new DoubleField(FixMessageTypeFields.PRICE.getValue(), 59.99);
		IntField execTransType = new IntField(FixMessageTypeFields.EXECTRANSTYPE.getValue(), FixMessageTypeFields.EXECTRANSTYPEVALUE.getValue());
		IntField execType = new IntField(FixMessageTypeFields.EXECTYPE.getValue(), FixMessageTypeFields.EXECTYPEVALUE.getValue());
		StringField orderID = new StringField(FixMessageTypeFields.ORDERID.getValue(), "123456789");
		BigDecimal origQuantity = BigDecimal.valueOf(100.00);
		String sideTemp = origQuantity.compareTo(BigDecimal.valueOf(0.0)) < 0 ? FixMessageTypeFields.BUY.getAssignedValue() : FixMessageTypeFields.SELL.getAssignedValue();
		StringField side = new StringField(FixMessageTypeFields.SIDE.getValue(), sideTemp);
		StringField underlierStockSymbol = new StringField(FixMessageTypeFields.UNDERLIERSTOCKSYMBOL.getValue(), ".SPX");
		StringField transactTime = new StringField(FixMessageTypeFields.TRANSACTTIME.getValue(), LocalDateTime.now().toString());
		StringField sendingTime = new StringField(FixMessageTypeFields.SENDINGTIME.getValue(), LocalDateTime.now().toString());
		StringField futSettDate = new StringField(FixMessageTypeFields.FUTSETTDATE.getValue(), LocalDateTime.now().plusDays(2).toString());
		StringField securityType  = new StringField(FixMessageTypeFields.SECURIRYTYPE.getValue(), FixMessageTypeFields.SECURITYTYPEVALUE.getAssignedValue());
		StringField send  = new StringField(FixMessageTypeFields.SEND.getValue(), FixMessageTypeFields.SENDVALUE.getAssignedValue());
		StringField traderID  = new StringField(FixMessageTypeFields.TRADERID.getValue(), "Java Honk");
		StringField legalEntity  = new StringField(FixMessageTypeFields.LEGALENTITIY.getValue(), "Java Honk");
		StringField accountName  = new StringField(FixMessageTypeFields.ACCOUNTNAME.getValue(), "123456");
		StringField execID  = new StringField(FixMessageTypeFields.EXECID.getValue(), "123654987");
		
		message.setField(msgType);
		message.setField(qty);
		message.setField(price);
		message.setField(execTransType);
		message.setField(execType);
		message.setField(orderID);
		message.setField(side);
		message.setField(underlierStockSymbol);
		message.setField(transactTime);
		message.setField(sendingTime);
		message.setField(futSettDate);
		message.setField(securityType);
		message.setField(send);
		message.setField(traderID);
		message.setField(legalEntity);
		message.setField(accountName);
		message.setField(execID);
		
		System.out.println(message.toString());

	}

}
  • Output:

Convert String FIX Message

Leave a Reply

Your email address will not be published. Required fields are marked *