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 687633a commit a1347fb
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 9 deletions.
6 changes: 2 additions & 4 deletions CoinEx.Net/CoinEx.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>10.0</LangVersion>
Expand Down Expand Up @@ -48,13 +48,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.1.0" />
<PackageReference Include="Crc32.NET" Version="1.2.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 CoinEx.Net/CoinEx.Net.xml

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

7 changes: 5 additions & 2 deletions Examples/CoinEx.Examples.Api/CoinEx.Examples.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CoinEx.Net" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

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

<ItemGroup>
<PackageReference Include="CoinEx.Net" Version="7.0.0" />
<ProjectReference Include="..\..\CoinEx.Net\CoinEx.Net.csproj" />
</ItemGroup>

</Project>
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="..\..\CoinEx.Net\CoinEx.Net.csproj" />
</ItemGroup>

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

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

var bookFactory = provider.GetRequiredService<ICoinExOrderBookFactory>();

// Create and start the order book
var book = bookFactory.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/CoinEx.Examples.Tracker/CoinEx.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="..\..\CoinEx.Net\CoinEx.Net.csproj" />
</ItemGroup>

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

var collection = new ServiceCollection();
collection.AddCoinEx();
collection.AddLogging(x =>
{
x.SetMinimumLevel(LogLevel.Trace);
x.AddProvider(new TraceLoggerProvider());
});
var provider = collection.BuildServiceProvider();

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

// Create 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);
}
18 changes: 18 additions & 0 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoinEx.Examples.Api", "Coin
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoinEx.Examples.Console", "CoinEx.Examples.Console\CoinEx.Examples.Console.csproj", "{FD4F95C8-D9B7-4F81-9245-4CE667DFD421}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoinEx.Net", "..\CoinEx.Net\CoinEx.Net.csproj", "{22411347-12D7-4993-A7BA-E8CC6D52CA4D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoinEx.Examples.OrderBook", "CoinEx.Examples.OrderBook\CoinEx.Examples.OrderBook.csproj", "{2668FF12-1C29-4CB0-B944-A938805CDF66}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoinEx.Examples.Tracker", "CoinEx.Examples.Tracker\CoinEx.Examples.Tracker.csproj", "{93B8A062-C9D7-497E-BD98-ACBF40666D8A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +27,18 @@ Global
{FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Release|Any CPU.Build.0 = Release|Any CPU
{22411347-12D7-4993-A7BA-E8CC6D52CA4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22411347-12D7-4993-A7BA-E8CC6D52CA4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22411347-12D7-4993-A7BA-E8CC6D52CA4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22411347-12D7-4993-A7BA-E8CC6D52CA4D}.Release|Any CPU.Build.0 = Release|Any CPU
{2668FF12-1C29-4CB0-B944-A938805CDF66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2668FF12-1C29-4CB0-B944-A938805CDF66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2668FF12-1C29-4CB0-B944-A938805CDF66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2668FF12-1C29-4CB0-B944-A938805CDF66}.Release|Any CPU.Build.0 = Release|Any CPU
{93B8A062-C9D7-497E-BD98-ACBF40666D8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93B8A062-C9D7-497E-BD98-ACBF40666D8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93B8A062-C9D7-497E-BD98-ACBF40666D8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93B8A062-C9D7-497E-BD98-ACBF40666D8A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
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 CoinEx.Net in a web API project

### CoinEx.Examples.Console
A simple console client demonstrating basic usage
A simple console client demonstrating basic usage

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

### CoinEx.Examples.Tracker
Example of using the trade tracker

0 comments on commit a1347fb

Please sign in to comment.