예제 #1
0
        /// <summary>
        /// 添加限价订单
        /// </summary>
        /// <param name="strUserKey"></param>
        /// <param name="UserID"></param>
        /// <param name="StockCode"></param>
        /// <param name="Market"></param>
        /// <param name="Volume"></param>
        /// <param name="OrderPrice"></param>
        /// <param name="Side"></param>
        /// <param name="ValidDays"></param>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public RI_Result RequestLimitedOrder(string strUserKey, int UserID, string StockCode, RI_Market Market, int Volume, double OrderPrice, bool Side, ushort ValidDays, out int OrderID)
        {
            OrderID = -1;
            try
            {
                if (strUserKey == null || string.Compare(strUserKey.Trim(),
                                                         Common.Config("Authorization", "UserKey").Trim()) != 0)
                {
                    if (strUserKey != null)
                    {
                        Common.Log("The Invoker [" + strUserKey.Trim() + "] Is Unauthorized. [UserID=" + UserID + "]");
                    }
                    else
                    {
                        Common.Log("The Invoker [null] Is Unauthorized. [UserID=" + UserID + "]");
                    }
                    return(RI_Result.Unauthorized);
                }
                else if (!Common.Switch_LimitedOrder || !Management.Work)
                {
                    Common.Log("The Interface [ProcessLimitedOrder] Is Closed. [UserID=" + UserID + "]");
                    return(RI_Result.Closed_Interface);
                }

                if (!Common.IsInInterfaceQuotationTime)
                {
                    return(RI_Result.Out_Of_Quotation_Time);
                }
                else if (Common.stkTrading == null)
                {
                    return(RI_Result.Internal_Error);
                }
                else if (UserID <= 0)
                {
                    return(RI_Result.Illegal_UserID);
                }

                TradingSystem.StockType stkType = Common.stkTrading.GetStockType(StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (Volume <= 0 || (Side &&
                                    (stkType != TradingSystem.StockType.SH_Warrant && stkType != TradingSystem.StockType.SZ_Warrant && Volume % 100 != 0) ||
                                    ((stkType == TradingSystem.StockType.SH_Warrant || stkType == TradingSystem.StockType.SZ_Warrant) && Volume % 1000 != 0)))
                {
                    return(RI_Result.Illegal_Volume);
                }
                else if (OrderPrice < 0.001)
                {
                    return(RI_Result.Illegal_Price);
                }
                else if ((stkType == TradingSystem.StockType.SH_A || stkType == TradingSystem.StockType.SZ_A) && OrderPrice < 0.01)
                {
                    return(RI_Result.Illegal_Price);
                }
                else if (StockCode == null || Market == RI_Market.Unknown)
                {
                    return(RI_Result.Banned_Stock);
                }
                StockCode = StockCode.Trim();
                TradingSystem.Currency Curr = TradingSystem.Currency.Unknown;
                switch (stkType)
                {
                case TradingSystem.StockType.SH_B:
                    Curr = TradingSystem.Currency.USD;
                    break;

                case TradingSystem.StockType.SZ_B:
                    Curr = TradingSystem.Currency.HKD;
                    break;

                default:
                    Curr = TradingSystem.Currency.RMB;
                    break;
                }
                if (IsBanned(stkType))
                {
                    Common.Debug("Banned_Stock:" + UserID + ";" + StockCode + ";" + Market.ToString().Trim() + ";" + stkType.ToString().Trim());
                    return(RI_Result.Banned_Stock);
                }
                Show2003DBFRecord SHRec = new Show2003DBFRecord(); SHRec.Clear();
                SjshqDBFRecord    SZRec = new SjshqDBFRecord(); SZRec.Clear();
                if (Common.stkQuotation.CheckSuspended(StockCode, (TradingSystem.StockMarket)((byte)Market)))
                {
                    Common.Debug("Suspended_Stock:" + UserID + ";" + StockCode + ";" + Market.ToString());
                    return(RI_Result.Suspended_Stock);
                }

                short sCurrCount = Common.stkTrading.GetUserOrdersCount(UserID);
                if (sCurrCount == -2)
                {
                    return(RI_Result.Illegal_UserID);
                }
                if (sCurrCount < 0 || sCurrCount >= Common.MaxOrders)
                {
                    Common.Debug("Too_Many_Orders:" + UserID + ";" + sCurrCount + "/" + Common.MaxOrders);
                    return(RI_Result.Too_Many_Orders);
                }
                int SellableVolume = Common.stkTrading.GetSellableStockVolume(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (!Side && (SellableVolume < Volume))
                {
                    Common.Debug("Not_Enough_Stock:" + UserID + "/" + StockCode.Trim() + "-" +
                                 (byte)Market + ";" + SellableVolume + "/" + Volume);
                    return(RI_Result.Not_Enough_Stock);
                }
                if (!Side && (Volume % 100 != 0) && (Volume % 100 != SellableVolume % 100))
                {
                    return(RI_Result.Illegal_Volume);
                }
                if (Side && !Common.stkTrading.CanBuy(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market), Curr, OrderPrice * Volume * (1 + Common.stkTrading.defaultBuyTax)))
                {
                    Common.Debug("Speculation_Behavior:" + UserID + "/" + StockCode.Trim() + "-"
                                 + (byte)Market + "/" + (OrderPrice * Volume * (1 + Common.stkTrading.defaultBuyTax)).ToString("f2"));
                    return(RI_Result.Speculation_Behavior);
                }

                TradingSystem.UserOrders userOrder = new TradingSystem.UserOrders();
                userOrder.Initialize();
                userOrder.OrderID     = Common.stkTrading.nLastOrderID++;
                userOrder.UserID      = UserID;
                userOrder.StockCode   = StockCode.Trim();
                userOrder.Market      = (TradingSystem.StockMarket)((byte)Market);
                userOrder.Side        = Side;
                userOrder.OrderVolume = Volume;
                userOrder.OrderPrice  = OrderPrice;
                userOrder.OrderDate   = DateTime.Now;
                userOrder.ExpiredDate = userOrder.OrderDate.Date.Add(new TimeSpan(ValidDays,
                                                                                  Common.EndPMTS.Hours, Common.EndPMTS.Minutes, Common.EndPMTS.Seconds));
                userOrder.OrdStatus = TradingSystem.OrderStatus.Waiting;
                userOrder.OrdType   = TradingSystem.OrderType.LimitedOrder;
                userOrder.Curr      = Curr;
                if (Side)
                {
                    lock (Common.stkTrading.listNewUserFund)
                    {
                        TradingSystem.UserFund usableFund = new TradingSystem.UserFund(); usableFund.Initialize();
                        for (int i = 0; i < Common.stkTrading.listNewUserFund.Count; i++)
                        {
                            if (Common.stkTrading.listNewUserFund[i].UserID == UserID &&
                                ((stkType == TradingSystem.StockType.SH_B && Common.stkTrading.listNewUserFund[i].Curr == TradingSystem.Currency.USD) ||
                                 (stkType == TradingSystem.StockType.SZ_B && Common.stkTrading.listNewUserFund[i].Curr == TradingSystem.Currency.HKD) ||
                                 Common.stkTrading.listNewUserFund[i].Curr == TradingSystem.Currency.RMB))
                            {
                                usableFund             = Common.stkTrading.listNewUserFund[i];
                                usableFund.UsableCash -= Common.ConvertPrice((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax) + 0.0099);
                                if (usableFund.UsableCash < 0)
                                {
                                    return(RI_Result.Not_Enough_Cash);
                                }
                                if (!Common.stkTrading.SetUserFund(UserID, usableFund))
                                {
                                    return(RI_Result.Illegal_UserID);
                                }
                                Common.stkTrading.listNewUserFund[i] = usableFund;
                                Common.DBSync.FundUpdate(usableFund, UserID);
                                lock (Common.stkTrading.listNewUserOrders)
                                {
                                    Common.stkTrading.listNewUserOrders.Add(userOrder);
                                    Common.DBSync.OrderAppended(userOrder, UserID);
                                }
                                Common.Debug("Limited Order Requested [UserID=" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "].");
                                OrderID = userOrder.OrderID;
                                return(RI_Result.Success);
                            }
                        }

                        Dictionary <byte, TradingSystem.UserFund> mapUsableFund = new Dictionary <byte, TradingSystem.UserFund>();
                        if (!Common.stkTrading.GetUserFund(UserID, out mapUsableFund))
                        {
                            return(RI_Result.Illegal_UserID);
                        }
                        switch (stkType)
                        {
                        case TradingSystem.StockType.SH_B:
                        {
                            if (mapUsableFund.ContainsKey((byte)TradingSystem.Currency.USD))
                            {
                                usableFund = mapUsableFund[(byte)TradingSystem.Currency.USD];
                            }
                            else
                            {
                                return(RI_Result.Not_Enough_Cash);
                            }
                        }
                        break;

                        case TradingSystem.StockType.SZ_B:
                        {
                            if (mapUsableFund.ContainsKey((byte)TradingSystem.Currency.HKD))
                            {
                                usableFund = mapUsableFund[(byte)TradingSystem.Currency.HKD];
                            }
                            else
                            {
                                return(RI_Result.Not_Enough_Cash);
                            }
                        }
                        break;

                        default:
                        {
                            if (mapUsableFund.ContainsKey((byte)TradingSystem.Currency.RMB))
                            {
                                usableFund = mapUsableFund[(byte)TradingSystem.Currency.RMB];
                            }
                            else
                            {
                                return(RI_Result.Not_Enough_Cash);
                            }
                        }
                        break;
                        }
                        usableFund.UsableCash -= Common.ConvertPrice((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax) + 0.0099);
                        if (usableFund.UsableCash < 0)
                        {
                            Common.Debug("Not_Enough_Cash:" + UserID + "/" + StockCode.Trim() + "-" + (byte)Market + ";" +
                                         (usableFund.UsableCash + Common.ConvertPrice((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax)))
                                         + "/" + ((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax)));
                            return(RI_Result.Not_Enough_Cash);
                        }
                        if (!Common.stkTrading.SetUserFund(UserID, usableFund))
                        {
                            return(RI_Result.Illegal_UserID);
                        }
                        Common.stkTrading.listNewUserFund.Add(usableFund);
                        Common.DBSync.FundUpdate(usableFund, UserID);
                    }
                }

                lock (Common.stkTrading.listNewUserOrders)
                {
                    Common.stkTrading.listNewUserOrders.Add(userOrder);
                    Common.DBSync.OrderAppended(userOrder, UserID);
                }
                Common.Debug("Limited Order Requested [UserID=" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "].");
                OrderID = userOrder.OrderID;
                return(RI_Result.Success);
            }
            catch
            {
                return(RI_Result.Internal_Error);
            }
        }
예제 #2
0
        /// <summary>
        /// 撤单
        /// </summary>
        /// <param name="strUserKey"></param>
        /// <param name="UserID"></param>
        /// <param name="OrderID"></param>
        /// <param name="ValidDays"></param>
        /// <returns></returns>
        public RI_Result RequestCancelOrder(string strUserKey, int UserID, int OrderID, ushort ValidDays)
        {
            try
            {
                if (strUserKey == null || string.Compare(strUserKey.Trim(),
                                                         Common.Config("Authorization", "UserKey").Trim()) != 0)
                {
                    if (strUserKey != null)
                    {
                        Common.Log("The Invoker [" + strUserKey.Trim() + "] Is Unauthorized. [UserID=" + UserID + "]");
                    }
                    else
                    {
                        Common.Log("The Invoker [null] Is Unauthorized. [UserID=" + UserID + "]");
                    }
                    return(RI_Result.Unauthorized);
                }
                else if (!Common.Switch_CancelOrder || !Management.Work)
                {
                    Common.Log("The Interface [ProcessCancelOrder] Is Closed. [UserID=" + UserID + "]");
                    return(RI_Result.Closed_Interface);
                }

                if (!Common.IsInInterfaceQuotationTime)
                {
                    return(RI_Result.Out_Of_Quotation_Time);
                }
                else if (Common.stkTrading == null)
                {
                    return(RI_Result.Internal_Error);
                }
                else if (UserID <= 0)
                {
                    return(RI_Result.Illegal_UserID);
                }
                else if (OrderID <= 0)
                {
                    return(RI_Result.Illegal_Cancelling);
                }
                TradingSystem.UserOrders targetOrder = new TradingSystem.UserOrders();
                targetOrder.Initialize();
                if (!Common.stkTrading.GetUserOrder(UserID, OrderID, out targetOrder))
                {
                    return(RI_Result.Illegal_Cancelling);
                }
                switch (targetOrder.OrdType)
                {
                case TradingSystem.OrderType.LimitedOrder:
                    break;

                default:
                    return(RI_Result.Illegal_Cancelling);
                }
                if (targetOrder.OrdStatus != TradingSystem.OrderStatus.Waiting)
                {
                    return(RI_Result.Illegal_Cancelling);
                }
                targetOrder.OrdStatus = TradingSystem.OrderStatus.Cancelling;
                if (!Common.stkTrading.CancelUserOrder(UserID, OrderID))
                {
                    return(RI_Result.Illegal_Cancelling);
                }

                Common.Debug("Cancel Order Requested [UserID=" + UserID + "; OrderID=" + OrderID + "].");
                return(RI_Result.Success);
            }
            catch
            {
                return(RI_Result.Internal_Error);
            }
        }
예제 #3
0
        /// <summary>
        /// 添加即时订单
        /// </summary>
        /// <param name="strUserKey"></param>
        /// <param name="UserID"></param>
        /// <param name="StockCode"></param>
        /// <param name="Market"></param>
        /// <param name="Volume"></param>
        /// <param name="Side"></param>
        /// <param name="ValidDays"></param>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public RI_Result RequestImmediateOrder(string strUserKey, int UserID, string StockCode, RI_Market Market, int Volume, bool Side, ushort ValidDays, out int OrderID)
        {
            OrderID = -1;
            try
            {
                if (strUserKey == null || string.Compare(strUserKey.Trim(),
                                                         Common.Config("Authorization", "UserKey").Trim()) != 0)
                {
                    if (strUserKey != null)
                    {
                        Common.Log("The Invoker [" + strUserKey.Trim() + "] Is Unauthorized. [UserID=" + UserID + "]");
                    }
                    else
                    {
                        Common.Log("The Invoker [null] Is Unauthorized. [UserID=" + UserID + "]");
                    }
                    return(RI_Result.Unauthorized);
                }
                else if (!Common.Switch_ImmediateOrder || !Management.Work)
                {
                    Common.Log("The Interface [ProcessImmediateOrder] Is Closed. [UserID=" + UserID + "]");
                    return(RI_Result.Closed_Interface);
                }

                if (!Common.IsInInterfaceQuotationTime)
                {
                    return(RI_Result.Out_Of_Quotation_Time);
                }
                else if (Common.stkTrading == null)
                {
                    return(RI_Result.Internal_Error);
                }
                else if (UserID <= 0)
                {
                    return(RI_Result.Illegal_UserID);
                }

                TradingSystem.StockType stkType = Common.stkTrading.GetStockType(StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (Volume <= 0 || (Side &&
                                    (stkType != TradingSystem.StockType.SH_Warrant && stkType != TradingSystem.StockType.SZ_Warrant && Volume % 100 != 0) ||
                                    ((stkType == TradingSystem.StockType.SH_Warrant || stkType == TradingSystem.StockType.SZ_Warrant) && Volume % 1000 != 0)))
                {
                    return(RI_Result.Illegal_Volume);
                }
                else if (StockCode == null || Market == RI_Market.Unknown)
                {
                    return(RI_Result.Banned_Stock);
                }

                StockCode = StockCode.Trim();
                if (IsBanned(stkType))
                {
                    return(RI_Result.Banned_Stock);
                }
                TradingSystem.Currency Curr = TradingSystem.Currency.Unknown;
                switch (stkType)
                {
                case TradingSystem.StockType.SH_B:
                    Curr = TradingSystem.Currency.USD;
                    break;

                case TradingSystem.StockType.SZ_B:
                    Curr = TradingSystem.Currency.HKD;
                    break;

                default:
                    Curr = TradingSystem.Currency.RMB;
                    break;
                }
                Show2003DBFRecord SHRec = new Show2003DBFRecord(); SHRec.Clear();
                SjshqDBFRecord    SZRec = new SjshqDBFRecord(); SZRec.Clear();
                if (Common.stkQuotation.CheckSuspended(StockCode, (TradingSystem.StockMarket)((byte)Market)))
                {
                    if (Market == RI_Market.Shanghai && SHRec.OpenPrice < 0.001)
                    {
                        return(RI_Result.Suspended_Stock);
                    }
                    else if (Market == RI_Market.Shenzhen && SZRec.OpenPrice < 0.001)
                    {
                        return(RI_Result.Suspended_Stock);
                    }
                }
                else
                {
                    return(RI_Result.Banned_Stock);
                }

                short sCurrCount = Common.stkTrading.GetUserOrdersCount(UserID);
                if (sCurrCount == -2)
                {
                    return(RI_Result.Illegal_UserID);
                }
                if (sCurrCount < 0 || sCurrCount >= Common.MaxOrders)
                {
                    return(RI_Result.Too_Many_Orders);
                }
                Dictionary <byte, TradingSystem.UserFund> mapUsableFund = new Dictionary <byte, TradingSystem.UserFund>();
                if (!Common.stkTrading.GetUserFund(UserID, out mapUsableFund))
                {
                    return(RI_Result.Illegal_UserID);
                }
                int UserVolume = Common.stkTrading.GetSellableStockVolume(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (!Side && (UserVolume < Volume))
                {
                    return(RI_Result.Not_Enough_Stock);
                }
                if (!Side && (Volume % 100 != 0) && (Volume % 100 != UserVolume % 100))
                {
                    return(RI_Result.Illegal_Volume);
                }
                if (Side && !Common.stkTrading.CanBuy(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market), Curr, 10 * Volume * (1 + Common.stkTrading.defaultBuyTax)))
                {
                    return(RI_Result.Speculation_Behavior);
                }

                TradingSystem.UserOrders userOrder = new TradingSystem.UserOrders();
                userOrder.Initialize();
                userOrder.OrderID     = Common.stkTrading.nLastOrderID++;
                userOrder.UserID      = UserID;
                userOrder.StockCode   = StockCode.Trim();
                userOrder.Market      = (TradingSystem.StockMarket)((byte)Market);
                userOrder.OrderVolume = Volume;
                userOrder.Side        = Side;
                userOrder.OrderDate   = DateTime.Now;
                userOrder.ExpiredDate = userOrder.OrderDate.Date.Add(new TimeSpan(ValidDays,
                                                                                  Common.EndPMTS.Hours, Common.EndPMTS.Minutes, Common.EndPMTS.Seconds));
                userOrder.OrdStatus = TradingSystem.OrderStatus.Waiting;
                userOrder.OrdType   = TradingSystem.OrderType.ImmediateOrder;
                userOrder.Curr      = Curr;
                lock (Common.stkTrading.listNewUserOrders)
                {
                    Common.stkTrading.listNewUserOrders.Add(userOrder);
                    Common.DBSync.OrderAppended(userOrder, UserID);
                }
                Common.Debug("Immediate Order Requested [UserID=" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "].");
                OrderID = userOrder.OrderID;
                return(RI_Result.Success);
            }
            catch (Exception ex)
            {
                Common.Debug("Immediate Order Requested error :" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "]." + ex.ToString());
                return(RI_Result.Internal_Error);
            }
        }
예제 #4
0
        /// <summary>
        /// ����
        /// </summary>
        /// <param name="strUserKey"></param>
        /// <param name="UserID"></param>
        /// <param name="OrderID"></param>
        /// <param name="ValidDays"></param>
        /// <returns></returns>
        public RI_Result RequestCancelOrder(string strUserKey, int UserID, int OrderID, ushort ValidDays)
        {
            try
            {
                if (strUserKey == null || string.Compare(strUserKey.Trim(),
                    Common.Config("Authorization", "UserKey").Trim()) != 0)
                {
                    if (strUserKey != null)
                        Common.Log("The Invoker [" + strUserKey.Trim() + "] Is Unauthorized. [UserID=" + UserID + "]");
                    else
                        Common.Log("The Invoker [null] Is Unauthorized. [UserID=" + UserID + "]");
                    return RI_Result.Unauthorized;
                }
                else if (!Common.Switch_CancelOrder || !Management.Work)
                {
                    Common.Log("The Interface [ProcessCancelOrder] Is Closed. [UserID=" + UserID + "]");
                    return RI_Result.Closed_Interface;
                }

                if (!Common.IsInInterfaceQuotationTime)
                    return RI_Result.Out_Of_Quotation_Time;
                else if (Common.stkTrading == null)
                    return RI_Result.Internal_Error;
                else if (UserID <= 0)
                    return RI_Result.Illegal_UserID;
                else if (OrderID <= 0)
                    return RI_Result.Illegal_Cancelling;
                TradingSystem.UserOrders targetOrder = new TradingSystem.UserOrders();
                targetOrder.Initialize();
                if (!Common.stkTrading.GetUserOrder(UserID, OrderID, out targetOrder))
                    return RI_Result.Illegal_Cancelling;
                switch (targetOrder.OrdType)
                {
                    case TradingSystem.OrderType.LimitedOrder:
                        break;
                    default:
                        return RI_Result.Illegal_Cancelling;
                }
                if (targetOrder.OrdStatus != TradingSystem.OrderStatus.Waiting)
                    return RI_Result.Illegal_Cancelling;
                targetOrder.OrdStatus = TradingSystem.OrderStatus.Cancelling;
                if (!Common.stkTrading.CancelUserOrder(UserID, OrderID))
                    return RI_Result.Illegal_Cancelling;

                Common.Debug("Cancel Order Requested [UserID=" + UserID + "; OrderID=" + OrderID + "].");
                return RI_Result.Success;
            }
            catch
            {
                return RI_Result.Internal_Error;
            }
        }
예제 #5
0
        /// <summary>
        /// ����޼۶���
        /// </summary>
        /// <param name="strUserKey"></param>
        /// <param name="UserID"></param>
        /// <param name="StockCode"></param>
        /// <param name="Market"></param>
        /// <param name="Volume"></param>
        /// <param name="OrderPrice"></param>
        /// <param name="Side"></param>
        /// <param name="ValidDays"></param>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public RI_Result RequestLimitedOrder(string strUserKey, int UserID, string StockCode, RI_Market Market, int Volume, double OrderPrice, bool Side, ushort ValidDays, out int OrderID)
        {
            OrderID = -1;
            try
            {
                if (strUserKey == null || string.Compare(strUserKey.Trim(),
                    Common.Config("Authorization", "UserKey").Trim()) != 0)
                {
                    if (strUserKey != null)
                        Common.Log("The Invoker [" + strUserKey.Trim() + "] Is Unauthorized. [UserID=" + UserID + "]");
                    else
                        Common.Log("The Invoker [null] Is Unauthorized. [UserID=" + UserID + "]");
                    return RI_Result.Unauthorized;
                }
                else if (!Common.Switch_LimitedOrder || !Management.Work)
                {
                    Common.Log("The Interface [ProcessLimitedOrder] Is Closed. [UserID=" + UserID + "]");
                    return RI_Result.Closed_Interface;
                }

                if (!Common.IsInInterfaceQuotationTime)
                    return RI_Result.Out_Of_Quotation_Time;
                else if (Common.stkTrading == null)
                    return RI_Result.Internal_Error;
                else if (UserID <= 0)
                    return RI_Result.Illegal_UserID;

                TradingSystem.StockType stkType = Common.stkTrading.GetStockType(StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (Volume <= 0 || (Side &&
                    (stkType != TradingSystem.StockType.SH_Warrant && stkType != TradingSystem.StockType.SZ_Warrant && Volume % 100 != 0) ||
                    ((stkType == TradingSystem.StockType.SH_Warrant || stkType == TradingSystem.StockType.SZ_Warrant) && Volume % 1000 != 0)))
                    return RI_Result.Illegal_Volume;
                else if (OrderPrice < 0.001)
                    return RI_Result.Illegal_Price;
                else if ((stkType == TradingSystem.StockType.SH_A || stkType == TradingSystem.StockType.SZ_A) && OrderPrice < 0.01)
                    return RI_Result.Illegal_Price;
                else if (StockCode == null || Market == RI_Market.Unknown)
                    return RI_Result.Banned_Stock;
                StockCode = StockCode.Trim();
                TradingSystem.Currency Curr = TradingSystem.Currency.Unknown;
                switch (stkType)
                {
                    case TradingSystem.StockType.SH_B:
                        Curr = TradingSystem.Currency.USD;
                        break;
                    case TradingSystem.StockType.SZ_B:
                        Curr = TradingSystem.Currency.HKD;
                        break;
                    default:
                        Curr = TradingSystem.Currency.RMB;
                        break;
                }
                if (IsBanned(stkType))
                {
                    Common.Debug("Banned_Stock:" + UserID + ";" + StockCode + ";" + Market.ToString().Trim() + ";" + stkType.ToString().Trim());
                    return RI_Result.Banned_Stock;
                }
                Show2003DBFRecord SHRec = new Show2003DBFRecord(); SHRec.Clear();
                SjshqDBFRecord SZRec = new SjshqDBFRecord(); SZRec.Clear();
                if (Common.stkQuotation.CheckSuspended(StockCode, (TradingSystem.StockMarket)((byte)Market)))
                {
                    Common.Debug("Suspended_Stock:" + UserID + ";" + StockCode + ";" + Market.ToString());
                    return RI_Result.Suspended_Stock;
                }

                short sCurrCount = Common.stkTrading.GetUserOrdersCount(UserID);
                if (sCurrCount == -2)
                    return RI_Result.Illegal_UserID;
                if (sCurrCount < 0 || sCurrCount >= Common.MaxOrders)
                {
                    Common.Debug("Too_Many_Orders:" + UserID + ";" + sCurrCount + "/" + Common.MaxOrders);
                    return RI_Result.Too_Many_Orders;
                }
                int SellableVolume = Common.stkTrading.GetSellableStockVolume(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (!Side && (SellableVolume < Volume))
                {
                    Common.Debug("Not_Enough_Stock:" + UserID + "/" + StockCode.Trim() + "-" +
                        (byte)Market + ";" + SellableVolume + "/" + Volume);
                    return RI_Result.Not_Enough_Stock;
                }
                if (!Side && (Volume % 100 != 0) && (Volume % 100 != SellableVolume % 100))
                    return RI_Result.Illegal_Volume;
                if (Side && !Common.stkTrading.CanBuy(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market), Curr, OrderPrice * Volume * (1 + Common.stkTrading.defaultBuyTax)))
                {
                    Common.Debug("Speculation_Behavior:" + UserID + "/" + StockCode.Trim() + "-"
                          + (byte)Market + "/" + (OrderPrice * Volume * (1 + Common.stkTrading.defaultBuyTax)).ToString("f2"));
                    return RI_Result.Speculation_Behavior;
                }

                TradingSystem.UserOrders userOrder = new TradingSystem.UserOrders();
                userOrder.Initialize();
                userOrder.OrderID = Common.stkTrading.nLastOrderID++;
                userOrder.UserID = UserID;
                userOrder.StockCode = StockCode.Trim();
                userOrder.Market = (TradingSystem.StockMarket)((byte)Market);
                userOrder.Side = Side;
                userOrder.OrderVolume = Volume;
                userOrder.OrderPrice = OrderPrice;
                userOrder.OrderDate = DateTime.Now;
                userOrder.ExpiredDate = userOrder.OrderDate.Date.Add(new TimeSpan(ValidDays,
                    Common.EndPMTS.Hours, Common.EndPMTS.Minutes, Common.EndPMTS.Seconds));
                userOrder.OrdStatus = TradingSystem.OrderStatus.Waiting;
                userOrder.OrdType = TradingSystem.OrderType.LimitedOrder;
                userOrder.Curr = Curr;
                if (Side)
                {
                    lock (Common.stkTrading.listNewUserFund)
                    {
                        TradingSystem.UserFund usableFund = new TradingSystem.UserFund(); usableFund.Initialize();
                        for (int i = 0; i < Common.stkTrading.listNewUserFund.Count; i++)
                        {
                            if (Common.stkTrading.listNewUserFund[i].UserID == UserID
                                && ((stkType == TradingSystem.StockType.SH_B && Common.stkTrading.listNewUserFund[i].Curr == TradingSystem.Currency.USD) ||
                                (stkType == TradingSystem.StockType.SZ_B && Common.stkTrading.listNewUserFund[i].Curr == TradingSystem.Currency.HKD) ||
                                Common.stkTrading.listNewUserFund[i].Curr == TradingSystem.Currency.RMB))
                            {
                                usableFund = Common.stkTrading.listNewUserFund[i];
                                usableFund.UsableCash -= Common.ConvertPrice((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax) + 0.0099);
                                if (usableFund.UsableCash < 0)
                                    return RI_Result.Not_Enough_Cash;
                                if (!Common.stkTrading.SetUserFund(UserID, usableFund))
                                    return RI_Result.Illegal_UserID;
                                Common.stkTrading.listNewUserFund[i] = usableFund;
                                Common.DBSync.FundUpdate(usableFund, UserID);
                                lock (Common.stkTrading.listNewUserOrders)
                                {
                                    Common.stkTrading.listNewUserOrders.Add(userOrder);
                                    Common.DBSync.OrderAppended(userOrder, UserID);
                                }
                                Common.Debug("Limited Order Requested [UserID=" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "].");
                                OrderID = userOrder.OrderID;
                                return RI_Result.Success;
                            }
                        }

                        Dictionary<byte, TradingSystem.UserFund> mapUsableFund = new Dictionary<byte, TradingSystem.UserFund>();
                        if (!Common.stkTrading.GetUserFund(UserID, out mapUsableFund))
                            return RI_Result.Illegal_UserID;
                        switch (stkType)
                        {
                            case TradingSystem.StockType.SH_B:
                                {
                                    if (mapUsableFund.ContainsKey((byte)TradingSystem.Currency.USD))
                                        usableFund = mapUsableFund[(byte)TradingSystem.Currency.USD];
                                    else
                                        return RI_Result.Not_Enough_Cash;
                                }
                                break;
                            case TradingSystem.StockType.SZ_B:
                                {
                                    if (mapUsableFund.ContainsKey((byte)TradingSystem.Currency.HKD))
                                        usableFund = mapUsableFund[(byte)TradingSystem.Currency.HKD];
                                    else
                                        return RI_Result.Not_Enough_Cash;
                                }
                                break;
                            default:
                                {
                                    if (mapUsableFund.ContainsKey((byte)TradingSystem.Currency.RMB))
                                        usableFund = mapUsableFund[(byte)TradingSystem.Currency.RMB];
                                    else
                                        return RI_Result.Not_Enough_Cash;
                                }
                                break;
                        }
                        usableFund.UsableCash -= Common.ConvertPrice((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax) + 0.0099);
                        if (usableFund.UsableCash < 0)
                        {
                            Common.Debug("Not_Enough_Cash:" + UserID + "/" + StockCode.Trim() + "-" + (byte)Market + ";" +
                                (usableFund.UsableCash + Common.ConvertPrice((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax)))
                                + "/" + ((userOrder.OrderVolume * userOrder.OrderPrice) * (1 + Common.stkTrading.defaultBuyTax)));
                            return RI_Result.Not_Enough_Cash;
                        }
                        if (!Common.stkTrading.SetUserFund(UserID, usableFund))
                            return RI_Result.Illegal_UserID;
                        Common.stkTrading.listNewUserFund.Add(usableFund);
                        Common.DBSync.FundUpdate(usableFund, UserID);
                    }
                }

                lock (Common.stkTrading.listNewUserOrders)
                {
                    Common.stkTrading.listNewUserOrders.Add(userOrder);
                    Common.DBSync.OrderAppended(userOrder, UserID);
                }
                Common.Debug("Limited Order Requested [UserID=" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "].");
                OrderID = userOrder.OrderID;
                return RI_Result.Success;
            }
            catch
            {
                return RI_Result.Internal_Error;
            }
        }
예제 #6
0
        /// <summary>
        /// ��Ӽ�ʱ����
        /// </summary>
        /// <param name="strUserKey"></param>
        /// <param name="UserID"></param>
        /// <param name="StockCode"></param>
        /// <param name="Market"></param>
        /// <param name="Volume"></param>
        /// <param name="Side"></param>
        /// <param name="ValidDays"></param>
        /// <param name="OrderID"></param>
        /// <returns></returns>
        public RI_Result RequestImmediateOrder(string strUserKey, int UserID, string StockCode, RI_Market Market, int Volume, bool Side, ushort ValidDays, out int OrderID)
        {
            OrderID = -1;
            try
            {
                if (strUserKey == null || string.Compare(strUserKey.Trim(),
                    Common.Config("Authorization", "UserKey").Trim()) != 0)
                {
                    if (strUserKey != null)
                        Common.Log("The Invoker [" + strUserKey.Trim() + "] Is Unauthorized. [UserID=" + UserID + "]");
                    else
                        Common.Log("The Invoker [null] Is Unauthorized. [UserID=" + UserID + "]");
                    return RI_Result.Unauthorized;
                }
                else if (!Common.Switch_ImmediateOrder || !Management.Work)
                {
                    Common.Log("The Interface [ProcessImmediateOrder] Is Closed. [UserID=" + UserID + "]");
                    return RI_Result.Closed_Interface;
                }

                if (!Common.IsInInterfaceQuotationTime)
                    return RI_Result.Out_Of_Quotation_Time;
                else if (Common.stkTrading == null)
                    return RI_Result.Internal_Error;
                else if (UserID <= 0)
                    return RI_Result.Illegal_UserID;

                TradingSystem.StockType stkType = Common.stkTrading.GetStockType(StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (Volume <= 0 || (Side &&
                    (stkType != TradingSystem.StockType.SH_Warrant && stkType != TradingSystem.StockType.SZ_Warrant && Volume % 100 != 0) ||
                    ((stkType == TradingSystem.StockType.SH_Warrant || stkType == TradingSystem.StockType.SZ_Warrant) && Volume % 1000 != 0)))
                    return RI_Result.Illegal_Volume;
                else if (StockCode == null || Market == RI_Market.Unknown)
                    return RI_Result.Banned_Stock;

                StockCode = StockCode.Trim();
                if (IsBanned(stkType))
                    return RI_Result.Banned_Stock;
                TradingSystem.Currency Curr = TradingSystem.Currency.Unknown;
                switch (stkType)
                {
                    case TradingSystem.StockType.SH_B:
                        Curr = TradingSystem.Currency.USD;
                        break;
                    case TradingSystem.StockType.SZ_B:
                        Curr = TradingSystem.Currency.HKD;
                        break;
                    default:
                        Curr = TradingSystem.Currency.RMB;
                        break;
                }
                Show2003DBFRecord SHRec = new Show2003DBFRecord(); SHRec.Clear();
                SjshqDBFRecord SZRec = new SjshqDBFRecord(); SZRec.Clear();
                if (Common.stkQuotation.CheckSuspended(StockCode, (TradingSystem.StockMarket)((byte)Market)))
                {
                    if (Market == RI_Market.Shanghai && SHRec.OpenPrice < 0.001)
                        return RI_Result.Suspended_Stock;
                    else if (Market == RI_Market.Shenzhen && SZRec.OpenPrice < 0.001)
                        return RI_Result.Suspended_Stock;
                }
                else
                    return RI_Result.Banned_Stock;

                short sCurrCount = Common.stkTrading.GetUserOrdersCount(UserID);
                if (sCurrCount == -2)
                    return RI_Result.Illegal_UserID;
                if (sCurrCount < 0 || sCurrCount >= Common.MaxOrders)
                    return RI_Result.Too_Many_Orders;
                Dictionary<byte, TradingSystem.UserFund> mapUsableFund = new Dictionary<byte, TradingSystem.UserFund>();
                if (!Common.stkTrading.GetUserFund(UserID, out mapUsableFund))
                    return RI_Result.Illegal_UserID;
                int UserVolume = Common.stkTrading.GetSellableStockVolume(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market));
                if (!Side && (UserVolume < Volume))
                    return RI_Result.Not_Enough_Stock;
                if (!Side && (Volume % 100 != 0) && (Volume % 100 != UserVolume % 100))
                    return RI_Result.Illegal_Volume;
                if (Side && !Common.stkTrading.CanBuy(UserID, StockCode, (TradingSystem.StockMarket)((byte)Market), Curr, 10 * Volume * (1 + Common.stkTrading.defaultBuyTax)))
                    return RI_Result.Speculation_Behavior;

                TradingSystem.UserOrders userOrder = new TradingSystem.UserOrders();
                userOrder.Initialize();
                userOrder.OrderID = Common.stkTrading.nLastOrderID++;
                userOrder.UserID = UserID;
                userOrder.StockCode = StockCode.Trim();
                userOrder.Market = (TradingSystem.StockMarket)((byte)Market);
                userOrder.OrderVolume = Volume;
                userOrder.Side = Side;
                userOrder.OrderDate = DateTime.Now;
                userOrder.ExpiredDate = userOrder.OrderDate.Date.Add(new TimeSpan(ValidDays,
                    Common.EndPMTS.Hours, Common.EndPMTS.Minutes, Common.EndPMTS.Seconds));
                userOrder.OrdStatus = TradingSystem.OrderStatus.Waiting;
                userOrder.OrdType = TradingSystem.OrderType.ImmediateOrder;
                userOrder.Curr = Curr;
                lock (Common.stkTrading.listNewUserOrders)
                {
                    Common.stkTrading.listNewUserOrders.Add(userOrder);
                    Common.DBSync.OrderAppended(userOrder, UserID);
                }
                Common.Debug("Immediate Order Requested [UserID=" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "].");
                OrderID = userOrder.OrderID;
                return RI_Result.Success;
            }
            catch(Exception ex)
            {
                Common.Debug("Immediate Order Requested error :" + UserID + "; StockCode=" + StockCode.Trim() + "-" + (byte)Market + "]."+ex.ToString());
                return RI_Result.Internal_Error;
            }
        }
예제 #7
0
        /// <summary>
        /// ��ʼ�� ���ʽ𣬶������ֹɣ�
        /// </summary>
        /// <param name="mapUserFund">�û��ʽ�</param>
        /// <param name="mapUserOrders">�û�����</param>
        /// <param name="mapUserStocks">�û��ֹ�</param>
        /// <param name="LastOrderID">��󶩵�ID</param>
        /// <returns></returns>
        public bool Initialize(ref Dictionary<int, Dictionary<byte, TradingSystem.UserFund>> mapUserFund
            , ref Dictionary<int, TradingSystem.UserOrders> mapUserOrders
            , ref Dictionary<int, List<TradingSystem.UserStocks>> mapUserStocks, ref int LastOrderID)
        {
            try
            {
                listOrdersHistory.Clear(); // ������ʷ����
                listTradingHistory.Clear();// ������ʷ����
                listUserCash.Clear(); // �û��ֽ����
                listUserOrders.Clear(); // �û���������
                listUserStocks.Clear();// �û��ֹɶ���
                listRemoveOrders.Clear(); // �Ƴ���������
                listTradingError.Clear(); // ���״������
                listFundHistory.Clear();// �ʽ���ʷ����
                listUserUsableCash.Clear(); // �û������ʽ����
                listNewUserFund.Clear();// ���û�����
                uSyncFlag = 0;// �첽��־

                if (mapUserFund == null)
                    mapUserFund = new Dictionary<int, Dictionary<byte, TradingSystem.UserFund>>();
                else
                    mapUserFund.Clear();

                if (mapUserOrders == null)
                    mapUserOrders = new Dictionary<int, TradingSystem.UserOrders>();
                else
                    mapUserOrders.Clear();

                if (mapUserStocks == null)
                    mapUserStocks = new Dictionary<int, List<TradingSystem.UserStocks>>();
                else
                    mapUserStocks.Clear();

                // �鲢�û��ֲּ�¼����������������۵����¼��㣩
                if (!MergeDB())
                    return false;

                try
                {
                    string strTable = "tbUserWealth";
                    DataSet DSUserWealth = Common.UserWealthSvc.MapUserWealth();
                    if (DSUserWealth != null && DSUserWealth.Tables.Contains(strTable) && DSUserWealth.Tables[strTable] != null &&
                        DSUserWealth.Tables[strTable].Rows != null && DSUserWealth.Tables[strTable].Rows.Count > 0 &&
                        DSUserWealth.Tables[strTable].Columns != null && DSUserWealth.Tables[strTable].Columns.Contains("USD") &&
                        DSUserWealth.Tables[strTable].Columns.Contains("HKD") && DSUserWealth.Tables[strTable].Columns.Contains("RMB")
                        && DSUserWealth.Tables[strTable].Columns.Contains("UserID"))
                    {
                        for (int i = 0; i < DSUserWealth.Tables[strTable].Rows.Count; i++)
                        {
                            UserWealth data = new UserWealth();
                            data.Initialize();
                            data.WealthUSD = Common.ConvertPrice(Convert.ToDouble(DSUserWealth.Tables[strTable].Rows[i]["USD"].ToString().Trim()));
                            data.WealthHKD = Common.ConvertPrice(Convert.ToDouble(DSUserWealth.Tables[strTable].Rows[i]["HKD"].ToString().Trim()));
                            data.WealthRMB = Common.ConvertPrice(Convert.ToDouble(DSUserWealth.Tables[strTable].Rows[i]["RMB"].ToString().Trim()));
                            mapUserWealth[Convert.ToInt32(DSUserWealth.Tables[strTable].Rows[i]["UserID"].ToString().Trim())] = data;
                        }
                        Common.UsersStatus(mapUserWealth);
                    }
                    else
                    {
                        Common.Log("Failed to get [UserWealth] info from RankSvr.");
                    }
                }
                catch
                {
                    Common.Log("Failed to get [UserWealth] info from RankSvr.");
                }

                if (sqlConn_Init.State == ConnectionState.Closed)
                    sqlConn_Init.Open();
                LastOrderID = 0;

                RemotingInterface.RI_Fund stiFund = new RemotingInterface.RI_Fund(); stiFund.Clear();
                sqlCmd_Init = new SqlCommand("SELECT * FROM [UserFund] ORDER BY UserID", sqlConn_Init);
                sqlReader_Init = sqlCmd_Init.ExecuteReader();
                while (sqlReader_Init.Read())
                {
                    Dictionary<byte, TradingSystem.UserFund> mapCurrFund = null;
                    if (mapUserFund.ContainsKey((int)sqlReader_Init["UserID"]))
                        mapCurrFund = mapUserFund[(int)sqlReader_Init["UserID"]];

                    if (mapCurrFund == null)
                        mapCurrFund = new Dictionary<byte, TradingSystem.UserFund>();

                    TradingSystem.UserFund currFund = new TradingSystem.UserFund();
                    currFund.Initialize();
                    currFund.UserID = (int)sqlReader_Init["UserID"];
                    currFund.Cash = Convert.ToDouble(sqlReader_Init["Cash"].ToString().Trim());
                    currFund.UsableCash = Convert.ToDouble(sqlReader_Init["UsableCash"].ToString().Trim());
                    currFund.Curr = (TradingSystem.Currency)Convert.ToByte(sqlReader_Init["Currency"].ToString().Trim());
                    if (currFund.Curr == TradingSystem.Currency.Unknown)
                        continue;
                    switch (currFund.Curr)
                    {
                        case TradingSystem.Currency.USD:
                            if (mapUserWealth.ContainsKey(currFund.UserID))
                                currFund.Wealth = mapUserWealth[currFund.UserID].WealthUSD;
                            else
                                currFund.Wealth = Common.stkTrading.defaultUSD;
                            break;
                        case TradingSystem.Currency.HKD:
                            if (mapUserWealth.ContainsKey(currFund.UserID))
                                currFund.Wealth = mapUserWealth[currFund.UserID].WealthHKD;
                            else
                                currFund.Wealth = Common.stkTrading.defaultHKD;
                            break;
                        default:
                            if (mapUserWealth.ContainsKey(currFund.UserID))
                                currFund.Wealth = mapUserWealth[currFund.UserID].WealthRMB;
                            else
                                currFund.Wealth = Common.stkTrading.defaultRMB;
                            break;
                    }

                    mapCurrFund[(byte)currFund.Curr] = currFund;
                    mapUserFund[currFund.UserID] = mapCurrFund;

                    if (Common.stkBuffer != null)
                    {
                        stiFund.Cash = Common.ConvertPrice(currFund.Cash);
                        stiFund.UsableCash = Common.ConvertPrice(currFund.UsableCash);
                        stiFund.Wealth = Common.ConvertPrice(currFund.Wealth);
                        stiFund.Curr = (RemotingInterface.RI_Currency)currFund.Curr;
                        Common.stkBuffer.SetUserFund(currFund.UserID, stiFund);
                    }
                }
                sqlReader_Init.Close();
                if (mapUserFund != null)
                    Common.Log(" *** [" + mapUserFund.Count + "] User Accounts've Been Loaded. *** ");

                RemotingInterface.RI_Order stiOrder = new RemotingInterface.RI_Order(); stiOrder.Clear();
                sqlCmd_Init = new SqlCommand("SELECT * FROM [UserOrders] WHERE " +
                    "(UserID IN (SELECT UserID FROM [UserFund])) AND " +
                    "(ExpiredDate >= DATENAME([year], GETDATE()) + '-' + DATENAME([month], GETDATE()) " +
                    "+ '-' + DATENAME([day], GETDATE())) ORDER BY OrderID", sqlConn_Init);
                sqlReader_Init = sqlCmd_Init.ExecuteReader();
                while (sqlReader_Init.Read())
                {
                    TradingSystem.UserOrders userOrders = new TradingSystem.UserOrders();
                    userOrders.Initialize();
                    userOrders.OrderID = (int)sqlReader_Init["OrderID"];
                    userOrders.UserID = (int)sqlReader_Init["UserID"];
                    userOrders.OrderDate = Convert.ToDateTime(sqlReader_Init["OrderDate"].ToString().Trim());
                    userOrders.UpdatedDate = Convert.ToDateTime(sqlReader_Init["UpdatedDate"].ToString().Trim());
                    userOrders.ExpiredDate = Convert.ToDateTime(sqlReader_Init["ExpiredDate"].ToString().Trim());
                    if (DateTime.Now.TimeOfDay > Common.EndPMTS && userOrders.ExpiredDate.TimeOfDay <= Common.EndPMTS)
                        continue;
                    userOrders.OrderPrice = Convert.ToDouble(sqlReader_Init["OrderPrice"].ToString().Trim());
                    userOrders.TradePrice = Convert.ToDouble(sqlReader_Init["TradePrice"].ToString().Trim());
                    userOrders.Curr = (TradingSystem.Currency)Convert.ToByte(sqlReader_Init["Currency"].ToString().Trim());
                    userOrders.OrdStatus = (TradingSystem.OrderStatus)Convert.ToByte(sqlReader_Init["OrderStatus"].ToString().Trim());
                    userOrders.OrdType = (TradingSystem.OrderType)Convert.ToByte(sqlReader_Init["OrderType"].ToString().Trim());
                    userOrders.StockCode = sqlReader_Init["StockCode"].ToString().Trim();
                    userOrders.Market = (TradingSystem.StockMarket)Convert.ToByte(sqlReader_Init["Market"].ToString().Trim());
                    userOrders.OrderVolume = (int)sqlReader_Init["OrderVolume"];
                    if (sqlReader_Init["OrderSide"].ToString().ToLower().Trim() == "true")
                        userOrders.Side = true;
                    else
                        userOrders.Side = false;
                    LastOrderID = userOrders.OrderID;
                    mapUserOrders[userOrders.OrderID] = userOrders;
                    if (Common.stkBuffer != null)
                    {
                        if (userOrders.OrderDate.Date == DateTime.Now.Date)
                        {
                            stiOrder = new RemotingInterface.RI_Order(); stiOrder.Clear();
                            stiOrder.ExpiredDate = userOrders.ExpiredDate;
                            stiOrder.OrderDate = userOrders.OrderDate;
                            stiOrder.OrderID = userOrders.OrderID;
                            stiOrder.OrderPrice = Common.ConvertPrice(userOrders.OrderPrice);
                            stiOrder.TradePrice = Common.ConvertPrice(userOrders.TradePrice);
                            stiOrder.Curr = (RemotingInterface.RI_Currency)userOrders.Curr;
                            stiOrder.OrderStatus = (RemotingInterface.RI_Status)userOrders.OrdStatus;
                            stiOrder.OrderType = (RemotingInterface.RI_Type)userOrders.OrdType;
                            stiOrder.OrderVolume = userOrders.OrderVolume;
                            stiOrder.Side = userOrders.Side;
                            stiOrder.StockCode = userOrders.StockCode.Trim();
                            stiOrder.StockMarket = (RemotingInterface.RI_Market)userOrders.Market;
                            stiOrder.UpdatedDate = userOrders.UpdatedDate;
                            Common.stkBuffer.SetUserOrders(userOrders.UserID, stiOrder);
                        }
                    }
                }
                sqlReader_Init.Close();

                RemotingInterface.RI_Stock stiStock = new RemotingInterface.RI_Stock(); stiStock.Clear();
                sqlCmd_Init = new SqlCommand("SELECT * FROM [UserStocks] WHERE " +
                    "(UserID IN (SELECT UserID FROM [UserFund])) AND (Volume > 0) ORDER BY UserID, StockCode, Market", sqlConn_Init);
                sqlReader_Init = sqlCmd_Init.ExecuteReader();
                List<TradingSystem.UserStocks> listStocks = new List<TradingSystem.UserStocks>();
                int nLastUserID = 0;
                while (sqlReader_Init.Read())
                {
                    TradingSystem.UserStocks userStock = new TradingSystem.UserStocks();
                    userStock.Initialize();
                    userStock.UserID = (int)sqlReader_Init["UserID"];
                    userStock.StockCode = sqlReader_Init["StockCode"].ToString().Trim();
                    userStock.Market = (TradingSystem.StockMarket)Convert.ToByte(sqlReader_Init["Market"].ToString().Trim());
                    userStock.Volume = (int)sqlReader_Init["Volume"];
                    userStock.AveragePrice = double.Parse(sqlReader_Init["AveragePrice"].ToString().Trim());
                    userStock.Curr = (TradingSystem.Currency)Convert.ToByte(sqlReader_Init["Currency"].ToString().Trim());
                    if (sqlReader_Init["Sellable"].ToString().Trim() == "1")
                        userStock.Sellable = true;
                    else
                        userStock.Sellable = false;
                    if (nLastUserID != userStock.UserID)
                    {
                        if (nLastUserID > 0)
                            mapUserStocks[nLastUserID] = listStocks;

                        listStocks = new List<TradingSystem.UserStocks>();
                        listStocks.Add(userStock);
                        nLastUserID = userStock.UserID;
                    }
                    else
                    {
                        listStocks.Add(userStock);
                    }
                    if (Common.stkBuffer != null)
                    {
                        stiStock.AveragePrice = Common.ConvertPrice(userStock.AveragePrice);
                        stiStock.Curr = (RemotingInterface.RI_Currency)userStock.Curr;
                        stiStock.Volume = userStock.Volume;
                        if (userStock.Sellable)
                            stiStock.SellableVolume = userStock.Volume;
                        else
                            stiStock.SellableVolume = 0;
                        stiStock.StockCode = userStock.StockCode.Trim();
                        stiStock.StockMarket = (RemotingInterface.RI_Market)userStock.Market;
                        if (Common.stkBuffer != null)
                        {
                            Common.stkBuffer.AddUserStocks(userStock.UserID, stiStock);
                        }
                    }
                }
                if (nLastUserID > 0 && listStocks.Count > 0)
                    mapUserStocks[nLastUserID] = listStocks;
                sqlReader_Init.Close();

                RemotingInterface.RI_Order stiHistoryOrder = new RemotingInterface.RI_Order();
                stiOrder.Clear();
                sqlCmd_Init = new SqlCommand("SELECT * FROM [OrdersHistory] WHERE " +
                    "(UserID IN (SELECT UserID FROM [UserFund])) AND (OrderDate >= " +
                    "DATENAME([year], GETDATE()) + '-' + DATENAME([month], GETDATE()) + '-' + " +
                    "DATENAME([day], GETDATE())) ORDER BY OrderID", sqlConn_Init);
                sqlReader_Init = sqlCmd_Init.ExecuteReader();
                while (sqlReader_Init.Read())
                {
                    TradingSystem.UserOrders userOrders = new TradingSystem.UserOrders();
                    userOrders.Initialize();
                    userOrders.OrderID = (int)sqlReader_Init["OrderID"];
                    userOrders.UserID = (int)sqlReader_Init["UserID"];
                    userOrders.OrderDate = Convert.ToDateTime(sqlReader_Init["OrderDate"].ToString().Trim());
                    userOrders.UpdatedDate = Convert.ToDateTime(sqlReader_Init["UpdatedDate"].ToString().Trim());
                    userOrders.ExpiredDate = Convert.ToDateTime(sqlReader_Init["ExpiredDate"].ToString().Trim());
                    if (DateTime.Now.TimeOfDay > Common.EndPMTS && userOrders.ExpiredDate.TimeOfDay <= Common.EndPMTS)
                        continue;
                    userOrders.OrderPrice = Convert.ToDouble(sqlReader_Init["OrderPrice"].ToString().Trim());
                    userOrders.TradePrice = Convert.ToDouble(sqlReader_Init["TradePrice"].ToString().Trim());
                    userOrders.OrdStatus = (TradingSystem.OrderStatus)Convert.ToByte(sqlReader_Init["OrderStatus"].ToString().Trim());
                    userOrders.OrdType = (TradingSystem.OrderType)Convert.ToByte(sqlReader_Init["OrderType"].ToString().Trim());
                    userOrders.StockCode = sqlReader_Init["StockCode"].ToString().Trim();
                    userOrders.Market = (TradingSystem.StockMarket)Convert.ToByte(sqlReader_Init["Market"].ToString().Trim());
                    userOrders.OrderVolume = (int)sqlReader_Init["OrderVolume"];
                    if (sqlReader_Init["OrderSide"].ToString().ToLower().Trim() == "true")
                        userOrders.Side = true;
                    else
                        userOrders.Side = false;
                    userOrders.Curr = (TradingSystem.Currency)Convert.ToByte(sqlReader_Init["Currency"].ToString().Trim());
                    mapUserOrders[userOrders.OrderID] = userOrders;
                    if (Common.stkBuffer != null)
                    {
                        if (userOrders.OrderDate.Date == DateTime.Now.Date)
                        {
                            stiHistoryOrder = new RemotingInterface.RI_Order();
                            stiOrder.Clear();
                            stiHistoryOrder.ExpiredDate = userOrders.ExpiredDate;
                            stiHistoryOrder.OrderDate = userOrders.OrderDate;
                            stiHistoryOrder.OrderID = userOrders.OrderID;
                            stiHistoryOrder.OrderPrice = Common.ConvertPrice(userOrders.OrderPrice);
                            stiHistoryOrder.TradePrice = Common.ConvertPrice(userOrders.TradePrice);
                            stiHistoryOrder.Curr = (RemotingInterface.RI_Currency)userOrders.Curr;
                            stiHistoryOrder.OrderStatus = (RemotingInterface.RI_Status)userOrders.OrdStatus;
                            stiHistoryOrder.OrderType = (RemotingInterface.RI_Type)userOrders.OrdType;
                            stiHistoryOrder.OrderVolume = userOrders.OrderVolume;
                            stiHistoryOrder.Side = userOrders.Side;
                            stiHistoryOrder.StockCode = userOrders.StockCode.Trim();
                            stiHistoryOrder.StockMarket = (RemotingInterface.RI_Market)userOrders.Market;
                            stiHistoryOrder.UpdatedDate = userOrders.UpdatedDate;
                            Common.stkBuffer.SetUserOrders(userOrders.UserID, stiHistoryOrder);
                        }
                    }
                }
                sqlReader_Init.Close();

                if (Common.stkBuffer != null)
                {
                    RemotingInterface.RI_Trading stiTrading = new RemotingInterface.RI_Trading();
                    sqlCmd_Init = new SqlCommand("SELECT * FROM [TradingHistory] WHERE (TradeDate >= " +
                        "DATENAME([year], GETDATE()) + '-' + DATENAME([month], GETDATE()) + '-' + DATENAME([day], GETDATE())) " +
                        "ORDER BY TradeDate", sqlConn_Init);
                    sqlReader_Init = sqlCmd_Init.ExecuteReader();
                    while (sqlReader_Init.Read())
                    {
                        stiTrading.Clear();
                        if (sqlReader_Init["OrderSide"].ToString().ToLower().Trim() == "true")
                            stiTrading.Side = true;
                        else
                            stiTrading.Side = false;
                        stiTrading.TradeDate = DateTime.Parse(sqlReader_Init["TradeDate"].ToString().Trim());
                        if (stiTrading.TradeDate.Date != DateTime.Now.Date ||
                            (DateTime.Now.TimeOfDay > Common.EndPMTS && stiTrading.TradeDate.TimeOfDay <= Common.EndPMTS))
                            continue;
                        stiTrading.StockCode = sqlReader_Init["StockCode"].ToString().Trim();
                        stiTrading.StockMarket = (RemotingInterface.RI_Market)byte.Parse(sqlReader_Init["Market"].ToString().Trim());
                        stiTrading.TradePrice = Common.ConvertPrice(double.Parse(sqlReader_Init["TradePrice"].ToString().Trim()));
                        stiTrading.Curr = (RemotingInterface.RI_Currency)Convert.ToByte(sqlReader_Init["Currency"].ToString().Trim());
                        stiTrading.TradeVolume = (int)sqlReader_Init["TradeVolume"];
                        if (Common.stkBuffer != null)
                        {
                            Common.stkBuffer.SetUserTradings((int)sqlReader_Init["UserID"], stiTrading);
                        }
                    }
                    sqlReader_Init.Close();

                    RemotingInterface.RI_FundChanges stiFundChanges = new RemotingInterface.RI_FundChanges();
                    sqlCmd_Init = new SqlCommand("SELECT * FROM [FundHistory] WHERE (ChangedTime >= " +
                        "DATENAME([year], GETDATE()) + '-' + DATENAME([month], GETDATE()) + '-' + DATENAME([day], GETDATE())) " +
                        "ORDER BY ChangedTime", sqlConn_Init);
                    sqlReader_Init = sqlCmd_Init.ExecuteReader();
                    while (sqlReader_Init.Read())
                    {
                        stiFundChanges.Clear();
                        stiFundChanges.ChangedCash = Convert.ToDouble(sqlReader_Init["ChangedCash"].ToString().Trim());
                        stiFundChanges.ChangedDate = Convert.ToDateTime(sqlReader_Init["ChangedTime"].ToString().Trim());
                        if (stiFundChanges.ChangedDate.Date != DateTime.Now.Date ||
                            (DateTime.Now.TimeOfDay > Common.EndPMTS && stiFundChanges.ChangedDate.TimeOfDay <= Common.EndPMTS))
                            continue;
                        stiFundChanges.Curr = (RemotingInterface.RI_Currency)Convert.ToByte(sqlReader_Init["Currency"].ToString().Trim());
                        stiFundChanges.OrderID = (int)sqlReader_Init["OrderID"];
                        stiFundChanges.OriginalCash = Convert.ToDouble(sqlReader_Init["OriginalCash"].ToString().Trim());
                        if (Common.stkBuffer != null)
                        {
                            Common.stkBuffer.SetUserFundChanges((int)sqlReader_Init["UserID"], stiFundChanges);
                        }
                    }
                    sqlReader_Init.Close();
                }

                sqlCmd_Init = new SqlCommand("SELECT TOP 1 OrderID FROM [OrdersHistory] ORDER BY OrderID DESC", sqlConn_Init);
                sqlReader_Init = sqlCmd_Init.ExecuteReader();
                if (sqlReader_Init.Read() && ((int)sqlReader_Init["OrderID"]) > LastOrderID)
                {
                    LastOrderID = (int)sqlReader_Init["OrderID"];
                }
                sqlReader_Init.Close();

                ThSync = new Thread(new ThreadStart(Synchronizing));
                ThSync.Name = "ThSynchronizing";
                ThSync.Start();
                return true;
            }
            catch (Exception err)
            {
                Common.Log(err);
                return false;
            }
            finally
            {
                if (sqlConn_Init.State != ConnectionState.Closed)
                    sqlConn_Init.Close();
            }
        }