Send and receive messages¶
Introduction¶
This article serves as an introduction to using FIX API for anyone interested in interacting with Spotware cServer using FIX.
In this article, we will use a C# example to describe in detail the principles of how to construct a FIX message, send it to the server and receive the response. This example is by no means a bullet-proof application and was kept as simple as possible to allow programmers to easily understand the concept of using FIX API messages.
In order to establish and maintain proper communication with the server and proper handling of the responses, additional functionality is required, which was skipped for the sake of simplicity and clarity. We will deal with these subjects in future articles.
Code sample¶
You can find the code sample discussed in this article on our GitHub repository
Overview of FIX communication¶
A FIX message is just a string composed of sets of numerical tags and values separated by a vertical bar (|). Each tag represents a different field for which a certain set of values is allowed. Below you can see a sample FIX message which requests authentication from the server.
8=FIX.4.4|9=126|35=A|49=theBroker.12345|56=CSERVER|34=1|52=20170117- 08:03:04|57=TRADE|50=any_string|98=0|108=30|141=Y|553=12345|554=passw0rd!|10=131|
As you can see, the repeatable pattern found in each FIX message is:
Tag=Value|Tag=Value|Tag=Value|...
Depending on the purpose of each message, a different set of tags and values is required each time. The tags and values required for each message are described in detail in the cTrader FIX engine Rules of Engagement (always check for the latest rules of engagement).
In a similar way, responses are sent back from the server. Below you can see the response of the server for the above message.
8=FIX.4.4|9=106|35=A|34=1|49=CSERVER|50=TRADE|52=20170117- 08:03:04.509|56=theBroker.12345|57=any_string|98=0|108=30|141=Y|10=066|
The steps involved in the process of communicating with a FIX server are the following:
-
Construct a FIX message
-
Transmit a FIX message
-
Receive a FIX message
-
Parse a FIX message
A raw FIX message is not a very readable format since it was designed with efficiency in mind rather than understandability. Therefore, for each software application there will always be a process of translating information provided into the respective FIX message.
In our C# sample application we have created a class for handling the message construction as well as functions for creating FIX messages based on the relevant information.
After the messages have been constructed, they are transmitted between a server and a client over the internet through network sockets. When the messages are received, they need to be parsed to be presented in a readable format.
In this article we will cover the process of construction, transmission and receipt of the reply. We will deal with parsing in a future article.
Construct a FIX message¶
Message structure¶
In our sample application we have created a class responsible of creating FIX messages. The class is called MessageConstructor and can be found in the FIX API Library project.
The MessageConstructor is initialised with the following parameters:
-
Host(*)– the address where our cServer is located. -
Username(*)– the account number -
Password(*)– the password -
SenderCompID(*)– it is provided in the FIX API form of cTrader. It is in the format -
SenderSubID*– it is the second part of SenderCompID -
TargetCompID(*)– it is provided in the FIX API form of cTrader (usually it is cServer)
You can find this information in your cTrader FIX API form.
After we have initialised a MessageConstructor then we are ready to construct FIX API messages.
All the messages are constructed in a similar manner. Below there is a code sample of constructing a Logon message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
You can see that we first construct the body part, then we pass it to the header function and at last we pass both parts into the trailer function. These 3 parts are detailed below.
The message construction process is just the addition of the required tags, values and separators into a string.
Body¶
First we will start by describing the body construction, since the body of the message needs to be created first. We can see an example above (creating the Logon message).
We start by initialising a StringBuilder class and we append the tags one-by-one based on the function inputs. Based on the message type, the body must be composed of different sets of tags, some of them being mandatory and others being optional.
You can find the structure of each message in our Rules of Engagement (/FIX).
Then we create a header for a Logon message and append to it the body of the message. Finally, using the headerAndBody string we generate the trailer. Following on, we will see how we construct a header and a trailer.
Header¶
The header is the first part of the FIX message and it is composed of the following fields (same for all the messages):
BeginString– the begin string defines the FIX protocol version and in our case is fixed to FIX4.4.BodyLength– the body length states the length of the message in characters, excluding theBeginString, theBodyLengthand the trailer fields.MsgType– in this field we define the message type, so that the receiver knows how to parse the body.SenderCompID– here we set theSenderCompID.TargetCompID– this is the target of our message. In our case, it will always be cServer.SenderSubID– the trader login.MsgSeqNum– this is the sequence number of the message. It needs to be increased for each message sent in the same session.Sending Time– the time of message transmission.
Below you can see the ConstructHeader function, responsible for constructing the headers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
Trailer¶
The trailer is just a tag containing the checksum of the rest of the message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
System messages¶
Our sample contains functions that return the following system messages:
- Heartbeat:
MessageConstructor.HeartbeatMessage() - Test request:
MessageConstructor.TestRequestMessage() - Logon:
MessageConstructor.LogonMessage() - Logout:
MessageConstructor.LogoutMessage() - Resend request:
MessageConstructor.ResendMessage() - Reject:
MessageConstructor.RejectMessage() - Sequence reset:
MessageConstructor.SequenceResetMessage()
Application messages¶
Our sample contains functions that return the following system messages:
- Market data request:
MessageConstructor.HeartbeatMessage() - Market data snapshot/full refresh:
MessageConstructor.MarketDataSnapshotMessage() - Market data incremental refresh:
MessageConstructor.MarketDataIncrementalRefreshMessage() - New order single:
MessageConstructor.NewOrderSingleMessage() - Order status request:
MessageConstructor.OrderStatusRequest() - Execution report:
MessageConstructor.ExecutionReport() - Business message reject:
MessageConstructor.BusinessMessageReject() - Request for positions:
MessageConstructor.RequestForPositions() - Position report:
MessageConstructor.PositionReport()
Send a message and receive a response¶
In order to send a FIX message to cServer you first need to establish a connection with the server. You can do this by creating a TcpClient. In our case we create two clients, since price quotation messages and trade messages are handled by different ports on the server.
Then, we need to get the two streams on which the messages will be sent. This process takes place in the form's constructor as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
In the constructor, we also initialise a MessageConstructor class which will be used for generating the messages.
Next, to send the messages, we created two different functions, SendPriceMessage() and SendTradeMessage(). Each takes the FIX message as an input and then calls the SendMessage() function with the message and respective stream as an input.
The SendMessage() function works as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
The detailed steps are as follows:
- Encode the message to a byte array.
- Write the byte array on the stream.
- Read the reply from the stream.
- Increase the message sequence number.
- Encode the message into a string.
The function should return the FIX message sent by the server.
As you would assume, you cannot show a raw FIX message to the user, so an additional step of parsing the incoming message should be developed.
Conclusion¶
This application is a brief demonstration on how to communicate with cServer using FIX messages. It is just an example illustrating the concepts of FIX Protocol and it is by no means a full FIX engine. If you would like to avoid building your own FIX engine, you might consider using one of the third-party FIX engines available.
Note
This article is up to date as of 03/02/2017 and developed with consideration for cTrader FIX engine, Rules of Engagement v2.9.1.