forked from Marfusios/bitmex-client-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
186 lines (150 loc) · 7.43 KB
/
Program.cs
File metadata and controls
186 lines (150 loc) · 7.43 KB
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
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 Serilog;
using Serilog.Events;
namespace Bitmex.Client.Websocket.Sample
{
class Program
{
private static readonly ManualResetEvent ExitEvent = new ManualResetEvent(false);
private static readonly string API_KEY = "your api key";
private static readonly string API_SECRET = "";
static void Main(string[] args)
{
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))
{
communicator.ReconnectTimeoutMs = (int)TimeSpan.FromSeconds(30).TotalMilliseconds;
communicator.ReconnectionHappened.Subscribe(type =>
Log.Information($"Reconnection happened, type: {type}"));
using (var client = new BitmexWebsocketClient(communicator))
{
client.Streams.InfoStream.Subscribe(info =>
{
Log.Information($"Reconnection happened, Message: {info.Info}, Version: {info.Version:D}");
SendSubscriptionRequests(client).Wait();
});
SubscribeToStreams(client);
communicator.Start();
ExitEvent.WaitOne();
}
}
Log.Debug("====================================");
Log.Debug(" STOPPING ");
Log.Debug("====================================");
Log.CloseAndFlush();
}
private static async Task SendSubscriptionRequests(BitmexWebsocketClient client)
{
await client.Send(new PingRequest());
//await client.Send(new BookSubscribeRequest());
await client.Send(new TradesSubscribeRequest("XBTUSD"));
await client.Send(new TradeBinSubscribeRequest("1m", "XBTUSD"));
//await client.Send(new TradeBinSubscribeRequest("5m", "XBTUSD"));
//await client.Send(new QuoteSubscribeRequest("XBTUSD"));
await client.Send(new LiquidationSubscribeRequest());
if (!string.IsNullOrWhiteSpace(API_SECRET))
await client.Send(new AuthenticationRequest(API_KEY, API_SECRET));
}
private static void SubscribeToStreams(BitmexWebsocketClient client)
{
client.Streams.ErrorStream.Subscribe(x =>
Log.Warning($"Error received, message: {x.Error}, status: {x.Status}"));
client.Streams.AuthenticationStream.Subscribe(x =>
{
Log.Information($"Authentication happened, success: {x.Success}");
client.Send(new WalletSubscribeRequest()).Wait();
client.Send(new OrderSubscribeRequest()).Wait();
client.Send(new PositionSubscribeRequest()).Wait();
});
client.Streams.SubscribeStream.Subscribe(x =>
Log.Information($"Subscribed ({x.Success}) to {x.Subscribe}"));
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} 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}, " +
$"Price: {x.LastPrice}, PNL: {x.UnrealisedPnl}"))
);
client.Streams.TradesStream.Subscribe(y =>
y.Data.ToList().ForEach(x =>
Log.Information($"Trade {x.Symbol} executed. Time: {x.Timestamp:mm:ss.fff}, Amount: {x.Size}, " +
$"Price: {x.Price}, Direction: {x.TickDirection}"))
);
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.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(
$"Liquadation 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}"))
);
}
private static void InitLogging()
{
var executingDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var logPath = Path.Combine(executingDir, "logs", "verbose.log");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day)
.WriteTo.ColoredConsole(LogEventLevel.Information)
.CreateLogger();
}
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();
}
}
}