Esempio n. 1
0
        public static List <SymbolMarket> InitSymbolMarkets(Settings setting, List <Symbol> symbols, HttpClient client)
        {
            ISymbolFactory      symbolFactory = new SymbolFactory();
            List <SymbolMarket> symbolMarkets = new List <SymbolMarket>();

            foreach (var SymSettingList in setting.Symbols)
            {
                //var responseString = client.GetStringAsync($"https://data.gateio.la/api2/1/ticker/{SymSettingList[0].ToLower()}_usdt");
                //JObject jsonObj = JObject.Parse(responseString.Result);
                //decimal coinUSDT = Convert.ToDecimal(jsonObj["last"], CultureInfo.InvariantCulture);
                //decimal minVolume = lotMinUsd / coinUSDT;

                if (Currencies.gate_okex_Intersect.Contains($"{SymSettingList[0]}-{SymSettingList[1]}") || Currencies.okex_huobiIntersect.Contains($"{SymSettingList[0]}-{SymSettingList[1]}") || Currencies.gate_huobiIntersect.Contains($"{SymSettingList[0]}-{SymSettingList[1]}"))
                {
                    decimal minDiffValue = Convert.ToDecimal(setting.General.minDiff, CultureInfo.InvariantCulture);
                    decimal minStepValue = Convert.ToDecimal(setting.General.minStep, CultureInfo.InvariantCulture);
                    decimal lotMinUsd    = Convert.ToDecimal(setting.General.lotMinUsd, CultureInfo.InvariantCulture);
                    decimal minVolume    = Currencies.getMinVolume(SymSettingList[0], lotMinUsd, client);

                    SymbolMarket symMarketObj = symbolFactory.CreateSymbolMarket($"{SymSettingList[0]}-{SymSettingList[1]}", minDiffValue, minStepValue, minVolume, symbols);
                    symbolMarkets.Add(symMarketObj);
                    symMarketObj.SymMarketStatus();
                }
                else
                {
                    //Console.WriteLine($"{SymSettingList[0]}-{SymSettingList[1]} does not support by any exchange!!!");
                    continue;
                }
            }
            return(symbolMarkets);
        }
Esempio n. 2
0
        public static WebsocketClient StartGateIO(string url, List <Symbol> symbols, List <SymbolMarket> symbolMarkets)
        {
            string        lastMessage = "";
            List <string> okSyms      = new List <string>()
            {
            };
            Uri                   gateio_url             = new Uri(url);
            var                   gateioClient           = new WebsocketClient(gateio_url);
            Symbol                processingSymObj       = new Symbol();
            SymbolMarket          processingSymMarketObj = new SymbolMarket();
            List <List <string> > asks = new List <List <string> >();
            List <List <string> > bids = new List <List <string> >();

            gateioClient.ReconnectTimeout = TimeSpan.FromSeconds(30);
            gateioClient.ReconnectionHappened.Subscribe(info =>
            {
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   Gate Reconnection happened, type: {info.Type}");
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   {lastMessage}");
            });
            gateioClient
            .MessageReceived
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(msg =>
            {
                asks.Clear();
                bids.Clear();
                string jsonString = msg.ToString();
                lastMessage       = jsonString;
                //LogWriters.WriteResponce("gate.io", jsonString);

                JObject jsonObj = JObject.Parse(jsonString);
                if (jsonObj.ContainsKey("result"))
                {
                    if (jsonObj["result"]["status"].ToString() == "success")
                    {
                        Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   gate.io server responds");
                    }
                }
                else if (jsonObj.ContainsKey("params"))
                {
                    string processingSym = jsonObj["params"][2].ToString();
                    processingSym        = processingSym.Replace("_", "-");
                    if (!okSyms.Contains(processingSym))
                    {
                        okSyms.Add(processingSym);
                        Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   Gate {processingSym}: OK");
                    }
                    foreach (Symbol symObj in symbols)
                    {
                        if (symObj.ExchangeName == "gate" && symObj.SymbolName == processingSym)
                        {
                            processingSymObj = symObj;
                            break;
                        }
                    }
                    foreach (SymbolMarket symMarketObj in symbolMarkets)
                    {
                        if (symMarketObj.SymbolName == processingSym)
                        {
                            processingSymMarketObj = symMarketObj;
                            break;
                        }
                    }

                    if (jsonObj["params"][1]["asks"] != null)
                    {
                        asks = jsonObj["params"][1]["asks"].ToObject <List <List <string> > >();
                    }
                    if (jsonObj["params"][1]["bids"] != null)
                    {
                        bids = jsonObj["params"][1]["bids"].ToObject <List <List <string> > >();
                    }

                    if (Convert.ToBoolean(jsonObj["params"][0].ToString()) == true)
                    {
                        processingSymObj.ClearGlass();
                        foreach (var valVol in asks)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddAskEl(value, volume);
                        }
                        foreach (var valVol in bids)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddBidEl(value * -1, volume);
                        }
                    }
                    else if (Convert.ToBoolean(jsonObj["params"][0].ToString()) == false)
                    {
                        foreach (var valVol in asks)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddAskElPartial(value, volume);
                        }
                        foreach (var valVol in bids)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddBidElPartial(value * -1, volume);
                        }
                    }
                    processingSymObj.RegisterGlass(processingSymMarketObj.MinVolume);
                    processingSymMarketObj.WriteMarket();
                }
            });
            return(gateioClient);
        }
