-
Notifications
You must be signed in to change notification settings - Fork 63
/
BybitInversePerpetualSymbolOrderBook.cs
118 lines (102 loc) · 4.07 KB
/
BybitInversePerpetualSymbolOrderBook.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bybit.Net.Objects.Models.Socket;
using Bybit.Net.Objects;
using Bybit.Net.Objects.Models;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.OrderBook;
using CryptoExchange.Net.Sockets;
using Microsoft.Extensions.Logging;
using Bybit.Net.Interfaces.Clients;
using Bybit.Net.Clients;
namespace Bitfinex.Net.SymbolOrderBooks
{
/// <summary>
/// Live order book implementation
/// </summary>
public class BybitInversePerpetualSymbolOrderBook : SymbolOrderBook
{
private readonly IBybitSocketClient socketClient;
private readonly bool _socketOwner;
/// <summary>
/// Create a new order book instance
/// </summary>
/// <param name="symbol">The symbol the order book is for</param>
/// <param name="options">Options for the order book</param>
public BybitInversePerpetualSymbolOrderBook(string symbol, BybitFuturesSymbolOrderBookOptions? options = null) : base("Bybit", symbol, options ?? new BybitFuturesSymbolOrderBookOptions())
{
socketClient = options?.SocketClient ?? new BybitSocketClient(new BybitSocketClientOptions
{
LogLevel = options?.LogLevel ?? LogLevel.Information
});
_socketOwner = options?.SocketClient == null;
Levels = options?.Limit ?? 25;
}
/// <inheritdoc />
protected override async Task<CallResult<UpdateSubscription>> DoStartAsync()
{
var result = await socketClient.InversePerpetualStreams.SubscribeToOrderBookUpdatesAsync(Symbol, Levels!.Value, ProcessSnapshot, ProcessUpdate).ConfigureAwait(false);
if (!result)
return result;
Status = OrderBookStatus.Syncing;
var setResult = await WaitForSetOrderBookAsync(30000).ConfigureAwait(false);
return setResult ? result : new CallResult<UpdateSubscription>(null, setResult.Error);
}
/// <inheritdoc />
protected override void DoReset()
{
}
private void ProcessSnapshot(DataEvent<IEnumerable<BybitOrderBookEntry>> snapshot)
{
var askEntries = snapshot.Data.Where(e => e.Side == Bybit.Net.Enums.OrderSide.Sell).ToList();
var bidEntries = snapshot.Data.Where(e => e.Side == Bybit.Net.Enums.OrderSide.Buy).ToList();
SetInitialOrderBook(DateTime.UtcNow.Ticks, bidEntries, askEntries);
}
private void ProcessUpdate(DataEvent<BybitDeltaUpdate<BybitOrderBookEntry>> data)
{
var bidEntries = new List<BybitOrderBookEntry>();
var askEntries = new List<BybitOrderBookEntry>();
foreach(var item in data.Data.Insert)
{
if (item.Side == Bybit.Net.Enums.OrderSide.Buy)
bidEntries.Add(item);
else
askEntries.Add(item);
}
foreach (var item in data.Data.Update)
{
if (item.Side == Bybit.Net.Enums.OrderSide.Buy)
bidEntries.Add(item);
else
askEntries.Add(item);
}
foreach (var item in data.Data.Delete)
{
item.Quantity = 0;
if (item.Side == Bybit.Net.Enums.OrderSide.Buy)
bidEntries.Add(item);
else
askEntries.Add(item);
}
UpdateOrderBook(DateTime.UtcNow.Ticks, bidEntries, askEntries);
}
/// <inheritdoc />
protected override async Task<CallResult<bool>> DoResyncAsync()
{
return await WaitForSetOrderBookAsync(30000).ConfigureAwait(false);
}
/// <summary>
/// Dispose
/// </summary>
public override void Dispose()
{
processBuffer.Clear();
asks.Clear();
bids.Clear();
if(_socketOwner)
socketClient?.Dispose();
}
}
}