-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
191 lines (159 loc) · 7.99 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
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;
using Bitstamp.Client.Websocket.Channels;
using Bitstamp.Client.Websocket.Client;
using Bitstamp.Client.Websocket.Communicator;
using Bitstamp.Client.Websocket.Requests;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;
namespace Bitstamp.Client.Websocket.Sample
{
internal class Program
{
private static readonly ManualResetEvent ExitEvent = new ManualResetEvent(false);
private static readonly string ApiKey = "your api key";
private static readonly string ApiSecret = "";
private static async Task Main(string[] args)
{
var logger = InitLogging();
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
AssemblyLoadContext.Default.Unloading += DefaultOnUnloading;
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
Console.WriteLine("|=======================|");
Console.WriteLine("| BITSTAMP CLIENT |");
Console.WriteLine("|=======================|");
Console.WriteLine();
Log.Debug("====================================");
Log.Debug(" STARTING ");
Log.Debug("====================================");
var url = BitstampValues.ApiWebsocketUrl;
using (var communicator = new BitstampWebsocketCommunicator(url, logger.CreateLogger<BitstampWebsocketCommunicator>()))
{
communicator.Name = "Bitstamp-1";
using (var client = new BitstampWebsocketClient(communicator, logger.CreateLogger<BitstampWebsocketClient>()))
{
SubscribeToStreams(client);
communicator.ReconnectionHappened.Subscribe(async type =>
{
Log.Information("Reconnection happened, type: {type}, resubscribing..", type.Type);
await SendSubscriptionRequests(client);
});
await communicator.Start();
ExitEvent.WaitOne();
}
}
Log.Debug("====================================");
Log.Debug(" STOPPING ");
Log.Debug("====================================");
Log.CloseAndFlush();
}
private static async Task SendSubscriptionRequests(BitstampWebsocketClient client)
{
//client.Send(new SubscribeRequest("btcusd", Channel.OrderBook));
//client.Send(new SubscribeRequest("btceur", Channel.OrderBook));
//client.Send(new SubscribeRequest("btcusd", Channel.OrderBookDetail));
//client.Send(new SubscribeRequest("btceur", Channel.OrderBookDetail));
//client.Send(new SubscribeRequest("btcusd", Channel.OrderBookDiff));
//client.Send(new SubscribeRequest("btceur", Channel.OrderBookDiff));
client.Send(new SubscribeRequest("btcusd", Channel.Ticker));
client.Send(new SubscribeRequest("btceur", Channel.Ticker));
}
private static void SubscribeToStreams(BitstampWebsocketClient client)
{
client.Streams.ErrorStream.Subscribe(x =>
Log.Warning($"Error received, message: {x?.Message}"));
client.Streams.SubscriptionSucceededStream.Subscribe(x =>
{
Log.Information($"Subscribed to {x?.Symbol} {x?.Channel}");
});
client.Streams.UnsubscriptionSucceededStream.Subscribe(x =>
{
Log.Information($"Unsubscribed from {x?.Symbol} {x?.Channel}");
});
client.Streams.OrderBookStream.Subscribe(x =>
{
Log.Information($"Order book L2 [{x.Symbol}]");
Log.Information($" {x.Data?.Asks.FirstOrDefault()?.Price} " +
$"{x.Data?.Asks.FirstOrDefault()?.Amount ?? 0} " +
$"{x.Data?.Asks.FirstOrDefault()?.Side} " +
$"({x.Data?.Asks?.Length})");
Log.Information($" {x.Data?.Bids.FirstOrDefault()?.Price} " +
$"{x.Data?.Bids.FirstOrDefault()?.Amount ?? 0} " +
$"{x.Data?.Bids.FirstOrDefault()?.Side} " +
$"({x.Data?.Bids?.Length})");
});
client.Streams.OrdersStream.Subscribe(x =>
{
//Log.Information($"{x.Data} {x.Data.Asks[0]} {x.Data.EventBids[0]}");
//Log.Information($"{x.Symbol} {x.Data.Channel} {x.Data.Amount}");
});
client.Streams.OrderBookDetailStream.Subscribe(x =>
{
Log.Information($"Order book L3 [{x.Symbol}]");
Log.Information($" {x.Data?.Asks.FirstOrDefault()?.Price} " +
$"{x.Data?.Asks.FirstOrDefault()?.Amount ?? 0} " +
$"{x.Data?.Asks.FirstOrDefault()?.Side} " +
$"({x.Data?.Asks?.Length}) " +
$"id: {x.Data?.Asks?.FirstOrDefault()?.OrderId}");
Log.Information($" {x.Data?.Bids.FirstOrDefault()?.Price} " +
$"{x.Data?.Bids.FirstOrDefault()?.Amount ?? 0} " +
$"{x.Data?.Bids.FirstOrDefault()?.Side} " +
$"({x.Data?.Bids?.Length}) " +
$"id: {x.Data?.Bids?.FirstOrDefault()?.OrderId}");
});
client.Streams.OrderBookDiffStream.Subscribe(x =>
{
Log.Information($"Order book L2 diffs [{x.Symbol}]");
Log.Information($" updates {x.Data?.Asks.Count(y => y.Amount > 0)} " +
$"deletes {x.Data?.Asks.Count(y => y.Amount <= 0)} " +
$"{x.Data?.Asks.FirstOrDefault()?.Side} " +
$"({x.Data?.Asks?.Length}) ");
Log.Information($" updates {x.Data?.Bids.Count(y => y.Amount > 0)} " +
$"deletes {x.Data?.Bids.Count(y => y.Amount <= 0)} " +
$"{x.Data?.Bids.FirstOrDefault()?.Side} " +
$"({x.Data?.Bids?.Length}) ");
});
client.Streams.HeartbeatStream.Subscribe(x =>
Log.Information($"Heartbeat received, product: {x?.Channel}, seq: {x?.Event}"));
client.Streams.TickerStream.Subscribe(x =>
{
Log.Information($"Trade executed [{x.Symbol}] {x.Data?.Side} price: {x.Data?.Price} size: {x.Data?.Amount}");
});
}
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.Debug)
.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();
}
}
}