public void CheckLostOrders(List <Order> currentOrder) { var lostOrders = currentOrder.Where(a => !SessionOrders.Exists(b => b.order_id == a.order_id)).ToList(); if (lostOrders.Count > 0) { foreach (var lost in lostOrders) { AddUpdateSessionOrder(lost); Log4NetUtility.Debug($"CurrentEvent,Add lostOrders Price:{lost.price.ToString("#0.00")} Id:{Utils.ShortID(lost.order_id)}"); } } }
private void CheckSessionOrders(long dotime) { #region objectLock if (SessionOrders.Count == 0 && checkRunning) { return; } if (checkSessionDoTime == -1) { lock (objectLock) { if (checkSessionDoTime == -1) { checkSessionDoTime = dotime; } } } #endregion if (checkSessionDoTime == dotime) { try { //判断Order与缓存验证互斥操作 lock (sessionLock) { if (SessionOrders.Count == 0 || checkRunning) { return; } checkRunning = true; #region 判断单边、平仓 if (SessionOrders.Count == 0) { return; } var lastFilledOrderTime = SessionOrders.Max(a => a.create_date); int buycount = SessionOrders.Count(a => a.type == "buy"); int sellcount = SessionOrders.Count(a => a.type == "sell"); //if (buycount == 0 || sellcount == 0 || Utils.GetDateTimeDec(-1000) < lastFilledOrderTime ) bool Reset = false; if (buycount == 0 || sellcount == 0) { if (Utils.GetUtcTimeDec(-180) > lastFilledOrderTime) { Reset = true; Log4NetUtility.Info("Reset", $"单边3分钟"); } } if (Utils.GetUtcTimeDec(-600) > lastFilledOrderTime) { Reset = true; Log4NetUtility.Info("Reset", $"缓存静止10分钟"); } if (Reset) { SessionEvent?.Invoke(null, new SessionEventArgs(SessionEventType.Reset)); return; } #endregion #region 撤销重复 bool flag = false; var doubleorder = SessionOrders.FirstOrDefault(b => SessionOrders.Exists(a => a.price == b.price && a.type == b.type && a.order_id != b.order_id)); if (doubleorder != null) { SessionEvent?.Invoke(null, new SessionEventArgs(SessionEventType.DoubleOrder, new List <Order>() { doubleorder })); //Log4NetUtility.Info("", "存在重复缓存单 Price:" + doubleorder.price); //string msg = ""; #region //CancelOrder //CancelOrder cancel = robotTrade.CancelOrder(symbol, doubleorder.order_id); //if (cancel.result) //{ // sessionOrders.Remove(doubleorder); // doubleorder.status = "3"; // //DbHelper.DBSaveChange(doubleorder, "UpDate"); // Log4NetUtility.Info("缓存单验证", "已撤销重复Order。id:" + Utils.ShortID(doubleorder.order_id) + " Price:" + doubleorder.price); // CheckSessionDoTime = -1; // return; //} //else //{ // string errmsg = ""; // if (cancel.error_code.Contains("3008")) // { // sessionOrders.Remove(doubleorder); // //DbHelper.CreateInstance().RemoveOrder(doubleorder); // errmsg = "已清除脏数据Order。id:" + Utils.ShortID(doubleorder.order_id) + " Price:" + doubleorder.price; // } // else // { // errmsg = "撤销重复Order失败。id:" + Utils.ShortID(doubleorder.order_id) + " msg:" + cancel.error_code + cancel.msg; // } // Log4NetUtility.Info("缓存单验证", errmsg); // //DbHelper.CreateInstance().AddErrInfo("缓存单验证", errmsg); //} #endregion flag = true; } #endregion #region 撤销异常价格 List <Order> errPriceOrders = SessionOrders.Where(a => a.price < 50m).ToList(); if (errPriceOrders.Count > 0) { SessionEvent?.Invoke(null, new SessionEventArgs(SessionEventType.ErrPrice, errPriceOrders)); foreach (var order in errPriceOrders) { //Log4NetUtility.Info("", "存在异常价格存单 Price:" + order.price); #region CancelOrder //CancelOrder cancel = robotTrade.CancelOrder(symbol, order.order_id); //if (cancel.result) //{ // sessionOrders.Remove(order); // order.status = "3"; // //DbHelper.DBSaveChange(doubleorder, "UpDate"); // Log4NetUtility.Info("缓存单验证", "已撤销重复Order。id:" + Utils.ShortID(order.order_id) + " Price:" + order.price); //} //else //{ // string errmsg = ""; // if (cancel.error_code.Contains("3008")) // { // sessionOrders.Remove(order); // //DbHelper.CreateInstance().RemoveOrder(doubleorder); // errmsg = "已清除脏数据Order。id:" + Utils.ShortID(order.order_id) + " Price:" + order.price; // } // else // { // errmsg = "撤销重复Order失败。id:" + Utils.ShortID(doubleorder.order_id) + " msg:" + cancel.error_code + cancel.msg; // } // Log4NetUtility.Info("缓存单验证", errmsg); // //DbHelper.CreateInstance().AddErrInfo("缓存单验证", errmsg); //} #endregion } //CheckSessionDoTime = -1; flag = true; } if (flag) { return; } #endregion #region 调整订单数量 if return int deff = buycount + sellcount - info.buyOrderCount - info.sellOrderCount; #region 多单 return if (deff > 1) { Log4NetUtility.Info("MoreOrder", string.Format("Order数大于初始值,现值:{0} 初始:{1}", buycount + sellcount, info.buyOrderCount + info.sellOrderCount)); Order cancelOrder; if (buycount > sellcount) { cancelOrder = SessionOrders.OrderBy(a => a.price).First(); } else { cancelOrder = SessionOrders.OrderBy(a => a.price).Last(); } SessionEvent?.Invoke(null, new SessionEventArgs(SessionEventType.MoreOrder, new List <Order>() { cancelOrder })); return; } #endregion #region 少单 return if (deff < -1) { Log4NetUtility.Info("LessOrder", string.Format("Order数小于初始值,现值{0} 初始{1}", buycount + sellcount, info.buyOrderCount + info.sellOrderCount)); SessionEvent?.Invoke(null, new SessionEventArgs(SessionEventType.LessOrder)); return; } #endregion #endregion #region 判断补充丢单 if return var lostorder = GetLostOrder(SessionOrders); if (lostorder != null) { SessionEvent?.Invoke(null, new SessionEventArgs(SessionEventType.LostOrder, new List <Order>() { lostorder })); return; } #endregion } } #region catch\finally catch (Exception e) { //DbHelper.CreateInstance().AddErrInfo("缓存验证", e); Log4NetUtility.Error("缓存验证", Utils.Exception2String(e)); DbHelper.CreateInstance().AddError("缓存验证", e); } finally { checkSessionDoTime = -1; checkRunning = false; } #endregion } }