public ChartManager GetChartManager(int accountID, int insID, Timeframes tf) { lock (_cmCache) { ChartManager cm = _cmCache.GetChartManager(accountID, insID, tf); if (cm != null) { return(cm); } var account = _accountDA.GetAccountByID(accountID); if (account == null) { return(null); } if (account.AccountType == AccountTypes.Real) { cm = new ChartManager(_instrumBL, _insStoreBL, _accountDA, _tickDisp); } else { DateTime end = DateTime.Today; DateTime start = end.AddDays(-1); var json = _reposBL.GetStringParam(TestRun.ACCOUNT_META + accountID.ToString()); try { var meta = JsonConvert.DeserializeObject <AccountMeta>(json); if (meta != null) { start = meta.TickSource_StartDate; end = meta.TickSource_EndDate; } } catch { } cm = new ChartManager(_instrumBL, _insStoreBL, _accountDA, start, end); } var chart = _chartDA.GetChart(accountID, insID, tf); if (chart != null) { cm.Initialize(chart.XmlData); } else { cm.Initialize(); cm.CreatePrices(insID, tf); } cm.LoadHistoryAsync().Wait(); // нехорошо ждать внутри лока _cmCache.AddChartManager(accountID, insID, tf, cm); return(cm); } }
/// <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.Account GetAccountByID(int accountID) { return(_accountDA.GetAccountByID(accountID)); }