首页 > 编程语言 >FIX tutorial in Java with QuickFIX/j simple example

FIX tutorial in Java with QuickFIX/j simple example

时间:2023-07-13 14:01:14浏览次数:56  
标签:00 Java simple CLIENT1 FIX FIX.4 FixServer import quickfix


http://www.tuicool.com/articles/v2me6r

 



时间 2014-07-31 12:22:00 Arulkumaran Kumaraswamipillai blog





主题 Log4J



 



Q. What is

FIX

Protocol?


 

A. FIX stands for Financial Information eXchange, which is an open protocol intended to streamline electronic communications in the financial securities industry. Most of the exchanges use this standard for communication like sending Order, Executions, MarketData, etc. There are many versions of the specifications like 4.2, 4.4, etc. Have a look at https://fixspec.com/FIX44.

Let's have a look at

QuickFIX/J

and Java example for sending and recieving FIX messages.


 

Step 1 : Required dependency JAR files

Step 2 : The quickfixj-msg-fix44-1.5.3.jar contains the data dictionary for FIX4.4 with domain objects like and XML "NewSingleOrder". Extract FIX44.xml and copy it to c:\\temp.

Step 3 : Define the configuration for the receiver or message acceptor. receiver.cfg file in /conf. Needs to be in the classpath.

[DEFAULT]
ConnectionType=acceptor
SocketAcceptPort=5001
SocketReuseAddress=Y
StartTime=00:00:00
EndTime=00:00:00
FileLogPath=log
FileStorePath=c:\\temp

[SESSION]
BeginString=FIX.4.4
SenderCompID=FixServer
TargetCompID=CLIENT1
DataDictionary=c:\\temp\\FIX44.xml

Step 4 : Define the FIXReceiver class. It extends MessageCracker implements Application . Relevant method gets invoked in this event driven architecture used in MINA.

package com.example;

import quickfix.Application;
import quickfix.DoNotSend;
import quickfix.FieldNotFound;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.Message;
import quickfix.RejectLogon;
import quickfix.SessionID;
import quickfix.UnsupportedMessageType;
import quickfix.fix44.MessageCracker;
import quickfix.fix44.NewOrderSingle;

public class FIXReceiver extends MessageCracker implements Application {

 @Override
 public void onMessage(NewOrderSingle order, SessionID sessionID) {
  System.out.println("Receiver onMessage..  " + order); 
 }
 
 @Override
 public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound, IncorrectDataFormat,
  IncorrectTagValue, RejectLogon {
 }

 @Override
 public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound, IncorrectDataFormat, 
 IncorrectTagValue, UnsupportedMessageType {
  System.out.println("Receiver fromApp..  " + arg0);
  crack(arg0, arg1); // calls onMessage(..,..)
 }

 @Override
 public void onCreate(SessionID arg0) {
  System.out.println("Receiver onCreate.. " + arg0);
 }

 @Override
 public void onLogon(SessionID arg0) {
  System.out.println("Receiver onLogon.." + arg0);
 }

 @Override
 public void onLogout(SessionID arg0) {}

 @Override
 public void toAdmin(Message arg0, SessionID arg1) {}

 @Override
 public void toApp(Message arg0, SessionID arg1) throws DoNotSend {}
}

Step 5 : The server socket code to receive messages. ReceiverApp.Java

package com.example;

import java.util.Scanner;

import quickfix.Application;
import quickfix.ConfigError;
import quickfix.DefaultMessageFactory;
import quickfix.FileStoreFactory;
import quickfix.ScreenLogFactory;
import quickfix.SessionSettings;
import quickfix.SocketAcceptor;

