/// <summary> /// Синхронизация позиций по деньгам /// </summary> /// <param name="localAccountID">Локальный AccountID</param> /// <param name="remoteAccountID">Удаленный AccountID</param> private async Task SyncCash(ISyncPipeServer sps, int localAccountID, int remoteAccountID) { var remCash = await sps.GetCash(remoteAccountID); if (remCash == null) { return; } if (remCash == null) { return; } var cash = _accountDA.GetCash(localAccountID); if (cash == null) { _accountDA.CreateCash(localAccountID, remCash.Initial, remCash.Current, remCash.Sell, remCash.Buy, remCash.SellComm, remCash.BuyComm); } else { bool isUpdate = false; if (cash.Buy != remCash.Buy) { cash.Buy = remCash.Buy; isUpdate = true; } if (cash.BuyComm != remCash.BuyComm) { cash.BuyComm = remCash.BuyComm; isUpdate = true; } if (cash.Sell != remCash.Sell) { cash.Sell = remCash.Sell; isUpdate = true; } if (cash.SellComm != remCash.SellComm) { cash.SellComm = remCash.SellComm; isUpdate = true; } if (cash.Current != remCash.Current) { cash.Current = remCash.Current; isUpdate = true; } if (cash.Initial != remCash.Initial) { cash.Initial = remCash.Initial; isUpdate = true; } if (isUpdate) { _accountDA.UpdateCash(cash); } } }
public void SetPosition(int accountID, decimal curPos) { var cash = _da.GetCashes(accountID).FirstOrDefault(); if (cash == null) { Cash c = new Cash(); c.AccountID = accountID; c.Current = curPos; _da.InsertCash(c); } else if (cash.Current != curPos) { cash.Current = curPos; _da.UpdateCash(cash); } }
/// <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); } }