MFClient  1.3.0
 All Classes Functions Variables Enumerations
MFTradingSession.h
1 /*
2  * Copyright (C) 2015 MarketFactory, Inc. All rights reserved.
3  * Use in source and binary forms, with or without modification, is permitted
4  * provided that the following conditions are met:
5  * 1. You may only use this software for internal evaluation and testing
6  * purposes and may not use the software for engaging
7  * in live trades, unless and until you have entered into a separate agreement
8  * with MarketFactory governing the use of
9  * MarketFactory software in a production environment.
10  * 2. You may not distribute the software (in either source or binary forms) to
11  * third parties.
12  * 3. All copies of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer.
14  * THIS SOFTWARE IS PROVIDED BY MARKETFACTORY AND ITS LICENSORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18  * NO EVENT SHALL MARKETFACTORY OR ITS LICENSORS BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef _MF_TRADINGSESSION_H_
29 #define _MF_TRADINGSESSION_H_
30 
31 #define USERNAME_LENGTH 32
32 #define PASSWORD_LENGTH 32
33 #define CLORDID_LENGTH 32
34 #define STRATEGY_PARAMETERS_LENGTH 32
35 
36 #include <MFProto.h>
37 #include <TimeUtil.h>
38 #include <MFProtoInputBuffer.h>
39 #include <MFProtoOutputBuffer.h>
40 #include <MFProtoMsgWrapper.h>
41 #include <SessionState.h>
42 
43 #include <mftrading/DecimalQtyNULL.hpp>
44 #include <mftrading/TradingSessionStatusRequest.hpp>
45 #include <mftrading/NewOrderSingle.hpp>
46 #include <mftrading/Logon.hpp>
47 #include <mftrading/MarketDefinitionRequest.hpp>
48 #include <mftrading/OrderCancelReplaceRequest.hpp>
49 #include <mftrading/OrderCancelRequest.hpp>
50 #include <mftrading/Logout.hpp>
51 #include <mftrading/LogoutResponse.hpp>
52 #include <mftrading/Heartbeat.hpp>
53 #include <mftrading/OrdType.hpp>
54 #include <mftrading/PriceNULL.hpp>
55 #include <mftrading/Side.hpp>
56 #include <mftrading/SubscriptionRequestType.hpp>
57 #include <mftrading/TimeInForce.hpp>
58 
59 #include <boost/asio.hpp>
60 #include <boost/thread/mutex.hpp>
61 #include <boost/noncopyable.hpp>
62 
63 class MFTradingHandler;
64 
69 class MFTradingSession : private boost::noncopyable {
70  friend class MFClient;
71 
72  public:
77  void logon();
78 
85  void logout();
86 
93  void logoutResponse();
94 
99 
103  uint64_t getSendingTime() const { return _inputBuffer.getSendingTime(); }
104 
105  //----------------------------------------------------------------------------------------------------
106  // Trading
107 
119  void tradingRequest
120  (
121  MFProto::VenueID venueID,
122  mftrading::SubscriptionRequestType::Value subType,
123  MFProto::SeqNum nextExpSeqNum
124  );
125 
132  void newOrderSingle();
133 
163  mftrading::NewOrderSingle& createNewOrderSingle
164  (
165  MFProto::VenueID venueID,
166  MFProto::SecurityID securityID,
167  const char* clOrdId,
168  mftrading::Side::Value side,
169  mftrading::OrdType::Value ordType,
170  mftrading::TimeInForce::Value timeInForce,
171  const char** strategyParamName = NULL,
172  mftrading::StrategyParameterType::Value* strategyParamType = NULL,
173  const char** strategyParamValue = NULL,
174  int nStrategyParams = 0
175  );
176 
184 
206  mftrading::OrderCancelReplaceRequest& createOrderCancelReplaceRequest
207  (
208  MFProto::VenueID venueID,
209  MFProto::SecurityID securityID,
210  const char* clOrdID,
211  const char* origOrdID,
212  MFProto::OrderID orderID,
213  mftrading::Side::Value side,
214  mftrading::OrdType::Value ordType,
215  mftrading::TimeInForce::Value timeInForce
216  );
217 
223  void orderCancelRequest();
224 
243  mftrading::OrderCancelRequest& createOrderCancelRequest
244  (
245  MFProto::VenueID venueID,
246  MFProto::SecurityID securityID,
247  const char* clOrdID,
248  const char* origClOrdID,
249  MFProto::OrderID orderID
250  );
251 
252  private:
253  void sendLogonMessage();
254  void sendLogoutMessage();
255  void relogonIfNecessary();
256 
261  bool disconnect(bool reconnect = true);
262 
267  void connect();
268 
269  //-- Socket methods
270  template <typename T>
271  void write(MFProtoMsgWrapper<T>& msgWrapper) {
272  write(msgWrapper.buf());
273  }
274 
275  void write(MFProtoOutputBuffer& outputBuffer);
276 
277  void handle_connect(const boost::system::error_code& ec);
278  void handle_read(const boost::system::error_code& ec,
279  size_t bytes_transferred);
280 
281  void onTimer(TimeUtil::Timestamp now);
282  void checkForDeadConnection(TimeUtil::Timestamp now);
283  void sendHeartbeatIfNecessary(TimeUtil::Timestamp now);
284  void closeConnectionIfWaitedTooLongForServerLogoutResponse(
285  TimeUtil::Timestamp now);
286 
287  MFTradingSession(MFTradingHandler& handler, boost::asio::io_service& ios,
288  const std::string& username, const std::string& password,
289  const std::string& hostname, uint16_t port,
290  bool reconnectOnDisconenect, uint16_t heartBtInt);
291  void onStart();
292  void onStop();
293 
294  void write_internal(MFProtoOutputBuffer& outputBuffer);
295 
296  MFTradingHandler& _handler;
297  const uint16_t _heartBtInt;
298  boost::asio::ip::tcp::socket _socket;
299  MFProtoInputBuffer _inputBuffer;
300  mutable boost::mutex _outputBufferLock;
301  uint32_t _outputBufferPacketSeqNum;
302 
303  const std::string _username;
304  const std::string _password;
305  const std::string _hostname;
306  const uint16_t _port;
307  const bool _relogonOnDisconnect;
308  SessionState _state;
309  long _lastMessageSentTimestamp;
310  long _lastMessageRecvTimestamp;
311 };
312 
313 #endif
A wrapper to encapsulate an SBE class instance and the buffer where the message payload is actually s...
Definition: MFProtoMsgWrapper.h:17
mftrading::OrderCancelReplaceRequest & createOrderCancelReplaceRequest(MFProto::VenueID venueID, MFProto::SecurityID securityID, const char *clOrdID, const char *origOrdID, MFProto::OrderID orderID, mftrading::Side::Value side, mftrading::OrdType::Value ordType, mftrading::TimeInForce::Value timeInForce)
Allocate mftrading::OrderCancelReplaceRequest object and populate it with the supplied parameters...
Definition: MFTradingSession.cpp:217
void orderCancelReplaceRequest()
Submit modification to an existing order using the mftrading::OrderCancelReplaceRequest reference ret...
Definition: MFTradingSession.cpp:212
mftrading::NewOrderSingle & createNewOrderSingle(MFProto::VenueID venueID, MFProto::SecurityID securityID, const char *clOrdId, mftrading::Side::Value side, mftrading::OrdType::Value ordType, mftrading::TimeInForce::Value timeInForce, const char **strategyParamName=NULL, mftrading::StrategyParameterType::Value *strategyParamType=NULL, const char **strategyParamValue=NULL, int nStrategyParams=0)
Allocate mftrading::NewOrderSingle object and populate it with the supplied parameters. MFProto::VenueIDMFProto::SecurityIDClient order id. A string up to 32 characters long which is null-terminated unless the length is 32 (in which case, no null-termination necessary). mftrading::Side::Valuemftrading::OrdType::Valuemftrading::TimeInForce::ValueArray of mftrading::NewOrderSingle::StrategyParametersGrp StrategyParamName entries. Each StrategyParamName entry is a string up to 32 characters long. Default is NULLArray of mftrading::StrategyParameterType::Value entries. Default is NULLArray of mftrading::NewOrderSingle::StrategyParametersGrp StrategyParamValue entries. Each StrategyParamValue entry is a string up to 32 characters long. Default is NULLNumber of mftrading::NewOrderSingle::StrategyParametersGrp entries. Default is 0
Definition: MFTradingSession.cpp:170
Applications implement this interface in order to process events from a MFTradingSession ...
Definition: MFTradingHandler.h:47
Central class for managing sessions with MarketFactory. Create sessions using this class...
Definition: MFClient.h:83
void newOrderSingle()
Submit an order. The order submitted will be the mftrading::NewOrderSingle object that was returned a...
Definition: MFTradingSession.cpp:165
void orderCancelRequest()
Submit cancel request for an existing order using the object created by the previous call to createOr...
Definition: MFTradingSession.cpp:240
void tradingRequest(MFProto::VenueID venueID, mftrading::SubscriptionRequestType::Value subType, MFProto::SeqNum nextExpSeqNum)
A request to subscribe or unsubscribe to/from a particular venue (as specified by the venueID paramet...
Definition: MFTradingSession.cpp:149
A session with MarketFactory for trading with all the venues for which a user has access...
Definition: MFTradingSession.h:69
mftrading::OrderCancelRequest & createOrderCancelRequest(MFProto::VenueID venueID, MFProto::SecurityID securityID, const char *clOrdID, const char *origClOrdID, MFProto::OrderID orderID)
Allocate mftrading::OrderCancelRequest object and populate it with the supplied parameters. MFProto::VenueIDMFProto::SecurityIDUnique identifier for Order as assigned by the buy-side. A string up to 32 characters long which is null-terminated unless the length is 32 (in which case, no null-termination necessary). ClOrdID of the order to cancel. A string up to 32 characters long which is null-terminated unless the length is 32 (in which case, no null-termination necessary). MFProto::OrderID
Definition: MFTradingSession.cpp:245
void logoutResponse()
The response to a logout from the server. This should be called if a logout is received so that the c...
Definition: MFTradingSession.cpp:135
void logon()
Sends a logon message with the supplied username and password. Should only be called in the event loo...
Definition: MFTradingSession.cpp:101
uint64_t getSendingTime() const
Retrieve the sending time of the current frame.
Definition: MFTradingSession.h:103
void logout()
Initiates logout. This should be called so a clean and dirty (one where the socket closes w/o a logou...
Definition: MFTradingSession.cpp:120
void marketDefinitionRequest()
A request to receive the definitions of each market.
Definition: MFTradingSession.cpp:140
Definition: SessionState.h:37
MFProtoOutputBuffer & buf()
Return a reference to the buffer where the object payload is stored.
Definition: MFProtoMsgWrapper.h:34