quickfix笔记
2020-03-31 本文已影响0人
我是电饭煲
接收服务器返回的函数(位于DefaultIoFilterChain类)
- messageReceived
使用用户名、密码登录,Logon
image.pnghttps://stackoverflow.com/questions/13348939/how-to-make-the-login-part-in-quickfix
查看配置参数(DefaultSessionFactory)
image.png增加已有类的自定义字段
-
在FIX50SP2.xml中搜索fields,在里面添加 <field number="1618" name="ApiId" type="STRING"/>
image.png -
在需要添加字段的类,添加字段
image.png -
使用
image.png
UserRequest赋值
String exchangeName = json.getString("exchangeName");
String type = json.getString("type");
String accountName = json.getString("accountName");
String publicKey = json.getJSONObject("data").getString("publicKey");
String secretKey = json.getJSONObject("data").getString("secretKey");
UserRequest userRequest = new UserRequest(new UserRequestID(exchangeName + "." + type),
new UserRequestType(1), // 注意里面的取值范围,点入类里,可以查看字段
new Username(accountName));
userRequest.set(new Password(publicKey));
userRequest.set(new NewPassword(secretKey));
NewOrderSingle赋值
JSONObject data = json.getJSONObject("data");
String side = data.getString("side");
String ordType = data.getString("ordType");
BigDecimal price = data.getBigDecimal("price");
BigDecimal quantity = data.getBigDecimal("quantity");
String symbol = json.getString("symbol");
NewOrderSingle newOrderSingle = new NewOrderSingle(
new ClOrdID("1"),
sideToFIXSide(side),
new TransactTime(),
typeToFIXType(ordType));
newOrderSingle.set(new OrderQty(quantity.doubleValue()));
newOrderSingle.set(new Symbol(symbol));
newOrderSingle.set(new HandlInst('1'));
if (ordType.equals(Type.MARKET.getValue())) {
// 没有价格
} else if (ordType.equals(Type.LIMIT.getValue())) {
newOrderSingle.setField(new Price(price.doubleValue()));
}
newOrderSingle.setField(new TimeInForce(TimeInForce.DAY));
当发送请求,接收方没有接收到数据,可以这样调试,查看错误信息。堆栈。
image.pngimage.png
查询quickfix有哪些类
image.png如果session.send(msg);后,对方没有接收到(没有反应),很大可能是参数没有没填好,字段是否必要参考https://www.onixs.biz/fix-dictionary/fixt1.1/msgtype_a_65.html
,例如填了 new ClOrdID("")、new TimeInForce()
/**
* {"subscribe":"true", "method":"order", "exchangeName":"deribit", "type":"future", "symbol":"BTC-PERPETUAL" ,
* "data":{"side": "buy", "ordType": "limit", "price":"100", "quantity":"0.1"}}
*/
JSONObject data = json.getJSONObject("data");
String side = data.getString("side");
String ordType = data.getString("ordType");
BigDecimal price = data.getBigDecimal("price");
BigDecimal quantity = data.getBigDecimal("quantity");
String symbol = json.getString("symbol");
NewOrderSingle newOrderSingle = new NewOrderSingle(
new ClOrdID("1"),
sideToFIXSide(side),
new TransactTime(),
typeToFIXType(ordType));
newOrderSingle.set(new OrderQty(quantity.doubleValue()));
newOrderSingle.set(new Symbol(symbol));
newOrderSingle.set(new HandlInst('1'));
if (ordType.equals(Type.MARKET.getValue())) {
// 没有价格
} else if (ordType.equals(Type.LIMIT.getValue())) {
newOrderSingle.setField(new Price(price.doubleValue()));
}
newOrderSingle.setField(new TimeInForce(TimeInForce.DAY));
quickfixj打印的日志量很大,需要配合linux定时任务,当磁盘满时,服务会关闭,需要定时删除日志
java quickfix,两个服务建立连接时,最少堆栈大小
- -Xms256m -Xmx1024m
- 不然会报错,生成类似hs_err_pid14145.log的文件
quickfix配置参数的根路径
- 根目录为jar路径,不能直接引用打包到jar包里的xml
# default settings for sessions
[DEFAULT]
ConnectionType=initiator
SenderCompID=market-data
# 文件夹生成路径与jar同一目录
FileLogPath=logs
FileStorePath=data
NonStopSession=Y
TimeZone=GMT+08:00
ResetOnDisconnect=Y
ResetOnLogout=Y
ResetOnLogon=Y
# session definition
[SESSION]
# inherit ConnectionType, ReconnectInterval and SenderCompID from default
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0SP2
TargetCompID=exchange-adapter
SocketAcceptPort=9001
# 根目录为jar路径,不能直接引用打包到jar包里的xml
TransportDataDictionary=./quickfix-cfg/fixt11.xml
AppDataDictionary=./quickfix-cfg/fix50sp2.xml
quickfix的类型不适用于Jackson2Json、gjson的序列化,RabbitMQ默认的SimpleConverter可以序列化,但不能跨语言,且数据量大。
通过继承MessageCracker,覆盖OnMessage方法,获取接收的数据
- public class MarketDataQfApp extends MessageCracker implements Application {}
@Override
public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
log.info("fromApp");
crack(message, sessionID);
}
@Override
public void onMessage(Message message, SessionID sessionID)
throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
try {
if (message instanceof MarketDataSnapshotFullRefresh) {
}
} catch (Exception e) {
log.error(e.getMessage());
}
}