Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Oct 28, 2024
1 parent 7c06f4a commit a64143c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 20 deletions.
25 changes: 5 additions & 20 deletions Examples/CryptoClients.Examples.Blazor/Pages/Books.razor
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
@page "/books"
@using CryptoExchange.Net.Interfaces
@using CryptoExchange.Net.SharedApis
@inject IExchangeOrderBookFactory BookFactory
@implements IDisposable

<PageTitle>Live Local Order Books</PageTitle>

<div>
<h2>@_baseAsset @_quoteAsset live books</h2>
<h2>BTC USDT live books</h2>

@foreach(var book in _books)
{
Expand Down Expand Up @@ -44,31 +45,15 @@
{
private Dictionary<string, ISymbolOrderBook> _books = new Dictionary<string, ISymbolOrderBook>();

private string _baseAsset = "BTC";
private string _quoteAsset = "USDT";
private Timer? _timer;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_books = new Dictionary<string, ISymbolOrderBook>
{
{ Exchange.Binance, BookFactory.Binance.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.BingX, BookFactory.BingX.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.Bitfinex, BookFactory.Bitfinex.Spot.Create(_baseAsset, "UST") },
{ Exchange.Bitget, BookFactory.Bitget.Spot.Create(_baseAsset, _quoteAsset) },
//{ Exchange.BitMart, BookFactory.BitMart.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.Bybit, BookFactory.Bybit.Spot.Create(_baseAsset, _quoteAsset) },
//{ Exchange.Coinbase, BookFactory.Coinbase.AdvancedTrade.Create(_baseAsset, _quoteAsset) },
{ Exchange.CoinEx, BookFactory.CoinEx.Spot.Create(_baseAsset, _quoteAsset) },
//{ Exchange.GateIo, BookFactory.GateIo.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.HTX, BookFactory.HTX.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.Kraken, BookFactory.Kraken.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.Kucoin, BookFactory.Kucoin.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.Mexc, BookFactory.Mexc.Spot.Create(_baseAsset, _quoteAsset) },
{ Exchange.OKX, BookFactory.OKX.Unified.Create(_baseAsset, _quoteAsset) },
};
var symbol = new SharedSymbol(TradingMode.Spot, "BTC", "USDT");
foreach (var exchange in Exchange.All)
_books.Add(exchange, BookFactory.Create(exchange, symbol, 5)!);

var tasks = new List<Task>();
foreach (var book in _books.Values)
Expand Down
85 changes: 85 additions & 0 deletions Examples/CryptoClients.Examples.Blazor/Pages/Trackers.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
@page "/trackers"
@using CryptoExchange.Net.Interfaces
@using CryptoExchange.Net.SharedApis
@using CryptoExchange.Net.Trackers.Trades
@inject IExchangeTrackerFactory TrackerFactory
@implements IDisposable

<PageTitle>Live Trade Tracking</PageTitle>

<div>
<h2>BTC USDT Live Trade Tracking</h2>

@foreach (var tracker in _trackers)
{
<div style="margin-top: 20px">
<b>@tracker.Exchange - @tracker.SymbolName [@tracker.Status]</b>
<table style="border: 1px solid;">
<tr>
<th></th>
<th style="padding: 0 10px;">-2 min</th>
<th style="padding: 0 10px;">-1 min</th>
<th style="padding: 0 10px;">last min</th>
<th style="padding: 0 10px;">Dif -2 to now</th>
</tr>

@{
var stats2Min = tracker.GetStats(DateTime.UtcNow.AddMinutes(-3), DateTime.UtcNow.AddMinutes(-2));
var stats1Min = tracker.GetStats(DateTime.UtcNow.AddMinutes(-2), DateTime.UtcNow.AddMinutes(-1));
var statsNow = tracker.GetStats(DateTime.UtcNow.AddMinutes(-1));
var compare = stats2Min.CompareTo(statsNow);

<tr>
<td>Trades</td>
<td style="padding: 0 10px;border: 1px solid;">@stats2Min.TradeCount</td>
<td style="padding: 0 10px;border: 1px solid;">@stats1Min.TradeCount</td>
<td style="padding: 0 10px;border: 1px solid;">@statsNow.TradeCount</td>
<td style="padding: 0 10px;border: 1px solid;">@compare.TradeCountDif.Difference</td>
</tr>
<tr>
<td>Avg Price</td>
<td style="padding: 0 10px;border: 1px solid;">@stats2Min.VolumeWeightedAveragePrice</td>
<td style="padding: 0 10px;border: 1px solid;">@stats1Min.VolumeWeightedAveragePrice</td>
<td style="padding: 0 10px;border: 1px solid;">@statsNow.VolumeWeightedAveragePrice</td>
<td style="padding: 0 10px;border: 1px solid;">@compare.VolumeWeightedAveragePriceDif?.PercentageDifference%</td>
</tr>
}
</table>
</div>
}
</div>

@code
{
private List<ITradeTracker> _trackers = new List<ITradeTracker>();

private Timer? _timer;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
var symbol = new SharedSymbol(TradingMode.Spot, "BTC", "USDT");
_trackers = new List<ITradeTracker>();
foreach (var exchange in Exchange.All)
{
_trackers.Add(TrackerFactory.CreateTradeTracker(exchange, symbol, period: TimeSpan.FromMinutes(5))!);
};

var tasks = new List<Task>();
foreach (var tracker in _trackers)
tasks.Add(tracker.StartAsync());
await Task.WhenAll(tasks);

_timer = new Timer(async (x) => await InvokeAsync(StateHasChanged), null, 0, 1000);
}
}

public void Dispose()
{
foreach (var tracker in _trackers)
tracker.StopAsync();

_timer?.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
new NavItem { Id = "1", Href = "/", IconName = IconName.HouseDoorFill, Text = "Request prices", Match=NavLinkMatch.All},
new NavItem { Id = "2", Href = "/live", IconName = IconName.PlusSquareFill, Text = "Live trades"},
new NavItem { Id = "3", Href = "/books", IconName = IconName.PlusSquareFill, Text = "Order books"},
new NavItem { Id = "4", Href = "/trackers", IconName = IconName.PlusSquareFill, Text = "Trackers"},
};

return navItems;
Expand Down

0 comments on commit a64143c

Please sign in to comment.