Skip to content

Commit b931bb5

Browse files
author
codez0mb1e
committed
Merge branch 'master' into develop
2 parents 8ca2d90 + f10ad97 commit b931bb5

15 files changed

Lines changed: 230 additions & 159 deletions

File tree

src/BinanceBot.Market/BaseMarketBot.cs renamed to src/BinanceBot.Market/Abstracts/BaseMarketBot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected BaseMarketBot(string symbol, TStrategy marketStrategy, Logger logger)
3434

3535
public abstract void Stop();
3636

37-
public abstract Task ValidateConnectionAsync();
37+
public abstract Task ValidateServerTimeAsync();
3838

3939
public abstract Task<IEnumerable<BinanceOrder>> GetOpenedOrdersAsync(string symbol);
4040

src/BinanceBot.Market/IMarketBot.cs renamed to src/BinanceBot.Market/Abstracts/IMarketBot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface IMarketBot
3030
/// <summary>
3131
/// Validate connection w/ stock
3232
/// </summary>
33-
Task ValidateConnectionAsync();
33+
Task ValidateServerTimeAsync();
3434

3535
/// <summary>
3636
/// Get currently opened orders

src/BinanceBot.Market/IMarketDepthPublisher.cs renamed to src/BinanceBot.Market/Abstracts/IMarketDepthPublisher.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using BinanceBot.Market.Core;
34

