private static void RunHistoryKline(CommonSymbols symbol) { Task.Run(() => { var countSuccess = 0; var countError = 0; PlatformApi api = PlatformApi.GetInstance("xx"); // 下面api和角色无关. 随便指定一个xx var begin = DateTime.Now; while (true) { try { var period = "1min"; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period); var key = HistoryKlinePools.GetKey(symbol, period); HistoryKlinePools.Init(key, klines); countSuccess++; } catch (Exception ex) { countError++; } if (countSuccess % 20 == 0) { Console.WriteLine($"RunHistoryKline -> {symbol.BaseCurrency}, Success:{countSuccess}, Error:{countError}, AvageSecond:{(DateTime.Now - begin).TotalSeconds / (countSuccess + countError)}"); } Thread.Sleep(1000 * 6); } }); }
private static void RunCoin(List <CommonSymbols> symbols) { Task.Run(() => { while (true) { foreach (var symbol in symbols) { try { // 判断kline存不存在, 不存在读取一次. var key = HistoryKlinePools.GetKey(symbol, "1min"); var historyKlineData = HistoryKlinePools.Get(key); if (historyKlineData == null || historyKlineData.Data == null || historyKlineData.Data.Count == 0 || historyKlineData.Date < DateTime.Now.AddSeconds(-10)) { KlineUtils.InitOneKine(symbol); } CoinTrade.Run(symbol); } catch (Exception ex) { logger.Error("RunCoin: " + ex.Message, ex); } //Thread.Sleep(1000 * 1); } } }); }
private static void RunCoin(List <CommonSymbol> symbols) { Task.Run(() => { while (true) { var begin = DateTime.Now; for (var i = 0; i < symbols.Count; i++) { var symbol = symbols[i]; Console.WriteLine($"---> {i} {symbol.BaseCurrency},{symbol.QuoteCurrency}"); try { // 判断kline存不存在, 不存在读取一次. var key = HistoryKlinePools.GetKey(symbol, "1min"); var historyKlineData = HistoryKlinePools.Get(key); if (historyKlineData == null || historyKlineData.Data == null || historyKlineData.Data.Count == 0 || historyKlineData.Date < DateTime.Now.AddSeconds(-10)) { //KlineUtils.InitOneKine(symbol); continue; } else { KlineUtils.InitKlineInToPool(symbol); } //CoinTrade.Run(i, symbol); } catch (Exception ex) { logger.Error("RunCoin: " + ex.Message, ex); } } var useTime = (DateTime.Now - begin).TotalSeconds; if (useTime >= 60) { if (useTime >= 80) { logger.Error("一轮总共耗时:" + (DateTime.Now - begin).TotalSeconds); } } else { Thread.Sleep(1000 * (60 - (int)useTime)); } } }); }
public static void InitOneKine(CommonSymbols symbol) { try { PlatformApi api = PlatformApi.GetInstance("xx"); // 下面api和角色无关. 随便指定一个xx var period = "1min"; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period); var key = HistoryKlinePools.GetKey(symbol, period); HistoryKlinePools.Init(key, klines); // 记录到数据库 Record(symbol.BaseCurrency, klines[0]); } catch (Exception ex) { logger.Error("InitOneKine --> " + ex.Message, ex); } }
public static void InitKlineInToPool(CommonSymbol symbol) { try { var period = "1min"; var key = HistoryKlinePools.GetKey(symbol, period); var dao = new KlineDao(); var lastKlines = dao.List24HourKline(symbol.QuoteCurrency, symbol.BaseCurrency); if (lastKlines.Count < 900) { logger.Error($"{symbol.BaseCurrency},{symbol.QuoteCurrency}数据量太少{lastKlines.Count},无法分析啊:"); } if (lastKlines.Count > 600) { HistoryKlinePools.Init(key, lastKlines); } } catch (Exception ex) { logger.Error("InitOneKine --> " + ex.Message, ex); } }
/// <summary> /// 获取行情数据 /// </summary> /// <param name="symbol"></param> public static void InitOneKine(CommonSymbol symbol) { try { var begin = DateTime.Now; PlatformApi api = PlatformApi.GetInstance("xx"); // 下面api和角色无关. 随便指定一个xx var period = "1min"; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period, 10); var key = HistoryKlinePools.GetKey(symbol, period); //HistoryKlinePools.Init(key, klines); var totalMilliseconds = (DateTime.Now - begin).TotalMilliseconds; if ((DateTime.Now - begin).TotalSeconds > 5) { logger.Error("一次请求时间太长,达到:" + totalMilliseconds); } // 记录到数据库, 记录最近得数据。 Record(symbol.BaseCurrency, klines[0]); var dao = new KlineDao(); var lastKlines = dao.List24HourKline(symbol.QuoteCurrency, symbol.BaseCurrency); var findList = lastKlines.FindAll(it => klines.Find(item => item.Id == it.Id) != null).ToList(); foreach (var kline in klines) { var finds = findList.FindAll(it => it.Id == kline.Id); if (finds.Count > 1) { // 删除,新增 new KlineDao().DeleteAndRecordKlines(symbol.BaseCurrency, kline); } else if (finds.Count == 1) { if (finds[0].Low != kline.Low || finds[0].High != kline.High || finds[0].Open != kline.Open || finds[0].Close != kline.Close) { // 删除新增 new KlineDao().DeleteAndRecordKlines(symbol.BaseCurrency, kline); } } else { // 直接新增 Record(symbol.BaseCurrency, kline); } } if (lastKlines.Count < 900) { logger.Error($"{symbol.BaseCurrency}数据量太少{lastKlines.Count},无法分析啊:" + totalMilliseconds); } if (lastKlines.Count > 600) { HistoryKlinePools.Init(key, lastKlines); } totalMilliseconds = (DateTime.Now - begin).TotalMilliseconds; if ((DateTime.Now - begin).TotalSeconds > 9) { logger.Error("一次请求时间太长 含插入数据库,达到:" + totalMilliseconds); } } catch (Exception ex) { logger.Error("InitOneKine --> " + ex.Message, ex); } }