View Javadoc
1   package org.oxerr.huobi.fix;
2   
3   import java.math.BigDecimal;
4   
5   import org.oxerr.huobi.fix.fix44.AccountInfoRequest;
6   import org.oxerr.huobi.fix.fix44.AccountInfoResponse;
7   import org.oxerr.huobi.fix.fix44.HuobiOrderInfoResponse;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  import quickfix.Application;
12  import quickfix.FieldNotFound;
13  import quickfix.IncorrectDataFormat;
14  import quickfix.IncorrectTagValue;
15  import quickfix.Message;
16  import quickfix.SessionID;
17  import quickfix.UnsupportedMessageType;
18  import quickfix.field.MsgType;
19  import quickfix.field.Password;
20  import quickfix.field.Username;
21  import quickfix.fix44.ExecutionReport;
22  import quickfix.fix44.NewOrderSingle;
23  import quickfix.fix44.OrderCancelReject;
24  import quickfix.fix44.OrderCancelRequest;
25  import quickfix.fix44.OrderMassStatusRequest;
26  import quickfix.fix44.OrderStatusRequest;
27  
28  /**
29   * {@link Application} for trading interface.
30   */
31  public class TradeApplication extends HuobiApplication {
32  
33  	private final Logger log = LoggerFactory.getLogger(TradeApplication.class);
34  
35  	private final String accessKey;
36  	private final String secretKey;
37  
38  	public TradeApplication(String accessKey, String secretKey) {
39  		this.accessKey = accessKey;
40  		this.secretKey = secretKey;
41  	}
42  
43  	/**
44  	 * {@inheritDoc}
45  	 */
46  	@Override
47  	public void toAdmin(Message message, SessionID sessionId) {
48  		super.toAdmin(message, sessionId);
49  
50  		String msgType;
51  		try {
52  			msgType = message.getHeader().getString(MsgType.FIELD);
53  		} catch (FieldNotFound e) {
54  			throw new RuntimeException(e.getMessage(), e);
55  		}
56  
57  		if (MsgType.LOGON.equals(msgType)) {
58  			message.setField(new Username(accessKey));
59  			message.setField(new Password(secretKey));
60  		}
61  	}
62  
63  	/**
64  	 * {@inheritDoc}
65  	 */
66  	@Override
67  	public void fromApp(Message message, SessionID sessionId)
68  			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue,
69  			UnsupportedMessageType {
70  		log.trace("fromApp: {}", message);
71  
72  		MsgType msgType = new MsgType();
73  		message.getHeader().getField(msgType);
74  		String msgTypeValue = msgType.getValue();
75  
76  		if (msgTypeValue.equals(AccountInfoResponse.MSGTYPE)) {
77  			onMessage((AccountInfoResponse) message, sessionId);
78  		} else if (msgTypeValue.equals(HuobiOrderInfoResponse.MSGTYPE)) {
79  			onMessage((HuobiOrderInfoResponse) message, sessionId);
80  		} else {
81  			crack(message, sessionId);
82  		}
83  	}
84  
85  	public void onMessage(AccountInfoResponse message, SessionID sessionId)
86  			throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
87  	}
88  
89  	public void onMessage(HuobiOrderInfoResponse message, SessionID sessionId)
90  			throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
91  	}
92  
93  	@Override
94  	public void onMessage(ExecutionReport message, SessionID sessionId)
95  			throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
96  	}
97  
98  	@Override
99  	public void onMessage(OrderCancelReject message, SessionID sessionId)
100 			throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
101 	}
102 
103 	public void requestAccountInfo(
104 			String accReqId,
105 			String symbol,
106 			SessionID sessionId) {
107 		AccountInfoRequest message = TradeRequests.buildAccountInfoRequest(
108 				accReqId, accessKey, symbol);
109 		sendMessage(message, sessionId);
110 	}
111 
112 	public void placeOrder(
113 			String clOrdId,
114 			char side,
115 			char ordType,
116 			BigDecimal minQty,
117 			BigDecimal price,
118 			String symbol,
119 			SessionID sessionId) {
120 		NewOrderSingle message = TradeRequests.buildNewOrderSingle(
121 				clOrdId, accessKey, side, ordType, minQty, price, symbol);
122 		sendMessage(message, sessionId);
123 	}
124 
125 	public void cancelOrder(
126 			String clOrdId,
127 			String origClOrdId,
128 			char side,
129 			String symbol,
130 			SessionID sessionId) {
131 		OrderCancelRequest message = TradeRequests.buildOrderCancelRequest(
132 				clOrdId, origClOrdId, side, symbol);
133 		sendMessage(message, sessionId);
134 	}
135 
136 	public void requestOrderMassStatus(
137 			String massStatusReqId,
138 			int massStatusReqType,
139 			String symbol,
140 			SessionID sessionId) {
141 		OrderMassStatusRequest message = TradeRequests.buildOrderMassStatusRequest(
142 				massStatusReqId, accessKey, massStatusReqType, symbol);
143 		sendMessage(message, sessionId);
144 	}
145 
146 	public void requestOrderStatus(
147 			String clOrdId,
148 			char side,
149 			String symbol,
150 			SessionID sessionId) {
151 		OrderStatusRequest message = TradeRequests.buildOrderStatusRequest(
152 				clOrdId, accessKey, side, symbol);
153 		sendMessage(message, sessionId);
154 	}
155 
156 }