java 如何使用 QuickFIX/J 发送 FIX 消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35983928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send FIX message with QuickFIX/J
提问by user3756506
I need a simple example of how to initialize a session and send one FIX message. I have this initial code:
我需要一个简单的示例来说明如何初始化会话并发送一条 FIX 消息。我有这个初始代码:
SessionSettings settings = new SessionSettings( new FileInputStream("fix.cfg"));
Application application = new Application(settings);
MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings);
LogFactory logFactory = new ScreenLogFactory( true, true, true);
MessageFactory messageFactory = new DefaultMessageFactory();
Initiator initiator = new SocketInitiator(application, messageStoreFactory, settings, logFactory, messageFactory);
initiator.start();
回答by Slimu
From the code above, I see that you have an initiator application (the client) and you need to also create an acceptor
application (the server). Below I've attached the two classes that will do what do you want.
从上面的代码中,我看到您有一个启动器应用程序(客户端),并且您还需要创建一个acceptor
应用程序(服务器)。下面我附上了两个可以做你想做的事。
First I'll list the acceptor
application:
首先我将列出acceptor
应用程序:
public class ServerApplication implements Application {
@Override
public void onCreate(SessionID sessionID) {
}
@Override
public void onLogon(SessionID sessionID) {
}
@Override
public void onLogout(SessionID sessionID) {
}
@Override
public void toAdmin(Message message, SessionID sessionID) {
}
@Override
public void fromAdmin(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon {
}
@Override
public void toApp(Message message, SessionID sessionID) throws DoNotSend {
}
@Override
public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
System.out.println("FromApp: " + message);
}
public static void main(String[] args) throws ConfigError, FileNotFoundException, InterruptedException, SessionNotFound {
SessionSettings settings = new SessionSettings("res/acceptor.config");
Application application = new ServerApplication();
MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings);
LogFactory logFactory = new ScreenLogFactory( true, true, true);
MessageFactory messageFactory = new DefaultMessageFactory();
Acceptor initiator = new SocketAcceptor(application, messageStoreFactory, settings, logFactory, messageFactory);
initiator.start();
CountDownLatch latch = new CountDownLatch(1);
latch.await();
}
}
This is a server application that will stay started and listen for messages from the clients which connect to it. Here is the configuration file (acceptor.properties
) used by it:
这是一个服务器应用程序,它将保持启动并侦听来自连接到它的客户端的消息。这是acceptor.properties
它使用的配置文件 ( ):
[default]
ApplicationID=server
FileStorePath=storage/messages/
ConnectionType=acceptor
StartTime=00:01:00 Europe/Bucharest
EndTime=23:59:00 Europe/Bucharest
HeartBtInt=30
UseDataDictionary=Y
DataDictionary=FIX42.xml
ValidateUserDefinedFields=N
ValidateIncomingMessage=N
RefreshOnLogon=Y
[session]
BeginString=FIX.4.2
SocketAcceptPort=9877
SenderCompID=server
TargetCompID=client
AcceptorTemplate=N
lockquote
Next is the client application code. It tries to connect to a server and after that it will send a message to it:
接下来是客户端应用程序代码。它尝试连接到服务器,然后它会向它发送一条消息:
public class ClientApplication implements Application {
private static volatile SessionID sessionID;
@Override
public void onCreate(SessionID sessionID) {
System.out.println("OnCreate");
}
@Override
public void onLogon(SessionID sessionID) {
System.out.println("OnLogon");
ClientApplication.sessionID = sessionID;
}
@Override
public void onLogout(SessionID sessionID) {
System.out.println("OnLogout");
ClientApplication.sessionID = null;
}
@Override
public void toAdmin(Message message, SessionID sessionID) {
System.out.println("ToAdmin");
}
@Override
public void fromAdmin(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon {
System.out.println("FromAdmin");
}
@Override
public void toApp(Message message, SessionID sessionID) throws DoNotSend {
System.out.println("ToApp: " + message);
}
@Override
public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
System.out.println("FromApp");
}
public static void main(String[] args) throws ConfigError, FileNotFoundException, InterruptedException, SessionNotFound {
SessionSettings settings = new SessionSettings("res/initiator.config");
Application application = new ClientApplication();
MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings);
LogFactory logFactory = new ScreenLogFactory( true, true, true);
MessageFactory messageFactory = new DefaultMessageFactory();
Initiator initiator = new SocketInitiator(application, messageStoreFactory, settings, logFactory, messageFactory);
initiator.start();
while (sessionID == null) {
Thread.sleep(1000);
}
final String orderId = "342";
NewOrderSingle newOrder = new NewOrderSingle(new ClOrdID(orderId), new HandlInst('1'), new Symbol("6758.T"),
new Side(Side.BUY), new TransactTime(new Date()), new OrdType(OrdType.MARKET));
Session.sendToTarget(newOrder, sessionID);
Thread.sleep(5000);
}
}
The configuration file for it (initiator.config
) is almost the same as the one used for the acceptor:
它的配置文件 ( initiator.config
) 与用于接受器的配置文件几乎相同:
[default]
ApplicationID=client
FileStorePath=storage/messages/
ConnectionType=initiator
StartTime=00:01:00 Europe/Bucharest
EndTime=23:59:00 Europe/Bucharest
HeartBtInt=30
UseDataDictionary=Y
DataDictionary=FIX42.xml
ValidateUserDefinedFields=N
ValidateIncomingMessage=N
RefreshOnLogon=Y
[session]
BeginString=FIX.4.2
SocketConnectHost=localhost
SocketConnectPort=9877
SenderCompID=client
TargetCompID=server
The configuration files both miss some options, but for testing purposes are enough. Each of the classes has a main method added just for testing the case you wanted.
配置文件都缺少一些选项,但用于测试目的就足够了。每个类都添加了一个 main 方法,仅用于测试您想要的情况。
Normally you would handle a bit different the way they are started or stopped. The server application listens for messages/connections and is never stopped, while the client application stops right after sending the first message.
通常,您会以稍微不同的方式处理它们的启动或停止方式。服务器应用程序侦听消息/连接并且永不停止,而客户端应用程序在发送第一条消息后立即停止。
回答by TT.
There are examples included in the installation of QuickFIX/J, namely Executor
and Banzai
. You can read about that here.
QuickFIX/J 的安装中包含了一些示例,即Executor
和Banzai
。您可以在此处阅读相关内容。
QuickFIX comes with several example applications. These application are in the quickfix/examplesdirectory. They are not meant to demonstrate good application design or meant to be used in a real production system. They are merely provided as a tutorial on how to build an application with QuickFIX.
Executoris a very simple order execution simulator. It only supports limit orders and always fills them completely.
Banzaiis a simple trading client. It can be used with the Executor to see a simple example of using QuickFIX/J on both the buy and sell side of an order execution.
QuickFIX 附带了几个示例应用程序。这些应用程序位于quickfix/examples目录中。它们并不是为了展示良好的应用程序设计,也不是为了在真实的生产系统中使用。它们仅作为关于如何使用 QuickFIX 构建应用程序的教程提供。
Executor是一个非常简单的订单执行模拟器。它只支持限价单并且总是完全成交。
Banzai是一个简单的交易客户端。它可以与 Executor 一起使用,以查看在订单执行的买卖双方使用 QuickFIX/J 的简单示例。