One of my Websocket here never get triggered in this code (but in a simpler code it works)

27 views Asked by At

I have the code below where I subscribe to two websockets

  1. WatchTicker -> This one works fine and raise the event properly
  2. WatchOrders -> This one doesn't work, and never get triggered in this code scenario. However If I just make a simple code like this:
while(true)
{
var orderUpdate = await exchange.WatchOrder();
}

it works...

Any idea what am I doing wrong here ? Here is the full code snippet below

using System;
using System.Linq;
using ccxt.pro;
using ccxt;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.CompilerServices;
using System.Diagnostics;

namespace Test_ccxt
{

    public class Program
    {
        public class Core
        {
            public List<Exchange> Exchanges {  get; set; } = new List<Exchange>();
            public List<string> Symbols { get; set; } = new List<string>();
            public event Action<List<Order>> OnNewOrderUpdate;
            public event Action<List<Trade>> OnNewTrades;
            public event Action<Ticker> OnNewTick;

            public Core() 
            {
            }
            //Launch sockets
            public void InitCore()
            {
                Task OrderWsTask = Task.Run(() => ListenToOrderAsync(this.Exchanges));
                Task TickWsTask = Task.Run(() => ListenToTickerAsync(this.Exchanges, this.Symbols));
            }

            private async Task<Ticker> ListenToTickerAsync(List<Exchange> exchanges, List<string>? symbols = null, Dictionary<string, object>? parameters = null)
            {
                while (true)
                {
                    foreach (var exchange in exchanges)
                    {
                        if (symbols != null)
                        {

                            try
                            {
                                var tickUpdates = await exchange.WatchTicker(symbols.First(), parameters);
                                if (tickUpdates.info != null)
                                {
                                    OnNewTick?.Invoke(tickUpdates);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Error while listenning to " + exchange.name + " " + ex.Message);
                                await Task.Delay(100);

                            }
                        }
                    }

                }

            }

            private async Task<List<Order>> ListenToOrderAsync(List<Exchange> exchanges)
            {
                while (true)
                {
                    foreach (var exchange in exchanges)
                    {

                        try
                        {
                            var orderUpdates = await exchange.WatchOrders();
                            // Raise Event
                            if (orderUpdates.Any())
                            {
                                OnNewOrderUpdate?.Invoke(orderUpdates);
                            }

                    }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error while listenning to orders from " + exchange.name + " " + ex.Message);
                            await Task.Delay(100);
                        }
                    }


                }

            }


        }

        public class Strategy
        {

        }
    
        async static Task Main(string[] args)
        {
            string symbol = "BTC/USDT:USDT";
            var woo = new ccxt.pro.Woo();
            bybit.newUpdates = true;
            woo.newUpdates = true;
            woo.uid = uid;
            woo.apiKey = apiKey;
            woo.secret = secret;
            //Init Markets
            /*await bybit.loadMarkets();*/
            await woo.loadMarkets();

            Core core = new Core();
            core.Exchanges.Add(woo);
            core.Symbols.Add(symbol);

            core.OnNewOrderUpdate += OnNewOrderUpdate;
            core.OnNewTick += OnNewTickUpdate;
            core.InitCore();
                       
            while (true) { }
        }

        public static void OnNewOrderUpdate(List<Order> o)
        {

        }
        public static void OnNewTickUpdate(Ticker t)
        {
            Console.WriteLine("Tick: " + t.info);
        }
    }
}

To anticipate the question, the reason I'm using event is because I will run several instances of class Foo and I don't want to subscribe to those websockets in each instances of course. My idea is to create a Core class that will publish events in each of those Foo instances where it has been subscribed. If I'm being clear here ?

1

There are 1 answers

2
Sixk On

I think I figured it out, but I still need your helpd guys or best practices. If I change all the Core part to a static class instead...Everything works. Not sure that would be the best...