PTMagic/Core/MarketAnalyzer/Bittrex.cs

308 lines
13 KiB
C#
Raw Normal View History

2018-05-22 10:11:50 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using Core.Main;
using Core.Helper;
using Core.Main.DataObjects.PTMagicData;
using Newtonsoft.Json;
using Core.ProfitTrailer;
namespace Core.MarketAnalyzer
{
public class Bittrex : BaseAnalyzer
{
public static double GetMainCurrencyPrice(string mainMarket, PTMagicConfiguration systemConfiguration, LogHelper log)
{
2018-05-22 10:11:50 +02:00
double result = 0;
try
{
2018-05-22 10:11:50 +02:00
string baseUrl = "https://bittrex.com/api/v1.1/public/getmarketsummary?market=USDT-" + mainMarket;
log.DoLogInfo("Bittrex - Getting main market price...");
2018-12-01 15:05:35 +01:00
Dictionary<string, dynamic> jsonObject = GetJsonFromURL(baseUrl, log, "");
if (jsonObject.Count > 0)
{
if (jsonObject["success"])
{
2018-05-22 10:11:50 +02:00
log.DoLogInfo("Bittrex - Market data received for USDT-" + mainMarket);
result = jsonObject["result"][0]["Last"];
log.DoLogInfo("Bittrex - Current price for USDT-" + mainMarket + ": " + result.ToString("#,#0.00") + " USD");
}
}
}
catch (Exception ex)
{
2018-05-22 10:11:50 +02:00
log.DoLogCritical(ex.Message, ex);
}
return result;
}
public static List<string> GetMarketData(string mainMarket, Dictionary<string, MarketInfo> marketInfos, PTMagicConfiguration systemConfiguration, LogHelper log)
{
2018-05-22 10:11:50 +02:00
List<string> result = new List<string>();
string lastMarket = "";
Newtonsoft.Json.Linq.JObject lastTicker = null;
try
{
2018-05-22 10:11:50 +02:00
string baseUrl = "https://bittrex.com/api/v2.0/pub/markets/GetMarketSummaries";
log.DoLogInfo("Bittrex - Getting market data...");
2018-12-01 15:05:35 +01:00
Dictionary<string, dynamic> jsonObject = GetJsonFromURL(baseUrl, log, "");
if (jsonObject.Count > 0)
{
if (jsonObject["success"])
{
2018-05-22 10:11:50 +02:00
log.DoLogInfo("Bittrex - Market data received for " + jsonObject["result"].Count.ToString() + " currencies");
double mainCurrencyPrice = 1;
if (!mainMarket.Equals("USDT", StringComparison.InvariantCultureIgnoreCase))
{
2018-05-22 10:11:50 +02:00
mainCurrencyPrice = Bittrex.GetMainCurrencyPrice(mainMarket, systemConfiguration, log);
}
if (mainCurrencyPrice > 0)
{
2018-05-22 10:11:50 +02:00
Dictionary<string, Market> markets = new Dictionary<string, Market>();
foreach (Newtonsoft.Json.Linq.JObject currencyTicker in jsonObject["result"])
{
2018-05-22 10:11:50 +02:00
string marketName = currencyTicker["Summary"]["MarketName"].ToString();
if (marketName.StartsWith(mainMarket, StringComparison.InvariantCultureIgnoreCase))
{
2018-05-22 10:11:50 +02:00
// 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["Summary"]["MarketName"].ToString();
if (currencyTicker["Summary"]["Last"].Type == Newtonsoft.Json.Linq.JTokenType.Float) market.Price = (double)currencyTicker["Summary"]["Last"];
if (currencyTicker["Summary"]["BaseVolume"].Type == Newtonsoft.Json.Linq.JTokenType.Float) market.Volume24h = (double)currencyTicker["Summary"]["BaseVolume"];
market.MainCurrencyPriceUSD = mainCurrencyPrice;
markets.Add(market.Name, market);
result.Add(market.Name);
// Save market info
MarketInfo marketInfo = null;
if (marketInfos.ContainsKey(marketName))
{
2018-05-22 10:11:50 +02:00
marketInfo = marketInfos[marketName];
}
if (marketInfo == null)
{
2018-05-22 10:11:50 +02:00
marketInfo = new MarketInfo();
marketInfo.Name = marketName;
marketInfos.Add(marketName, marketInfo);
}
if (currencyTicker["Summary"]["Created"].Type == Newtonsoft.Json.Linq.JTokenType.Date) marketInfo.FirstSeen = (DateTime)currencyTicker["Summary"]["Created"];
marketInfo.LastSeen = DateTime.UtcNow;
2018-05-22 10:11:50 +02:00
}
}
BaseAnalyzer.SaveMarketInfosToFile(marketInfos, systemConfiguration, log);
Bittrex.CheckForMarketDataRecreation(mainMarket, markets, systemConfiguration, log);
DateTime fileDateTime = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, DateTime.UtcNow.Hour, DateTime.UtcNow.Minute, 0).ToUniversalTime();
2018-05-22 10:11:50 +02:00
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("Bittrex - 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("Bittrex - Market data cleaned.");
}
else
{
2018-05-22 10:11:50 +02:00
log.DoLogError("Bittrex - Failed to get main market price for " + mainMarket + ".");
result = null;
}
}
}
}
catch (Exception ex)
{
2018-05-22 10:11:50 +02:00
log.DoLogCritical("Exception while getting data for '" + lastMarket + "': " + ex.Message, ex);
result = null;
}
return result;
}
public static List<MarketTick> GetMarketTicks(string marketName, PTMagicConfiguration systemConfiguration, LogHelper log)
{
2018-05-22 10:11:50 +02:00
List<MarketTick> result = new List<MarketTick>();
try
{
2018-05-22 10:11:50 +02:00
string baseUrl = "https://bittrex.com/Api/v2.0/pub/market/GetTicks?tickInterval=oneMin&marketName=" + marketName;
log.DoLogDebug("Bittrex - Getting ticks for '" + marketName + "'...");
2018-12-01 15:05:35 +01:00
Dictionary<string, dynamic> jsonObject = GetJsonFromURL(baseUrl, log, "");
if (jsonObject.Count > 0)
{
if (jsonObject["success"])
{
if (jsonObject["result"] != null)
{
2018-05-22 10:11:50 +02:00
log.DoLogDebug("Bittrex - " + jsonObject["result"].Count.ToString() + " ticks received.");
foreach (var marketTick in jsonObject["result"])
{
2018-05-22 10:11:50 +02:00
MarketTick tick = new MarketTick();
tick.Price = (double)marketTick["C"];
tick.Time = SystemHelper.TextToDateTime(marketTick["T"].ToString(), Constants.confMinDate);
result.Add(tick);
}
}
else
{
2018-05-22 10:11:50 +02:00
log.DoLogDebug("Bittrex - No ticks received.");
}
}
}
}
catch (Exception ex)
{
2018-05-22 10:11:50 +02:00
log.DoLogCritical(ex.Message, ex);
}
return result;
}
public static void CheckForMarketDataRecreation(string mainMarket, Dictionary<string, Market> markets, PTMagicConfiguration systemConfiguration, LogHelper log)
{
2018-05-22 10:11:50 +02:00
string bittrexDataDirectoryPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathData + Path.DirectorySeparatorChar + Constants.PTMagicPathExchange + Path.DirectorySeparatorChar;
if (!Directory.Exists(bittrexDataDirectoryPath))
{
2018-05-22 10:11:50 +02:00
Directory.CreateDirectory(bittrexDataDirectoryPath);
}
DirectoryInfo dataDirectory = new DirectoryInfo(bittrexDataDirectoryPath);
// Check for existing market files
DateTime latestMarketDataFileDateTime = Constants.confMinDate;
List<FileInfo> marketFiles = dataDirectory.EnumerateFiles("MarketData*").ToList();
FileInfo latestMarketDataFile = null;
if (marketFiles.Count > 0)
{
2018-05-22 10:11:50 +02:00
latestMarketDataFile = marketFiles.OrderByDescending(mdf => mdf.LastWriteTimeUtc).First();
latestMarketDataFileDateTime = latestMarketDataFile.LastWriteTimeUtc;
}
if (latestMarketDataFileDateTime < DateTime.UtcNow.AddMinutes(-20))
{
int lastMarketDataAgeInSeconds = (int)Math.Ceiling(DateTime.UtcNow.Subtract(latestMarketDataFileDateTime).TotalSeconds);
2018-05-22 10:11:50 +02:00
// Go back in time and create market data
DateTime startDateTime = DateTime.UtcNow;
DateTime endDateTime = DateTime.UtcNow.AddHours(-systemConfiguration.AnalyzerSettings.MarketAnalyzer.StoreDataMaxHours);
if (latestMarketDataFileDateTime != Constants.confMinDate && latestMarketDataFileDateTime > endDateTime)
{
2018-05-22 10:11:50 +02:00
// Existing market files too old => Recreate market data for configured timeframe
log.DoLogInfo("Bittrex - Recreating market data for " + markets.Count + " markets over " + SystemHelper.GetProperDurationTime(lastMarketDataAgeInSeconds) + ". This may take a while...");
endDateTime = latestMarketDataFileDateTime;
}
else
{
2018-05-22 10:11:50 +02:00
// No existing market files found => Recreate market data for configured timeframe
log.DoLogInfo("Bittrex - Recreating market data for " + markets.Count + " markets over " + systemConfiguration.AnalyzerSettings.MarketAnalyzer.StoreDataMaxHours + " hours. This may take a while...");
}
// Get Ticks for main market
List<MarketTick> mainMarketTicks = new List<MarketTick>();
if (!mainMarket.Equals("USDT", StringComparison.InvariantCultureIgnoreCase))
{
2018-05-22 10:11:50 +02:00
mainMarketTicks = Bittrex.GetMarketTicks("USDT-" + mainMarket, systemConfiguration, log);
}
// Get Ticks for all markets
log.DoLogDebug("Bittrex - Getting ticks for '" + markets.Count + "' markets");
Dictionary<string, List<MarketTick>> marketTicks = new Dictionary<string, List<MarketTick>>();
foreach (string key in markets.Keys)
{
2018-05-22 10:11:50 +02:00
marketTicks.Add(key, Bittrex.GetMarketTicks(key, systemConfiguration, log));
if ((marketTicks.Count % 10) == 0)
{
2018-05-22 10:11:50 +02:00
log.DoLogInfo("Bittrex - No worries, I am still alive... " + marketTicks.Count + "/" + markets.Count + " markets done...");
}
}
log.DoLogInfo("Bittrex - Ticks completed.");
log.DoLogInfo("Bittrex - Creating initial market data ticks. This may take another while...");
int totalTicks = (int)Math.Ceiling(startDateTime.Subtract(endDateTime).TotalMinutes);
int completedTicks = 0;
if (marketTicks.Count > 0)
{
for (DateTime tickTime = startDateTime.ToUniversalTime(); tickTime >= endDateTime.ToUniversalTime(); tickTime = tickTime.AddMinutes(-1))
{
2018-05-22 10:11:50 +02:00
completedTicks++;
double mainCurrencyPrice = 1;
if (mainMarketTicks.Count > 0)
{
2018-05-22 10:11:50 +02:00
List<MarketTick> mainCurrencyTickRange = mainMarketTicks.FindAll(t => t.Time <= tickTime);
if (mainCurrencyTickRange.Count > 0)
{
2018-05-22 10:11:50 +02:00
MarketTick mainCurrencyTick = mainCurrencyTickRange.OrderByDescending(t => t.Time).First();
mainCurrencyPrice = mainCurrencyTick.Price;
}
}
Dictionary<string, Market> tickMarkets = new Dictionary<string, Market>();
foreach (string key in markets.Keys)
{
2018-05-22 10:11:50 +02:00
List<MarketTick> tickRange = marketTicks[key].FindAll(t => t.Time <= tickTime);
if (tickRange.Count > 0)
{
2018-05-22 10:11:50 +02:00
MarketTick marketTick = tickRange.OrderByDescending(t => t.Time).First();
Market market = new Market();
market.Position = markets.Count + 1;
market.Name = key;
market.Symbol = key;
market.Price = marketTick.Price;
//market.Volume24h = marketTick.Volume24h;
market.MainCurrencyPriceUSD = mainCurrencyPrice;
tickMarkets.Add(market.Name, market);
}
}
DateTime fileDateTime = new DateTime(tickTime.ToLocalTime().Year, tickTime.ToLocalTime().Month, tickTime.ToLocalTime().Day, tickTime.ToLocalTime().Hour, tickTime.ToLocalTime().Minute, 0).ToUniversalTime();
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(tickMarkets), fileDateTime, fileDateTime);
log.DoLogDebug("Bittrex - Market data saved for tick " + fileDateTime.ToString() + " - MainCurrencyPrice=" + mainCurrencyPrice.ToString("#,#0.00") + " USD.");
if ((completedTicks % 100) == 0)
{
2018-05-22 10:11:50 +02:00
log.DoLogInfo("Bittrex - Our magicbots are still at work, hang on... " + completedTicks + "/" + totalTicks + " ticks done...");
}
}
}
log.DoLogInfo("Bittrex - Initial market data created. Ready to go!");
}
}
}
}