public class RecieverApp {
 public static void main(String[] args) throws ConfigError {
  
  SessionSettings settings = new SessionSettings("receiver.cfg");
  Application myApp = new FIXReceiver();
  FileStoreFactory fileStoreFactory = new FileStoreFactory(settings);
  ScreenLogFactory screenLogFactory = new ScreenLogFactory(settings);
  DefaultMessageFactory msgFactory = new DefaultMessageFactory();
  SocketAcceptor acceptor = new SocketAcceptor(myApp, fileStoreFactory,
    settings, screenLogFactory, msgFactory);
  
  acceptor.start();
  
  Scanner reader = new Scanner(System.in);
  System.out.println("press <enter> to quit");
  
  //get user input for a
  reader.nextLine();
  
  acceptor.stop();
 }

}

Step 5 : The sender or initiator configuration file sender.cfg .

[DEFAULT]
ConnectionType=initiator
HeartBtInt=30
ReconnectInterval=0
FileStorePath=c:\\temp
FileLogPath=log
StartTime=00:00:00
EndTime=00:00:00
UseDataDictionary=N
SocketConnectHost=localhost
ContinueInitializationOnError=Y

[SESSION]
BeginString=FIX.4.4
SenderCompID=CLIENT1
TargetCompID=FixServer
SocketConnectPort=5001

Step 6 : Define the FixSender class that implements Application . Relevant method gets invoked in this event driven architecture used in MINA.

package com.example;

import quickfix.Application;
import quickfix.DoNotSend;
import quickfix.FieldNotFound;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.Message;
import quickfix.RejectLogon;
import quickfix.SessionID;
import quickfix.UnsupportedMessageType;

public class FIXSender implements Application {

 @Override
 public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound, IncorrectDataFormat, 
 IncorrectTagValue, RejectLogon {
 }

 @Override
 public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound, IncorrectDataFormat, 
         IncorrectTagValue, UnsupportedMessageType { }

 @Override
 public void onCreate(SessionID arg0) {}

 @Override
 public void onLogon(SessionID arg0) {}

 @Override
 public void onLogout(SessionID arg0) {}

 @Override
 public void toAdmin(Message arg0, SessionID arg1) {}

 @Override
 public void toApp(Message msg, SessionID sessionId) throws DoNotSend {
  System.out.println("Sender toApp: " + msg.toString());
 }
}

Step 7 : The client socket initiator to send FIX messages. SenderApp.java .

package com.example;

import java.util.Date;

import quickfix.Application;
import quickfix.ConfigError;
import quickfix.DefaultMessageFactory;
import quickfix.FileStoreFactory;
import quickfix.ScreenLogFactory;
import quickfix.Session;
import quickfix.SessionID;
import quickfix.SessionNotFound;
import quickfix.SessionSettings;
import quickfix.SocketInitiator;
import quickfix.field.ClOrdID;
import quickfix.field.OrdType;
import quickfix.field.OrderQty;
import quickfix.field.Price;
import quickfix.field.Side;
import quickfix.field.Symbol;
import quickfix.field.TransactTime;
import quickfix.fix44.NewOrderSingle;

public class SenderApp {

 public static void main(String[] args) throws ConfigError, InterruptedException, SessionNotFound {

  SessionSettings settings = new SessionSettings("sender.cfg");
  Application myApp = new FIXSender();
  FileStoreFactory fileStoreFactory = new FileStoreFactory(settings);
  ScreenLogFactory screenLogFactory = new ScreenLogFactory(settings);
  DefaultMessageFactory msgFactory = new DefaultMessageFactory();
  SocketInitiator initiator = new SocketInitiator(myApp, fileStoreFactory, settings, 
                                       screenLogFactory, msgFactory);

  initiator.start();

  Thread.sleep(3000);

  // matching values from sender.cfg
  SessionID sessionID = new SessionID("FIX.4.4", "CLIENT1", "FixServer");
  NewOrderSingle order = new NewOrderSingle(new ClOrdID("DLF"), new Side(Side.BUY), 
    new TransactTime(new Date()), new OrdType(OrdType.LIMIT));

  order.set(new OrderQty(45.00));
  order.set(new Price(25.40));
  order.set(new Symbol("BHP"));
  Session.sendToTarget(order, sessionID);
  
  Thread.sleep(60000);

  initiator.stop();
 }
}

