-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathProgram.cs
242 lines (200 loc) · 9.93 KB
/
Program.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
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;
using Bitmex.Client.Websocket.Client;
using Bitmex.Client.Websocket.Requests;
using Bitmex.Client.Websocket.Websockets;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;
namespace Bitmex.Client.Websocket.Sample
{
class Program
{
private static readonly ManualResetEvent _exitEvent = new(false);
private const string ApiKey = "your_api_key";
private const string ApiSecret = "";
static void Main(string[] args)
{
var logger = InitLogging();
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
AssemblyLoadContext.Default.Unloading += DefaultOnUnloading;
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
Console.WriteLine("|=======================|");
Console.WriteLine("| BITMEX CLIENT |");
Console.WriteLine("|=======================|");
Console.WriteLine();
Log.Debug("====================================");
Log.Debug(" STARTING ");
Log.Debug("====================================");
var url = BitmexValues.ApiWebsocketUrl;
using (var communicator = new BitmexWebsocketCommunicator(url, logger.CreateLogger<BitmexWebsocketCommunicator>()))
{
communicator.Name = "Bitmex-1";
communicator.ReconnectTimeout = TimeSpan.FromMinutes(10);
communicator.ReconnectionHappened.Subscribe(type =>
Log.Information("Reconnection happened, type: {type}", type.Type));
using (var client = new BitmexWebsocketClient(communicator, logger.CreateLogger<BitmexWebsocketClient>()))
{
client.Streams.InfoStream.Subscribe(info =>
{
Log.Information("Reconnection happened, Message: {info}, Version: {version}", info.Info, info.Version);
SendSubscriptionRequests(client);
});
SubscribeToStreams(client);
communicator.Start();
_ = StartPinging(client);
_exitEvent.WaitOne();
}
}
Log.Debug("====================================");
Log.Debug(" STOPPING ");
Log.Debug("====================================");
Log.CloseAndFlush();
}
private static async Task StartPinging(BitmexWebsocketClient client)
{
await Task.Delay(TimeSpan.FromSeconds(30));
client.Send(new PingRequest());
}
private static void SendSubscriptionRequests(BitmexWebsocketClient client)
{
client.Send(new PingRequest());
//client.Send(new BookSubscribeRequest("XBTUSD"));
client.Send(new TradesSubscribeRequest("XBTUSD"));
//client.Send(new TradeBinSubscribeRequest("1m", "XBTUSD"));
//client.Send(new TradeBinSubscribeRequest("5m", "XBTUSD"));
client.Send(new QuoteSubscribeRequest("XBTUSD"));
client.Send(new LiquidationSubscribeRequest());
client.Send(new InstrumentSubscribeRequest("XBTUSD"));
client.Send(new FundingsSubscribeRequest("XBTUSD"));
//client.Send(new Book25SubscribeRequest("XBTUSD"));
//client.Send(new Book10SubscribeRequest("XBTUSD"));
if (!string.IsNullOrWhiteSpace(ApiSecret))
client.Send(new AuthenticationRequest(ApiKey, ApiSecret));
}
private static void SubscribeToStreams(BitmexWebsocketClient client)
{
client.Streams.ErrorStream.Subscribe(x =>
Log.Warning("Error received, message: {error}, status: {status}", x.Error, x.Status));
client.Streams.AuthenticationStream.Subscribe(x =>
{
Log.Information($"Authentication happened, success: {x.Success}");
client.Send(new WalletSubscribeRequest());
client.Send(new OrderSubscribeRequest());
client.Send(new PositionSubscribeRequest());
});
client.Streams.SubscribeStream.Subscribe(x =>
{
Log.Information(x.IsSubscription
? $"Subscribed ({x.Success}) to {x.Subscribe}"
: $"Unsubscribed ({x.Success}) from {x.Unsubscribe}");
});
client.Streams.PongStream.Subscribe(x =>
Log.Information($"Pong received ({x.Message})"));
client.Streams.WalletStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information($"Wallet {x.Account}, {x.Currency} amount: {x.BalanceBtc}"))
);
client.Streams.OrderStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information(
$"Order {x.Symbol} {x.OrderId} updated. Time: {x.Timestamp:HH:mm:ss.fff}, Amount: {x.OrderQty}, " +
$"Price: {x.Price}, Direction: {x.Side}, Working: {x.WorkingIndicator}, Status: {x.OrdStatus}"))
);
client.Streams.PositionStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information(
$"Position {x.Symbol}, {x.Currency} updated. Time: {x.Timestamp:HH:mm:ss.fff}, Amount: {x.CurrentQty}, " +
$"Entry: {x.AvgEntryPrice}, Price: {x.LastPrice}, Liq: {x.LiquidationPrice}, " +
$"PNL: {x.UnrealisedPnl}"))
);
client.Streams.TradesStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information($"Trade {x.Symbol} executed. Time: {x.Timestamp:HH:mm:ss.fff}, [{x.Side}] Amount: {x.Size}, " +
$"Price: {x.Price}, Match: {x.TrdMatchId}"))
);
client.Streams.BookStream.Subscribe(book =>
book.Data.Take(100).ToList().ForEach(x => Log.Information(
$"Book | {book.Action} pair: {x.Symbol}, price: {x.Price}, amount {x.Size}, side: {x.Side}"))
);
client.Streams.Book25Stream.Subscribe(book =>
book.Data.Take(100).ToList().ForEach(x => Log.Information(
$"Book | {book.Action} pair: {x.Symbol}, price: {x.Price}, amount {x.Size}, side: {x.Side}"))
);
client.Streams.QuoteStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information($"Quote {x.Symbol}. Bid: {x.BidPrice} - {x.BidSize} Ask: {x.AskPrice} - {x.AskSize}"))
);
client.Streams.LiquidationStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information(
$"Liquidation Action: {y.Action}, OrderID: {x.OrderID}, Symbol: {x.Symbol}, Side: {x.Side}, Price: {x.Price}, LeavesQty: {x.leavesQty}"))
);
client.Streams.TradeBinStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information($"TradeBin table:{y.Table} {x.Symbol} executed. Time: {x.Timestamp:mm:ss.fff}, Open: {x.Open}, " +
$"Close: {x.Close}, Volume: {x.Volume}, Trades: {x.Trades}"))
);
client.Streams.InstrumentStream.Subscribe(x =>
{
x.Data.ToList().ForEach(y =>
{
Log.Information($"Instrument, {y.Symbol}, " +
$"price: {y.MarkPrice}, last: {y.LastPrice}, " +
$"mark: {y.MarkMethod}, fair: {y.FairMethod}, direction: {y.LastTickDirection}, " +
$"funding: {y.FundingRate} i: {y.IndicativeFundingRate} s: {y.FundingQuoteSymbol}");
});
});
client.Streams.FundingStream.Subscribe(x =>
{
x.Data.ToList().ForEach(f =>
{
Log.Information($"Funding {f.Symbol}, Timestamp: {f.Timestamp}, Interval: {f.FundingInterval}, " +
$"Rate: {f.FundingRate}, RateDaily: {f.FundingRateDaily}");
});
});
// example of unsubscribe requests
//Task.Run(async () =>
//{
// await Task.Delay(5000);
// client.Send(new BookSubscribeRequest("XBTUSD") { IsUnsubscribe = true });
// await Task.Delay(5000);
// client.Send(new TradesSubscribeRequest() { IsUnsubscribe = true });
//});
}
private static SerilogLoggerFactory InitLogging()
{
var executingDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var logPath = Path.Combine(executingDir, "logs", "verbose.log");
var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day)
.WriteTo.Console(LogEventLevel.Information)
.CreateLogger();
Log.Logger = logger;
return new SerilogLoggerFactory(logger);
}
private static void CurrentDomainOnProcessExit(object sender, EventArgs eventArgs)
{
Log.Warning("Exiting process");
_exitEvent.Set();
}
private static void DefaultOnUnloading(AssemblyLoadContext assemblyLoadContext)
{
Log.Warning("Unloading process");
_exitEvent.Set();
}
private static void ConsoleOnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Log.Warning("Canceling process");
e.Cancel = true;
_exitEvent.Set();
}
}
}