public void AddNewInvalidQuotationShouldFail() { var numOfQuotationBefore = DalService.CreateUnitOfWork().Quotations.CountAsync().Result; //Given an invalid new quotation var invalidQuotation = CreateValidQuotation(); invalidQuotation.Items = new List <DocItemEntity>(); //quotation without items is invalid //When adding the new quotation using (var unitOfWork = DalService.CreateUnitOfWork()) { QuotationEntity addedQuotation = null; var transaction = 0; Exception exception = null; try { addedQuotation = unitOfWork.Quotations.AddAsync(invalidQuotation).Result; _addedQuotationsToCancelAtDispose.Add(addedQuotation); transaction = unitOfWork.CompleteAsync().Result; } catch (Exception e) { exception = e; } finally { //Then the result should be null addedQuotation.Should().BeNull(); //and the transaction should fail transaction.Should().Be(0); //and throw exception exception.Should().NotBeNull(); //and the repository should not affected var numOfQuotationAfter = DalService.CreateUnitOfWork().Quotations.CountAsync().Result; numOfQuotationAfter.Should().Be(numOfQuotationBefore); } } }
// /// <summary> // /// 后退 // /// </summary> // public ICommand BackCommand { get { return new RelayCommand(BackExecuteChanged, BackCanExecuteChanged); } } // public void BackExecuteChanged() // { // BackButtonAction(); // } // public bool BackCanExecuteChanged() // { // return true; // } // /// <summary> // /// 起始页 // /// </summary> // public ICommand HomeCommand { get { return new RelayCommand(HomeExecuteChanged, HomeCanExecuteChanged); } } // public void HomeExecuteChanged() // { // IsInternationalCheck = true; // } // public bool HomeCanExecuteChanged() // { // return true; // } // /// <summary> // /// 放大 // /// </summary> // public ICommand EnlargeCommand { get { return new RelayCommand(EnlargeExecuteChanged, EnlargeCanExecuteChanged); } } // public void EnlargeExecuteChanged() // { // SetDataGridStyle(1); // } // public bool EnlargeCanExecuteChanged() // { // return true; // } // /// <summary> // /// 缩小 // /// </summary> // public ICommand NarrowCommand { get { return new RelayCommand(NarrowExecuteChanged, NarrowCanExecuteChanged); } } // public void NarrowExecuteChanged() // { // SetDataGridStyle(2); // } // public bool NarrowCanExecuteChanged() // { // return true; // } public void AddOptionalData(OptionalContractModel item) { int seq = TradeInfoHelper.OptionalModelList.Count; var datamodel = TradeInfoHelper.ContractModelList.FirstOrDefault(o => string.Equals(o.contractCode, item.contract_id)); if (datamodel != null) { var viewModel = new FuturesViewModel(datamodel); viewModel.Seq = seq; viewModel.IsOptionalStock = true; viewModel.OptionalSerialNumber = item.serial_number; QuotationEntity quotesmodel = null; lock (TradeInfoHelper.SubscribedContractList) { quotesmodel = TradeInfoHelper.SubscribedContractList.FirstOrDefault(o => string.Equals(o.cd, item.contract_id)); } if (quotesmodel != null) { // ScoketManager.GetInstance().UpdateModelInfo(viewModel, quotesmodel); } OptionalList.Add(viewModel); SetOptionalList(); } }
/// <summary> /// 提供行情信息 显示要交易的期货行情的信息 /// </summary> /// <param name="futures"></param> public void SendMarket(QuotationEntity futures) { if (futures == null) { return; } if (PMList.Count == 0) { return; } List <PotionDetailModelViewModel> items = PMList.Where(x => x.ContractId == futures.cd).ToList(); VarietyModel vm = null; ParitiesModel pm = null; lock (DetPMList) { foreach (PotionDetailModelViewModel itemde in DetPMList) { if (itemde.ContractCode == futures.cd) { string[] values = futures.cd.Split(' '); if (values.Length != 3) { return; } string varietie = values[1]; if (ContractVariety.Varieties.ContainsKey(varietie)) { vm = ContractVariety.Varieties[varietie]; } if (vm == null) { continue; } if (itemde.Direction == "B") { if (futures.lp > 0) { itemde.PositionProfitLoss = (futures.lp - itemde.OpenPrice) * itemde.PositionVolume * vm.multiple;//合约乘数 } else if (futures.lp == 0 && futures.pslp > 0) { itemde.PositionProfitLoss = (futures.pslp - itemde.OpenPrice) * itemde.PositionVolume * vm.multiple;//合约乘数 } else if (futures.lp == 0 && futures.pslp == 0) { continue; } } else { if (futures.lp > 0) { itemde.PositionProfitLoss = (itemde.OpenPrice - futures.lp) * itemde.PositionVolume * vm.multiple;//合约乘数 } else if (futures.lp == 0 && futures.pslp > 0) { itemde.PositionProfitLoss = (itemde.OpenPrice - futures.pslp) * itemde.PositionVolume * vm.multiple;//合约乘数 } else if (futures.lp == 0 && futures.pslp == 0) { continue; } } } } if (items != null) { foreach (var item in items) { string[] values = futures.cd.Split(' '); if (values.Length != 3) { return; } string varietie = values[1]; if (ContractVariety.Varieties.ContainsKey(varietie)) { vm = ContractVariety.Varieties[varietie]; } if (vm == null) { return; } // if (item.Direction == "B") item.PositionProfitLoss = Math.Round(DetPMList.Where(x => x.ContractId == item.ContractId && x.Direction == item.Direction).Sum(x => x.PositionProfitLoss), vm.precision);//合约乘数 if (ContractVariety.Parities.ContainsKey(vm.currency_code)) { pm = ContractVariety.Parities[vm.currency_code]; } if (pm == null) { return; } // if (item.Direction == "B") item.PositionProfitLossJB = DetPMList.Where(x => x.ContractId == item.ContractId && x.Direction == item.Direction).Sum(x => x.PositionProfitLoss) * pm.exchange_rate; //合约乘数*汇率 // item.PositionProfitLossJB = (item.OpenPrice - futures.lastPrice) * item.PositionVolume * vm.multiple * pm.exchange_rate;//合约乘数*汇率 } CommParameterSetting.StartAutoStopLoss(futures); } } }
/// <summary> /// 触发自动止盈止损 /// </summary> public static void StartAutoStopLoss(QuotationEntity futures) { if (ContractVariety.ContracPostionID.Count == 0) { return; } if (ContractVariety.ContracPostionID.ContainsKey(futures.cd)) { //如果自动止盈止损包含此合约号 //循环查询持仓明细,进行比较 foreach (string value in ContractVariety.ContracPostionID[futures.cd]) { PotionDetailModelViewModel item = PositionAllViewModel.Instance().DetPMList.FirstOrDefault(x => x.PsitionId == value); if (item != null) { AutoStopLossModel aslm = CommParameterSetting.AutoStopLossModel.FirstOrDefault(x => x.Direction == item.Direction && x.Agreement == item.ContractId); if (aslm == null) { break; } string[] VarietiesKey = futures.cd.Split(' '); if (VarietiesKey.Length != 3) { return; } string varietie = VarietiesKey[1]; VarietyModel vm = null; if (ContractVariety.Varieties.ContainsKey(varietie)) { vm = ContractVariety.Varieties[varietie]; } if (vm == null) { break; } if (aslm.StopLossPotion > 0) { //止损价》最新价触发止损 if (item.Direction == "B") { if (ContractVariety.PostionPrice[item.PsitionId].LossPrice >= futures.lp) { //平仓 if (!PostinIds.Contains(item.PsitionId)) { OpenCloseing(item, 0); PostinIds.Add(item.PsitionId); int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "delete from AutoStopLoss where ContractID='" + item.ContractId + "' and PostionID='" + item.PsitionId + "';"); LogHelper.Debug("买:持仓的止损价" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ": 行情最新价" + futures.lp); continue; } } } else { if (ContractVariety.PostionPrice[item.PsitionId].LossPrice <= futures.lp) { //平仓 if (!PostinIds.Contains(item.PsitionId)) { OpenCloseing(item, 0); PostinIds.Add(item.PsitionId); int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "delete from AutoStopLoss where ContractID='" + item.ContractId + "' and PostionID='" + item.PsitionId + "';"); LogHelper.Debug("卖:持仓的止损价" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ": 行情最新价" + futures.lp); continue; } } } } if (aslm.StopProfitPotion > 0) { //止盈价《最新价触发止盈 if (item.Direction == "B") { if ((item.OpenPrice + aslm.StopProfitPotion * vm.tick_size) <= futures.lp) { //平仓 if (!PostinIds.Contains(item.PsitionId)) { OpenCloseing(item, 0); PostinIds.Add(item.PsitionId); int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "delete from AutoStopLoss where ContractID='" + item.ContractId + "' and PostionID='" + item.PsitionId + "';"); LogHelper.Debug("买:持仓的止盈价" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ": 行情最新价" + futures.lp); continue; } } } else { if ((item.OpenPrice - aslm.StopProfitPotion * vm.tick_size) >= futures.lp) { //平仓 if (!PostinIds.Contains(item.PsitionId)) { OpenCloseing(item, 0); PostinIds.Add(item.PsitionId); int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "delete from AutoStopLoss where ContractID='" + item.ContractId + "' and PostionID='" + item.PsitionId + "';"); LogHelper.Debug("卖:持仓的止盈价" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ": 行情最新价" + futures.lp); continue; } } } } if (aslm.FloatingProfitAndLoss > 0 && aslm.StopLossPotion > 0) { if (item.Direction == "B") { double cha = futures.lp - ContractVariety.PostionPrice[item.PsitionId].NewPrice; if (cha > aslm.FloatingProfitAndLoss * vm.tick_size) { int bs = (int)(cha / aslm.FloatingProfitAndLoss * vm.tick_size); ContractVariety.PostionPrice[item.PsitionId].NewPrice = ContractVariety.PostionPrice[item.PsitionId].NewPrice + (bs * (aslm.FloatingProfitAndLoss * vm.tick_size)); ContractVariety.PostionPrice[item.PsitionId].LossPrice = ContractVariety.PostionPrice[item.PsitionId].LossPrice + (bs * (aslm.FloatingProfitAndLoss * vm.tick_size)); // 修改数据库中数据 int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "UPDATE AutoStopLoss set LossPrice=" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ",newprice=" + ContractVariety.PostionPrice[item.PsitionId].NewPrice + " WHERE UserID='" + UserInfoHelper.UserId + "' and PostionID='" + item.PsitionId + "' and ContractID='" + item.ContractId + "';"); LogHelper.Debug("买:更新止损价:" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ":止损价基数" + ContractVariety.PostionPrice[item.PsitionId].NewPrice); } } else { double cha = ContractVariety.PostionPrice[item.PsitionId].NewPrice - futures.lp;//买就是反过来减 if (cha > aslm.FloatingProfitAndLoss * vm.tick_size) { int bs = (int)(cha / aslm.FloatingProfitAndLoss * vm.tick_size); ContractVariety.PostionPrice[item.PsitionId].NewPrice = ContractVariety.PostionPrice[item.PsitionId].NewPrice - (bs * (aslm.FloatingProfitAndLoss * vm.tick_size)); ContractVariety.PostionPrice[item.PsitionId].LossPrice = ContractVariety.PostionPrice[item.PsitionId].LossPrice - (bs * (aslm.FloatingProfitAndLoss * vm.tick_size)); // 修改数据库中数据 int count = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.DBPath, CommandType.Text, "UPDATE AutoStopLoss set LossPrice=" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ",newprice=" + ContractVariety.PostionPrice[item.PsitionId].NewPrice + " WHERE UserID='" + UserInfoHelper.UserId + "' and PostionID='" + item.PsitionId + "' and ContractID='" + item.ContractId + "';"); LogHelper.Debug("卖:更新止损价:" + ContractVariety.PostionPrice[item.PsitionId].LossPrice + ":止损价基数" + ContractVariety.PostionPrice[item.PsitionId].NewPrice); } } } } } } }
/// <summary> /// 提供行情信息 显示要交易的期货行情的信息 /// </summary> /// <param name="futures"></param> public void SendMarket(QuotationEntity futures) { }
public Quotation(QuotationEntity QuotationEntity) : base(QuotationEntity) { }
public Tuple <bool, string> Post(QuotationEntity entity) { string QId = string.Empty; SqlTransaction objTrans = null; using (sqlConnection = new SqlConnection(GlobalVariable.ConnectionString)) { DataSet ds = new DataSet(); sqlCommand = new SqlCommand(); try { if (sqlConnection.State == 0) { sqlConnection.Open(); } objTrans = sqlConnection.BeginTransaction(); sqlCommand = new SqlCommand(); sqlCommand.Transaction = objTrans; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = "InsertQuotationDetails"; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.AddWithValue("@EmailID", entity.EmailID); sqlCommand.Parameters.AddWithValue("@PhoneNo", entity.PhoneNo); sqlCommand.Parameters.AddWithValue("@Remarks", entity.Remarks); sqlCommand.Parameters.AddWithValue("@GCode", entity.GCode); sqlCommand.Parameters.AddWithValue("@RCode", entity.RCode); sqlCommand.Parameters.Add("@Q_ID", SqlDbType.Int, 8); sqlCommand.Parameters["@Q_ID"].Direction = ParameterDirection.Output; sqlCommand.ExecuteNonQuery(); QId = Convert.ToString(sqlCommand.Parameters["@Q_ID"].Value); foreach (var item in entity.ProductID) { sqlCommand.Parameters.Clear(); sqlCommand.Dispose(); sqlCommand = new SqlCommand(); sqlCommand.Transaction = objTrans; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = "InsertQuotationProductDeatils"; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.AddWithValue("@Q_Id", QId); sqlCommand.Parameters.AddWithValue("@P_Id", item); sqlCommand.ExecuteNonQuery(); } objTrans.Commit(); sqlCommand.Parameters.Clear(); sqlCommand.Dispose(); ///MailSending ///sqlCommand = new SqlCommand(); sqlCommand.Transaction = objTrans; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = "GetMailDetails"; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.ExecuteNonQuery(); SendMail sendMail = new SendMail(); BodyMessageEntity bodyMessageEntity = new BodyMessageEntity { Mailid = entity.EmailID, PhoneNumber = entity.PhoneNo, Products = entity.Products, Remarks = entity.Remarks, RName = entity.RNname, GName = entity.GName }; using (SqlDataReader oReader = sqlCommand.ExecuteReader()) { while (oReader.Read()) { MailEntity mailEntity = new MailEntity { FromMailid = oReader["FromMailId"].ToString(), ToMailid = oReader["ToId"].ToString(), FromPassword = oReader["FromPassword"].ToString(), ToCC = oReader["ToCC"].ToString(), SMTP = oReader["Host"].ToString(), Port = Convert.ToInt16(oReader["Port"]), Subject = oReader["MailSubject"].ToString(), BodyMessage = sendMail.BodyMessage(bodyMessageEntity) }; Task.Run(() => sendMail.MailSending(mailEntity)); } } objTrans.Commit(); sqlCommand.Parameters.Clear(); sqlCommand.Dispose(); return(new Tuple <bool, string>(true, GlobalVariable.SavedMessage)); } catch (Exception ex) { objTrans.Rollback(); AuditLog.WriteError(ex.Message + " : " + ex.StackTrace); return(new Tuple <bool, string>(false, GlobalVariable.ErrorMessage)); } finally { sqlConnection.Close(); sqlCommand.Dispose(); ds.Dispose(); } } }