public string LogSring() { int buynum = SessionOrders.Count(a => a.type == "buy"); int sellnum = SessionOrders.Count(a => a.type == "sell"); var list = SessionOrders.OrderByDescending(a => a.price); StringBuilder sb_orders = new StringBuilder(); sb_orders.AppendLine(); foreach (var item in list) { sb_orders.AppendLine(string.Format("{0} Price:{1},Amount:{2},ID:{3}", item.type == "buy" ? "+B" : "-S", item.price, item.amount, item.order_id)); } return($"Buy Count:{buynum},Sell Count:{sellnum}. {sb_orders.ToString()}"); }
public int CountOrder(bool buy) { try { if (buy) { return(SessionOrders.Count(a => a.type == "buy")); } else { return(SessionOrders.Count(a => a.type == "sell")); } } catch (Exception e) { throw (e); } }
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 } }