Step 8 : Run the ReceiverApp .

log4j:WARN No appenders could be found for logger (quickfix.SessionSchedule).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
<20140731-01:59:36, FIX.4.4:FixServer->CLIENT1, event> (Session FIX.4.4:FixServer->CLIENT1 schedule is daily, 00:00:00-UTC - 00:00:00-UTC)
<20140731-01:59:36, FIX.4.4:FixServer->CLIENT1, event> (Created session: FIX.4.4:FixServer->CLIENT1)
Receiver onCreate.. FIX.4.4:FixServer->CLIENT1
press <enter> to quit

Step 9 : Run the SenderApp .

log4j:WARN No appenders could be found for logger (quickfix.SessionSchedule).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
<20140731-01:59:44, FIX.4.4:CLIENT1->FixServer, event> (Session FIX.4.4:CLIENT1->FixServer schedule is daily, 00:00:00-UTC - 00:00:00-UTC)
<20140731-01:59:44, FIX.4.4:CLIENT1->FixServer, event> (Created session: FIX.4.4:CLIENT1->FixServer)
<20140731-01:59:45, FIX.4.4:CLIENT1->FixServer, outgoing> (8=FIX.4.4 9=72 35=A 34=52 49=CLIENT1 52=20140731-01:59:45.635 56=FixServer 98=0 108=30 10=203 )
<20140731-01:59:45, FIX.4.4:CLIENT1->FixServer, event> (Initiated logon request)
<20140731-01:59:45, FIX.4.4:CLIENT1->FixServer, incoming> (8=FIX.4.4 9=72 35=A 34=41 49=FixServer 52=20140731-01:59:45.672 56=CLIENT1 98=0 108=30 10=202 )
<20140731-01:59:45, FIX.4.4:CLIENT1->FixServer, event> (Received logon)
Sender toApp: 8=FIX.4.4 9=123 35=D 34=53 49=CLIENT1 52=20140731-01:59:47.671 56=FixServer 11=DLF 38=45 40=2 44=25.4 54=1 55=BHP 60=20140731-01:59:47.669 10=238 
<20140731-01:59:47, FIX.4.4:CLIENT1->FixServer, outgoing> (8=FIX.4.4 9=123 35=D 34=53 49=CLIENT1 52=20140731-01:59:47.671 56=FixServer 11=DLF 38=45 40=2 44=25.4 54=1 55=BHP 60=20140731-01:59:47.669 10=238 )
<20140731-02:00:15, FIX.4.4:CLIENT1->FixServer, incoming> (8=FIX.4.4 9=60 35=0 34=42 49=FixServer 52=20140731-02:00:15.918 56=CLIENT1 10=145 )
<20140731-02:00:18, FIX.4.4:CLIENT1->FixServer, outgoing> (8=FIX.4.4 9=60 35=0 34=54 49=CLIENT1 52=20140731-02:00:18.604 56=FixServer 10=143 )
<20140731-02:00:46, FIX.4.4:CLIENT1->FixServer, incoming> (8=FIX.4.4 9=60 35=0 34=43 49=FixServer 52=20140731-02:00:46.916 56=CLIENT1 10=148 )
<20140731-02:00:48, FIX.4.4:CLIENT1->FixServer, event> (Initiated logout request)
<20140731-02:00:48, FIX.4.4:CLIENT1->FixServer, outgoing> (8=FIX.4.4 9=60 35=5 34=55 49=CLIENT1 52=20140731-02:00:48.603 56=FixServer 10=151 )
<20140731-02:00:48, FIX.4.4:CLIENT1->FixServer, incoming> (8=FIX.4.4 9=60 35=5 34=44 49=FixServer 52=20140731-02:00:48.607 56=CLIENT1 10=153 )
<20140731-02:00:48, FIX.4.4:CLIENT1->FixServer, error> (quickfix.SessionException Logon state is not valid for message (MsgType=5))
<20140731-02:00:48, FIX.4.4:CLIENT1->FixServer, event> (Already disconnected: Verifying message failed: quickfix.SessionException: Logon state is not valid for message (MsgType=5))

