//---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2016/1/16 16:31:15 //Author //Description Create //---------------------------------------------------------------- using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Account; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Muchinfo.MTPClient.Trade.ViewModels { public class MakeCloseViewModel : CloseOrderBase { /// /// 按单平仓 /// /// /// /// public MakeCloseViewModel(HoldingOrder holdOrder, QuoteGoods goods, OrderType orderType = OrderType.MarketCloseOrder) : base(holdOrder, goods, orderType) { if (HoldOrder.RelateOrderID > 0) { IsOrderTypeEnable = false; Task.Factory.StartNew(() => { var limitItems = UserManager.GetCacheOrders(); if (limitItems != null) { var entrurstOrder = limitItems.FirstOrDefault((item) => item.OrderID == HoldOrder.RelateOrderID); if (entrurstOrder != null) { CurrentExpirationType = entrurstOrder.VailType; IsExpiration = false; } } }); } SetQuotePrice(); IsBidMarket = false; IsOrderTypeEnable = true; } /// /// 汇总平仓 /// /// /// public MakeCloseViewModel(HoldingSummary holdingSummary, QuoteGoods goods) : base(holdingSummary, goods) { IsBidMarket = false; this._holdingSummary = holdingSummary; CurrentOrderType = OrderType.MarketCloseOrder; SetQuotePrice(); } /// /// 按商品平仓 /// /// public MakeCloseViewModel(QuoteGoods goods, List holdingSummaries, bool isDirection) : base(goods) { CurrentOrderType = OrderType.MarketCloseOrder; IsDirectionEnable = isDirection; _holdingSummaries = holdingSummaries; var holdingSummary = holdingSummaries.FirstOrDefault(); if (holdingSummary != null) { IsBidDirection = holdingSummary.Direction == Direction.Ask; SetHoldingSummay(holdingSummary); IsAskDirection = !IsBidDirection; SetQuotePrice(); } } private double _priceMinUnit; /// /// 可输入小数位数 /// public double PriceMinUnit { get { return _priceMinUnit; } set { Set(() => PriceMinUnit, ref _priceMinUnit, value); } } #region 止盈止损 private int _countRow = 8; /// /// 设置数量控件位置 /// public int CountRow { get { return _countRow; } set { Set(() => CountRow, ref _countRow, value); } } private decimal _stopLoss; private decimal _stopProfit; /// /// StopLossChecked /// private bool _stopLossChecked; /// /// StopProfitChecked /// private bool _stopProfitChecked; /// /// 设置止损价 /// public decimal StopLoss { get { return _stopLoss; } set { Set(() => StopLoss, ref _stopLoss, value); // RaisePropertyChanged(() => ProfitLossPipsVailed); } } /// /// 设置止盈价 /// public decimal StopProfit { get { return _stopProfit; } set { Set(() => StopProfit, ref _stopProfit, value); // RaisePropertyChanged(() => ProfitLossPipsVailed); } } /// /// Sets and gets the StopLossChecked property. /// Changes to that property's value raise the PropertyChanged event. /// public bool StopLossChecked { get { return _stopLossChecked; } set { Set(() => StopLossChecked, ref _stopLossChecked, value); switch (this.OpenDirection) { case Direction.Ask: StopLoss = MaxLoss + (decimal)PriceMinUnit; break; case Direction.Bid: StopLoss = MaxLoss - (decimal)PriceMinUnit; break; } // StopLoss = _stopLossChecked ? QuotePrice : 0; // StopLoss = _stopLossChecked ? 0 : 0; SetUpdateProfitLossPrice(); } } /// /// Sets and gets the StopProfitChecked property. /// Changes to that property's value raise the PropertyChanged event. /// public bool StopProfitChecked { get { return _stopProfitChecked; } set { Set(() => StopProfitChecked, ref _stopProfitChecked, value); switch (this.OpenDirection) { case Direction.Ask: StopProfit = MaxProfit - (decimal)PriceMinUnit; break; case Direction.Bid: StopProfit = MaxProfit + (decimal)PriceMinUnit; break; } //StopProfit = _stopProfitChecked ? QuotePrice : 0; // StopLoss = _stopLossChecked ? 0 : 0; SetUpdateProfitLossPrice(); } } /// /// 设置最大止损价 /// public decimal MaxLoss { get { if (this.HoldOrder == null) { return decimal.Zero; } if (CurrentGoods == null || CurrentGoods.GoodsParameters == null) { return decimal.Zero; } bool flag = CurrentGoods.GoodsParameters.HqExchFigures < 20 && CurrentGoods.GoodsParameters.HqExchFigures > -20; if (this.HoldOrder.Direction == Direction.Bid) { //止损价 < 卖价 - (止损点差 * 最小变动单位) //||_orderQuoteGoods.GoodsParameters.GoodsStatus != GoodsStatusType.Open return (CurrentGoods.AskPrice - (flag ? CurrentGoods.GoodsParameters.LimitslSellptSub / (decimal)Math.Pow(10, CurrentGoods.GoodsParameters.HqExchFigures) : 0)); } else { //止损价 > 买价 + (止损点差 * 最小变动单位) //||_orderQuoteGoods.GoodsParameters.GoodsStatus != GoodsStatusType.Open return (CurrentGoods.BidPrice + (flag ? CurrentGoods.GoodsParameters.LimitslBuyptSub / (decimal)Math.Pow(10, CurrentGoods.GoodsParameters.HqExchFigures) : 0)); } } } /// /// 设置最大止盈价 /// public decimal MaxProfit { get { if (this.HoldOrder == null) { return decimal.Zero; } if (CurrentGoods == null || CurrentGoods.GoodsParameters == null) { return decimal.Zero; } bool flag = CurrentGoods.GoodsParameters.HqExchFigures < 20 && CurrentGoods.GoodsParameters.HqExchFigures > -20; if (this.HoldOrder.Direction == Direction.Bid) { //止盈价 > 买价 + (止盈点差 * 最小变动单位) // ||_orderQuoteGoods.GoodsParameters.GoodsStatus != GoodsStatusType.Open return CurrentGoods.BidPrice + (flag ? CurrentGoods.GoodsParameters.LimittpBuyptSub / (decimal)Math.Pow(10, CurrentGoods.GoodsParameters.HqExchFigures) : 0); } else { //止盈价 < 卖价 - (止盈点差 * 最小变动单位) //||_orderQuoteGoods.GoodsParameters.GoodsStatus != GoodsStatusType.Open return CurrentGoods.AskPrice - (flag ? CurrentGoods.GoodsParameters.LimittpSellptSub / (decimal) Math.Pow(10, CurrentGoods.GoodsParameters.HqExchFigures) : 0); } } } /// /// 止盈 格式化 /// public string DisplayMaxProfit { get { return MaxProfit.ToString(DisplayFormat); } } /// /// 止损 格式化 /// public string DisplayMaxLoss { get { return MaxLoss.ToString(DisplayFormat); } } /// /// 止盈跟据买卖设置符号方向 /// public string ProfitChar { get { switch (this.OpenDirection) { case Direction.Ask: { //if(canProfitCharChange) //{ // StopProfit = (decimal)(MaxProfit - (decimal)PriceMinUnit); //} //if(canLossCharChange) //{ // StopLoss = (decimal)(MaxLoss + (decimal)PriceMinUnit); //} return "<"; } case Direction.Bid: { //if(canProfitCharChange) //{ // StopProfit = (decimal)(MaxProfit + (decimal)PriceMinUnit); //} //if(canLossCharChange) //{ // StopLoss = (decimal)(MaxLoss - (decimal)PriceMinUnit); //} return ">"; } default: { return string.Empty; } } } } /// /// 止损跟据买卖设置符号方向 /// public string LossChar { get { switch (OpenDirection) { case Direction.Ask: { return ">"; } case Direction.Bid: { return "<"; } default: { return string.Empty; } } } } /// /// 更新止盈止损 /// private void SetUpdateProfitLossPrice() { if (CurrentOrderType == OrderType.LimitCloseOrder && CurrentGoods != null && CurrentGoods.GoodsParameters != null) { //if (OpenDirection == Direction.Bid) //{ // MaxLimit = _currentGoods.GoodsParameters.LimitslBuyptSub + _currentGoods.AskPrice; // MinLimit = _currentGoods.AskPrice - _currentGoods.GoodsParameters.LimitslBuyptSub; //} //else //{ // MaxLimit = _currentGoods.GoodsParameters.LimitslSellptSub + _currentGoods.BidPrice; // MinLimit = _currentGoods.BidPrice - _currentGoods.GoodsParameters.LimitslSellptSub; //} RaisePropertyChanged(() => DisplayMaxProfit); RaisePropertyChanged(() => DisplayMaxLoss); } } /// /// /// private void SetQuotePrice() { QuotePrice = (IsBidDirection ? CurrentGoods.BidPrice : CurrentGoods.AskPrice); //todo:暂时设置默认值 ////注册实时行情消息 ApplicationParameter.QuotationMessenger.Register>(this, MessengerTokens.ReceiveRealTimeQuote, (quoteList) => { if (CurrentGoods != null) { QuotePrice = (IsBidDirection ? CurrentGoods.BidPrice : CurrentGoods.AskPrice); } }); } #endregion #region 虚函数 public override NewEntrustOrder BuildEntrustOrder() { var entrustOrder = base.BuildEntrustOrder(); entrustOrder.EntrustPrice = (CurrentOrderType == OrderType.LimitCloseOrder) ? ExecutePrice : QuotePrice; entrustOrder.PriceMode = OrderPriceMode.MarketPrice; if (CurrentOrderType == OrderType.LimitCloseOrder) { entrustOrder.PriceMode = OrderPriceMode.LimitPrice; entrustOrder.SLPrice = StopLossChecked ? this.StopLoss : 0; entrustOrder.SPPrice = StopProfitChecked ? this.StopProfit : 0; entrustOrder.ValidType = CurrentExpirationType; if (this.HoldOrder != null && this.HoldOrder.RelateOrderID > 0) //修改止盈止损 { // entrustOrder.OrderType = OrderType.AlterProtfitLoss; entrustOrder.RelationTicket = HoldOrder.RelateOrderID; } else { entrustOrder.RelationTicket = HoldOrder.OrderID; } } ////if (this._openOrder != null && CurrentOrderType == OrderType.MarketCloseOrder) ////{ ////} if ((CurrentOrderType == OrderType.MarketCloseOrder) && IsAllowPips) { entrustOrder.AllowTradeSub = (int)Pips; } //var goodsPips = UserManager.GetTraderGoodsPips( // UserManager.CurrentTradeAccount.TradeCode, // CurrentGoods.GoodsCode); if (this.HoldOrder != null) { entrustOrder.SpecialAccount = HoldOrder.SettlementMember; } else if (UserManager.CurrentTradeAccount.AccountType == eAccountType.ACCOUNTTYPE_TRADER) { //会员交易员按商品平仓时,委托服务需校验特别会员账号,与委托服务确认客户端传任意一特别会员账号,服务端不使用这个账号 var holdingDetials = UserManager.CacheOpenOrders(UserManager.CurrentTradeAccount); var holdingDetial = holdingDetials.FirstOrDefault( (holder) => holder.GoodsCode == CurrentGoods.GoodsCode && holder.Direction == OpenDirection); if (holdingDetial != null) { entrustOrder.SpecialAccount = holdingDetial.SettlementMember; } } if (OpenDirection == Direction.Ask) { entrustOrder.BuyOrSell = Direction.Bid; ////委托单点差=商品点差+商品增量点差 //entrustOrder.PtSub = goodsPips.BidPips + CurrentGoods.GoodsParameters.BuyptSub; entrustOrder.CurtQuotePrice = CurrentGoods.BidPrice; } else { entrustOrder.BuyOrSell = Direction.Ask; // entrustOrder.PtSub = goodsPips.AskPips + CurrentGoods.GoodsParameters.SellptSub; //委托单点差=商品点差+商品增量点差 entrustOrder.CurtQuotePrice = CurrentGoods.AskPrice; } return entrustOrder; } public override void PostOrder(Action successAction, Action errorAction) { var entrustOrder = BuildEntrustOrder(); var orderService = SimpleIoc.Default.GetInstance(); orderService.MakeMarketEntrustOrder(entrustOrder, successAction, errorAction); } #endregion } }