如何使用 QuickFIX/J 发送 FIX 消息
How to send FIX message with QuickFIX/J
我需要一个简单的示例来说明如何初始化会话并发送一条 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();
QuickFIX/J的安装中包含了示例,即Executor
和Banzai
。你可以阅读 here.
QuickFIX comes with several example applications. These application are in the quickfix/examples directory. 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.
Executor is a very simple order execution simulator. It only supports limit orders and always fills them completely.
Banzai is 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.
从上面的代码中,我看到您有一个启动器应用程序(客户端),您还需要创建一个 acceptor
应用程序(服务器)。下面我附上了两个 类,它们可以满足您的需求。
首先,我将列出 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();
}
}
这是一个服务器应用程序,它将保持启动状态并侦听来自连接到它的客户端的消息。这是它使用的配置文件(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
接下来是客户端应用程序代码。它会尝试连接到服务器,然后会向它发送一条消息:
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);
}
}
它的配置文件(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
配置文件都缺少一些选项,但对于测试目的来说已经足够了。每个 类 都添加了一个主要方法,仅用于测试您想要的案例。
通常您会以不同的方式处理它们的启动或停止方式。服务器应用程序侦听 messages/connections 并且永不停止,而客户端应用程序在发送第一条消息后立即停止。
我需要一个简单的示例来说明如何初始化会话并发送一条 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();
QuickFIX/J的安装中包含了示例,即Executor
和Banzai
。你可以阅读 here.
QuickFIX comes with several example applications. These application are in the quickfix/examples directory. 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.
Executor is a very simple order execution simulator. It only supports limit orders and always fills them completely.
Banzai is 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.
从上面的代码中,我看到您有一个启动器应用程序(客户端),您还需要创建一个 acceptor
应用程序(服务器)。下面我附上了两个 类,它们可以满足您的需求。
首先,我将列出 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();
}
}
这是一个服务器应用程序,它将保持启动状态并侦听来自连接到它的客户端的消息。这是它使用的配置文件(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
接下来是客户端应用程序代码。它会尝试连接到服务器,然后会向它发送一条消息:
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);
}
}
它的配置文件(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
配置文件都缺少一些选项,但对于测试目的来说已经足够了。每个 类 都添加了一个主要方法,仅用于测试您想要的案例。
通常您会以不同的方式处理它们的启动或停止方式。服务器应用程序侦听 messages/connections 并且永不停止,而客户端应用程序在发送第一条消息后立即停止。