using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Data; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Helper; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Account; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using Muchinfo.MTPClient.Resources; using System; using System.Collections.Generic; using System.IO; using System.Linq; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2016/1/16 16:30:29 //Author //Description Create //---------------------------------------------------------------- using System.Windows; using System.Windows.Media.Imaging; namespace Muchinfo.MTPClient.Trade.ViewModels { public class CloseOrderBase : ViewModelBase { #region Fields private ExpirationType _currentExpirationType; private bool _isBidDirection; private QuoteGoods _currentGoods; ////当前商品 private OrderType _currentOrderType; private decimal _executePrice; protected IGoodsService _goodsService; protected IOrderService _orderService; protected List _holdingSummaries; private Visibility _limitOrderGridVisibility; private Visibility _marketOrderGridVisibility; protected HoldingSummary _holdingSummary; //持仓汇总记录。 private decimal _pips = 5; //修改止盈/止损 private bool canProfitCharChange = false; private bool canLossCharChange = false; private decimal _minPips = 0m; private decimal _maxPips = 100m; private decimal _incrementPips = 10m; private decimal _marketPrice; private Direction _openDirection; private bool _isAllowPips = true;//是否允许点差 private bool _isAllowEnsturt; //是否允许限价平仓 #endregion Fields /// /// 当前商品 /// public QuoteGoods CurrentGoods { get { return _currentGoods; } set { _currentGoods = value; } } /// /// 最大手数 /// private decimal _maxLot; /// /// Sets and gets the MaxLot property. /// Changes to that property's value raise the PropertyChanged event. /// public decimal MaxLot { get { return _maxLot; } set { Set(() => MaxLot, ref _maxLot, value); } } private bool _isDirectionEnable; /// /// 是否选择方向 /// public bool IsDirectionEnable { get { return _isDirectionEnable; } set { Set(() => IsDirectionEnable, ref _isDirectionEnable, value); } } private decimal _lot; /// /// Sets and gets the Lot property. /// Changes to that property's value raise the PropertyChanged event. /// public decimal Lot { get { return _lot; } set { Set(() => Lot, ref _lot, value); } } public CloseOrderBase(HoldingOrder holdOrder, QuoteGoods selectQuoteGoods, OrderType orderType = OrderType.MarketCloseOrder) { if (holdOrder != null) { HoldOrder = holdOrder; OpenDirection = HoldOrder.Direction; ExecutePrice = HoldOrder.ClosePrice; IsBidDirection = holdOrder.Direction == Direction.Ask; IsAskDirection = !IsBidDirection; MaxLot = holdOrder.Lot; } IsDirectionEnable = false; CurrentGoods = selectQuoteGoods; CurrentOrderType = orderType; } /// /// 汇总平仓 /// /// /// public CloseOrderBase(HoldingSummary holdingSummary, QuoteGoods selectQuoteGoods) { if (holdingSummary != null) { _holdingSummary = holdingSummary; MaxLot = holdingSummary.Lot; OpenDirection = holdingSummary.Direction; IsBidDirection = holdingSummary.Direction == Direction.Ask; IsAskDirection = !IsBidDirection; } CurrentGoods = selectQuoteGoods; IsDirectionEnable = false; } /// /// 汇总平仓 /// /// public CloseOrderBase(QuoteGoods selectQuoteGoods) { _goodsService = SimpleIoc.Default.GetInstance(); _orderService = SimpleIoc.Default.GetInstance(); CurrentGoods = selectQuoteGoods; } private HoldingOrder _holdOrder; /// /// 所平建仓单 /// public HoldingOrder HoldOrder { get { return _holdOrder; } set { Set(() => HoldOrder, ref _holdOrder, value); //RaisePropertyChanged(() => OpeneOrderDetail); if (_holdOrder != null) { MaxLot = _holdOrder.Lot - _holdOrder.FrozenQty; //可平仓数量=总数-冻结数量 Lot = MaxLot; } } } #region Properties #region 验证字符串 /// /// 验证是否选择商品 /// public string GoodsVailedString { get { return this.CurrentGoods != null ? string.Empty : Muchinfo_Resource.GoodsSelect_Vailed; } } private bool _isOrderTypeEnable; /// /// 是否可以选择平仓类型 /// public bool IsOrderTypeEnable { get { return _isOrderTypeEnable; } set { Set(() => IsOrderTypeEnable, ref _isOrderTypeEnable, value); } } #endregion #region Public Properties /// /// 判断价格方向 /// public string EntructPriceChar { get { switch (OpenDirection) { case Direction.Ask: { return "≤"; } case Direction.Bid: { return "≥"; } default: { return string.Empty; } } } } private bool _isExpiration = true; /// /// 修改止盈止损时,是否可选 /// public bool IsExpiration { get { return _isExpiration; } set { Set(() => IsExpiration, ref _isExpiration, value); } } /// /// 委托价格范围 /// private decimal _entructPriceRange; /// /// 委托价格范围 /// public decimal EntructPriceRange { get { return _entructPriceRange; } set { _entructPriceRange = value; RaisePropertyChanged(() => EntructPriceRangeDisplay); } } /// /// 委托价范围 格式化 /// public string EntructPriceRangeDisplay { get { return EntructPriceRange.ToString(PriceFormat); } } private bool _isLotEnable = true; /// /// 是否可设置数量 /// public bool IsLotEnable { get { return _isLotEnable; } set { Set(() => IsLotEnable, ref _isLotEnable, value); } } /// /// 价格格式化 /// public string PriceFormat { get { if (this.CurrentGoods != null) { return CurrentGoods.FormatPrice; } else { return "F0"; } } } private double _windowHeight = 460; /// /// 窗口高度 /// public double WindowHeight { get { return _windowHeight; } set { Set(() => WindowHeight, ref _windowHeight, value); } } private string _busyTips; /// /// 服务忙提示 /// public string BusyTips { get { return _busyTips; } set { Set(() => BusyTips, ref _busyTips, value); } } public string TradeCode { get { if (UserManager.CurrentTradeAccount == null) { return string.Empty; } return UserManager.CurrentTradeAccount.TradeCode; } } /// /// 表单买卖方向 /// public bool IsBidDirection { get { return _isBidDirection; } set { Set(() => IsBidDirection, ref _isBidDirection, value); if (value && IsDirectionEnable) { //设置方向的持仓汇总 var holdingSummary = this._holdingSummaries.FirstOrDefault((item) => item.Direction == Direction.Ask); SetHoldingSummay(holdingSummary); } } } private bool _isAskDirection; public bool IsAskDirection { get { return _isAskDirection; } set { Set(() => IsAskDirection, ref _isAskDirection, value); if (value && IsDirectionEnable) { //设置买方向的持仓汇总 var holdingSummary = this._holdingSummaries.FirstOrDefault((item) => item.Direction == Direction.Bid); SetHoldingSummay(holdingSummary); } } } /// /// 取消命令 /// public RelayCommand CancelCommand { get { return new RelayCommand((view) => { //todo:取消平仓 view.DialogResult = true; // }); } } /// /// Sets and gets the CurrentExpirationType property. /// Changes to that property's value raise the PropertyChanged event. /// public ExpirationType CurrentExpirationType { get { return _currentExpirationType; } set { Set(() => CurrentExpirationType, ref _currentExpirationType, value); } } private decimal _openLot; /// /// 反手建仓数量 /// public decimal OpenLot { get { return _openLot; } set { Set(() => OpenLot, ref _openLot, value); } } /// /// 商品图片资源 /// public BitmapImage ImageSource { get { if (File.Exists(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension)) { return new BitmapImage(new Uri(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension)); } else { return null; } } } /// /// 图片可见性 /// public Visibility ImageVisibility { get { if (File.Exists(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension)) { return Visibility.Visible; } else { return Visibility.Collapsed; } } } /// /// Sets and gets the CurrentOrderType property. /// Changes to that property's value raise the PropertyChanged event. /// public OrderType CurrentOrderType { get { return _currentOrderType; } set { Set(() => CurrentOrderType, ref _currentOrderType, value); if (value == OrderType.MarketCloseOrder || value == OrderType.BidMarketClose) { MarketOrderGridVisibility = Visibility.Visible; LimitOrderGridVisibility = Visibility.Collapsed; } else if (value == OrderType.LimitCloseOrder || value == OrderType.BidLimitClose) { MarketOrderGridVisibility = Visibility.Collapsed; LimitOrderGridVisibility = Visibility.Visible; } } } /// /// 获取报价 /// public string QuotePriceDisplay { get { return QuotePrice.ToString(PriceFormat); } } private decimal _quotePrice; //当前行情价 /// /// 当前行情价 /// public decimal QuotePrice { get { return _quotePrice; } set { Set(() => QuotePrice, ref _quotePrice, value); RaisePropertyChanged(() => QuotePriceDisplay); } } /// /// 显示商品行情小数位 /// public int Figures { get { if (this.CurrentGoods != null && this.CurrentGoods.GoodsParameters != null && CurrentGoods.GoodsParameters.HqExchFigures < 20 && CurrentGoods.GoodsParameters.HqExchFigures > -20) { return this.CurrentGoods.GoodsParameters.HqExchFigures; } return 0; } } /// /// 执行价格 /// public decimal ExecutePrice { get { return _executePrice; } set { Set(() => ExecutePrice, ref _executePrice, value); // RaisePropertyChanged(() => PriceVailedString); } } /// /// Cleanups this instance. /// public override void Cleanup() { ////取消注册消息 base.Cleanup(); ApplicationParameter.QuotationMessenger.Unregister(this); } /// /// 获取和设置the expiration types /// public Dictionary ExpirationTypes { get { var types = new Dictionary(); ExpirationType expirationType; try { string[] expirationData = ApplicationParameter.ExpirationType.Split(','); for (int i = 0; i < expirationData.Length; i++) { expirationType = (ExpirationType)System.Enum.Parse(typeof(ExpirationType), expirationData[i], true); types.Add(expirationType, expirationType.Discription()); } } catch (Exception e) { // LogHelper.WriteInfo(e.Message); } //types.Add(ExpirationType.Today, ExpirationType.Today.Discription()); //types.Add(ExpirationType.SomeTime, "指定时间有效"); //types.Add(ExpirationType.Weekday, ExpirationType.Weekday.Discription()); return types; } } /// /// Sets and gets the LimitOrderVisibility property. /// Changes to that property's value raise the PropertyChanged event. /// public Visibility LimitOrderGridVisibility { get { return _limitOrderGridVisibility; } set { Set(() => LimitOrderGridVisibility, ref _limitOrderGridVisibility, value); } } private bool _isBidMarket; /// /// 是否是竞价商品 /// public bool IsBidMarket { get { return _isBidMarket; } set { Set(() => IsBidMarket, ref _isBidMarket, value); } } /// /// Sets and gets the MarketOrderVisibility property. /// Changes to that property's value raise the PropertyChanged event. /// public Visibility MarketOrderGridVisibility { get { return _marketOrderGridVisibility; } set { Set(() => MarketOrderGridVisibility, ref _marketOrderGridVisibility, value); } } /// /// 获取和设置the order types /// public virtual Dictionary OrderTypes { get { var types = new Dictionary { {OrderType.MarketCloseOrder, OrderType.MarketCloseOrder.Discription()}, {OrderType.LimitCloseOrder, Muchinfo_Resource.OrderType_Enum_LimitClose} }; return types; } } /// /// Sets and gets the Pips property. /// Changes to that property's value raise the PropertyChanged event. /// public decimal Pips { get { return _pips; } set { Set(() => Pips, ref _pips, value); } } /// /// 最小点差值 /// public decimal MinPips { get { return _minPips; } set { Set(() => MinPips, ref _minPips, value); } } /// /// 最大点差值 /// public decimal MaxPips { get { return _maxPips; } set { Set(() => MaxPips, ref _maxPips, value); } } /// /// 点差递增值 /// public decimal IncrementPips { get { return _incrementPips; } set { Set(() => IncrementPips, ref _incrementPips, value); } } /// /// 格式化字符串 /// public string DisplayFormat { get { if (this.CurrentGoods == null) { return "F0"; } else { return CurrentGoods.FormatPrice; } } } /// /// 是否允许点差 /// public bool IsAllowPips { get { return _isAllowPips; } set { Set(() => IsAllowPips, ref _isAllowPips, value); } } /// /// 关闭清除订阅消息 /// public RelayCommand ClosedCommand { get { return new RelayCommand(() => { this.Cleanup(); }); } } private bool _isAllowOpen; /// /// 是否允许反手建仓 /// public bool IsAllowOpen { get { return _isAllowOpen; } set { Set(() => IsAllowOpen, ref _isAllowOpen, value); } } /// /// 暂时使用点差列表 /// public List PipsInts { //todo:点差计算方式从服务器取 get { var intList = new List(); for (int i = 0; i < 11; i++) { intList.Add(i * 10); } return intList; } } /// /// 持仓单据方向 /// public Direction OpenDirection { get { return _openDirection; } set { Set(() => OpenDirection, ref _openDirection, value); } } /// /// 设置汇总平仓参数设置 /// protected void SetHoldingSummay(HoldingSummary holding) { if (holding != null) { _holdingSummary = holding; MaxLot = holding.Lot; } } #endregion Public Properties #endregion Properties #region 虚函数定义 OTC与竞价不同的市场使用重写不同的 /// /// 验证信息 /// /// public virtual bool Validated() { return true; } /// /// 创建下单委托对象 /// /// 下单委托对象 public virtual NewEntrustOrder BuildEntrustOrder() { var entrustOrder = new NewEntrustOrder() { AccountId = UserManager.CurrentTradeAccount.AccountId, GoodsCode = this.CurrentGoods.GoodsCode, GoodId = (uint)this.CurrentGoods.GoodsParameters.GoodsId, MemberId = (uint)UserManager.CurrentTradeAccount.MemberAreaId, MarketId = (uint)this.CurrentGoods.GoodsParameters.MarketId, AccountType = UserManager.CurrentTradeAccount.AccountType, BuyOrSell = OpenDirection == Direction.Ask ? Direction.Bid : Direction.Ask, EntrustQuantity = Lot, EntrurstTime = ApplicationParameter.ServerTimeNow, OpenType = OpenClose.Close, }; if (this.HoldOrder != null) { entrustOrder.RelationTicket = HoldOrder.OrderID; entrustOrder.SpecialAccount = HoldOrder.SettlementMember; } entrustOrder.OrderMode = OrderMode.InValid; // entrustOrder.TradeCloseMode = TradeCloseMode.IsVailed; entrustOrder.ValidType = CurrentExpirationType; return entrustOrder; } /// /// 提交表单 /// /// 成功回调 /// 错误返回 public virtual void PostOrder(Action successAction, Action errorAction) { } #endregion } }