-
Notifications
You must be signed in to change notification settings - Fork 42
/
FIX API Sample.cs
257 lines (227 loc) · 11 KB
/
FIX API Sample.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using FIX_API_Library;
using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace FIX_API_Sample
{
public partial class frmFIXAPISample : Form
{
private int _pricePort = 5211;
private int _tradePort = 5212;
private string _host = "h28.p.ctrader.com";
private string _username = "3006156";
private string _password = "sp0tw@re";
private string _senderCompID = "sales.3006156";
private string _senderSubID = "3006156";
private string _targetCompID = "CSERVER";
private int _messageSequenceNumber = 1;
private int _testRequestID = 1;
private TcpClient _priceClient;
private SslStream _priceStreamSSL;
private TcpClient _tradeClient;
private SslStream _tradeStreamSSL;
private MessageConstructor _messageConstructor;
public frmFIXAPISample()
{
InitializeComponent();
_priceClient = new TcpClient(_host, _pricePort);
_priceStreamSSL = new SslStream(_priceClient.GetStream(), false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
_priceStreamSSL.AuthenticateAsClient(_host);
_tradeClient = new TcpClient(_host, _tradePort);
_tradeStreamSSL = new SslStream(_tradeClient.GetStream(), false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
_tradeStreamSSL.AuthenticateAsClient(_host);
_messageConstructor = new MessageConstructor(_host, _username,
_password, _senderCompID, _senderSubID, _targetCompID);
}
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
return false;
}
private void ClearText()
{
txtMessageSend.Text = "";
txtMessageReceived.Text = "";
}
private string SendPriceMessage(string message, bool readResponse = true)
{
return SendMessage(message, _priceStreamSSL, readResponse);
}
private string SendTradeMessage(string message, bool readResponse = true)
{
return SendMessage(message, _tradeStreamSSL, readResponse);
}
private string SendMessage(string message, SslStream stream, bool readResponse = true)
{
var byteArray = Encoding.ASCII.GetBytes(message);
stream.Write(byteArray, 0, byteArray.Length);
var buffer = new byte[1024];
if (readResponse)
{
Thread.Sleep(100);
stream.Read(buffer, 0, 1024);
}
_messageSequenceNumber++;
var returnMessage = Encoding.ASCII.GetString(buffer);
return returnMessage;
}
private void btnLogon_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.LogonMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, 30, false);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
}
private void btnTestRequest_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.TestRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, _testRequestID);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
}
private void btnLogout_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.LogoutMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
_messageSequenceNumber = 1;
}
private void btnMarketDataRequest_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.MarketDataRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, "EURUSD:WDqsoT", 1, 0, 0, 1, 1);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
}
private void btnHeartbeat_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.HeartbeatMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message,false);
}
private void btnResendRequest_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.ResendMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, _messageSequenceNumber - 1);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
}
private void btnReject_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.RejectMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, 0);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
}
private void btnSequenceReset_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.SequenceResetMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, 0);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
}
private void btnNewOrderSingle_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "1408471", 1, 1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, 1, "1");
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
private void btnOrderStatusRequest_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.OrderStatusRequest(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "1408471");
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
private string Timestamp()
{
return (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
}
private void btnRequestForPositions_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.RequestForPositions(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "1408471");
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
private void btnLogonT_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.LogonMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, 30, false);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
private void btnHeartbeatT_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.HeartbeatMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message, false);
}
private void btnTestRequestT_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.TestRequestMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, _testRequestID);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
private void btnLogoutT_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.LogoutMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
_messageSequenceNumber = 1;
}
private void btnResendRequestT_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.ResendMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, _messageSequenceNumber - 1);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
private void btnSpotMarketData_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.MarketDataRequestMessage(MessageConstructor.SessionQualifier.QUOTE, _messageSequenceNumber, "EURUSD:WDqsoT", 1, 1, 0, 1, 1);
txtMessageSend.Text = message;
txtMessageReceived.Text = SendPriceMessage(message);
}
private void btnStopOrder_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "10", 1, 1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, 3, "3", 0, (decimal)1.08);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
private void btnLimitOrder_Click(object sender, EventArgs e)
{
ClearText();
var message = _messageConstructor.NewOrderSingleMessage(MessageConstructor.SessionQualifier.TRADE, _messageSequenceNumber, "10", 1, 1, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss"), 1000, 2, "3", (decimal)1.08);
_testRequestID++;
txtMessageSend.Text = message;
txtMessageReceived.Text = SendTradeMessage(message);
}
}
}