| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- //----------------------------------------------------------------
- //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
- {
- /// <summary>
- /// 按单平仓
- /// </summary>
- /// <param name="holdOrder"></param>
- /// <param name="goods"></param>
- /// <param name="orderType"></param>
- 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<OrderDetail>();
- 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;
- }
- /// <summary>
- /// 汇总平仓
- /// </summary>
- /// <param name="holdingSummary"></param>
- /// <param name="goods"></param>
- public MakeCloseViewModel(HoldingSummary holdingSummary, QuoteGoods goods)
- : base(holdingSummary, goods)
- {
- IsBidMarket = false;
- this._holdingSummary = holdingSummary;
- CurrentOrderType = OrderType.MarketCloseOrder;
- SetQuotePrice();
- }
- /// <summary>
- /// 按商品平仓
- /// </summary>
- /// <param name="goods"></param>
- public MakeCloseViewModel(QuoteGoods goods, List<HoldingSummary> 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;
- /// <summary>
- /// 可输入小数位数
- /// </summary>
- public double PriceMinUnit
- {
- get { return _priceMinUnit; }
- set { Set(() => PriceMinUnit, ref _priceMinUnit, value); }
-
- }
- #region 止盈止损
- private int _countRow = 8;
- /// <summary>
- /// 设置数量控件位置
- /// </summary>
- public int CountRow
- {
- get { return _countRow; }
- set { Set(() => CountRow, ref _countRow, value); }
- }
- private decimal _stopLoss;
- private decimal _stopProfit;
- /// <summary>
- /// StopLossChecked
- /// </summary>
- private bool _stopLossChecked;
- /// <summary>
- /// StopProfitChecked
- /// </summary>
- private bool _stopProfitChecked;
- /// <summary>
- /// 设置止损价
- /// </summary>
- public decimal StopLoss
- {
- get
- {
- return _stopLoss;
- }
- set
- {
- Set(() => StopLoss, ref _stopLoss, value);
- // RaisePropertyChanged(() => ProfitLossPipsVailed);
- }
- }
- /// <summary>
- /// 设置止盈价
- /// </summary>
- public decimal StopProfit
- {
- get
- {
- return _stopProfit;
- }
- set
- {
- Set(() => StopProfit, ref _stopProfit, value);
- // RaisePropertyChanged(() => ProfitLossPipsVailed);
- }
- }
- /// <summary>
- /// Sets and gets the StopLossChecked property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- 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();
- }
- }
- /// <summary>
- /// Sets and gets the StopProfitChecked property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- 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();
- }
- }
- /// <summary>
- /// 设置最大止损价
- /// </summary>
- 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));
- }
- }
- }
- /// <summary>
- /// 设置最大止盈价
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// 止盈 格式化
- /// </summary>
- public string DisplayMaxProfit
- {
- get
- {
- return MaxProfit.ToString(DisplayFormat);
- }
- }
- /// <summary>
- /// 止损 格式化
- /// </summary>
- public string DisplayMaxLoss
- {
- get
- {
- return MaxLoss.ToString(DisplayFormat);
- }
- }
- /// <summary>
- /// 止盈跟据买卖设置符号方向
- /// </summary>
- 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;
- }
- }
- }
- }
- /// <summary>
- /// 止损跟据买卖设置符号方向
- /// </summary>
- public string LossChar
- {
- get
- {
- switch (OpenDirection)
- {
- case Direction.Ask:
- {
- return ">";
- }
- case Direction.Bid:
- {
- return "<";
- }
- default:
- {
- return string.Empty;
- }
- }
- }
- }
- /// <summary>
- /// 更新止盈止损
- /// </summary>
- 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);
- }
- }
- /// <summary>
- ///
- /// </summary>
- private void SetQuotePrice()
- {
- QuotePrice = (IsBidDirection ? CurrentGoods.BidPrice : CurrentGoods.AskPrice);
- //todo:暂时设置默认值
- ////注册实时行情消息
- ApplicationParameter.QuotationMessenger.Register<List<QuoteGoods>>(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<OrderDetail> successAction, Action<Data.ErrorEntity> errorAction)
- {
- var entrustOrder = BuildEntrustOrder();
- var orderService = SimpleIoc.Default.GetInstance<IOrderService>();
- orderService.MakeMarketEntrustOrder(entrustOrder, successAction, errorAction);
- }
- #endregion
- }
- }
|