Convert FIX Message XML

Convert FIX Message XML

In previous tutorial you saw how to convert String to FIX message and Parse FIX message read its actual value. Everything is same from previous example only when your FIX message got build you will have to toXML method to convert it XML format. I will be writing complete program here as well because if somebody directly comes to this page without visiting previous tutorial.

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.

  • 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;
	}
	
}
  • ParseFIXMessageToXML.java:
package com.javahonk;

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

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

public class ParseFIXMessageToXML {

	public static void main(String[] args) throws MessageParseError {
		
		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("Fix Message:--> "+message.toString()+"\n");	
		System.out.println("XML conversion:-->\n");
		
		System.out.println(message.toXML());		

	}

}
  • Output:

Convert FIX Message XML

Leave a Reply

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