/// <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); } } }
/// <summary> /// Загрузить все данные по торговому счету /// </summary> /// <param name="accountID">Торговый счет</param> public void LoadData(int accountID) { _account = _accountDA.GetAccountByID(accountID); if (_account == null) { throw new ApplicationException("Счет не найден."); } _cash = _accountDA.GetCash(accountID); if (_cash == null) { _cash = new Cash(); _cash.AccountID = accountID; } _holdings = _accountDA.GetHoldings(accountID).ToList(); _orders = _accountDA.GetOrders(accountID).ToList(); _stopOrders = _accountDA.GetStopOrders(accountID).ToList(); _trades = _accountDA.GetTrades(accountID).ToList(); }
public Task Generate(int accountID, Timeline timeline) { return(Task.Run(() => { _cashRow.Clear(); _portfolioRow.Clear(); _equityRow.Clear(); _prices.Clear(); var trades = _accountDA.GetTrades(accountID).OrderBy(r => r.Time).ToList(); var account = _accountDA.GetAccountByID(accountID); var cash = _accountDA.GetCash(accountID); List <EqHold> eqHoldList = new List <EqHold>(); decimal cashSumma = cash.Initial; decimal pfSumma = 0; var bd1 = timeline.GetBarDate(0); var bd2 = timeline.GetBarDate(timeline.Count - 1); if (bd1 == null || bd2 == null) { return; } DateTime date1 = bd1.Value.Start.Date; DateTime date2 = bd2.Value.NextStart.Date; // для таймфреймов day и week загрузим историю на один лишний день int tradeIdx = 0; for (int i = 0; i < timeline.Count; i++) { var barDates = timeline.GetBarDate(i); if (barDates == null) { continue; } while (tradeIdx < trades.Count && trades[tradeIdx].Time < barDates.Value.NextStart) { var trade = trades[tradeIdx]; var instrum = _instrumBL.GetInstrumByID(trade.InsID); // из кеша var tradeSumma = trade.Price * trade.LotCount * instrum.LotSize; var hold = eqHoldList.FirstOrDefault(r => r.InsID == instrum.InsID); if (trade.BuySell == BuySell.Buy) { cashSumma -= tradeSumma; if (hold != null) { hold.LotCount += trade.LotCount; } else { eqHoldList.Add(new EqHold() { InsID = instrum.InsID, LotSize = instrum.LotSize, LotCount = trade.LotCount }); } } else { cashSumma += tradeSumma; if (hold != null) { hold.LotCount -= trade.LotCount; if (hold.LotCount == 0) { eqHoldList.Remove(hold); } } else { eqHoldList.Add(new EqHold() { InsID = instrum.InsID, LotSize = instrum.LotSize, LotCount = -trade.LotCount }); } } tradeIdx++; } // вычисляем сумму портфеля на конец бара pfSumma = 0; foreach (var hold in eqHoldList) { if (!_prices.ContainsKey(hold.InsID)) { BarRow barRow = new BarRow(timeline.Timeframe, hold.InsID); _insStoreBL.LoadHistoryAsync(barRow, hold.InsID, date1, date2).Wait(); _prices.Add(hold.InsID, barRow); } var bars = _prices[hold.InsID]; var bar = bars.Bars.FirstOrDefault(r => r.Time == barDates.Value.Start); if (bar != null) { pfSumma += bar.Close * hold.LotCount * hold.LotSize; } } _cashRow.Add(cashSumma); _portfolioRow.Add(pfSumma); _equityRow.Add(cashSumma + pfSumma); } })); }
/// <summary> /// Получить запись "Позиции по деньгам" /// </summary> /// <param name="accountID">ID счета</param> /// <returns>Позиция по деньгам</returns> public CommonData.Cash GetCash(int accountID) { return(_accountDA.GetCash(accountID)); }