Skip to content

Commit be83750

Browse files
committed
Updated examples
1 parent 3bb918c commit be83750

8 files changed

Lines changed: 128 additions & 17 deletions

File tree

Examples/BitMEX.Examples.Api/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BitMEX.Net;
12
using BitMEX.Net.Interfaces.Clients;
23
using Microsoft.AspNetCore.Mvc;
34

@@ -13,7 +14,7 @@
1314
/*
1415
builder.Services.AddBitMEX(options =>
1516
{
16-
options.ApiCredentials = new ApiCredentials("<APIKEY>", "<APISECRET>");
17+
options.ApiCredentials = new BitMEXCredentials("<APIKEY>", "<APISECRET>");
1718
options.Rest.RequestTimeout = TimeSpan.FromSeconds(5);
1819
});
1920
*/
@@ -27,16 +28,20 @@
2728
app.MapGet("/{Symbol}", async ([FromServices] IBitMEXRestClient client, string symbol) =>
2829
{
2930
var result = await client.ExchangeApi.ExchangeData.GetSymbolsAsync(symbol);
30-
return result.Data.Single().LastPrice;
31+
return result.Success
32+
? Results.Ok(result.Data.Single().LastPrice)
33+
: Results.Problem(result.Error?.Message, statusCode: 502);
3134
})
3235
.WithOpenApi();
3336

3437

3538
app.MapGet("/Balances", async ([FromServices] IBitMEXRestClient client) =>
3639
{
3740
var result = await client.ExchangeApi.Account.GetBalancesAsync();
38-
return (object)(result.Success ? result.Data : result.Error!);
41+
return result.Success
42+
? Results.Ok(result.Data)
43+
: Results.Problem(result.Error?.Message, statusCode: 502);
3944
})
4045
.WithOpenApi();
4146

42-
app.Run();
47+
app.Run();

Examples/BitMEX.Examples.Console/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// REST
55
var restClient = new BitMEXRestClient();
66
var ticker = await restClient.ExchangeApi.ExchangeData.GetSymbolsAsync("ETHUSDT");
7+
if (!ticker.Success)
8+
{
9+
Console.WriteLine($"Failed to get ticker: {ticker.Error}");
10+
return;
11+
}
12+
713
Console.WriteLine($"Rest client ticker price for ETHUSDT: {ticker.Data.First().LastPrice}");
814

915
Console.WriteLine();
@@ -18,4 +24,10 @@
1824
Console.WriteLine($"Websocket client ticker price for ETHUSDT: {update.Data.LastPrice}");
1925
});
2026

27+
if (!subscription.Success)
28+
{
29+
Console.WriteLine($"Failed to subscribe to symbol updates: {subscription.Error}");
30+
return;
31+
}
32+
2133
Console.ReadLine();

Examples/BitMEX.Examples.OrderBook/BitMEX.Examples.OrderBook.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
11+
<PackageReference Include="Spectre.Console" Version="0.49.1" />
1212
</ItemGroup>
1313

