public static void RefreshHistoryMaxMinAsync(string symbolName, string quoteCurrency) { try { // 先计算最近500天的数据, 如果数据量少, 则计算4小时数据1000天 PlatformApi api = PlatformApi.GetInstance("xx"); var klines = api.GetHistoryKline(symbolName + quoteCurrency, "1day", 500); if (klines.Count < 180) { klines = api.GetHistoryKline(symbolName + quoteCurrency, "4hour", 1000); } var min = decimal.MinValue; var max = decimal.MaxValue; min = klines.Where(it => it.Low > min).Min(it => it.Low); min = klines.Where(it => it.Low > min).Min(it => it.Low); min = klines.Where(it => it.Low > min).Min(it => it.Low); max = klines.Where(it => it.High < max).Max(it => it.High); max = klines.Where(it => it.High < max).Max(it => it.High); max = klines.Where(it => it.High < max).Max(it => it.High); // 判断max var maxNotSell = new DogMoreBuyDao().GetMaxPriceOfNotSellFinished(quoteCurrency, symbolName); if (maxNotSell > max) { max = maxNotSell; } var avgPrice = (decimal)0; foreach (var item in klines) { avgPrice += (item.Open + item.Close) / 2; } avgPrice = avgPrice / klines.Count; var dogControl = new DogControl() { HistoryMax = max, HistoryMin = min, SymbolName = symbolName, QuoteCurrency = quoteCurrency, AvgPrice = avgPrice }; new DogControlDao().UpdateDogControlMaxAndMin(dogControl); } catch (Exception ex) { logger.Error(ex.Message, ex); throw ex; } }
public static void RunAlert(CommonSymbol symbol, decimal big, decimal small) { Task.Run(() => { while (true) { try { PlatformApi api = PlatformApi.GetInstance("xx"); var period = "1min"; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period); var nowPrice = klines[0].Close; Console.WriteLine(klines[0].Close); if (nowPrice > big) { System.Diagnostics.Process.Start("explorer.exe", "http://blog.csdn.net/testcs_dn"); } if (nowPrice < small) { System.Diagnostics.Process.Start("explorer.exe", "http://blog.csdn.net/testcs_dn"); } } catch (Exception ex) { } Thread.Sleep(1000 * 5); } }); }
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); } }); }
public static void InitMarketInDB(int index, CommonSymbol symbol, bool forceUpdate = false) { try { PlatformApi api = PlatformApi.GetInstance("xx"); // 下面api和角色无关. 随便指定一个xx var period = "1min"; var count = 12; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period, count); if (klines == null || klines.Count == 0) { return; } var client = new RestClient("http://118.31.44.235/api/Control/newSymbolData"); RestRequest req = new RestRequest(Method.POST); req.AddHeader("content-type", "application/json"); req.AddHeader("cache-type", "no-cache"); req.AddJsonBody(new { BaseCurrency = symbol.BaseCurrency, QuoteCurrency = symbol.QuoteCurrency, HistoryKlines = klines }); var response = client.ExecuteTaskAsync(req).Result; } catch (Exception ex) { logger.Error("InitMarketInDB --> " + ex.Message, ex); } }
public static List <HistoryKline> ListKlines(CommonSymbol symbol) { var now = DateTime.Now; PlatformApi api = PlatformApi.GetInstance("xx"); var period = "1min"; var count = 200; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period, count); if (klines == null || klines.Count == 0) { return(null); } Console.WriteLine($"listklines: 花费时间:{(DateTime.Now - now).TotalMilliseconds}, {klines[0].Id} {klines[klines.Count - 1].Id}"); return(klines); }
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 RunAlert() { var symbols = CoinUtils.GetAllCommonSymbols("usdt"); Task.Run(() => { while (true) { try { foreach (var symbol in symbols) { PlatformApi api = PlatformApi.GetInstance("xx"); var period = "1min"; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period); var nowPrice = klines[0].Close; // 5分钟内有5%, 或者 15分钟内有10% foreach (var item in klines) { if (Utils.GetDateById(item.Id) > DateTime.Now.AddMinutes(-5) && ( item.Close / nowPrice > (decimal)1.05 || nowPrice / item.Close > (decimal)1.05)) { Console.WriteLine($"{symbol.BaseCurrency}, now:{nowPrice} ->{ item.Close} "); OpenUrlUtils.Open(); } if (Utils.GetDateById(item.Id) > DateTime.Now.AddMinutes(-15) && ( item.Close / nowPrice > (decimal)1.10 || nowPrice / item.Close > (decimal)1.10)) { Console.WriteLine($"{symbol.BaseCurrency}, now:{nowPrice} ->{ item.Close} "); OpenUrlUtils.Open(); } } } } catch (Exception ex) { } } }); }
public static void ForceShouge(CommonSymbol symbol, decimal price, string orderId) { try { PlatformApi api = PlatformApi.GetInstance("xx"); var period = "1min"; var klines = api.GetHistoryKline(symbol.BaseCurrency + symbol.QuoteCurrency, period); var nowPrice = klines[0].Close; Console.WriteLine(klines[0].Close); if (nowPrice < price) { System.Diagnostics.Process.Start("explorer.exe", "http://118.31.44.235/api/empty/forceShouge?orderid=" + orderId); } } catch (Exception ex) { } Thread.Sleep(1000 * 5); }
private async Task <Dictionary <string, decimal> > GetUsdtPrice() { // 获取btc价格 var btcPrice = new decimal(1000); PlatformApi api = PlatformApi.GetInstance("xx"); // 下面api和角色无关. 随便指定一个xx var period = "1min"; var count = 3; var klines = api.GetHistoryKline("btcusdt", period, count); btcPrice = klines[0].Close; Dictionary <string, decimal> closeDic = new Dictionary <string, decimal>(); closeDic.Add("usdt", (decimal)1); { var nowPriceList = new DogNowPriceDao().ListDogNowPrice("usdt"); foreach (var item in nowPriceList) { if (item.QuoteCurrency != "usdt") { continue; } closeDic.Add(item.SymbolName, item.NowPrice); } } { var nowPriceList = new DogNowPriceDao().ListDogNowPrice("btc"); foreach (var item in nowPriceList) { if (item.QuoteCurrency != "btc") { continue; } if (closeDic.ContainsKey(item.SymbolName)) { continue; } closeDic.Add(item.SymbolName, item.NowPrice * btcPrice); } } { var nowPriceList = new DogNowPriceDao().ListDogNowPrice("eth"); foreach (var item in nowPriceList) { if (item.QuoteCurrency != "eth") { continue; } if (closeDic.ContainsKey(item.SymbolName)) { continue; } closeDic.Add(item.SymbolName, item.NowPrice * closeDic["eth"]); } } { var nowPriceList = new DogNowPriceDao().ListDogNowPrice("ht"); foreach (var item in nowPriceList) { if (item.QuoteCurrency != "ht") { continue; } if (closeDic.ContainsKey(item.SymbolName)) { continue; } closeDic.Add(item.SymbolName, item.NowPrice * closeDic["ht"]); } } return(closeDic); }
/// <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); } }