45
namespace BinanceBot.Market
56
{
File renamed without changes.

src/BinanceBot.Market/MarketStrategyConfiguration.cs renamed to src/BinanceBot.Market/Configurations/MarketStrategyConfiguration.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace BinanceBot.Market
3+
namespace BinanceBot.Market.Configurations
44
{
55
/// <summary>
66
/// Market Strategy configuration
@@ -9,7 +9,7 @@ namespace BinanceBot.Market
99
/// <see cref="MarketStrategyConfiguration"/> limits must not contradict Stock limits.
1010
/// Binance limits: <see href="https://api.binance.com/api/v1/exchangeInfo"/>
1111
/// </remarks>
12-
public class MarketStrategyConfiguration
12+
public record MarketStrategyConfiguration
1313
{
1414
/// <summary>
1515
/// Start trading when spread greater than that values (in percentage point)
@@ -27,6 +27,17 @@ public class MarketStrategyConfiguration
2727
/// Maximum order volume
2828
/// </summary>
2929
public decimal MaxOrderVolume { get; set; }
30+
31+
32+
/// <summary>
33+
/// Precision of the quote asset
34+
/// </summary>
35+
public int QuoteAssetPrecision { get; set; }
36+
37+
/// <summary>
38+
/// Price precision
39+
/// </summary>
40+
public int PricePrecision { get; set; }
3041
#endregion
3142

3243

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
using Binance.Net.Objects.Models;
66
using BinanceBot.Market.Utility;
77

8-
9-
namespace BinanceBot.Market
8+
namespace BinanceBot.Market.Core
109
{
1110
/// <summary>
1211
/// Order book
@@ -72,11 +71,25 @@ public MarketDepth(string symbol)
7271

7372

7473
#region Update depth section
75-
private const decimal IgnoreVolumeValue = 0.00000000M;
74+
private const decimal IgnoreVolumeValue = 1e-11M;
7675

7776
/// <summary>
7877
/// Update market depth
7978
/// </summary>
79+
/// <remarks>
80+
/// How to manage a local order book correctly [1]:
81+
/// 1. Open a stream to wss://stream.binance.com:9443/ws/bnbbtc@depth
82+
/// 2. Buffer the events you receive from the stream
83+
/// 3. Get a depth snapshot from https://www.binance.com/api/v1/depth?symbol=BNBBTC&amp;limit=1000
84+
/// -> 4. Drop any event where u is less or equal lastUpdateId in the snapshot
85+
/// 5. The first processed should have U less or equal lastUpdateId+1 AND u equal or greater lastUpdateId+1
86+
/// -> 6. While listening to the stream, each new event's U should be equal to the previous event's u+1
87+
/// -> 7. The data in each event is the absolute quantity for a price level
88+
/// -> 8. If the quantity is 0, remove the price level
89+
/// 9. Receiving an event that removes a price level that is not in your local order book can happen and is normal.
90+
/// Reference:
91+
/// 1. https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#how-to-manage-a-local-order-book-correctly
92+
/// </remarks>
8093
public void UpdateDepth(IEnumerable<BinanceOrderBookEntry> asks, IEnumerable<BinanceOrderBookEntry> bids, long updateTime)
8194
{
8295
if (updateTime <= 0)
@@ -89,14 +102,19 @@ public void UpdateDepth(IEnumerable<BinanceOrderBookEntry> asks, IEnumerable<Bin
89102

90103
static void UpdateOrderBook(IEnumerable<BinanceOrderBookEntry> updates, IDictionary<decimal, decimal> orders)
91104
{
105+
if (orders == null) throw new ArgumentNullException(nameof(orders));
92106
if (updates == null) return;
93107

108+
// WARN: clean orders in cases when connector received orderbook snapshots instead of orderbook updates
109+
// orders.Clear();
110+
111+
// update order book
94112
foreach (BinanceOrderBookEntry t in updates)
95113
{
96-
if (t.Quantity != IgnoreVolumeValue)
114+
if (t.Quantity > IgnoreVolumeValue)
97115
orders[t.Price] = t.Quantity;
98116
else
99-
if (orders.ContainsKey(t.Price)) orders.Remove(t.Price);
117+
if (orders.ContainsKey(t.Price)) orders.Remove(t.Price);
100118
}
101119
}
102120

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
3+
namespace BinanceBot.Market.Core
4+
{
5+
/// <summary>
6+
/// Order book's ask-bid pair
7+
/// </summary>
8+
public record MarketDepthPair
9+
{
10+
public MarketDepthPair(Quote ask, Quote bid, long updateTime)
11+
{
12+
if (ask == null)
13+
throw new ArgumentNullException(nameof(ask));
14+
if (bid == null)
15+
throw new ArgumentNullException(nameof(bid));
16+
if (ask.Price < bid.Price)
17+
throw new ArgumentNullException(nameof(bid), "Best sell price (ask) cannot be less the best buy price (bid)");
18+
if (updateTime <= 0)
19+
throw new ArgumentOutOfRangeException(nameof(updateTime));
20+
21+
Ask = ask;
22+
Bid = bid;
23+
UpdateTime = updateTime;
24+
}
25+
26+
27+
public Quote Ask { get; }
28+
29+
public Quote Bid { get; }
30+
31+
public long UpdateTime { get; }
32+
33+
/// <summary>
34+
/// Flag that <see cref="MarketDepth"/> has orders on 2 sides
35+
/// </summary>
36+
public bool IsFull => Ask != null && Bid != null;
37+
38+
public decimal? PriceSpread => IsFull ? Ask.Price - Bid.Price : default;
39+
40+
public decimal? VolumeSpread => IsFull ? Math.Abs(Ask.Volume - Bid.Volume) : default;
41+
42+
public decimal? MediumPrice => IsFull ? (Ask.Price + Bid.Price)/2 : default;
43+
}
44+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using Binance.Net.Enums;
33

4-
namespace BinanceBot.Market
4+
namespace BinanceBot.Market.Core
55
{
66
/// <summary>
77
/// <see cref="MarketDepth"/> quote representing bid or ask
88
/// </summary>
9-
public class Quote
9+
public record Quote
1010
{
1111
public Quote(decimal price, decimal volume, OrderSide direction)
1212
{

src/BinanceBot.Market/MarketDepthManager.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace BinanceBot.Market
1414
/// How to manage a local order book correctly [1]:
1515
/// 1. Open a stream to wss://stream.binance.com:9443/ws/bnbbtc@depth
1616
/// 2. Buffer the events you receive from the stream
17-
/// 3. Get a depth snapshot from https://www.binance.com/api/v1/depth?symbol=BNBBTC&amp;limit=1000
17+
/// -> 3. Get a depth snapshot from https://www.binance.com/api/v1/depth?symbol=BNBBTC&amp;limit=1000
1818
/// 4. Drop any event where u is less or equal lastUpdateId in the snapshot
1919
/// 5. The first processed should have U less or equal lastUpdateId+1 AND u equal or greater lastUpdateId+1
2020
/// 6. While listening to the stream, each new event's U should be equal to the previous event's u+1
@@ -49,7 +49,7 @@ public MarketDepthManager(IBinanceClient binanceRestClient, IBinanceSocketClient
4949
/// </summary>
5050
/// <param name="marketDepth">Market depth</param>
5151
/// <param name="limit">Limit of returned orders count</param>
52-
public async Task BuildAsync(MarketDepth marketDepth, int limit = 100)
52+
public async Task BuildAsync(MarketDepth marketDepth, short limit = 10)
5353
{
5454
if (marketDepth == null)
5555
throw new ArgumentNullException(nameof(marketDepth));
@@ -67,14 +67,15 @@ public async Task BuildAsync(MarketDepth marketDepth, int limit = 100)
6767
/// Stream <see cref="MarketDepth"/> updates
6868
/// </summary>
6969
/// <param name="marketDepth">Market depth</param>
70-
public void StreamUpdates(MarketDepth marketDepth)
70+
/// <param name="updateInterval"></param>
71+
public void StreamUpdates(MarketDepth marketDepth, TimeSpan? updateInterval = default)
7172
{
7273
if (marketDepth == null)
7374
throw new ArgumentNullException(nameof(marketDepth));
7475

7576
_webSocketClient.SpotStreams.SubscribeToOrderBookUpdatesAsync(
7677
marketDepth.Symbol,
77-
1000,
78+
(int)updateInterval?.TotalMilliseconds,
7879
marketData => marketDepth.UpdateDepth(marketData.Data.Asks, marketData.Data.Bids, marketData.Data.LastUpdateId));
7980
}
8081
}

src/BinanceBot.Market/MarketDepthPair.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)