|
| 1 | +using HTX.Net.Interfaces; |
| 2 | +using CryptoExchange.Net.SharedApis; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Spectre.Console; |
| 5 | +using System.Globalization; |
| 6 | + |
| 7 | +var collection = new ServiceCollection(); |
| 8 | +collection.AddHTX(); |
| 9 | +var provider = collection.BuildServiceProvider(); |
| 10 | + |
| 11 | +var trackerFactory = provider.GetRequiredService<IHTXTrackerFactory>(); |
| 12 | + |
| 13 | +// Creat and start the tracker, keep track of the last 10 minutes |
| 14 | +var tracker = trackerFactory.CreateTradeTracker(new SharedSymbol(TradingMode.Spot, "ETH", "USDT"), period: TimeSpan.FromMinutes(10)); |
| 15 | +var result = await tracker.StartAsync(); |
| 16 | +if (!result.Success) |
| 17 | +{ |
| 18 | + Console.WriteLine(result); |
| 19 | + return; |
| 20 | +} |
| 21 | + |
| 22 | +// Create Spectre table |
| 23 | +var table = new Table(); |
| 24 | +table.ShowRowSeparators = true; |
| 25 | +table.AddColumn("5 Min Data").AddColumn("-5 Min", x => { x.RightAligned(); }) |
| 26 | + .AddColumn("Now", x => { x.RightAligned(); }) |
| 27 | + .AddColumn("Dif", x => { x.RightAligned(); }); |
| 28 | + |
| 29 | +table.AddRow("Count", "", "", ""); |
| 30 | +table.AddRow("Average price", "", "", ""); |
| 31 | +table.AddRow("Average weighted price", "", "", ""); |
| 32 | +table.AddRow("Buy/Sell Ratio", "", "", ""); |
| 33 | +table.AddRow("Volume", "", "", ""); |
| 34 | +table.AddRow("Value", "", "", ""); |
| 35 | +table.AddRow("Complete", "", "", ""); |
| 36 | +table.AddRow("", "", "", ""); |
| 37 | +table.AddRow("Status", "", "", ""); |
| 38 | +table.AddRow("Synced From", "", "", ""); |
| 39 | + |
| 40 | +// Set default culture for currency display |
| 41 | +CultureInfo ci = new CultureInfo("en-US"); |
| 42 | +Thread.CurrentThread.CurrentCulture = ci; |
| 43 | +Thread.CurrentThread.CurrentUICulture = ci; |
| 44 | + |
| 45 | +await AnsiConsole.Live(table) |
| 46 | + .StartAsync(async ctx => |
| 47 | + { |
| 48 | + while (true) |
| 49 | + { |
| 50 | + // Get the stats from 10 minutes until 5 minutes ago |
| 51 | + var secondLastMinute = tracker.GetStats(DateTime.UtcNow.AddMinutes(-10), DateTime.UtcNow.AddMinutes(-5)); |
| 52 | + |
| 53 | + // Get the stats from 5 minutes ago until now |
| 54 | + var lastMinute = tracker.GetStats(DateTime.UtcNow.AddMinutes(-5)); |
| 55 | + |
| 56 | + // Get the differences between them |
| 57 | + var compare = secondLastMinute.CompareTo(lastMinute); |
| 58 | + |
| 59 | + // Update the columns |
| 60 | + UpdateDec(0, 1, secondLastMinute.TradeCount); |
| 61 | + UpdateDec(0, 2, lastMinute.TradeCount); |
| 62 | + UpdateStr(0, 3, $"[{(compare.TradeCountDif.Difference < 0 ? "red" : "green")}]{compare.TradeCountDif.Difference} / {compare.TradeCountDif.PercentageDifference}%[/]"); |
| 63 | + |
| 64 | + UpdateStr(1, 1, secondLastMinute.AveragePrice?.ToString("C")); |
| 65 | + UpdateStr(1, 2, lastMinute.AveragePrice?.ToString("C")); |
| 66 | + UpdateStr(1, 3, $"[{(compare.AveragePriceDif?.Difference < 0 ? "red" : "green")}]{compare.AveragePriceDif?.Difference?.ToString("C")} / {compare.AveragePriceDif?.PercentageDifference}%[/]"); |
| 67 | + |
| 68 | + UpdateStr(2, 1, secondLastMinute.VolumeWeightedAveragePrice?.ToString("C")); |
| 69 | + UpdateStr(2, 2, lastMinute.VolumeWeightedAveragePrice?.ToString("C")); |
| 70 | + UpdateStr(2, 3, $"[{(compare.VolumeWeightedAveragePriceDif?.Difference < 0 ? "red" : "green")}]{compare.VolumeWeightedAveragePriceDif?.Difference?.ToString("C")} / {compare.VolumeWeightedAveragePriceDif?.PercentageDifference}%[/]"); |
| 71 | + |
| 72 | + UpdateDec(3, 1, secondLastMinute.BuySellRatio); |
| 73 | + UpdateDec(3, 2, lastMinute.BuySellRatio); |
| 74 | + UpdateStr(3, 3, $"[{(compare.BuySellRatioDif?.Difference < 0 ? "red" : "green")}]{compare.BuySellRatioDif?.Difference} / {compare.BuySellRatioDif?.PercentageDifference}%[/]"); |
| 75 | + |
| 76 | + UpdateDec(4, 1, secondLastMinute.Volume); |
| 77 | + UpdateDec(4, 2, lastMinute.Volume); |
| 78 | + UpdateStr(4, 3, $"[{(compare.VolumeDif.Difference < 0 ? "red" : "green")}]{compare.VolumeDif.Difference} / {compare.VolumeDif.PercentageDifference}%[/]"); |
| 79 | + |
| 80 | + UpdateStr(5, 1, secondLastMinute.QuoteVolume.ToString("C")); |
| 81 | + UpdateStr(5, 2, lastMinute.QuoteVolume.ToString("C")); |
| 82 | + UpdateStr(5, 3, $"[{(compare.QuoteVolumeDif.Difference < 0 ? "red" : "green")}]{compare.QuoteVolumeDif.Difference?.ToString("C")} / {compare.QuoteVolumeDif.PercentageDifference}%[/]"); |
| 83 | + |
| 84 | + UpdateStr(6, 1, secondLastMinute.Complete.ToString()); |
| 85 | + UpdateStr(6, 2, lastMinute.Complete.ToString()); |
| 86 | + |
| 87 | + UpdateStr(8, 1, tracker.Status.ToString()); |
| 88 | + UpdateStr(9, 1, tracker.SyncedFrom?.ToString()); |
| 89 | + |
| 90 | + ctx.Refresh(); |
| 91 | + await Task.Delay(500); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + |
| 96 | +void UpdateDec(int row, int col, decimal? val) |
| 97 | +{ |
| 98 | + table.UpdateCell(row, col, val?.ToString() ?? string.Empty); |
| 99 | +} |
| 100 | + |
| 101 | +void UpdateStr(int row, int col, string? val) |
| 102 | +{ |
| 103 | + table.UpdateCell(row, col, val ?? string.Empty); |
| 104 | +} |
0 commit comments