Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Oct 28, 2024
1 parent fc250d4 commit 0a8d780
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 8 deletions.
22 changes: 20 additions & 2 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32002.185
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GateIo.Net.Examples.Api", "GateIo.Net.Examples.Api\GateIo.Net.Examples.Api.csproj", "{8D7694ED-912A-4F92-B924-CFD107EB20CA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GateIo.Examples.Api", "GateIo.Examples.Api\GateIo.Examples.Api.csproj", "{8D7694ED-912A-4F92-B924-CFD107EB20CA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GateIo.Net.Examples.Console", "GateIo.Net.Examples.Console\GateIo.Net.Examples.Console.csproj", "{5B869493-8271-45C5-93AE-5357B45FE005}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GateIo.Examples.Console", "GateIo.Examples.Console\GateIo.Examples.Console.csproj", "{5B869493-8271-45C5-93AE-5357B45FE005}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GateIo.Net", "..\GateIo.Net\GateIo.Net.csproj", "{5348A907-34D8-484F-8381-B8A475B690AA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GateIo.Examples.OrderBook", "GateIo.Examples.OrderBook\GateIo.Examples.OrderBook.csproj", "{DFA26183-D1A1-4D56-BA70-7176815E70F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GateIo.Examples.Tracker", "GateIo.Examples.Tracker\GateIo.Examples.Tracker.csproj", "{519DF5A6-A2EB-41A1-AFE7-A30A67E84AE2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +27,18 @@ Global
{5B869493-8271-45C5-93AE-5357B45FE005}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B869493-8271-45C5-93AE-5357B45FE005}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B869493-8271-45C5-93AE-5357B45FE005}.Release|Any CPU.Build.0 = Release|Any CPU
{5348A907-34D8-484F-8381-B8A475B690AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5348A907-34D8-484F-8381-B8A475B690AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5348A907-34D8-484F-8381-B8A475B690AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5348A907-34D8-484F-8381-B8A475B690AA}.Release|Any CPU.Build.0 = Release|Any CPU
{DFA26183-D1A1-4D56-BA70-7176815E70F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DFA26183-D1A1-4D56-BA70-7176815E70F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DFA26183-D1A1-4D56-BA70-7176815E70F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DFA26183-D1A1-4D56-BA70-7176815E70F0}.Release|Any CPU.Build.0 = Release|Any CPU
{519DF5A6-A2EB-41A1-AFE7-A30A67E84AE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{519DF5A6-A2EB-41A1-AFE7-A30A67E84AE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{519DF5A6-A2EB-41A1-AFE7-A30A67E84AE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{519DF5A6-A2EB-41A1-AFE7-A30A67E84AE2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GateIo.Net" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\GateIo.Net\GateIo.Net.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GateIo.Net" Version="1.0.0" />
<ProjectReference Include="..\..\GateIo.Net\GateIo.Net.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\GateIo.Net\GateIo.Net.csproj" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions Examples/GateIo.Examples.OrderBook/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using GateIo.Net.Interfaces;
using CryptoExchange.Net;
using CryptoExchange.Net.SharedApis;
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;

var collection = new ServiceCollection();
collection.AddGateIo();
var provider = collection.BuildServiceProvider();

var trackerFactory = provider.GetRequiredService<IGateIoOrderBookFactory>();

// Creat and start the order book
var book = trackerFactory.Create(new SharedSymbol(TradingMode.Spot, "ETH", "USDT"));
var result = await book.StartAsync();
if (!result.Success)
{
Console.WriteLine(result);
return;
}

// Create Spectre table
var table = new Table();
table.ShowRowSeparators = true;
table.AddColumn("Bid Quantity", x => { x.RightAligned(); })
.AddColumn("Bid Price", x => { x.RightAligned(); })
.AddColumn("Ask Price", x => { x.LeftAligned(); })
.AddColumn("Ask Quantity", x => { x.LeftAligned(); });

for(var i = 0; i < 10; i++)
table.AddEmptyRow();

await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
while (true)
{
var snapshot = book.Book;
for (var i = 0; i < 10; i++)
{
var bid = snapshot.bids.ElementAt(i);
var ask = snapshot.asks.ElementAt(i);
table.UpdateCell(i, 0, ExchangeHelpers.Normalize(bid.Quantity).ToString());
table.UpdateCell(i, 1, ExchangeHelpers.Normalize(bid.Price).ToString());
table.UpdateCell(i, 2, ExchangeHelpers.Normalize(ask.Price).ToString());
table.UpdateCell(i, 3, ExchangeHelpers.Normalize(ask.Quantity).ToString());
}
ctx.Refresh();
await Task.Delay(500);
}
});
18 changes: 18 additions & 0 deletions Examples/GateIo.Examples.Tracker/GateIo.Examples.Tracker.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\GateIo.Net\GateIo.Net.csproj" />
</ItemGroup>

</Project>
104 changes: 104 additions & 0 deletions Examples/GateIo.Examples.Tracker/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using GateIo.Net.Interfaces;
using CryptoExchange.Net.SharedApis;
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;
using System.Globalization;

var collection = new ServiceCollection();
collection.AddGateIo();
var provider = collection.BuildServiceProvider();

var trackerFactory = provider.GetRequiredService<IGateIoTrackerFactory>();

// Creat and start the tracker, keep track of the last 10 minutes
var tracker = trackerFactory.CreateTradeTracker(new SharedSymbol(TradingMode.Spot, "ETH", "USDT"), period: TimeSpan.FromMinutes(10));
var result = await tracker.StartAsync();
if (!result.Success)
{
Console.WriteLine(result);
return;
}

// Create Spectre table
var table = new Table();
table.ShowRowSeparators = true;
table.AddColumn("5 Min Data").AddColumn("-5 Min", x => { x.RightAligned(); })
.AddColumn("Now", x => { x.RightAligned(); })
.AddColumn("Dif", x => { x.RightAligned(); });

table.AddRow("Count", "", "", "");
table.AddRow("Average price", "", "", "");
table.AddRow("Average weighted price", "", "", "");
table.AddRow("Buy/Sell Ratio", "", "", "");
table.AddRow("Volume", "", "", "");
table.AddRow("Value", "", "", "");
table.AddRow("Complete", "", "", "");
table.AddRow("", "", "", "");
table.AddRow("Status", "", "", "");
table.AddRow("Synced From", "", "", "");

// Set default culture for currency display
CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
while (true)
{
// Get the stats from 10 minutes until 5 minutes ago
var secondLastMinute = tracker.GetStats(DateTime.UtcNow.AddMinutes(-10), DateTime.UtcNow.AddMinutes(-5));
// Get the stats from 5 minutes ago until now
var lastMinute = tracker.GetStats(DateTime.UtcNow.AddMinutes(-5));
// Get the differences between them
var compare = secondLastMinute.CompareTo(lastMinute);
// Update the columns
UpdateDec(0, 1, secondLastMinute.TradeCount);
UpdateDec(0, 2, lastMinute.TradeCount);
UpdateStr(0, 3, $"[{(compare.TradeCountDif.Difference < 0 ? "red" : "green")}]{compare.TradeCountDif.Difference} / {compare.TradeCountDif.PercentageDifference}%[/]");
UpdateStr(1, 1, secondLastMinute.AveragePrice?.ToString("C"));
UpdateStr(1, 2, lastMinute.AveragePrice?.ToString("C"));
UpdateStr(1, 3, $"[{(compare.AveragePriceDif?.Difference < 0 ? "red" : "green")}]{compare.AveragePriceDif?.Difference?.ToString("C")} / {compare.AveragePriceDif?.PercentageDifference}%[/]");
UpdateStr(2, 1, secondLastMinute.VolumeWeightedAveragePrice?.ToString("C"));
UpdateStr(2, 2, lastMinute.VolumeWeightedAveragePrice?.ToString("C"));
UpdateStr(2, 3, $"[{(compare.VolumeWeightedAveragePriceDif?.Difference < 0 ? "red" : "green")}]{compare.VolumeWeightedAveragePriceDif?.Difference?.ToString("C")} / {compare.VolumeWeightedAveragePriceDif?.PercentageDifference}%[/]");
UpdateDec(3, 1, secondLastMinute.BuySellRatio);
UpdateDec(3, 2, lastMinute.BuySellRatio);
UpdateStr(3, 3, $"[{(compare.BuySellRatioDif?.Difference < 0 ? "red" : "green")}]{compare.BuySellRatioDif?.Difference} / {compare.BuySellRatioDif?.PercentageDifference}%[/]");
UpdateDec(4, 1, secondLastMinute.Volume);
UpdateDec(4, 2, lastMinute.Volume);
UpdateStr(4, 3, $"[{(compare.VolumeDif.Difference < 0 ? "red" : "green")}]{compare.VolumeDif.Difference} / {compare.VolumeDif.PercentageDifference}%[/]");
UpdateStr(5, 1, secondLastMinute.QuoteVolume.ToString("C"));
UpdateStr(5, 2, lastMinute.QuoteVolume.ToString("C"));
UpdateStr(5, 3, $"[{(compare.QuoteVolumeDif.Difference < 0 ? "red" : "green")}]{compare.QuoteVolumeDif.Difference?.ToString("C")} / {compare.QuoteVolumeDif.PercentageDifference}%[/]");
UpdateStr(6, 1, secondLastMinute.Complete.ToString());
UpdateStr(6, 2, lastMinute.Complete.ToString());
UpdateStr(8, 1, tracker.Status.ToString());
UpdateStr(9, 1, tracker.SyncedFrom?.ToString());
ctx.Refresh();
await Task.Delay(500);
}
});


void UpdateDec(int row, int col, decimal? val)
{
table.UpdateCell(row, col, val?.ToString() ?? string.Empty);
}

void UpdateStr(int row, int col, string? val)
{
table.UpdateCell(row, col, val ?? string.Empty);
}
8 changes: 7 additions & 1 deletion Examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
A minimal API showing how to integrate GateIo.Net in a web API project

### GateIo.Net.Examples.Console
A simple console client demonstrating basic usage
A simple console client demonstrating basic usage

### GateIo.Examples.OrderBook
Example of using the client side order book implementation

### GateIo.Examples.Tracker
Example of using the trade tracker
4 changes: 1 addition & 3 deletions GateIo.Net/GateIo.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\CryptoExchange.Net\CryptoExchange.Net\CryptoExchange.Net.csproj" />
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions GateIo.Net/GateIo.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a8d780

Please sign in to comment.