2018-05-22 10:11:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Core.Main;
|
|
|
|
|
using Core.Helper;
|
|
|
|
|
using Core.Main.DataObjects.PTMagicData;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
namespace Core.MarketAnalyzer
|
|
|
|
|
{
|
|
|
|
|
public class CoinMarketCap : BaseAnalyzer
|
|
|
|
|
{
|
|
|
|
|
public static string GetMarketData(PTMagicConfiguration systemConfiguration, LogHelper log)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
string result = "";
|
2018-12-15 22:07:29 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-01 15:05:35 +01:00
|
|
|
|
string baseUrl = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=200";
|
|
|
|
|
string cmcAPI = systemConfiguration.GeneralSettings.Application.CoinMarketCapAPIKey;
|
2018-05-22 10:11:50 +02:00
|
|
|
|
|
|
|
|
|
log.DoLogInfo("CoinMarketCap - Getting market data...");
|
|
|
|
|
|
2018-12-01 15:05:35 +01:00
|
|
|
|
Dictionary<string, dynamic> jsonObject = GetJsonFromURL(baseUrl, log, cmcAPI);
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (jsonObject.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
if (jsonObject["data"] != null)
|
|
|
|
|
{
|
2018-12-01 15:05:35 +01:00
|
|
|
|
log.DoLogInfo("CoinMarketCap - Market data received for " + jsonObject["data"].Count + " currencies");
|
2018-05-22 10:11:50 +02:00
|
|
|
|
|
|
|
|
|
Dictionary<string, Market> markets = new Dictionary<string, Market>();
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
for (int i = 0; i < jsonObject["data"].Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (jsonObject["data"][i]["quote"]["USD"] != null)
|
|
|
|
|
{
|
|
|
|
|
Market market = new Market();
|
|
|
|
|
market.Position = markets.Count + 1;
|
|
|
|
|
market.Name = jsonObject["data"][i]["name"].ToString();
|
|
|
|
|
market.Symbol = jsonObject["data"][i]["symbol"].ToString();
|
|
|
|
|
market.Price = (double)jsonObject["data"][i]["quote"]["USD"]["price"];
|
|
|
|
|
market.Volume24h = (double)jsonObject["data"][i]["quote"]["USD"]["volume_24h"];
|
|
|
|
|
if (!String.IsNullOrEmpty(jsonObject["data"][i]["quote"]["USD"]["percent_change_24h"].ToString()))
|
|
|
|
|
{
|
|
|
|
|
market.TrendChange24h = (double)jsonObject["data"][i]["quote"]["USD"]["percent_change_24h"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
markets.Add(market.Name, market);
|
2018-05-22 10:11:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-15 22:07:29 +01:00
|
|
|
|
|
2018-05-22 10:11:50 +02:00
|
|
|
|
CoinMarketCap.CheckForMarketDataRecreation(markets, systemConfiguration, log);
|
|
|
|
|
|
|
|
|
|
DateTime fileDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0).ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
FileHelper.WriteTextToFile(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathCoinMarketCap + Path.DirectorySeparatorChar, "MarketData_" + fileDateTime.ToString("yyyy-MM-dd_HH.mm") + ".json", JsonConvert.SerializeObject(markets), fileDateTime, fileDateTime);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.DoLogInfo("CoinMarketCap - Market data saved.");
|
|
|
|
|
|
|
|
|
|
FileHelper.CleanupFiles(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathCoinMarketCap + Path.DirectorySeparatorChar, systemConfiguration.AnalyzerSettings.MarketAnalyzer.StoreDataMaxHours);
|
|
|
|
|
log.DoLogInfo("CoinMarketCap - Market data cleaned.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-15 22:07:29 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
log.DoLogCritical(ex.Message, ex);
|
|
|
|
|
result = ex.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public static void CheckForMarketDataRecreation(Dictionary<string, Market> markets, PTMagicConfiguration systemConfiguration, LogHelper log)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
string coinMarketCapDataDirectoryPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathCoinMarketCap + Path.DirectorySeparatorChar;
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (!Directory.Exists(coinMarketCapDataDirectoryPath))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
Directory.CreateDirectory(coinMarketCapDataDirectoryPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectoryInfo dataDirectory = new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathCoinMarketCap + Path.DirectorySeparatorChar);
|
|
|
|
|
|
|
|
|
|
List<FileInfo> marketFiles = dataDirectory.EnumerateFiles("MarketData*")
|
|
|
|
|
.Select(x => { x.Refresh(); return x; })
|
|
|
|
|
.Where(x => x.LastWriteTimeUtc <= DateTime.Now.AddHours(-24))
|
|
|
|
|
.ToArray().OrderByDescending(f => f.LastWriteTimeUtc).ToList();
|
|
|
|
|
|
|
|
|
|
bool build24hMarketDataFile = false;
|
|
|
|
|
FileInfo marketFile = null;
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (marketFiles.Count > 0)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
marketFile = marketFiles.First();
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (marketFile.LastWriteTimeUtc <= DateTime.Now.AddHours(-24).AddMinutes(-systemConfiguration.AnalyzerSettings.MarketAnalyzer.IntervalMinutes).AddSeconds(-10))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
log.DoLogDebug("CoinMarketCap - 24h market data file too old (" + marketFile.LastWriteTimeUtc.ToString() + "). Rebuilding data...");
|
|
|
|
|
build24hMarketDataFile = true;
|
|
|
|
|
}
|
2018-12-15 22:07:29 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
marketFiles = dataDirectory.EnumerateFiles("MarketData*")
|
|
|
|
|
.Select(x => { x.Refresh(); return x; })
|
|
|
|
|
.Where(x => x.LastWriteTimeUtc >= DateTime.Now.AddHours(-24))
|
|
|
|
|
.ToArray().OrderBy(f => f.LastWriteTimeUtc).ToList();
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (marketFiles.Count > 0)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
marketFile = marketFiles.First();
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (marketFile.LastWriteTimeUtc >= DateTime.Now.AddHours(-24).AddMinutes(systemConfiguration.AnalyzerSettings.MarketAnalyzer.IntervalMinutes).AddSeconds(10))
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
log.DoLogDebug("CoinMarketCap - 24h market data file too young (" + marketFile.LastWriteTimeUtc.ToString() + "). Rebuilding data...");
|
|
|
|
|
build24hMarketDataFile = true;
|
|
|
|
|
}
|
2018-12-15 22:07:29 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
log.DoLogDebug("CoinMarketCap - 24h market data not found. Rebuilding data...");
|
|
|
|
|
build24hMarketDataFile = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
if (build24hMarketDataFile)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
Dictionary<string, Market> markets24h = new Dictionary<string, Market>();
|
2018-12-15 22:07:29 +01:00
|
|
|
|
foreach (string key in markets.Keys)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
Market market24h = new Market();
|
|
|
|
|
market24h.Position = markets.Count + 1;
|
|
|
|
|
market24h.Name = markets[key].Name;
|
|
|
|
|
market24h.Symbol = markets[key].Symbol;
|
|
|
|
|
market24h.Price = markets[key].Price / (1 + (markets[key].TrendChange24h / 100));
|
|
|
|
|
market24h.Volume24h = markets[key].Volume24h;
|
|
|
|
|
|
|
|
|
|
markets24h.Add(markets[key].Name, market24h);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DateTime fileDateTime = new DateTime(DateTime.Now.ToLocalTime().AddHours(-24).Year, DateTime.Now.ToLocalTime().AddHours(-24).Month, DateTime.Now.ToLocalTime().AddHours(-24).Day, DateTime.Now.ToLocalTime().AddHours(-24).Hour, DateTime.Now.ToLocalTime().AddHours(-24).Minute, 0).ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
FileHelper.WriteTextToFile(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathCoinMarketCap + Path.DirectorySeparatorChar, "MarketData_" + fileDateTime.ToString("yyyy-MM-dd_HH.mm") + ".json", JsonConvert.SerializeObject(markets24h), fileDateTime, fileDateTime);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.DoLogInfo("CoinMarketCap - 24h market data rebuilt.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|