Closed Oshirowanen closed 3 years ago
Hi. I think, that your problems are:
You create socketClient without defining ApiCreadentials and BaseAddress - this is the most important, because you want to use testing URL wss://testnet.binance.vision/ws,
This line of code "socketClient.UnsubscribeAll();". You are immediately unsubscribing after subscribe, so you will never receive some update via websocket.
Hello,
You're right, I did change those 2 lines around, but it made no difference.
Since I posted that example code, I have noticed that the code works perfectly when using the binance mainnet, but it does nothing using the binance testnet. If I connect to the binance testnet and sent a buy/sell order, nothing is detected by the websocket code above.
Is the binance testnet broken?
Probably a question for binance instead of here.
I tryied it today and it worked in testnet. I am looking now on your POST. You are posting normal order, however, you are waiting for onOcoOrderUpdateMessage - second action in SubscribeToUserDataUpdates method. You should use first action - onOrderUpdateMessage.
Just a bit edited version.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Binance.Net;
using Binance.Net.Enums;
using Binance.Net.Objects;
using Binance.Net.Objects.Spot;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Logging;
namespace binance_stream_user_data_updates
{
class Program
{
static async Task Main(string[] args)
{
// TODO: Dispose BinanceClient and BinanceSocketClient at some point.
var client = new BinanceClient(new BinanceClientOptions
{
ApiCredentials = new ApiCredentials("key", "secret"),
AutoTimestamp = true,
AutoTimestampRecalculationInterval = TimeSpan.FromMinutes(30),
BaseAddress = "https://testnet.binance.vision",
LogVerbosity = LogVerbosity.Debug,
LogWriters = new List<TextWriter> { Console.Out }
});
var socketClient = new BinanceSocketClient(new BinanceSocketClientOptions
{
ApiCredentials = new ApiCredentials("key", "secret"),
AutoReconnect = true,
BaseAddress = "wss://testnet.binance.vision/ws",
ReconnectInterval = TimeSpan.FromSeconds(15)
});
var listenKey = await client.Spot.UserStream.StartUserStreamAsync();
if (!listenKey.Success)
throw new Exception($"Failed to start user stream: {listenKey.Error}");
var subscription = await socketClient.Spot.SubscribeToUserDataUpdatesAsync(listenKey.Data,
null,
data =>
{
Console.WriteLine(data.Orders);
},
null,
null);
if (!subscription.Success)
throw new Exception($"Failed to subscribe to web socket stream: {listenKey.Error}");
// TODO: Put a CancellationToken in order to stop it gracefully
var keepAliveTask = Task.Run(async () =>
{
while (true)
{
await client.Spot.UserStream.KeepAliveUserStreamAsync(listenKey.Data);
await Task.Delay(TimeSpan.FromMinutes(30));
}
});
Console.ReadLine();
}
}
}
I found these differences between your and my code:
var socketClient = new BinanceSocketClient(new BinanceSocketClientOptions { ApiCredentials = new ApiCredentials("apiKey", "secret"), BaseAddress = "wss://testnet.binance.vision/ws", AutoReconnect = true });
socketClient.Spot.SubscribeToUserDataUpdates(startResult.Data, onOrderUpdateMessage => { _logger.LogInformation("onOrderUpdateMessage: "); }, onOcoOrderUpdateMessage => { _logger.LogInformation("onOcoOrderUpdateMessage: "); }, onAccountPositionMessage => { _logger.LogInformation("onAccountPositionMessage: "); }, onAccountBalanceUpdate => { _logger.LogInformation("onAccountBalanceUpdate: "); });
I have not tried it with SubscribeUserDataUpdatesAsync yet, but the code above works for me.
@hosulik, the code I posted, works just fine. I forgot to add BaseAddress to the web socket stream, but corrected it.
OP's way of creating orders is probably wrong.
var result = await client.Spot.Order.PlaceOrderAsync("TRXUSDT", OrderSide.Buy, OrderType.Market, quoteOrderQuantity: 100m).ConfigureAwait(false);
Closing because of inactivity; if there are still issues let me know
I have installed the wrapper from https://github.com/JKorf/Binance.Net
Using their example code, I have the following in my app:
I then run the above app which seems to connect and starts waiting, as the following is shown in the terminal:
I then post an order
POST https://testnet.binance.vision/api/v3/order?symbol=BNBUSDT&side=SELL&type=MARKET&quantity=0.1&newClientOrderId=my_order_id_201&newOrderRespType=FULL×tamp=1614370483356&signature=58cfd86cffc626703eac32f14bf0fa2e9af4850fb33974a03d1eee3f666df15f
When I do that, the websocket running from the above code outputs nothing. Does the example code need updating?