BidCloseViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //----------------------------------------------------------------
  7. //Module Name: $safeprojectname$
  8. //Purpose:
  9. //CopyRight: Muchinfo
  10. //History:
  11. //----------------------------------------------------------------
  12. //DateTime 2016/1/16 16:27:18
  13. //Author
  14. //Description Create
  15. //----------------------------------------------------------------
  16. using System.Windows;
  17. using Muchinfo.MTPClient.Data.Enums;
  18. using Muchinfo.MTPClient.Data.Model;
  19. using Muchinfo.MTPClient.Data.Model.Account;
  20. using Muchinfo.MTPClient.Infrastructure.Utilities;
  21. using Muchinfo.MTPClient.Resources;
  22. using GalaSoft.MvvmLight.Ioc;
  23. using Muchinfo.MTPClient.IService;
  24. namespace Muchinfo.MTPClient.Trade.ViewModels
  25. {
  26. public class BidCloseViewModel : CloseOrderBase
  27. {
  28. private bool _executePriceGotFocus;
  29. private OrderMode _orderMode = OrderMode.STD; //设置成交属性
  30. // private HoldingSummary _holdingSummary; //持仓汇总记录。
  31. private decimal _pips = 5;
  32. private Visibility _isTimeVisibility = Visibility.Collapsed;
  33. //修改止盈/止损
  34. private bool canProfitCharChange = false;
  35. private bool canLossCharChange = false;
  36. /// <summary>
  37. /// 最大手数
  38. /// </summary>
  39. private decimal _maxLot;
  40. private decimal _minPips = 0m;
  41. private decimal _maxPips = 100m;
  42. private decimal _incrementPips = 10m;
  43. private decimal _marketPrice;
  44. private Direction _openDirection;
  45. private bool _isAllowEnsturt; //是否允许限价平仓
  46. // private readonly int BidMarketHideType = ApplicationParameter.BidMarketHideType;//竞价商品卖方向隐藏方式
  47. public BidCloseViewModel(HoldingOrder holdOrder,QuoteGoods goods, OrderType orderType = OrderType.BidMarketClose)
  48. : base(holdOrder,goods, orderType)
  49. {
  50. IsBidMarket = true;
  51. SetCommissions(goods);
  52. IsBidDirection = holdOrder.Direction == Direction.Ask;
  53. IsAskDirection = !IsBidDirection;
  54. }
  55. /// <summary>
  56. /// 汇总平仓
  57. /// </summary>
  58. /// <param name="holdingSummary"></param>
  59. /// <param name="goods"></param>
  60. public BidCloseViewModel(HoldingSummary holdingSummary, QuoteGoods goods,OrderType orderType=OrderType.BidMarketClose )
  61. : base(holdingSummary,goods)
  62. {
  63. IsBidMarket = true;
  64. this._holdingSummary = holdingSummary;
  65. CurrentOrderType = orderType;
  66. SetCommissions(goods);
  67. IsBidDirection = holdingSummary.Direction == Direction.Ask;
  68. IsAskDirection = !IsBidDirection;
  69. }
  70. /// <summary>
  71. /// 按商品平仓
  72. /// </summary>
  73. /// <param name="goods"></param>
  74. public BidCloseViewModel(QuoteGoods goods, List<HoldingSummary> holdingSummaries, bool isDirection)
  75. : base(goods)
  76. {
  77. IsBidMarket = true;
  78. CurrentOrderType = OrderType.BidMarketClose;
  79. IsDirectionEnable = isDirection;
  80. _holdingSummaries = holdingSummaries;
  81. var holdingSummary= holdingSummaries.FirstOrDefault();
  82. if (holdingSummary != null)
  83. {
  84. IsBidDirection = holdingSummary.Direction == Direction.Ask;
  85. SetHoldingSummay(holdingSummary);
  86. IsAskDirection = !IsBidDirection;
  87. }
  88. SetCommissions(goods);
  89. }
  90. private TradeCloseMode _tradeCloseMode;
  91. /// <summary>
  92. /// 平仓范围
  93. /// </summary>
  94. public TradeCloseMode TradeCloseMode
  95. {
  96. get
  97. {
  98. return _tradeCloseMode;
  99. }
  100. set
  101. {
  102. Set(() => TradeCloseMode, ref _tradeCloseMode, value);
  103. // TradeCloseModeChange();
  104. }
  105. }
  106. private Dictionary<TradeCloseMode, string> _tradeCloseModes;
  107. /// <summary>
  108. /// 平仓范围枚举
  109. /// </summary>
  110. public Dictionary<TradeCloseMode, string> TradeCloseModes
  111. {
  112. get
  113. {
  114. //todo:显示平仓类型
  115. var diction = new Dictionary<TradeCloseMode, string>();
  116. diction.Add(TradeCloseMode.CloseAll, Muchinfo.MTPClient.Resources.Muchinfo_Resource.Trade_No);
  117. diction.Add(TradeCloseMode.CloseToday, Muchinfo.MTPClient.Resources.Muchinfo_Resource.Trade_CloseToday);
  118. diction.Add(TradeCloseMode.CloseHistory, Muchinfo.MTPClient.Resources.Muchinfo_Resource.Trade_CloseHistory);
  119. return diction;
  120. // return _tradeCloseModes;
  121. }
  122. set
  123. {
  124. Set(() => TradeCloseModes, ref _tradeCloseModes, value);
  125. }
  126. }
  127. /// <summary>
  128. /// 获取和设置OrderModes
  129. /// </summary>
  130. public Dictionary<OrderMode, string> OrderModes
  131. {
  132. get
  133. {
  134. var types = new Dictionary<OrderMode, string>
  135. {
  136. {OrderMode.STD, Muchinfo.MTPClient.Resources.Muchinfo_Resource.Trade_No},
  137. {OrderMode.FAK, "FAK"},
  138. {OrderMode.FOK,"FOK"}
  139. };
  140. return types;
  141. }
  142. }
  143. /// <summary>
  144. /// 设置成交属性
  145. /// </summary>
  146. public OrderMode OrderMode
  147. {
  148. get { return _orderMode; }
  149. set { Set(() => OrderMode, ref _orderMode, value); }
  150. }
  151. private Visibility _upPriceVisibility;
  152. /// <summary>
  153. /// 是否显示涨停价提示
  154. /// </summary>
  155. public Visibility UpPriceVisibility
  156. {
  157. get { return _upPriceVisibility; }
  158. set { Set(() => UpPriceVisibility, ref _upPriceVisibility, value); }
  159. }
  160. private Visibility _fallPriceVisibility;
  161. /// <summary>
  162. /// 是否显示跌停价
  163. /// </summary>
  164. public Visibility FallPriceVisibility
  165. {
  166. get
  167. {
  168. return _fallPriceVisibility;
  169. }
  170. set
  171. {
  172. Set(() => FallPriceVisibility, ref _fallPriceVisibility, value);
  173. }
  174. }
  175. #region 竞价策略单
  176. private bool _reverseOpenChecked;
  177. public bool ReverseOpenChecked
  178. {
  179. get
  180. {
  181. return _reverseOpenChecked;
  182. }
  183. set
  184. {
  185. Set(() => ReverseOpenChecked, ref _reverseOpenChecked, value);
  186. }
  187. }
  188. #endregion
  189. #region 竞价报价
  190. private List<Commission> _bidCommissions;
  191. /// <summary>
  192. /// 显示买入档
  193. /// </summary>
  194. public List<Commission> BidCommissions
  195. {
  196. get { return _bidCommissions; }
  197. set { Set(() => BidCommissions, ref _bidCommissions, value); }
  198. }
  199. private List<Commission> _askCommissions;
  200. /// <summary>
  201. /// 显示卖出档
  202. /// </summary>
  203. public List<Commission> AskCommissions
  204. {
  205. get { return _askCommissions; }
  206. set { Set(() => AskCommissions, ref _askCommissions, value); }
  207. }
  208. /// <summary>
  209. /// 设置买卖档
  210. /// </summary>
  211. /// <param name="bid">if set to <c>true</c> [bid].</param>
  212. /// <param name="commissions">The commissions.</param>
  213. /// <returns>List{Commission}.</returns>
  214. private List<Commission> SortCommissions(bool bid, Commission[] commissions)
  215. {
  216. var commissionLsit = new List<Commission>();
  217. int index = 1;
  218. foreach (var commission in commissions)
  219. {
  220. commission.Index = index;
  221. commission.FormatString = this.PriceFormat;
  222. commissionLsit.Add(commission);
  223. index++;
  224. }
  225. if (!bid)
  226. {
  227. commissionLsit = commissionLsit.OrderByDescending((item) => item.Index).ToList();
  228. }
  229. return commissionLsit;
  230. }
  231. private void SetCommissions(QuoteGoods goods)
  232. {
  233. BidCommissions = SortCommissions(true, goods.BidList);
  234. AskCommissions = SortCommissions(false, goods.AskList);
  235. }
  236. #endregion
  237. #region 虚函数
  238. /// <summary>
  239. /// 获取和设置the order types
  240. /// </summary>
  241. public override Dictionary<OrderType, string> OrderTypes
  242. {
  243. get
  244. {
  245. var types = new Dictionary<OrderType, string>
  246. {
  247. {OrderType.BidMarketClose, Muchinfo_Resource.OrderType_Enum_MarketClose},
  248. {OrderType.BidLimitClose, Muchinfo_Resource.OrderType_Enum_LimitClose}
  249. };
  250. return types;
  251. }
  252. }
  253. public override NewEntrustOrder BuildEntrustOrder()
  254. {
  255. var entrustOrder = base.BuildEntrustOrder();
  256. if (CurrentOrderType == OrderType.BidLimitClose)
  257. {
  258. entrustOrder.EntrustPrice = ExecutePrice;
  259. entrustOrder.PriceMode = OrderPriceMode.LimitPrice;
  260. }
  261. else
  262. {
  263. entrustOrder.EntrustPrice = QuotePrice;
  264. entrustOrder.PriceMode = OrderPriceMode.MarketPrice;
  265. }
  266. if (this.HoldOrder != null)
  267. {
  268. entrustOrder.RelationTicket = HoldOrder.OrderID;
  269. }
  270. // entrustOrder.TradeCloseMode = TradeCloseMode;
  271. entrustOrder.OrderMode = OrderMode;
  272. //entrustOrder.StrategyType = ReverseOpenChecked ? StrategyType.ReverseOpen : StrategyType.None;
  273. //entrustOrder.ReverseOpenPrice = entrustOrder.EntrustPrice;
  274. entrustOrder.AllowTradeSub = Pips;
  275. return entrustOrder;
  276. }
  277. public override void PostOrder(Action<OrderDetail> successAction, Action<Data.ErrorEntity> errorAction)
  278. {
  279. var entrustOrder = BuildEntrustOrder();
  280. var orderService = SimpleIoc.Default.GetInstance<IOrderService>();
  281. orderService.BidMarketEntrustOrder(entrustOrder, successAction, errorAction);
  282. }
  283. #endregion
  284. }
  285. }