1414
<ItemGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\BitMEX.Net\BitMEX.Net.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using BitMEX.Net;
2+
using BitMEX.Net.Clients;
3+
using BitMEX.Net.Enums;
4+
using BitMEX.Net.ExtensionMethods;
5+
6+
const string symbol = "ETHUSDT";
7+
8+
// Replace with valid credentials or order placement will always fail
9+
var apiKey = "APIKEY";
10+
var apiSecret = "APISECRET";
11+
12+
Console.WriteLine("BitMEX.Net order placement example");
13+
Console.WriteLine();
14+
Console.WriteLine("This example can place real orders when valid credentials are configured.");
15+
Console.WriteLine();
16+
17+
var client = new BitMEXRestClient(options =>
18+
{
19+
options.ApiCredentials = new BitMEXCredentials(apiKey, apiSecret);
20+
});
21+
22+
Console.WriteLine($"Placing limit buy order for {symbol}...");
23+
24+
var ticker = await client.ExchangeApi.ExchangeData.GetSymbolsAsync(symbol);
25+
if (!ticker.Success)
26+
{
27+
Console.WriteLine($"Failed to get ticker: {ticker.Error}");
28+
return;
29+
}
30+
31+
await BitMEXUtils.UpdateSymbolInfoAsync();
32+
var quantityToPlace = 0.01m;
33+
var quantityBitMEX = quantityToPlace.ToBitMEXSymbolQuantity(symbol); // For example 1000000
34+
35+
var symbolInfo = ticker.Data.Single();
36+
var safePrice = Math.Round(symbolInfo.LastPrice * 0.95m, 2);
37+
var order = await client.ExchangeApi.Trading.PlaceOrderAsync(
38+
symbol: symbol,
39+
orderSide: OrderSide.Buy,
40+
orderType: OrderType.Limit,
41+
quantity: quantityBitMEX,
42+
price: safePrice,
43+
timeInForce: TimeInForce.GoodTillCancel);
44+
45+
if (!order.Success)
46+
{
47+
Console.WriteLine($"Failed to place order: {order.Error}");
48+
return;
49+
}
50+
51+
Console.WriteLine($"Placed order {order.Data.OrderId}, status: {order.Data.Status}");
52+
53+
var orderStatus = await client.ExchangeApi.Trading.GetOrdersAsync(
54+
symbol: symbol,
55+
filter: new Dictionary<string, object>
56+
{
57+
{ "orderID", order.Data.OrderId }
58+
},
59+
limit: 1);
60+
61+
if (orderStatus.Success)
62+
{
63+
var status = orderStatus.Data.SingleOrDefault();
64+
Console.WriteLine(status == null
65+
? "Order was not returned by the status lookup"
66+
: $"Order status: {status.Status}, filled: {status.QuantityFilled}, remaining: {status.QuantityRemaining}");
67+
}
68+
else
69+
{
70+
Console.WriteLine($"Failed to query order: {orderStatus.Error}");
71+
}
72+
73+
var cancel = await client.ExchangeApi.Trading.CancelOrderAsync(order.Data.OrderId);
74+
Console.WriteLine(cancel.Success
75+
? $"Cancelled order {order.Data.OrderId}"
76+
: $"Failed to cancel order: {cancel.Error}");

Examples/BitMEX.Examples.Tracker/BitMEX.Examples.Tracker.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
11+
<PackageReference Include="Spectre.Console" Version="0.49.1" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

Examples/Examples.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitMEX.Examples.OrderBook",
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitMEX.Examples.Tracker", "BitMEX.Examples.Tracker\BitMEX.Examples.Tracker.csproj", "{42497D03-064D-4EB8-8EC8-E592E97761E3}"
1515
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitMEX.Examples.OrderPlacement", "BitMEX.Examples.OrderPlacement\BitMEX.Examples.OrderPlacement.csproj", "{94D64BA3-7A66-496B-83FD-B16C08E988E9}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{42497D03-064D-4EB8-8EC8-E592E97761E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{42497D03-064D-4EB8-8EC8-E592E97761E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{42497D03-064D-4EB8-8EC8-E592E97761E3}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{94D64BA3-7A66-496B-83FD-B16C08E988E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{94D64BA3-7A66-496B-83FD-B16C08E988E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{94D64BA3-7A66-496B-83FD-B16C08E988E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{94D64BA3-7A66-496B-83FD-B16C08E988E9}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE

Examples/README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Examples
22

3-
### BitMEX.Examples.Api
4-
A minimal API showing how to integrate BitMEX.Net in a web API project
5-
6-
### BitMEX.Examples.Console
7-
A simple console client demonstrating basic usage
8-
9-
### BitMEX.Examples.OrderBook
10-
Example of using the client side order book implementation
11-
12-
### BitMEX.Examples.Tracker
13-
Example of using the trade tracker
3+
| Project | Topic |
4+
|---|---|
5+
| `BitMEX.Examples.Api` | Minimal ASP.NET Core API integration, dependency injection, public ticker endpoint, private balances endpoint |
6+
| `BitMEX.Examples.Console` | Basic REST ticker request and WebSocket symbol subscription |
7+
| `BitMEX.Examples.OrderPlacement` | Limit order placement, order status lookup, and cancellation |
8+
| `BitMEX.Examples.OrderBook` | Client-side order book implementation with live console display |
9+
| `BitMEX.Examples.Tracker` | Trade tracker with rolling market statistics |
10+
11+
The `ai-friendly` folder contains standalone copy/paste examples intended for quick onboarding and AI coding assistant context.

0 commit comments

Comments
 (0)