Esempio n. 3
0
        public static WebsocketClient StartHuobi(string url, List <Symbol> symbols, List <SymbolMarket> symbolMarkets)
        {
            string                lastMessage            = "";
            Uri                   okex_url               = new Uri(url);
            var                   huobiClient            = new WebsocketClient(okex_url);
            Symbol                processingSymObj       = new Symbol();
            SymbolMarket          processingSymMarketObj = new SymbolMarket();
            List <List <string> > asks = new List <List <string> >();
            List <List <string> > bids = new List <List <string> >();

            huobiClient.ReconnectTimeout = TimeSpan.FromSeconds(30);
            huobiClient.ReconnectionHappened.Subscribe(info =>
            {
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   Huobi Reconnection happened, type: {info.Type}");
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   {lastMessage}");
            });
            huobiClient
            .MessageReceived
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(msg =>
            {
                asks.Clear();
                bids.Clear();
                byte[] bytes      = msg.Binary;
                string jsonString = Encoding.UTF8.GetString(DecompressHuobi(bytes));
                lastMessage       = jsonString;
                //LogWriters.WriteResponce("Huobi", jsonString);

                JObject jsonObj = JObject.Parse(jsonString);

                if (jsonObj.ContainsKey("status"))
                {
                    if (jsonObj["status"].ToString() == "ok")
                    {
                        string okSym = jsonObj["subbed"].ToString();
                        okSym        = okSym.Substring(7);
                        okSym        = okSym.Remove(okSym.Length - 15).ToUpper();
                        Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   Huobi: {okSym} OK");
                    }
                }

                if (jsonObj.ContainsKey("ping"))
                {
                    long pingId = jsonObj["ping"].ToObject <long>();
                    Requests.HuobiPong huobiPong = new Requests.HuobiPong()
                    {
                        PongId = pingId
                    };
                    string json = JsonConvert.SerializeObject(huobiPong, Formatting.Indented);
                    Task.Run(() => huobiClient.Send($"{json}"));
                }

                if (jsonObj.ContainsKey("ch"))
                {
                    string processingSym = jsonObj["ch"].ToString();

                    processingSym = processingSym.Substring(7);
                    processingSym = processingSym.Remove(processingSym.Length - 15).ToUpper();

                    foreach (Symbol symObj in symbols)
                    {
                        if (symObj.ExchangeName == "huobi" && symObj.SymbolName.Replace("-", "") == processingSym)
                        {
                            processingSymObj = symObj;
                            break;
                        }
                    }

                    foreach (SymbolMarket symMarketObj in symbolMarkets)
                    {
                        if (symMarketObj.SymbolName.Replace("-", "") == processingSym)
                        {
                            processingSymMarketObj = symMarketObj;
                            break;
                        }
                    }

                    asks = jsonObj["tick"]["asks"].ToObject <List <List <string> > >();
                    bids = jsonObj["tick"]["bids"].ToObject <List <List <string> > >();

                    processingSymObj.ClearGlass();

                    foreach (var valVol in asks)
                    {
                        decimal value;
                        decimal volume;
                        try
                        {
                            value = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                        }
                        catch
                        {
                            value = decimal.Parse(valVol[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        }

                        try
                        {
                            volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                        }
                        catch
                        {
                            //Console.WriteLine($"Exception with {valVol[1]}");
                            volume = decimal.Parse(valVol[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                            //Console.WriteLine($"{valVol[1]} {volume}");
                        }
                        processingSymObj.AddAskEl(value, volume);
                    }
                    foreach (var valVol in bids)
                    {
                        decimal value;
                        decimal volume;
                        try
                        {
                            value = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                        }
                        catch
                        {
                            value = decimal.Parse(valVol[0], NumberStyles.Float, CultureInfo.InvariantCulture);
                        }

                        try
                        {
                            volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                        }
                        catch
                        {
                            //Console.WriteLine($"Exception with {valVol[1]}");
                            volume = decimal.Parse(valVol[1], NumberStyles.Float, CultureInfo.InvariantCulture);
                            //Console.WriteLine($"{valVol[1]} {volume}");
                        }
                        processingSymObj.AddBidEl(value * -1, volume);
                    }

                    processingSymObj.RegisterGlass(processingSymMarketObj.MinVolume);
                    processingSymMarketObj.WriteMarket();
                }
            });
            return(huobiClient);
        }
Esempio n. 4
0
        public static WebsocketClient StartOkex(string url, List <Symbol> symbols, List <SymbolMarket> symbolMarkets)
        {
            string                lastMessage            = "";
            Uri                   okex_url               = new Uri(url);
            var                   okexClient             = new WebsocketClient(okex_url);
            Symbol                processingSymObj       = new Symbol();
            SymbolMarket          processingSymMarketObj = new SymbolMarket();
            List <List <string> > asks = new List <List <string> >();
            List <List <string> > bids = new List <List <string> >();

            okexClient.ReconnectTimeout = TimeSpan.FromSeconds(30);
            okexClient.ReconnectionHappened.Subscribe(info =>
            {
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   Okex Reconnection happened, type: {info.Type}");
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   {lastMessage}");
            });
            okexClient
            .MessageReceived
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(msg =>
            {
                asks.Clear();
                bids.Clear();
                byte[] bytes      = msg.Binary;
                string jsonString = DecompressOkex(bytes);
                lastMessage       = jsonString;
                //LogWriters.WriteResponce("Okex", jsonString);

                JObject jsonObj = JObject.Parse(jsonString);

                if (jsonObj.ContainsKey("event"))
                {
                    if (jsonObj["event"].ToString() == $"subscribe")
                    {
                        string okSym = jsonObj["channel"].ToString();
                        okSym        = okSym.Substring(11);
                        Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture)}   Okex: {okSym} OK");
                    }
                }
                else if (jsonObj.ContainsKey("data"))
                {
                    string processingSym = jsonObj["data"][0]["instrument_id"].ToString();

                    foreach (Symbol symObj in symbols)
                    {
                        if (symObj.ExchangeName == "okex" && symObj.SymbolName == processingSym)
                        {
                            processingSymObj = symObj;
                            break;
                        }
                    }
                    foreach (SymbolMarket symMarketObj in symbolMarkets)
                    {
                        if (symMarketObj.SymbolName == processingSym)
                        {
                            processingSymMarketObj = symMarketObj;
                            break;
                        }
                    }
                    asks = jsonObj["data"][0]["asks"].ToObject <List <List <string> > >();
                    bids = jsonObj["data"][0]["bids"].ToObject <List <List <string> > >();

                    if (jsonObj["action"].ToString() == "partial")
                    {
                        processingSymObj.ClearGlass();
                        foreach (var valVol in asks)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddAskEl(value, volume);
                        }
                        foreach (var valVol in bids)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddBidEl(value * -1, volume);
                        }
                        processingSymObj.RegisterGlass(processingSymMarketObj.MinVolume);
                        processingSymMarketObj.WriteMarket();
                    }
                    if (jsonObj["action"].ToString() == "update")
                    {
                        foreach (var valVol in asks)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddAskElPartial(value, volume);
                        }
                        foreach (var valVol in bids)
                        {
                            decimal value  = Convert.ToDecimal(valVol[0], CultureInfo.InvariantCulture);
                            decimal volume = Convert.ToDecimal(valVol[1], CultureInfo.InvariantCulture);
                            processingSymObj.AddBidElPartial(value * -1, volume);
                        }
                        processingSymObj.RegisterGlass(processingSymMarketObj.MinVolume);
                        processingSymMarketObj.WriteMarket();
                    }
                }
            });
            return(okexClient);
        }