ChannelTradeViewMode.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Muchinfo.MTPClient.Data.Enums;
  6. using Muchinfo.MTPClient.Data.Model;
  7. using Muchinfo.MTPClient.Data.Model.Account;
  8. using Muchinfo.MTPClient.Infrastructure.Helpers;
  9. using Muchinfo.MTPClient.Infrastructure.Utilities;
  10. using System.Windows;
  11. using Muchinfo.MTPClient.Infrastructure.Cache;
  12. using Muchinfo.MTPClient.Service;
  13. using Muchinfo.MTPClient.Data;
  14. using Muchinfo.MTPClient.Resources;
  15. namespace Muchinfo.MTPClient.Trade.ViewModels
  16. {
  17. public class ChannelTradeViewMode : TradeBaseViewModel
  18. {
  19. #region Implement Methods
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="ChannelTradeViewMode"/> class.
  22. /// </summary>
  23. /// <param name="goods">The goods.</param>
  24. /// <param name="direction">The direction.</param>
  25. public ChannelTradeViewMode(QuoteGoods goods, Direction direction)
  26. : base(goods, direction)
  27. {
  28. ChannelMarketInit(direction);
  29. SetMinMaxQtyVaule();
  30. }
  31. /// <summary>
  32. /// 平仓ty
  33. /// </summary>
  34. /// <param name="goods">The goods.</param>
  35. /// <param name="orderBase">The order base.</param>
  36. public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase)
  37. : base(goods, orderBase)
  38. {
  39. OpenCloseMode = OpenCloseMode.BUILDTYPE_CLOSE;
  40. ChannelMarketInit(this.Direction);
  41. SetCloseQty();
  42. }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="ChannelTradeViewMode"/> class.
  45. /// </summary>
  46. /// <param name="goods">The goods.</param>
  47. /// <param name="orderBase">The order base.</param>
  48. /// <param name="priceMode">The price mode.</param>
  49. public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase, ePriceMode priceMode)
  50. : this(goods, orderBase)
  51. {
  52. PriceMode = priceMode;
  53. }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="ChannelTradeViewMode"/> class.
  56. /// </summary>
  57. /// <param name="goods">The goods.</param>
  58. /// <param name="orderBase">The order base.</param>
  59. /// <param name="orderModel">The order model.</param>
  60. public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase, OrderModel orderModel)
  61. : this(goods, orderBase)
  62. {
  63. OrderModel = orderModel;
  64. OpenCloseMode = OpenCloseMode.BUILDTYPE_CLOSE;
  65. ChannelMarketInit(this.Direction);
  66. SetCloseQty();
  67. }
  68. private void ChannelMarketInit(Direction direction)
  69. {
  70. //默认使用市价 fixme 没有市价,就默认显示超价
  71. CurrentChannelPriceMode = ApplicationParameter.CanMarketPrice(CurrentGoods) ? ChannelPriceMode.MarketPrice : ChannelPriceMode.SuperPrice;
  72. CurrentSPSLOrderType = SPSLOrderType.SLOrder;
  73. //SetEntrustPriceRange();
  74. DefautEntrustPrice();
  75. ////注册实时行情消息
  76. MessengerHelper.QuoteRegister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
  77. {
  78. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  79. {
  80. if (_currentGoods != null)
  81. {
  82. if (quoteList.FirstOrDefault((item) => item.Symbol == _currentGoods.Symbol) != null)
  83. {
  84. //SetEntrustPriceRange();
  85. DefautEntrustPrice(false);
  86. }
  87. }
  88. }));
  89. });
  90. }
  91. #endregion
  92. #region Override Methods
  93. protected override void SetMinMaxQtyVaule()
  94. {
  95. if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_OPEN)
  96. {
  97. SetOpenQty();
  98. }
  99. else if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_CLOSE) //平仓设置最大最小数量
  100. {
  101. SetCloseQty();
  102. }
  103. }
  104. public override void PostOrder(Action<OrderDetail> successAction, Action<ErrorEntity> errorAction)
  105. {
  106. var entrustOrder = BuildEntrustOrder();
  107. entrustOrder.CurtQuotePrice = Direction == Direction.Ask ? _currentGoods.BidPrice : _currentGoods.AskPrice;
  108. if (OrderModel == OrderModel.Normal)
  109. {
  110. _orderService.ChannelEntrustOrder(entrustOrder, successAction, errorAction);
  111. }
  112. else if (OrderModel == OrderModel.SPSL) // 止盈止损下单
  113. {
  114. entrustOrder.ChannelPriceMode = this.CurrentChannelPriceMode;
  115. if (this.CanUseSlPrice && !this.CanUseSpPrice)// 只有止损价
  116. {
  117. entrustOrder.SPSLOrderType = SPSLOrderType.SLOrder;// 传入止盈止损
  118. entrustOrder.SLPrice = (decimal)this.SlPrice;
  119. }
  120. else if (this.CanUseSpPrice && !this.CanUseSlPrice)// 只有止盈价
  121. {
  122. entrustOrder.SPSLOrderType = SPSLOrderType.SPOrder;// 传入止盈止损
  123. entrustOrder.SPPrice = (decimal)this.SpPrice;
  124. }
  125. else if (this.CanUseSlPrice && this.CanUseSlPrice)// 有止盈止损价
  126. {
  127. entrustOrder.SPSLOrderType = SPSLOrderType.SPSL;// 传入止盈止损
  128. entrustOrder.SLPrice = (decimal)this.SlPrice;
  129. entrustOrder.SPPrice = (decimal)this.SpPrice;
  130. }
  131. /*entrustOrder.TriggerPrice = this.SPSLTriggerPrice;*/
  132. // 本身就是止盈止损价,应该就不需要上传止盈止损点数了。
  133. // 资管下止损止盈的方向应该是持仓方向
  134. entrustOrder.BuyOrSell = this.Direction == Direction.Ask ? Direction.Bid : Direction.Ask;
  135. _orderService.ChannelSPSLOrder(entrustOrder, successAction, errorAction);
  136. }
  137. //通知更新资金
  138. //MessengerHelper.DefaultSend(string.Empty, MessengerTokens.MoneyNoticeToken);
  139. }
  140. #endregion
  141. #region Private Methods
  142. /// <summary>
  143. /// 设置建仓最大最小数量
  144. /// </summary>
  145. private void SetOpenQty()
  146. {
  147. MinLot = defaut_minLot;
  148. MaxLot = defaut_maxLot;
  149. }
  150. /// <summary>
  151. /// 设置平仓最大最小数量
  152. /// </summary>
  153. private void SetCloseQty()
  154. {
  155. var currentAccount = UserManager.CurrentTradeAccount.FundsAccountId;// == 0 ? CacheManager.FundsAccountId : UserManager.CurrentTradeAccount.FundsAccountId;
  156. if (currentAccount == 0) return;
  157. // 需要判断集合中是否有数据。
  158. if (CacheManager.HoldingSummaries.Count == 0 || !CacheManager.HoldingSummaries.ContainsKey(currentAccount)/*有数据,但是没有这个资金账户的东西,那也返回,不做处理*/) return;
  159. var currentHolding = CacheManager.HoldingSummaries[currentAccount].FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
  160. if (currentHolding == null) return;
  161. var agreeUnit = currentHolding.AgreeUnit;
  162. if (agreeUnit == 0)
  163. {
  164. // 如果持仓汇总中拿不到合约单位,则从商品信息里面拿
  165. var goods = CacheManager.CacheGoodsBaseInfos.FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
  166. if (goods != null)
  167. {
  168. agreeUnit = goods.AgreeUnit;
  169. }
  170. }
  171. var qty = Direction == Direction.Ask ? currentHolding.AvailableBuyHolderQty() : currentHolding.AvailableSellHolderQty();
  172. //最小平仓数
  173. MinLot = Math.Min(defaut_minLot, qty);
  174. //按寸头平仓
  175. MaxLot = Math.Max(0, qty);
  176. if (this.OrderModel == OrderModel.SPSL)
  177. {
  178. //初始化止损止盈相关字段
  179. //HoldAvgPriceDisplay = Direction == Direction.Ask
  180. // ? (currentHolding.BuyCurHolderAmount / currentHolding.BuyCurPositionQty / currentHolding.AgreeUnit).ToString(CurrentGoods.FormatPrice)
  181. // : (currentHolding.SellCurHolderAmount / currentHolding.SellCurPositionQty / currentHolding.AgreeUnit).ToString(CurrentGoods.FormatPrice);
  182. HoldAvgPriceDisplay = Direction == Direction.Ask
  183. ? (currentHolding.BuyCurHolderAmount / currentHolding.BuyCurPositionQty / agreeUnit).ToString(CurrentGoods.FormatPrice)
  184. : (currentHolding.SellCurHolderAmount / currentHolding.SellCurPositionQty / agreeUnit).ToString(CurrentGoods.FormatPrice);
  185. DirectionDisplay = Direction == Direction.Ask ? Client_Resource.Delivery_BuyIn : Client_Resource.Delivery_SellOut;
  186. HoldTotalQtyDisplay = Direction == Direction.Ask
  187. ? currentHolding.BuyCurPositionQty.ToString()
  188. : currentHolding.SellCurPositionQty.ToString();
  189. QuerySpslOrderByFundAccountId();
  190. }
  191. }
  192. #endregion
  193. #region 界面项目是否显示控制
  194. /// <summary>
  195. /// 是否显示报价牌
  196. /// </summary>
  197. public override bool IsBidAskVsb
  198. {
  199. get { return false; }
  200. }
  201. /// <summary>
  202. /// 是否可按商品平仓 (不可按商品平仓)
  203. /// </summary>
  204. public override bool IsCanGoodsEdit
  205. {
  206. get
  207. {
  208. return true;//MTP2.0 NWE[只能按头寸平仓]
  209. }
  210. }
  211. /// <summary>
  212. /// 是否显示价格类型可见 默认显示
  213. /// </summary>
  214. public override bool IsPriceModeVisible
  215. {
  216. get { return false; }
  217. }
  218. /// <summary>
  219. /// 是否通道交易模式
  220. /// </summary>
  221. public override bool IsVisibilityChannelTrade
  222. {
  223. get { return true; }
  224. }
  225. #endregion
  226. }
  227. }