mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-11 16:32:57 +00:00
Updated examples
This commit is contained in:
@@ -6,15 +6,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Binance.Net" Version="8.0.6" />
|
||||
<PackageReference Include="Bitfinex.Net" Version="5.0.3" />
|
||||
<PackageReference Include="Bittrex.Net" Version="7.0.4" />
|
||||
<PackageReference Include="Bybit.Net" Version="0.0.4" />
|
||||
<PackageReference Include="CoinEx.Net" Version="5.0.3" />
|
||||
<PackageReference Include="FTX.Net" Version="1.0.4" />
|
||||
<PackageReference Include="Huobi.Net" Version="4.0.4" />
|
||||
<PackageReference Include="KrakenExchange.Net" Version="3.0.3" />
|
||||
<PackageReference Include="Kucoin.Net" Version="4.0.3" />
|
||||
<PackageReference Include="Binance.Net" Version="9.0.1" />
|
||||
<PackageReference Include="Bitfinex.Net" Version="6.0.0" />
|
||||
<PackageReference Include="Bittrex.Net" Version="8.0.0" />
|
||||
<PackageReference Include="Bybit.Net" Version="3.0.0" />
|
||||
<PackageReference Include="CoinEx.Net" Version="6.0.0" />
|
||||
<PackageReference Include="Huobi.Net" Version="5.0.0" />
|
||||
<PackageReference Include="KrakenExchange.Net" Version="4.0.0" />
|
||||
<PackageReference Include="Kucoin.Net" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -17,21 +17,21 @@ namespace ConsoleClient.Exchanges
|
||||
|
||||
public async Task<WebCallResult> CancelOrder(string symbol, string id)
|
||||
{
|
||||
using var client = new BinanceClient();
|
||||
using var client = new BinanceRestClient();
|
||||
var result = await client.SpotApi.Trading.CancelOrderAsync(symbol, long.Parse(id));
|
||||
return result.AsDataless();
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, decimal>> GetBalances()
|
||||
{
|
||||
using var client = new BinanceClient();
|
||||
using var client = new BinanceRestClient();
|
||||
var result = await client.SpotApi.Account.GetAccountInfoAsync();
|
||||
return result.Data.Balances.ToDictionary(b => b.Asset, b => b.Total);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OpenOrder>> GetOpenOrders()
|
||||
{
|
||||
using var client = new BinanceClient();
|
||||
using var client = new BinanceRestClient();
|
||||
var result = await client.SpotApi.Trading.GetOpenOrdersAsync();
|
||||
// Should check result success status here
|
||||
return result.Data.Select(o => new OpenOrder
|
||||
@@ -49,7 +49,7 @@ namespace ConsoleClient.Exchanges
|
||||
|
||||
public async Task<decimal> GetPrice(string symbol)
|
||||
{
|
||||
using var client = new BinanceClient();
|
||||
using var client = new BinanceRestClient();
|
||||
var result = await client.SpotApi.ExchangeData.GetPriceAsync(symbol);
|
||||
// Should check result success status here
|
||||
return result.Data.Price;
|
||||
@@ -57,7 +57,7 @@ namespace ConsoleClient.Exchanges
|
||||
|
||||
public async Task<WebCallResult<string>> PlaceOrder(string symbol, string side, string type, decimal quantity, decimal? price)
|
||||
{
|
||||
using var client = new BinanceClient();
|
||||
using var client = new BinanceRestClient();
|
||||
var result = await client.SpotApi.Trading.PlaceOrderAsync(
|
||||
symbol,
|
||||
side.ToLower() == "buy" ? Binance.Net.Enums.OrderSide.Buy: Binance.Net.Enums.OrderSide.Sell,
|
||||
@@ -70,7 +70,7 @@ namespace ConsoleClient.Exchanges
|
||||
|
||||
public async Task<UpdateSubscription> SubscribePrice(string symbol, Action<decimal> handler)
|
||||
{
|
||||
var sub = await _socketClient.SpotStreams.SubscribeToMiniTickerUpdatesAsync(symbol, data => handler(data.Data.LastPrice));
|
||||
var sub = await _socketClient.SpotApi.ExchangeData.SubscribeToMiniTickerUpdatesAsync(symbol, data => handler(data.Data.LastPrice));
|
||||
return sub.Data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using Bybit.Net.Clients;
|
||||
using Bybit.Net.Interfaces.Clients;
|
||||
using ConsoleClient.Models;
|
||||
using CryptoExchange.Net.Objects;
|
||||
using CryptoExchange.Net.Sockets;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ConsoleClient.Exchanges
|
||||
{
|
||||
internal class BybitExchange : IExchange
|
||||
{
|
||||
private IBybitSocketClient _socketClient = new BybitSocketClient();
|
||||
|
||||
public async Task<WebCallResult> CancelOrder(string symbol, string id)
|
||||
{
|
||||
using var client = new BybitRestClient();
|
||||
var result = await client.V5Api.Trading.CancelOrderAsync(Bybit.Net.Enums.Category.Spot, symbol, id);
|
||||
return result.AsDataless();
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, decimal>> GetBalances()
|
||||
{
|
||||
using var client = new BybitRestClient();
|
||||
var result = await client.V5Api.Account.GetBalancesAsync(Bybit.Net.Enums.AccountType.Spot);
|
||||
return result.Data.List.First().Assets.ToDictionary(d => d.Asset, d => d.WalletBalance);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OpenOrder>> GetOpenOrders()
|
||||
{
|
||||
using var client = new BybitRestClient();
|
||||
var order = await client.V5Api.Trading.GetOrdersAsync(Bybit.Net.Enums.Category.Spot);
|
||||
return order.Data.List.Select(o => new OpenOrder
|
||||
{
|
||||
Symbol = o.Symbol,
|
||||
OrderSide = o.Side.ToString(),
|
||||
OrderStatus = o.Status.ToString(),
|
||||
OrderTime = o.CreateTime,
|
||||
OrderType = o.OrderType.ToString(),
|
||||
Price = o.Price ?? 0,
|
||||
Quantity = o.Quantity,
|
||||
QuantityFilled = o.QuantityFilled ?? 0
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<decimal> GetPrice(string symbol)
|
||||
{
|
||||
using var client = new BybitRestClient();
|
||||
var result = await client.V5Api.ExchangeData.GetSpotTickersAsync(symbol);
|
||||
return result.Data.List.First().LastPrice;
|
||||
}
|
||||
|
||||
public async Task<WebCallResult<string>> PlaceOrder(string symbol, string side, string type, decimal quantity, decimal? price)
|
||||
{
|
||||
using var client = new BybitRestClient();
|
||||
var result = await client.V5Api.Trading.PlaceOrderAsync(
|
||||
Bybit.Net.Enums.Category.Spot,
|
||||
symbol,
|
||||
side.ToLower() == "buy" ? Bybit.Net.Enums.OrderSide.Buy : Bybit.Net.Enums.OrderSide.Sell,
|
||||
type == "market" ? Bybit.Net.Enums.NewOrderType.Market : Bybit.Net.Enums.NewOrderType.Limit,
|
||||
quantity,
|
||||
price: price);
|
||||
return result.As(result.Data?.OrderId.ToString());
|
||||
}
|
||||
|
||||
public async Task<UpdateSubscription> SubscribePrice(string symbol, Action<decimal> handler)
|
||||
{
|
||||
var sub = await _socketClient.V5SpotApi.SubscribeToTickerUpdatesAsync(symbol, data => handler(data.Data.LastPrice));
|
||||
return sub.Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
using ConsoleClient.Models;
|
||||
using CryptoExchange.Net.Objects;
|
||||
using CryptoExchange.Net.Sockets;
|
||||
using FTX.Net.Clients;
|
||||
using FTX.Net.Interfaces.Clients;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ConsoleClient.Exchanges
|
||||
{
|
||||
internal class FTXExchange : IExchange
|
||||
{
|
||||
private IFTXSocketClient _socketClient = new FTXSocketClient();
|
||||
|
||||
public async Task<WebCallResult> CancelOrder(string symbol, string id)
|
||||
{
|
||||
using var client = new FTXClient();
|
||||
var result = await client.TradeApi.Trading.CancelOrderAsync(long.Parse(id));
|
||||
return result.AsDataless();
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, decimal>> GetBalances()
|
||||
{
|
||||
using var client = new FTXClient();
|
||||
var result = await client.TradeApi.Account.GetBalancesAsync();
|
||||
return result.Data.ToDictionary(d => d.Asset, d => d.Total);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OpenOrder>> GetOpenOrders()
|
||||
{
|
||||
using var client = new FTXClient();
|
||||
var order = await client.TradeApi.Trading.GetOpenOrdersAsync();
|
||||
return order.Data.Select(o => new OpenOrder
|
||||
{
|
||||
Symbol = o.Symbol,
|
||||
OrderSide = o.Side.ToString(),
|
||||
OrderStatus = o.Status.ToString(),
|
||||
OrderTime = o.CreateTime,
|
||||
OrderType = o.Type.ToString(),
|
||||
Price = o.Price ?? 0,
|
||||
Quantity = o.Quantity,
|
||||
QuantityFilled = o.QuantityFilled ?? 0
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<decimal> GetPrice(string symbol)
|
||||
{
|
||||
using var client = new FTXClient();
|
||||
var result = await client.TradeApi.ExchangeData.GetSymbolAsync(symbol);
|
||||
return result.Data.LastPrice ?? 0;
|
||||
}
|
||||
|
||||
public async Task<WebCallResult<string>> PlaceOrder(string symbol, string side, string type, decimal quantity, decimal? price)
|
||||
{
|
||||
using var client = new FTXClient();
|
||||
var result = await client.TradeApi.Trading.PlaceOrderAsync(
|
||||
symbol,
|
||||
side.ToLower() == "buy" ? FTX.Net.Enums.OrderSide.Buy : FTX.Net.Enums.OrderSide.Sell,
|
||||
type == "market" ? FTX.Net.Enums.OrderType.Market : FTX.Net.Enums.OrderType.Limit,
|
||||
quantity,
|
||||
price: price);
|
||||
return result.As(result.Data?.Id.ToString());
|
||||
}
|
||||
|
||||
public async Task<UpdateSubscription> SubscribePrice(string symbol, Action<decimal> handler)
|
||||
{
|
||||
var sub = await _socketClient.Streams.SubscribeToTickerUpdatesAsync(symbol, data => handler(data.Data.LastPrice ?? 0));
|
||||
return sub.Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,10 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Binance.Net.Clients;
|
||||
using Binance.Net.Objects;
|
||||
using Bybit.Net.Clients;
|
||||
using ConsoleClient.Exchanges;
|
||||
using CryptoExchange.Net.Authentication;
|
||||
using CryptoExchange.Net.Sockets;
|
||||
using FTX.Net.Clients;
|
||||
using FTX.Net.Objects;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ConsoleClient
|
||||
{
|
||||
@@ -19,20 +17,18 @@ namespace ConsoleClient
|
||||
static Dictionary<string, IExchange> _exchanges = new Dictionary<string, IExchange>
|
||||
{
|
||||
{ "Binance", new BinanceExchange() },
|
||||
{ "FTX", new FTXExchange() }
|
||||
{ "Bybit", new BybitExchange() }
|
||||
};
|
||||
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
BinanceClient.SetDefaultOptions(new BinanceClientOptions
|
||||
BinanceRestClient.SetDefaultOptions(options =>
|
||||
{
|
||||
LogLevel = LogLevel.Trace,
|
||||
ApiCredentials = new ApiCredentials("APIKEY", "APISECRET")
|
||||
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
|
||||
});
|
||||
FTXClient.SetDefaultOptions(new FTXClientOptions
|
||||
BybitRestClient.SetDefaultOptions(options =>
|
||||
{
|
||||
LogLevel = LogLevel.Trace,
|
||||
ApiCredentials = new ApiCredentials("APIKEY", "APISECRET")
|
||||
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
|
||||
});
|
||||
|
||||
while (true)
|
||||
|
||||
Reference in New Issue
Block a user