Exemplo n.º 1
0
        /// <summary>
        /// This function adds a parse option tot the table, this should only be used to initialize the environment.
        /// </summary>
        /// <param name="tradingPairString">String representation of the trading pair.</param>
        /// <param name="tradingPair">The trading pair.</param>
        public static void AddParseEntry(string tradingPairString, TradingPair tradingPair)
        {
            Guard.Argument(tradingPairString).NotNull().NotEmpty().NotWhiteSpace();
            Guard.Argument(tradingPair).NotNull();

            // Clean the pair string of all whitespace
            string cleanedPairString = RemoveAllWhiteSpace(tradingPairString);

            if (Table.ContainsKey(cleanedPairString))
            {
                Table[cleanedPairString] = tradingPair;
                return;
            }

            Table.Add(cleanedPairString, tradingPair);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var str = serializer.Deserialize <string>(reader);

            return(TradingPair.Parse(str));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Download all currencies from Binance.
        /// </summary>
        public static void Sync()
        {
            using (var client = new BinanceClient())
            {
                // Disect by extracting the known base pairs.
                Regex rx = new Regex(
                    "(.*)(BNB|BTC|ETH|XRP|USD|USDT|TUSD|PAX|USDC|USDS)",
                    RegexOptions.Compiled | RegexOptions.IgnoreCase);

                var listQuery = client.GetExchangeInfo();
                if (!listQuery.Success)
                {
                    throw new Exception("Could not fetch TradingPair info, no connection to Binance!");
                }

                foreach (var item in listQuery.Data.Symbols)
                {
                    decimal stepSize       = 0;
                    decimal pricePrecision = 0;

                    // Extract the pair from the string
                    var pair = rx.Match(item.Name);
                    if (!pair.Success)
                    {
                        Console.WriteLine($"Could not extract pairs from {item.Name}, skipping");
                        continue;
                    }

                    string left  = pair.Groups[1].Value;
                    string right = pair.Groups[2].Value;
                    if (string.IsNullOrEmpty(left) || string.IsNullOrEmpty(right))
                    {
                        Console.WriteLine($"Either left: |{left}| or right: |{right}|  --> was a null or empty string (from {item.Name})");
                        continue;
                    }

                    // Extract the stepSize from the filter
                    foreach (var filter in item.Filters)
                    {
                        if (filter is BinanceSymbolLotSizeFilter filter1)
                        {
                            stepSize = filter1.StepSize;
                        }

                        if (filter is BinanceSymbolPriceFilter filter2)
                        {
                            pricePrecision = filter2.TickSize;
                        }
                    }

                    if (stepSize == 0 || pricePrecision == 0)
                    {
                        Console.WriteLine($"Could not extract all filters from {item.Name}, skipping");
                        continue;
                    }

                    // Add the instance to the parseTable to make it available for parsing
                    int quantityDecimals = -(int)Math.Log10((double)stepSize);
                    int priceDecimals    = -(int)Math.Log10((double)pricePrecision);
                    var result           = new TradingPair(new Currency(left), new Currency(right), quantityDecimals, priceDecimals);
                    try
                    {
                        AddParseEntry(pair.Value, result);
                    }
                    catch (ArgumentException)
                    {
                        // Double entries because of binance
                    }
                }
            }
        }