Пример #1
0
        private void DeleteData(DateTime date)
        {
            try
            {
                mainWindow.ProgressInfoVisible(3);
                using (StatementContext statement = new StatementContext())
                {
                    mainWindow.SetProgressText("(1/3)正在删除数据...");
                    statement.Delete(date, _Session.SelectedAccountId, typeof(ClosedTradeDetail), typeof(CommoditySummarization), typeof(FundStatus), typeof(PositionDetail), typeof(Position), typeof(Remittance), typeof(Stock), typeof(TradeDetail), typeof(Trade));
                    mainWindow.AddProgressValue(1);

                    mainWindow.SetProgressText("(2/3)正在校准权益...");
                    var stocks     = statement.Stocks.Where(m => m.AccountId == _Session.SelectedAccountId).OrderBy(m => m.Date);
                    var fundStatus = statement.FundStatus.Where(m => m.AccountId == _Session.SelectedAccountId).OrderBy(m => m.Date);
                    if (stocks.Count() > 0 && fundStatus.Count() > 0)
                    {
                        var lastStock      = stocks.ToList().LastOrDefault();
                        var lastFundStatus = fundStatus.ToList().LastOrDefault();
                        var diff           = lastFundStatus.CustomerRights - lastStock.Close;
                        if (diff != 0)
                        {
                            foreach (var stc in stocks)
                            {
                                stc.Open  += diff;
                                stc.High  += diff;
                                stc.Low   += diff;
                                stc.Close += diff;
                            }
                            mainWindow.AddProgressValue(1);
                            statement.UpdateStocks(stocks);
                        }
                    }

                    mainWindow.SetProgressText("(3/3)正在保存数据...");
                    statement.SaveChanged();
                    mainWindow.AddProgressValue(1);

                    MessageBox.Show(string.Concat("账号【", statement.Accounts.FirstOrDefault(m => m.Id == _Session.SelectedAccountId).AccountNumber, "】 ", date.ToString("yyyy年MM月dd日"), " 以及之后的数据已全部删除。"));
                }
                mainWindow.InitializeUserControlsThreadStart();
            }
            catch (Exception ex)
            {
                OverStatusAndProgressBar();
                MessageBoxSync(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                mainWindow.ProgressInfoHidden();
            }
        }
Пример #2
0
        private void LoadStatementFromCFMMC(CookieContainer cookie, Guid accountId)
        {
            StatementParseWay parseWay = StatementParseWay.html;

            lock (locker)
            {
                //
                StatementContext statement = new StatementContext();
                Account          account   = statement.Accounts.FirstOrDefault(model => model.Id == accountId);

                //try
                //{
                DateTime   startDate = DateTime.Now.AddDays(-186).Date;
                DateTime   endDate = DateTime.Now.Date;
                decimal    close = 0;
                decimal?   yesterdayBalance, todayBalance, remittance, amount;
                int        dataCount = 0;
                FundStatus lastFundStatus;
                Stock      lastStock;
                //
                TransactionOptions transactionOptions = new TransactionOptions();
                transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                transactionOptions.Timeout        = new TimeSpan(0, 30, 0);
                //using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
                //{



                lastStock = statement.Stocks.Where(s => s.AccountId == account.Id).OrderByDescending(s => s.Date).FirstOrDefault();
                if (lastStock == null)
                {
                    ProgressInfoVisible((endDate - startDate).Days + 1);
                    //
                    bool firstLine = true;
                    //
                    while (startDate <= endDate)
                    {
                        SetProgressText(string.Concat("【", account.AccountNumber, "】  ", startDate.ToString("yyyy年MM月dd日")));

                        if (parseWay == StatementParseWay.html)
                        {
                            _Helper.GetStatementByRequestHtml(cookie, startDate, SettlementType.date, statement, out yesterdayBalance, out todayBalance, out remittance, out amount, account.Id);
                        }
                        else
                        {
                            _Helper.GetStatementByLoadExcel(cookie, startDate, SettlementType.date, statement, out yesterdayBalance, out todayBalance, out remittance, out amount, account.Id);
                        }

                        if (yesterdayBalance.HasValue && todayBalance.HasValue && remittance.HasValue && amount.HasValue)
                        {
                            Stock stock = new Stock();
                            stock.Id = Guid.NewGuid();
                            if (firstLine)
                            {
                                stock.Date  = startDate.Date;
                                stock.Open  = yesterdayBalance.Value + remittance.Value;
                                stock.Close = close = todayBalance.Value;
                                if (stock.Close > stock.Open)
                                {
                                    stock.High = stock.Close;
                                    stock.Low  = stock.Open;
                                }
                                else
                                {
                                    stock.High = stock.Open;
                                    stock.Low  = stock.Close;
                                }
                                stock.Volume    = amount.Value;
                                stock.AccountId = account.Id;
                                firstLine       = false;
                            }
                            else
                            {
                                stock.Date  = startDate.Date;
                                stock.Open  = close;
                                stock.Close = close += todayBalance.Value - yesterdayBalance.Value - remittance.Value;
                                if (stock.Close > stock.Open)
                                {
                                    stock.High = stock.Close;
                                    stock.Low  = stock.Open;
                                }
                                else
                                {
                                    stock.High = stock.Open;
                                    stock.Low  = stock.Close;
                                }
                                stock.Volume    = amount.Value;
                                stock.AccountId = account.Id;
                            }
                            statement.AddStock(stock);
                            dataCount++;
                        }
                        //
                        startDate = startDate.AddDays(1);
                        AddProgressValue(1);
                    }
                }
                else
                {
                    startDate = lastStock.Date.AddDays(1).Date;
                    ProgressInfoVisible((endDate - startDate).Days + 1);
                    //
                    close = lastStock.Close;
                    while (startDate <= endDate)
                    {
                        SetProgressText(string.Concat("【", account.AccountNumber, "】  ", startDate.ToString("yyyy年MM月dd日")));

                        if (parseWay == StatementParseWay.html)
                        {
                            _Helper.GetStatementByRequestHtml(cookie, startDate, SettlementType.date, statement, out yesterdayBalance, out todayBalance, out remittance, out amount, account.Id);
                        }
                        else
                        {
                            _Helper.GetStatementByLoadExcel(cookie, startDate, SettlementType.date, statement, out yesterdayBalance, out todayBalance, out remittance, out amount, account.Id);
                        }

                        if (yesterdayBalance.HasValue && todayBalance.HasValue && remittance.HasValue && amount.HasValue)
                        {
                            Stock stock = new Stock();
                            stock.Id    = Guid.NewGuid();
                            stock.Date  = startDate.Date;
                            stock.Open  = close;
                            stock.Close = close += todayBalance.Value - yesterdayBalance.Value - remittance.Value;
                            if (stock.Close > stock.Open)
                            {
                                stock.High = stock.Close;
                                stock.Low  = stock.Open;
                            }
                            else
                            {
                                stock.High = stock.Open;
                                stock.Low  = stock.Close;
                            }
                            stock.Volume    = amount.Value;
                            stock.AccountId = account.Id;
                            statement.AddStock(stock);
                            dataCount++;
                        }
                        //
                        startDate = startDate.AddDays(1);
                        AddProgressValue(1);
                    }
                }


                // 复权处理
                var stocks = statement.Stocks.Where(s => s.AccountId == account.Id).OrderBy(s => s.Date);
                lastStock      = stocks.LastOrDefault();
                lastFundStatus = statement.FundStatus.Where(fs => fs.AccountId == account.Id).OrderBy(fs => fs.Date).LastOrDefault();
                if (lastStock != null && lastFundStatus != null)
                {
                    decimal tmp = lastStock.Close - lastFundStatus.TodayBalance;
                    if (tmp != 0)
                    {
                        SetProgressText(string.Format("<{0}>图表复权...", account.AccountNumber));
                        foreach (var stock in stocks)
                        {
                            stock.Open  -= tmp;
                            stock.High  -= tmp;
                            stock.Low   -= tmp;
                            stock.Close -= tmp;
                        }
                        statement.UpdateStocks(stocks);
                    }
                }

                //
                SetProgressText(string.Format("<{0}>保存数据...", account.AccountNumber));
                statement.SaveChanged();


                if (dataCount > 0)
                {
                    InitializeUserControlsThreadStart();
                }
                _settingUserControl.AddLog(string.Format("账户<{0}>下载数据完毕,新增 {1} 天数据。", account.AccountNumber, dataCount));
                ProgressInfoHidden();
                //}
                //catch (Exception)
                //{
                //    throw;
                //}
                //finally
                //{
                //    this.ProgressInfoHidden();
                //}


                // 备份数据至邮箱。
                //MailYunBackup(true);
            }
        }
Пример #3
0
        private void _button确认_Click(object sender, RoutedEventArgs e)
        {
            decimal profit = 0, commission = 0, remittance = 0, volume = 0;
            int     bing = 0;

            try
            {
                profit     = Decimal.Parse(_textBox盈利.Text.Trim());
                bing       = 1;
                commission = Decimal.Parse(_textBox手续费.Text.Trim());
                bing       = 2;
                remittance = Decimal.Parse(_textBox出入金.Text.Trim());
                bing       = 3;
                volume     = Decimal.Parse(_textBox成交额.Text.Trim());
            }
            catch (FormatException)
            {
                switch (bing)
                {
                case 0: MessageBox.Show("盈利必须为数字!");
                    break;

                case 1: MessageBox.Show("手续费必须为数字!");
                    break;

                case 2: MessageBox.Show("出入金必须为数字!");
                    break;

                case 3: MessageBox.Show("成交额必须为数字!");
                    break;
                }
            }
            catch (OverflowException)
            {
                switch (bing)
                {
                case 0: MessageBox.Show("盈利超出数字范围!");
                    break;

                case 1: MessageBox.Show("手续费超出数字范围!");
                    break;

                case 2: MessageBox.Show("出入金超出数字范围!");
                    break;

                case 3: MessageBox.Show("成交额超出数字范围!");
                    break;
                }
            }

            using (StatementContext statement = new StatementContext(typeof(FundStatus), typeof(Account), typeof(Stock)))
            {
                var cooperate = statement.Accounts.FirstOrDefault(m => m.Id == accountId);
                if (cooperate == null)
                {
                    throw new Exception(string.Concat("不存在ID为:", accountId, "的配资账户!"));
                }

                var sto = statement.Stocks.FirstOrDefault(m => m.AccountId == accountId && m.Date == _datePicker日期.SelectedDate.Value);
                if (sto != null)
                {
                    MessageBox.Show(string.Concat("此账户【", _datePicker日期.SelectedDate.Value, "】数据已经存在!"));
                    return;
                }

                var allStocks    = statement.Stocks.Where(m => m.AccountId == accountId).OrderBy(m => m.Date);
                var beforeStocks = allStocks.Where(m => m.Date < _datePicker日期.SelectedDate.Value);
                var afterStocks  = allStocks.Where(m => m.Date > _datePicker日期.SelectedDate.Value);
                var lastStock    = beforeStocks.LastOrDefault();

                Stock stock = new Stock();
                if (lastStock != null)
                {
                    stock.Open = lastStock.Close;
                }
                else
                {
                    stock.Open = remittance;
                }
                stock.Close     = stock.Open + profit - commission;
                stock.High      = stock.Open < stock.Close ? stock.Close : stock.Open;
                stock.Low       = stock.Open > stock.Close ? stock.Close : stock.Open;
                stock.Date      = _datePicker日期.SelectedDate.Value;
                stock.Volume    = volume;
                stock.AccountId = accountId;

                //
                statement.AddStock(stock);

                //
                if (afterStocks.Count() != 0 && stock.Close != stock.Open)
                {
                    foreach (var st in afterStocks)
                    {
                        st.Open  += stock.Close - stock.Open;
                        st.Close += stock.Close - stock.Open;
                        st.High  += stock.Close - stock.Open;
                        st.Low   += stock.Close - stock.Open;
                    }
                    statement.UpdateStocks(afterStocks);
                }

                if (remittance != 0)
                {
                    var stocks = statement.Stocks.Where(m => m.AccountId == accountId);
                    foreach (var st in stocks)
                    {
                        st.Open  += remittance;
                        st.Close += remittance;
                        st.High  += remittance;
                        st.Low   += remittance;
                    }
                    statement.UpdateStocks(stocks);
                }
                statement.SaveChanged();
            }
            DialogResult = true;
            MessageBox.Show("添加成功!");
        }