Step 10: The receiver or acceptor logs grow further to

<20140731-01:59:45, FIX.4.4:FixServer->CLIENT1, incoming> (8=FIX.4.4 9=72 35=A 34=52 49=CLIENT1 52=20140731-01:59:45.635 56=FixServer 98=0 108=30 10=203 )
<20140731-01:59:45, FIX.4.4:FixServer->CLIENT1, event> (Accepting session FIX.4.4:FixServer->CLIENT1 from /127.0.0.1:60832)
<20140731-01:59:45, FIX.4.4:FixServer->CLIENT1, event> (Acceptor heartbeat set to 30 seconds)
<20140731-01:59:45, FIX.4.4:FixServer->CLIENT1, event> (Received logon)
<20140731-01:59:45, FIX.4.4:FixServer->CLIENT1, event> (Responding to Logon request)
<20140731-01:59:45, FIX.4.4:FixServer->CLIENT1, outgoing> (8=FIX.4.4 9=72 35=A 34=41 49=FixServer 52=20140731-01:59:45.672 56=CLIENT1 98=0 108=30 10=202 )
Receiver onLogon..FIX.4.4:FixServer->CLIENT1
<20140731-01:59:47, FIX.4.4:FixServer->CLIENT1, incoming> (8=FIX.4.4 9=123 35=D 34=53 49=CLIENT1 52=20140731-01:59:47.671 56=FixServer 11=DLF 38=45 40=2 44=25.4 54=1 55=BHP 60=20140731-01:59:47.669 10=238 )
Receiver fromApp..  8=FIX.4.4 9=123 35=D 34=53 49=CLIENT1 52=20140731-01:59:47.671 56=FixServer 11=DLF 38=45 40=2 44=25.4 54=1 55=BHP 60=20140731-01:59:47.669 10=238 
Receiver onMessage..  8=FIX.4.4 9=123 35=D 34=53 49=CLIENT1 52=20140731-01:59:47.671 56=FixServer 11=DLF 38=45 40=2 44=25.4 54=1 55=BHP 60=20140731-01:59:47.669 10=238 
<20140731-02:00:15, FIX.4.4:FixServer->CLIENT1, outgoing> (8=FIX.4.4 9=60 35=0 34=42 49=FixServer 52=20140731-02:00:15.918 56=CLIENT1 10=145 )
<20140731-02:00:18, FIX.4.4:FixServer->CLIENT1, incoming> (8=FIX.4.4 9=60 35=0 34=54 49=CLIENT1 52=20140731-02:00:18.604 56=FixServer 10=143 )
<20140731-02:00:46, FIX.4.4:FixServer->CLIENT1, outgoing> (8=FIX.4.4 9=60 35=0 34=43 49=FixServer 52=20140731-02:00:46.916 56=CLIENT1 10=148 )
<20140731-02:00:48, FIX.4.4:FixServer->CLIENT1, incoming> (8=FIX.4.4 9=60 35=5 34=55 49=CLIENT1 52=20140731-02:00:48.603 56=FixServer 10=151 )
<20140731-02:00:48, FIX.4.4:FixServer->CLIENT1, event> (Received logout request)
<20140731-02:00:48, FIX.4.4:FixServer->CLIENT1, outgoing> (8=FIX.4.4 9=60 35=5 34=44 49=FixServer 52=20140731-02:00:48.607 56=CLIENT1 10=153 )
<20140731-02:00:48, FIX.4.4:FixServer->CLIENT1, event> (Sent logout response)

标签:00,Java,simple,CLIENT1,FIX,FIX.4,FixServer,import,quickfix
From: https://blog.51cto.com/u_16087105/6710491

