|
| 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}"); |
0 commit comments