public decimal AnalyzeNeedSell(decimal comparePrice, DateTime compareDate, string coin, string toCoin, out decimal nowOpen) { // 当前open nowOpen = 0; decimal higher = new decimal(0); try { ResponseKline res = new AnaylyzeApi().kline(coin + toCoin, "1min", 1440); nowOpen = res.data[0].open; List <FlexPoint> flexPointList = new List <FlexPoint>(); decimal openHigh = res.data[0].open; decimal openLow = res.data[0].open; long idHigh = 0; long idLow = 0; int lastHighOrLow = 0; // 1 high, -1: low foreach (var item in res.data) { if (Utils.GetDateById(item.id) < compareDate) { continue; } if (item.open > higher) { higher = item.open; } } } catch (Exception ex) { logger.Error(ex.Message, ex); Console.WriteLine("1111111111111111111111 over " + ex.Message); } return(higher); }
public decimal GetMinAndMax(string coin, string toCoin, string minPeriod = "1min") { ResponseKline res = new AnaylyzeApi().kline(coin + toCoin, minPeriod, 1440); Console.WriteLine(Utils.GetDateById(res.data[0].id)); Console.WriteLine(Utils.GetDateById(res.data[res.data.Count - 1].id)); decimal max = 0; decimal min = 999999; decimal now = res.data[0].open; foreach (var item in res.data) { if (max < item.open) { max = item.open; } if (min > item.open) { min = item.open; } } return((now - min) / (max - min)); }
public bool CheckCalcMaxhuoluo(string coin, string toCoin, string minPeriod = "1min") { ResponseKline res = new AnaylyzeApi().kline(coin + toCoin, minPeriod, 1440); Console.WriteLine(Utils.GetDateById(res.data[0].id)); Console.WriteLine(Utils.GetDateById(res.data[res.data.Count - 1].id)); decimal max = 0; decimal min = 999999; decimal now = res.data[0].open; foreach (var item in res.data) { if (max < item.open) { max = item.open; } if (min > item.open) { min = item.open; } } logger.Error($"火币回落, {max}, {min} {res.data[0].open}"); return(max > res.data[0].open * (decimal)1.06); // 是否下降6% }
public static void BusinessRun(string coin) { var accountId = ""; ResponseKline res = new AnaylyzeApi().kline(coin + "usdt", "1min", 1440); // 获取最近行情 decimal lastLow; decimal nowOpen; // 分析是否下跌, 下跌超过一定数据,可以考虑 var flexPointList = new CoinAnalyze().Analyze(res, out lastLow, out nowOpen); if (flexPointList.Count == 0) { logger.Error($"--------------> 分析结果数量为0 {coin}"); return; } decimal recommendAmount = GetRecommendBuyAmount(coin); Console.Write($"spot--------> 开始 {coin} 推荐额度:{decimal.Round(recommendAmount, 2)} "); try { // 查询出结果还没好的数据, 去搜索一下 var noSetBuySuccess = new CoinDao().ListNotSetBuySuccess(accountId, coin); foreach (var item in noSetBuySuccess) { QueryDetailAndUpdate(item.BuyOrderId); } } catch (Exception ex) { logger.Error(ex.Message, ex); } try { // 查询出结果还没好的数据, 去搜索一下 var noSetSellSuccess = new CoinDao().ListHasSellNotSetSellSuccess(accountId, coin); foreach (var item in noSetSellSuccess) { Console.WriteLine("----------> " + JsonConvert.SerializeObject(item)); QuerySellDetailAndUpdate(item.SellOrderId); } } catch (Exception ex) { logger.Error(ex.Message, ex); } if (!flexPointList[0].isHigh && CheckBalance() && recommendAmount > (decimal)0.3 && !IsQuickRise(res)) { var noSellCount = new CoinDao().GetNoSellRecordCount(accountId, coin); // 最后一次是高位, 没有交易记录, 则判断是否少于最近的6% if (noSellCount <= 0 && CheckCanBuy(nowOpen, flexPointList[0].open) && new CoinAnalyze().CheckCalcMaxhuoluo(coin, "usdt", "5min")) { // 可以考虑 decimal buyQuantity = recommendAmount / nowOpen; buyQuantity = decimal.Round(buyQuantity, GetBuyQuantityPrecisionNumber(coin)); decimal orderPrice = decimal.Round(nowOpen * (decimal)1.005, getPrecisionNumber(coin)); ResponseOrder order = new AccountOrder().NewOrderBuy(accountId, buyQuantity, orderPrice, null, coin, "usdt"); if (order.status != "error") { new CoinDao().CreateSpotRecord(new OkexRecord() { Coin = coin, UserName = AccountConfig.userName, BuyTotalQuantity = buyQuantity, BuyOrderPrice = orderPrice, BuyDate = DateTime.Now, HasSell = false, BuyOrderResult = JsonConvert.SerializeObject(order), BuyAnalyze = JsonConvert.SerializeObject(flexPointList), AccountId = accountId, BuySuccess = false, BuyTradePrice = 0, BuyOrderId = order.data, BuyOrderQuery = "", SellAnalyze = "", SellOrderId = "", SellOrderQuery = "", SellOrderResult = "" }); ClearData(); // 下单成功马上去查一次 QueryDetailAndUpdate(order.data); } else { logger.Error($"下单结果 coin{coin} accountId:{accountId} 购买数量{buyQuantity} nowOpen{nowOpen} {JsonConvert.SerializeObject(order)}"); logger.Error($"下单结果 分析 {JsonConvert.SerializeObject(flexPointList)}"); } } if (noSellCount > 0) { // 获取最小的那个, 如果有, decimal minBuyPrice = 9999; var noSellList = new CoinDao().ListNoSellRecord(accountId, coin); foreach (var item in noSellList) { if (item.BuyOrderPrice < minBuyPrice) { minBuyPrice = item.BuyOrderPrice; } } // 再少于5%, var per = new CoinAnalyze().CalcPercent(coin); decimal pecent = getCalcPencent222(per);//noSellCount >= 15 ? (decimal)1.03 : (decimal)1.025; if (nowOpen * pecent < minBuyPrice) { decimal buyQuantity = recommendAmount / nowOpen; buyQuantity = decimal.Round(buyQuantity, GetBuyQuantityPrecisionNumber(coin)); decimal orderPrice = decimal.Round(nowOpen * (decimal)1.005, getPrecisionNumber(coin)); ResponseOrder order = new AccountOrder().NewOrderBuy(accountId, buyQuantity, orderPrice, null, coin, "usdt"); if (order.status != "error") { new CoinDao().CreateSpotRecord(new OkexRecord() { Coin = coin, UserName = AccountConfig.userName, BuyTotalQuantity = buyQuantity, BuyOrderPrice = orderPrice, BuyDate = DateTime.Now, HasSell = false, BuyOrderResult = JsonConvert.SerializeObject(order), BuyAnalyze = JsonConvert.SerializeObject(flexPointList), AccountId = accountId, BuySuccess = false, BuyTradePrice = 0, BuyOrderId = order.data, BuyOrderQuery = "", SellAnalyze = "", SellOrderId = "", SellOrderQuery = "", SellOrderResult = "" }); ClearData(); // 下单成功马上去查一次 QueryDetailAndUpdate(order.data); } else { logger.Error($"下单结果 coin{coin} accountId:{accountId} 购买数量{buyQuantity} nowOpen{nowOpen} {JsonConvert.SerializeObject(order)}"); logger.Error($"下单结果 分析 {JsonConvert.SerializeObject(flexPointList)}"); } } } } // 查询数据库中已经下单数据,如果有,则比较之后的最高值,如果有,则出售 var needSellList = new CoinDao().ListBuySuccessAndNoSellRecord(accountId, coin); foreach (var item in needSellList) { // 分析是否 大于 decimal itemNowOpen = 0; decimal higher = new CoinAnalyze().AnalyzeNeedSell(item.BuyOrderPrice, item.BuyDate, coin, "usdt", out itemNowOpen); decimal gaoyuPercentSell = (decimal)1.03; if (needSellList.Count > 10) { gaoyuPercentSell = (decimal)1.045; } else if (needSellList.Count > 9) { gaoyuPercentSell = (decimal)1.042; } else if (needSellList.Count > 8) { gaoyuPercentSell = (decimal)1.04; } else if (needSellList.Count > 7) { gaoyuPercentSell = (decimal)1.038; } else if (needSellList.Count > 6) { gaoyuPercentSell = (decimal)1.035; } else if (needSellList.Count > 5) { gaoyuPercentSell = (decimal)1.032; } if (CheckCanSell(item.BuyOrderPrice, higher, itemNowOpen, gaoyuPercentSell)) { decimal sellQuantity = item.BuyTotalQuantity * (decimal)0.99; sellQuantity = decimal.Round(sellQuantity, getSellPrecisionNumber(coin)); // 出售 decimal sellPrice = decimal.Round(itemNowOpen * (decimal)0.985, getPrecisionNumber(coin)); ResponseOrder order = new AccountOrder().NewOrderSell(accountId, sellQuantity, sellPrice, null, coin, "usdt"); if (order.status != "error") { new CoinDao().ChangeDataWhenSell(item.Id, sellQuantity, sellPrice, JsonConvert.SerializeObject(order), JsonConvert.SerializeObject(flexPointList), order.data); // 下单成功马上去查一次 QuerySellDetailAndUpdate(order.data); } else { logger.Error($"出售结果 coin{coin} accountId:{accountId} 出售数量{sellQuantity} itemNowOpen{itemNowOpen} higher{higher} {JsonConvert.SerializeObject(order)}"); logger.Error($"出售结果 分析 {JsonConvert.SerializeObject(flexPointList)}"); } ClearData(); } } }