| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924 |
- 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<HoldingSummary> _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
- /// <summary>
- /// 当前商品
- /// </summary>
- public QuoteGoods CurrentGoods
- {
- get
- {
- return _currentGoods;
- }
- set { _currentGoods = value; }
- }
- /// <summary>
- /// 最大手数
- /// </summary>
- private decimal _maxLot;
- /// <summary>
- /// Sets and gets the MaxLot property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- public decimal MaxLot
- {
- get
- {
- return _maxLot;
- }
- set
- {
- Set(() => MaxLot, ref _maxLot, value);
- }
- }
- private bool _isDirectionEnable;
- /// <summary>
- /// 是否选择方向
- /// </summary>
- public bool IsDirectionEnable
- {
- get
- {
- return _isDirectionEnable;
- }
- set
- {
- Set(() => IsDirectionEnable, ref _isDirectionEnable, value);
- }
- }
- private decimal _lot;
- /// <summary>
- /// Sets and gets the Lot property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- 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;
- }
- /// <summary>
- /// 汇总平仓
- /// </summary>
- /// <param name="holdingSummary"></param>
- /// <param name="orderType"></param>
- 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;
- }
- /// <summary>
- /// 汇总平仓
- /// </summary>
- /// <param name="selectQuoteGoods"></param>
- public CloseOrderBase(QuoteGoods selectQuoteGoods)
- {
- _goodsService = SimpleIoc.Default.GetInstance<IGoodsService>();
- _orderService = SimpleIoc.Default.GetInstance<IOrderService>();
- CurrentGoods = selectQuoteGoods;
- }
- private HoldingOrder _holdOrder;
- /// <summary>
- /// 所平建仓单
- /// </summary>
- 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 验证字符串
- /// <summary>
- /// 验证是否选择商品
- /// </summary>
- public string GoodsVailedString
- {
- get
- {
- return this.CurrentGoods != null ? string.Empty : Muchinfo_Resource.GoodsSelect_Vailed;
- }
- }
- private bool _isOrderTypeEnable;
- /// <summary>
- /// 是否可以选择平仓类型
- /// </summary>
- public bool IsOrderTypeEnable
- {
- get
- {
- return _isOrderTypeEnable;
- }
- set
- {
- Set(() => IsOrderTypeEnable, ref _isOrderTypeEnable, value);
- }
- }
- #endregion
- #region Public Properties
- /// <summary>
- /// 判断价格方向
- /// </summary>
- public string EntructPriceChar
- {
- get
- {
- switch (OpenDirection)
- {
- case Direction.Ask:
- {
- return "≤";
- }
- case Direction.Bid:
- {
- return "≥";
- }
- default:
- {
- return string.Empty;
- }
- }
- }
- }
- private bool _isExpiration = true;
- /// <summary>
- /// 修改止盈止损时,是否可选
- /// </summary>
- public bool IsExpiration
- {
- get
- {
- return _isExpiration;
- }
- set
- {
- Set(() => IsExpiration, ref _isExpiration, value);
- }
- }
- /// <summary>
- /// 委托价格范围
- /// </summary>
- private decimal _entructPriceRange;
- /// <summary>
- /// 委托价格范围
- /// </summary>
- public decimal EntructPriceRange
- {
- get
- {
- return _entructPriceRange;
- }
- set
- {
- _entructPriceRange = value;
- RaisePropertyChanged(() => EntructPriceRangeDisplay);
- }
- }
- /// <summary>
- /// 委托价范围 格式化
- /// </summary>
- public string EntructPriceRangeDisplay
- {
- get { return EntructPriceRange.ToString(PriceFormat); }
- }
- private bool _isLotEnable = true;
- /// <summary>
- /// 是否可设置数量
- /// </summary>
- public bool IsLotEnable
- {
- get { return _isLotEnable; }
- set { Set(() => IsLotEnable, ref _isLotEnable, value); }
- }
- /// <summary>
- /// 价格格式化
- /// </summary>
- public string PriceFormat
- {
- get
- {
- if (this.CurrentGoods != null)
- {
- return CurrentGoods.FormatPrice;
- }
- else
- {
- return "F0";
- }
- }
- }
- private double _windowHeight = 460;
- /// <summary>
- /// 窗口高度
- /// </summary>
- public double WindowHeight
- {
- get { return _windowHeight; }
- set { Set(() => WindowHeight, ref _windowHeight, value); }
- }
- private string _busyTips;
- /// <summary>
- /// 服务忙提示
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 表单买卖方向
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// 取消命令
- /// </summary>
- public RelayCommand<Window> CancelCommand
- {
- get
- {
- return new RelayCommand<Window>((view) =>
- {
- //todo:取消平仓
- view.DialogResult = true; //
- });
- }
- }
- /// <summary>
- /// Sets and gets the CurrentExpirationType property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- public ExpirationType CurrentExpirationType
- {
- get
- {
- return _currentExpirationType;
- }
- set
- {
- Set(() => CurrentExpirationType, ref _currentExpirationType, value);
- }
- }
- private decimal _openLot;
- /// <summary>
- /// 反手建仓数量
- /// </summary>
- public decimal OpenLot
- {
- get { return _openLot; }
- set { Set(() => OpenLot, ref _openLot, value); }
- }
- /// <summary>
- /// 商品图片资源
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// 图片可见性
- /// </summary>
- public Visibility ImageVisibility
- {
- get
- {
- if (File.Exists(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension))
- {
- return Visibility.Visible;
- }
- else
- {
- return Visibility.Collapsed;
- }
- }
- }
- /// <summary>
- /// Sets and gets the CurrentOrderType property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// 获取报价
- /// </summary>
- public string QuotePriceDisplay
- {
- get
- {
- return QuotePrice.ToString(PriceFormat);
- }
- }
- private decimal _quotePrice; //当前行情价
- /// <summary>
- /// 当前行情价
- /// </summary>
- public decimal QuotePrice
- {
- get
- {
- return _quotePrice;
- }
- set
- {
- Set(() => QuotePrice, ref _quotePrice, value);
- RaisePropertyChanged(() => QuotePriceDisplay);
- }
- }
- /// <summary>
- /// 显示商品行情小数位
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 执行价格
- /// </summary>
- public decimal ExecutePrice
- {
- get
- {
- return _executePrice;
- }
- set
- {
- Set(() => ExecutePrice, ref _executePrice, value);
- // RaisePropertyChanged(() => PriceVailedString);
- }
- }
- /// <summary>
- /// Cleanups this instance.
- /// </summary>
- public override void Cleanup()
- {
- ////取消注册消息
- base.Cleanup();
- ApplicationParameter.QuotationMessenger.Unregister(this);
- }
- /// <summary>
- /// 获取和设置the expiration types
- /// </summary>
- public Dictionary<ExpirationType, string> ExpirationTypes
- {
- get
- {
- var types = new Dictionary<ExpirationType, string>();
- 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;
- }
- }
- /// <summary>
- /// Sets and gets the LimitOrderVisibility property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- public Visibility LimitOrderGridVisibility
- {
- get
- {
- return _limitOrderGridVisibility;
- }
- set
- {
- Set(() => LimitOrderGridVisibility, ref _limitOrderGridVisibility, value);
- }
- }
- private bool _isBidMarket;
- /// <summary>
- /// 是否是竞价商品
- /// </summary>
- public bool IsBidMarket
- {
- get { return _isBidMarket; }
- set { Set(() => IsBidMarket, ref _isBidMarket, value); }
- }
- /// <summary>
- /// Sets and gets the MarketOrderVisibility property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- public Visibility MarketOrderGridVisibility
- {
- get
- {
- return _marketOrderGridVisibility;
- }
- set
- {
- Set(() => MarketOrderGridVisibility, ref _marketOrderGridVisibility, value);
- }
- }
- /// <summary>
- /// 获取和设置the order types
- /// </summary>
- public virtual Dictionary<OrderType, string> OrderTypes
- {
- get
- {
- var types = new Dictionary<OrderType, string>
- {
- {OrderType.MarketCloseOrder, OrderType.MarketCloseOrder.Discription()},
- {OrderType.LimitCloseOrder, Muchinfo_Resource.OrderType_Enum_LimitClose}
- };
- return types;
- }
- }
- /// <summary>
- /// Sets and gets the Pips property.
- /// Changes to that property's value raise the PropertyChanged event.
- /// </summary>
- public decimal Pips
- {
- get
- {
- return _pips;
- }
- set
- {
- Set(() => Pips, ref _pips, value);
- }
- }
- /// <summary>
- /// 最小点差值
- /// </summary>
- public decimal MinPips
- {
- get { return _minPips; }
- set { Set(() => MinPips, ref _minPips, value); }
- }
- /// <summary>
- /// 最大点差值
- /// </summary>
- public decimal MaxPips
- {
- get { return _maxPips; }
- set { Set(() => MaxPips, ref _maxPips, value); }
- }
- /// <summary>
- /// 点差递增值
- /// </summary>
- public decimal IncrementPips
- {
- get { return _incrementPips; }
- set { Set(() => IncrementPips, ref _incrementPips, value); }
- }
- /// <summary>
- /// 格式化字符串
- /// </summary>
- public string DisplayFormat
- {
- get
- {
- if (this.CurrentGoods == null)
- {
- return "F0";
- }
- else
- {
- return CurrentGoods.FormatPrice;
- }
- }
- }
- /// <summary>
- /// 是否允许点差
- /// </summary>
- public bool IsAllowPips
- {
- get
- {
- return _isAllowPips;
- }
- set
- {
- Set(() => IsAllowPips, ref _isAllowPips, value);
- }
- }
- /// <summary>
- /// 关闭清除订阅消息
- /// </summary>
- public RelayCommand ClosedCommand
- {
- get
- {
- return new RelayCommand(() =>
- {
- this.Cleanup();
- });
- }
- }
- private bool _isAllowOpen;
- /// <summary>
- /// 是否允许反手建仓
- /// </summary>
- public bool IsAllowOpen
- {
- get { return _isAllowOpen; }
- set { Set(() => IsAllowOpen, ref _isAllowOpen, value); }
- }
- /// <summary>
- /// 暂时使用点差列表
- /// </summary>
- public List<decimal> PipsInts
- {
- //todo:点差计算方式从服务器取
- get
- {
- var intList = new List<decimal>();
- for (int i = 0; i < 11; i++)
- {
- intList.Add(i * 10);
- }
- return intList;
- }
- }
- /// <summary>
- /// 持仓单据方向
- /// </summary>
- public Direction OpenDirection
- {
- get
- {
- return _openDirection;
- }
- set
- {
- Set(() => OpenDirection, ref _openDirection, value);
- }
- }
- /// <summary>
- /// 设置汇总平仓参数设置
- /// </summary>
- protected void SetHoldingSummay(HoldingSummary holding)
- {
- if (holding != null)
- {
- _holdingSummary = holding;
- MaxLot = holding.Lot;
- }
- }
- #endregion Public Properties
- #endregion Properties
- #region 虚函数定义 OTC与竞价不同的市场使用重写不同的
- /// <summary>
- /// 验证信息
- /// </summary>
- /// <returns></returns>
- public virtual bool Validated()
- {
- return true;
- }
- /// <summary>
- /// 创建下单委托对象
- /// </summary>
- /// <returns>下单委托对象</returns>
- 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;
- }
- /// <summary>
- /// 提交表单
- /// </summary>
- /// <param name="successAction">成功回调</param>
- /// <param name="errorAction">错误返回</param>
- public virtual void PostOrder(Action<OrderDetail> successAction, Action<ErrorEntity> errorAction)
- {
- }
- #endregion
- }
- }
|