示例#1
0
        public static List <string> GetMarketData(string mainMarket, Dictionary <string, MarketInfo> marketInfos, PTMagicConfiguration systemConfiguration, LogHelper log)
        {
            List <string> result = new List <string>();

            string lastMarket = "";

            Newtonsoft.Json.Linq.JObject lastTicker = null;
            try
            {
                string baseUrl = "https://fapi.binance.com/fapi/v1/ticker/24hr";

                log.DoLogInfo("BinanceFutures - Getting market data...");
                Newtonsoft.Json.Linq.JArray jsonArray = GetSimpleJsonArrayFromURL(baseUrl, log);
                if (jsonArray.Count > 0)
                {
                    double mainCurrencyPrice = 1;
                    if (!mainMarket.Equals("USDT", StringComparison.InvariantCultureIgnoreCase))
                    {
                        mainCurrencyPrice = BinanceFutures.GetMainCurrencyPrice(mainMarket, systemConfiguration, log);
                    }

                    log.DoLogInfo("BinanceFutures - Market data received for " + jsonArray.Count.ToString() + " currencies");

                    if (mainCurrencyPrice > 0)
                    {
                        Dictionary <string, Market> markets = new Dictionary <string, Market>();
                        foreach (Newtonsoft.Json.Linq.JObject currencyTicker in jsonArray)
                        {
                            string marketName = currencyTicker["symbol"].ToString();
                            //New variables for filtering out bad markets
                            float marketLastPrice = currencyTicker["lastPrice"].ToObject <float>();
                            float marketVolume    = currencyTicker["volume"].ToObject <float>();
                            if (marketName.EndsWith(mainMarket, StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (marketLastPrice > 0 && marketVolume > 0)
                                {
                                    // Set last values in case any error occurs
                                    lastMarket = marketName;
                                    lastTicker = currencyTicker;

                                    Market market = new Market();
                                    market.Position             = markets.Count + 1;
                                    market.Name                 = marketName;
                                    market.Symbol               = currencyTicker["symbol"].ToString();
                                    market.Price                = SystemHelper.TextToDouble(currencyTicker["lastPrice"].ToString(), 0, "en-US");
                                    market.Volume24h            = SystemHelper.TextToDouble(currencyTicker["quoteVolume"].ToString(), 0, "en-US");
                                    market.MainCurrencyPriceUSD = mainCurrencyPrice;

                                    markets.Add(market.Name, market);

                                    result.Add(market.Name);
                                }
                                else
                                {
                                    //Let the user know that the problem market was ignored.
                                    log.DoLogInfo("BinanceFutures - Ignoring bad market data for " + marketName);
                                }
                            }
                        }

                        BinanceFutures.CheckFirstSeenDates(markets, ref marketInfos, systemConfiguration, log);

                        BaseAnalyzer.SaveMarketInfosToFile(marketInfos, systemConfiguration, log);

                        BinanceFutures.CheckForMarketDataRecreation(mainMarket, markets, systemConfiguration, log);

                        DateTime fileDateTime = DateTime.UtcNow;

                        FileHelper.WriteTextToFile(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathExchange + Path.DirectorySeparatorChar, "MarketData_" + fileDateTime.ToString("yyyy-MM-dd_HH.mm") + ".json", JsonConvert.SerializeObject(markets), fileDateTime, fileDateTime);

                        log.DoLogInfo("BinanceFutures - Market data saved for " + markets.Count.ToString() + " markets with " + mainMarket + ".");

                        FileHelper.CleanupFiles(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathExchange + Path.DirectorySeparatorChar, systemConfiguration.AnalyzerSettings.MarketAnalyzer.StoreDataMaxHours);
                        log.DoLogInfo("BinanceFutures - Market data cleaned.");
                    }
                    else
                    {
                        log.DoLogError("BinanceFutures - Failed to get main market price for " + mainMarket + ".");
                        result = null;
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    using (HttpWebResponse errorResponse = (HttpWebResponse)ex.Response)
                    {
                        using (StreamReader reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            Dictionary <string, string> errorData = JsonConvert.DeserializeObject <Dictionary <string, string> >(reader.ReadToEnd());
                            if (errorData != null)
                            {
                                string errorMessage = "Unable to get data from BinanceFutures with URL '" + errorResponse.ResponseUri + "'!";
                                if (errorData.ContainsKey("code"))
                                {
                                    errorMessage += " - Code: " + errorData["code"];
                                }

                                if (errorData.ContainsKey("msg"))
                                {
                                    errorMessage += " - Message: " + errorData["msg"];
                                }

                                log.DoLogError(errorMessage);
                            }
                        }
                    }
                }
                result = null;
            }
            catch (Exception ex)
            {
                log.DoLogCritical("Exception while getting data for '" + lastMarket + "': " + ex.Message, ex);
                result = null;
            }

            return(result);
        }