Esempio n. 1
0
        /// <summary>
        /// Синхронизация позиций по бумагам
        /// </summary>
        /// <param name="localAccountID">Локальный AccountID</param>
        /// <param name="remoteAccountID">Удаленный AccountID</param>
        private async Task SyncHoldings(ISyncPipeServer sps, int localAccountID, int remoteAccountID)
        {
            var remHoldings = await sps.GetHoldingList(remoteAccountID);

            if (remHoldings == null)
            {
                return;
            }

            var holdings = _accountDA.GetHoldings(localAccountID);

            foreach (var r_hold in remHoldings)
            {
                if (!_instrum_rid_lid.ContainsKey(r_hold.InsID))
                {
                    continue;
                }

                int l_insID = _instrum_rid_lid[r_hold.InsID];
                var l_hold  = holdings.FirstOrDefault(r => r.InsID == l_insID);
                if (l_hold == null)
                {
                    _accountDA.CreateHolding(localAccountID, l_insID, r_hold.LotCount);
                }
                else if (l_hold.LotCount != r_hold.LotCount)
                {
                    l_hold.LotCount = r_hold.LotCount;
                    _accountDA.UpdateHolding(l_hold);
                }
            }

            foreach (var l_hold in holdings)
            {
                if (!_instrum_rid_lid.ContainsValue(l_hold.InsID))
                {
                    continue;
                }

                var r_insID = _instrum_rid_lid.FirstOrDefault(r => r.Value == l_hold.InsID).Key;
                if (remHoldings.FirstOrDefault(r => r.InsID == r_insID) == null) // в локальной базе есть запись с инструментом, а в удаленной базе нет такого инструмента, значит удаляем
                {
                    _accountDA.DeleteHolding(l_hold.HoldingID);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Сохранить все данные по торговому счету
        /// </summary>
        public void SaveData()
        {
            if (_account.AccountID <= 0)
            {
                _account        = _accountDA.CreateAccount(_account.Code, _account.Name, _account.CommPerc, _account.IsShortEnable, _account.AccountType);
                _cash.AccountID = _account.AccountID;
                foreach (Holding holding in _holdings)
                {
                    holding.AccountID = _account.AccountID;
                }
                foreach (Order order in _orders)
                {
                    order.AccountID = _account.AccountID;
                }
                foreach (StopOrder stopOrder in _stopOrders)
                {
                    stopOrder.AccountID = _account.AccountID;
                }
                foreach (Trade trade in _trades)
                {
                    trade.AccountID = _account.AccountID;
                }
            }
            else
            {
                _accountDA.UpdateAccount(_account);
            }

            if (_cash.CashID <= 0)
            {
                _cash = _accountDA.CreateCash(_cash.AccountID, _cash.Initial, _cash.Current, _cash.Sell, _cash.Buy, _cash.SellComm, _cash.BuyComm);
            }
            else
            {
                _accountDA.UpdateCash(_cash);
            }

            foreach (var holding in _holdings)
            {
                if (holding.HoldingID <= 0)
                {
                    _accountDA.CreateHolding(holding.AccountID, holding.InsID, holding.LotCount);
                }
                else
                {
                    _accountDA.UpdateHolding(holding); // holdings всегда обновляем, так проще
                }
            }

            var createStopOrders = _stopOrders.Where(r => r.StopOrderID <= 0).ToList();

            _stopOrders.RemoveAll(r => r.StopOrderID <= 0);
            foreach (var so in _stopOrders)
            {
                if (_modifiedStopOrders.Contains(so))
                {
                    _accountDA.UpdateStopOrder(so);
                }
            }
            _modifiedStopOrders.Clear();
            foreach (var so in createStopOrders)
            {
                var newSo = _accountDA.CreateStopOrder(so.AccountID, so.Time, so.InsID, so.BuySell, so.StopType, so.EndTime,
                                                       so.AlertPrice, so.Price, so.LotCount, so.Status, so.CompleteTime, so.StopOrderNo);
                _stopOrders.Add(newSo);
                var orders = _orders.Where(r => r.StopOrderID == so.StopOrderID).ToList();
                foreach (var ord in orders)
                {
                    ord.StopOrderID = newSo.StopOrderID;
                    if (ord.OrderID > 0)
                    {
                        MarkAsModified(ord);
                    }
                }
            }

            var createOrders = _orders.Where(r => r.OrderID <= 0).ToList();

            _orders.RemoveAll(r => r.OrderID <= 0);
            foreach (var order in _orders)
            {
                if (_modifiedOrders.Contains(order))
                {
                    _accountDA.UpdateOrder(order);
                }
            }
            _modifiedOrders.Clear();
            foreach (var ord in createOrders)
            {
                var newOrd = _accountDA.CreateOrder(ord.AccountID, ord.Time, ord.InsID, ord.BuySell, ord.LotCount, ord.Price,
                                                    ord.Status, ord.StopOrderID, ord.OrderNo);
                _orders.Add(newOrd);
                var trades = _trades.Where(r => r.OrderID == ord.OrderID).ToList();
                foreach (var trd in trades)
                {
                    trd.OrderID = newOrd.OrderID;
                }
            }

            var newTrades = _trades.Where(r => r.TradeID <= 0).ToList();

            _trades.RemoveAll(r => r.TradeID <= 0);
            foreach (var nt in newTrades)
            {
                var t = _accountDA.CreateTrade(nt.AccountID, nt.OrderID, nt.Time, nt.InsID, nt.BuySell, nt.LotCount, nt.Price,
                                               nt.Comm, nt.TradeNo);
                _trades.Add(t);
            }
        }