相关文章

  • 解决查看java占用的端口的具体操作步骤
    查看Java占用的端口在开发和运维过程中,我们经常需要查看Java应用程序所占用的端口。这对于排查问题、调试和监控都非常重要。本文将介绍几种方法来查看Java占用的端口,并提供相应的代码示例。方法一:使用jps命令jps命令是Java开发工具包(JDK)自带的一个命令行工具,用于查看Java进程的......
  • 解决查看java进程的参数的具体操作步骤
    查看java进程的参数在开发和运维过程中,我们经常需要查看Java进程的参数,以了解应用程序的配置和运行情况。本文将介绍如何使用命令行和Java代码来查看Java进程的参数。命令行方式1.jps命令jps命令是JDK自带的一个工具,用于查看Java进程的信息,包括进程ID和进程名。我们可以通过jp......
  • 如何实现布尔方法java的具体操作步骤
    实现布尔方法(Java)作为一名经验丰富的开发者,我来教你如何实现布尔方法(BooleanMethods)在Java编程中的应用。布尔方法是一种返回布尔值(true或false)的方法,它可以用于判断逻辑条件,决定程序的执行路径。整体流程下面是实现布尔方法的整体流程:步骤描述1定义布尔方法的名称......
  • java JDK安装及配置
    javaJDK安装及配置windows11jdk-8u261-windows-x64.exe1、点击安装,一路默认即可2、设置系统环境变量新建JAVA_HOME指明JDK安装路径,就是刚才安装时所选择的路径C:\ProgramFiles\Java\jdk1.8.0_261,此路径下包括lib,bin,jre等文件夹(此变量最好设置,因为以后运行tomcat,eclipse等......
  • JavaScript
    引入JavaScript1.内部标签<script></script>2.外部引用<scriptsrc="引入JavaScript.js"></script>浏览器控制台使用1.alert(弹窗)alert();2.console.log(控制台打印)console.log();数据类型1.等于和绝对等于==等于(类型不一样,值相等,判断为true)===绝......
  • SimpleITK 简单使用
    SimpleITKITK是一个开源、跨平台的框架,提供给开发者增强功能的图像分析和处理套件(推荐使用)。Note:注意SimpleITK不支持中文,即路径中不能有中文X射线图像对应的读取1#@file:itk_p1.py2#@Time:2021/8/2816:273#@Author:wmz4importSimpleITKassitk......
  • OAF:第五章-实现服务端java实体对象
    一、关于实体对象实体对象:实体对象为应用表封装了业务逻辑和DML操作;1.对象模型和关键类oracle.apps.fnd.framework.server.==OAEntityCache==:该缓存类用于存储特定实体对象的查询结果集。映射到相同实体对象的多个视图对象共享相同的实体缓存;oracle.apps.fnd.framework.server......
  • 解决生产环境调试 java的具体操作步骤
    生产环境调试Java在开发Java应用程序时,我们通常需要在生产环境中进行调试以解决问题和优化性能。本文将介绍如何在生产环境中进行Java调试的流程和步骤,并提供相应的代码示例。流程概述下表展示了生产环境调试Java的步骤及相应的操作。步骤操作1在项目中添加调试标志......
  • 【Java 新的选择】,Solon v2.3.8 发布
    Solon是什么开源项目?一个,Java新的生态型应用开发框架。它从零开始构建,有自己的标准规范与开放生态(历时五年,已有全球第二级别的生态规模)。与其他框架相比,它解决了两个重要的痛点:启动慢,费内存。解决痛点?由于SolonBean容器的独特设计,不会因为扩展依赖变多而启动很慢(开发调试时,......
  • 解决正则匹配 在线 JAVA的具体操作步骤
    正则匹配在线JAVA简介正则表达式是一种用于匹配和操作字符串的强大工具。在JAVA中,我们可以使用正则表达式来进行字符串的模式匹配、查找和替换等操作。本文将介绍如何使用JAVA的正则表达式库来进行在线JAVA代码的匹配。正则表达式的基本语法正则表达式由普通字符和特殊字符组成......