我们在做FIX开发的时候,经常遇到原生的FIX标签并不能满足我们的业务需求;这个时候就需要涉及到自定义的“字段”,“类”,“集合”。
message.setField(new DoubleField(6123), "value");
DoubleField field = message.getField(new DoubleField(6123));
上面的自定义方法在团队开发中,经常会引起不必要的麻烦(标签被重用,标签难以理解等问题。)
FIXJ官网推荐我们使用如下方法自定义Field
// RemiteNOField.java
import quickfix.StringField;
public class RemiteNOField extends StringField
{
public RemiteNOField () { super(C); }
public RemiteNOField (String data) { super(RemiteNOField , data);
}}
当然自定义完字段以后!需要在FIX4.4.xml文件中添加相应的Field描述。
<fields>
<field number="1" name="RemiteNOField" type="String" />
...
<field number="4" name="AdvSide" type="CHAR">
<value enum="B" description="BUY" />
<value enum="S" description="SELL" />
<value enum="X" description="CROSS" />
<value enum="T" description="TRADE" />
</field>
FIX中的Message相当我们理解的JAVA类。你可以自定义Message来描述一个对象。然后再XML文件中添加相应的Message描述。
注意:记得在35标签的位置添加相应的枚举
<value enum="Z6666" description="OrdersAfterSomeIDRequest"/>
import quickfix.Message;
import quickfix.field.OrdStatus;
import quickfix.field.OrderID;
import quickfix.field.SecurityType;
import quickfix.field.StrikeCurrency;
import quickfix.field.Symbol;
import quickfix.field.TradeRequestID;
import quickfix.field.TradeRequestType;
import com.okcoin.okfix.okserver.response.future.PageNO;
/** * @ClassName: OrdersRequest * @Description:获取指定ID的订单号 * */
public class OrdersAfterSomeIDRequest extends Message {
private static final long serialVersionUID = 7922839466982400795L;
public static final String MSGTYPE = "Z6666";
public OrdersAfterSomeIDRequest() {
getHeader().setField(new quickfix.field.MsgType("Z6666"));
}
public void set(Symbol field) { // 55
setField(field);
}
public void set(OrderID field) { // 37
setField(field);
}
public void set(OrdStatus field) { // 39
setField(field);
}
public void set(TradeRequestID field) { // 568
setField(field);
}
public void set(TradeRequestType field) { // 569
setField(field);
}
public void set(PageNO field) { // 8214 页码
setField(field);
}
public void set(StrikeCurrency field) { // 947
setField(field);
}
public void set(SecurityType field) { // 167
setField(field);
}
}
<message name="TradeOrdersAfterOneID" msgtype="Z6666" msgcat="app">
<field name="Symbol" required="N"/>
<field name="OrderID" required="N"/>
<field name="OrdStatus" required="N"/>
<field name="TradeRequestID" required="N"/>
<field name="TradeRequestType" required="N"/>
<field name="PageNO" required="N"/>
<field name="StrikeCurrency" required="N"/>
<field name="SecurityType" required="N"/>
</message>
Group本质上是一个集合,其用来描述相同的属性的一类集合。qFixJ 充分的体现了其灵活性。
注意!在fields标签中添加相应的Field
/** * @ClassName: FuturePositionsResponse * @Description: 持仓信息 * */
public class FuturePositionsResponse extends Message {
private static final long serialVersionUID = 1L;
private static final String MSGTYPE = "Z3333";
public FuturePositionsResponse() {
getHeader().setField(new quickfix.field.MsgType(MSGTYPE));
}
public void set(Currency field) { // 货币类型
setField(field);
}
public void set(Symbol field) { // 标志
setField(field);
}
/** * @ClassName: NoUserPostion * @Description: 用户信息持仓信息 </group> */
public static class NoUserPostion extends Group {
private static final long serialVersionUID = 6684058490806701199L;
public NoUserPostion() {
super(8303, 55, new int[] { 55,8213, 6, 38, 8212, 44, 99, 54, 8001, 8203,
898, 8207, 8208,8210,8209});
}
public void set(ContractID field) { // 期货合约ID
setField(field);
}
public void set(AvgPx field) {// 开仓均价
setField(field);
}
public void set(OrderQty field) { // 持仓量
setField(field);
}
public void set(ContractName field) { // 合约类型 contract_name LTC0108
setField(field);
}
public void set(Price field) {// 开仓价
setField(field);
}
public void set(StopPx field) {// 强平价格
setField(field);
}
public void set(Side field) { // position: 开多,开空
setField(field);
}
public void set(Balance field) {// 合约账户余额
setField(field);
}
public void set(PositionRealProfit field) { // 已实现盈亏
setField(field);
}
public void set(MarginRatio field) { // position: 保证率(杠杆倍数)
setField(field);
}
public void set(FrozenAmt field) {// 当前合约冻结保证金
setField(field);
}
public void set(MarginCash field) { // position: 保证金
setField(field);
}
public void set(PositionType field) { // 仓位Type
setField(field);
}
public void set(PositionsID field) { // 仓位ID
setField(field);
}
public void set(Symbol field) { // 合约类型
setField(field);
}
public void set(Eveningup field){ //可平仓量
setField(field);
}
}
}
<message name="FuturePositionsResponse" msgtype="Z3333" msgcat="app">
<field name="Currency" required="N"/>
<field name="Symbol" required="N"/>
<group name="NoUserPostion" required="N">
<field name="Symbol" required="Y"/>
<field name="ContractID" required="N"/>
<field name="AvgPx" required="N"/>
<field name="OrderQty" required="N"/>
<field name="ContractName" required="N"/>
<field name="Price" required="N"/>
<field name="StopPx" required="N"/>
<field name="Side" required="N"/>
<field name="Balance" required="N"/>
<field name="PositionRealProfit" required="N"/>
<field name="MarginRatio" required="N"/>
<field name="FrozenAmt" required="N"/>
<field name="MarginCash" required="N"/>
<field name="PositionType" required="N"/>
<field name="PositionsID" required="N"/>
<field name="Eveningup" required="N"/>
</group>
</message>