ChannelTradeViewMode.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. CurrentChannelPriceMode = ChannelPriceMode.MatchPrice;
  73. CurrentSPSLOrderType = SPSLOrderType.SLOrder;
  74. //SetEntrustPriceRange();
  75. DefautEntrustPrice();
  76. ////注册实时行情消息
  77. MessengerHelper.QuoteRegister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
  78. {
  79. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  80. {
  81. if (_currentGoods != null)
  82. {
  83. if (quoteList.FirstOrDefault((item) => item.Symbol == _currentGoods.Symbol) != null)
  84. {
  85. //SetEntrustPriceRange();
  86. DefautEntrustPrice(false);
  87. }
  88. }
  89. }));
  90. });
  91. }
  92. #endregion
  93. #region Override Methods
  94. protected override void SetMinMaxQtyVaule()
  95. {
  96. if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_OPEN)
  97. {
  98. SetOpenQty();
  99. }
  100. else if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_CLOSE) //平仓设置最大最小数量
  101. {
  102. SetCloseQty();
  103. }
  104. }
  105. public override void PostOrder(Action<OrderDetail> successAction, Action<ErrorEntity> errorAction)
  106. {
  107. var entrustOrder = BuildEntrustOrder();
  108. entrustOrder.CurtQuotePrice = Direction == Direction.Ask ? _currentGoods.BidPrice : _currentGoods.AskPrice;
  109. if (OrderModel == OrderModel.Normal)
  110. {
  111. _orderService.ChannelEntrustOrder(entrustOrder, successAction, errorAction);
  112. }
  113. else if (OrderModel == OrderModel.SPSL) // 止盈止损下单
  114. {
  115. entrustOrder.ChannelPriceMode = this.CurrentChannelPriceMode;
  116. if (this.CanUseSlPrice && !this.CanUseSpPrice)// 只有止损价
  117. {
  118. entrustOrder.SPSLOrderType = SPSLOrderType.SLOrder;// 传入止盈止损
  119. entrustOrder.SLPrice = (decimal)this.SlPrice;
  120. }
  121. else if (this.CanUseSpPrice && !this.CanUseSlPrice)// 只有止盈价
  122. {
  123. entrustOrder.SPSLOrderType = SPSLOrderType.SPOrder;// 传入止盈止损
  124. entrustOrder.SPPrice = (decimal)this.SpPrice;
  125. }
  126. else if (this.CanUseSlPrice && this.CanUseSlPrice)// 有止盈止损价
  127. {
  128. entrustOrder.SPSLOrderType = SPSLOrderType.SPSL;// 传入止盈止损
  129. entrustOrder.SLPrice = (decimal)this.SlPrice;
  130. entrustOrder.SPPrice = (decimal)this.SpPrice;
  131. }
  132. /*entrustOrder.TriggerPrice = this.SPSLTriggerPrice;*/
  133. // 本身就是止盈止损价,应该就不需要上传止盈止损点数了。
  134. // 资管下止损止盈的方向应该是持仓方向
  135. entrustOrder.BuyOrSell = this.Direction == Direction.Ask ? Direction.Bid : Direction.Ask;
  136. _orderService.ChannelSPSLOrder(entrustOrder, successAction, errorAction);
  137. }
  138. //通知更新资金
  139. //MessengerHelper.DefaultSend(string.Empty, MessengerTokens.MoneyNoticeToken);
  140. }
  141. #endregion
  142. #region Private Methods
  143. /// <summary>
  144. /// 设置建仓最大最小数量
  145. /// </summary>
  146. private void SetOpenQty()
  147. {
  148. MinLot = defaut_minLot;
  149. MaxLot = defaut_maxLot;
  150. }
  151. /// <summary>
  152. /// 设置平仓最大最小数量
  153. /// </summary>
  154. private void SetCloseQty()
  155. {
  156. var currentAccount = UserManager.CurrentTradeAccount.FundsAccountId;// == 0 ? CacheManager.FundsAccountId : UserManager.CurrentTradeAccount.FundsAccountId;
  157. if (currentAccount == 0) return;
  158. // 需要判断集合中是否有数据。
  159. if (CacheManager.HoldingSummaries.Count == 0 || !CacheManager.HoldingSummaries.ContainsKey(currentAccount)/*有数据,但是没有这个资金账户的东西,那也返回,不做处理*/) return;
  160. var currentHolding = CacheManager.HoldingSummaries[currentAccount].FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
  161. if (currentHolding == null) return;
  162. var agreeUnit = currentHolding.AgreeUnit;
  163. if (agreeUnit == 0)
  164. {
  165. // 如果持仓汇总中拿不到合约单位,则从商品信息里面拿
  166. var goods = CacheManager.CacheGoodsBaseInfos.FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
  167. if (goods != null)
  168. {
  169. agreeUnit = goods.AgreeUnit;
  170. }
  171. }
  172. var qty = Direction == Direction.Ask ? currentHolding.AvailableBuyHolderQty() : currentHolding.AvailableSellHolderQty();
  173. //最小平仓数
  174. MinLot = Math.Min(defaut_minLot, qty);
  175. //按寸头平仓
  176. MaxLot = Math.Max(0, qty);
  177. if (this.OrderModel == OrderModel.SPSL)
  178. {
  179. //初始化止损止盈相关字段
  180. //HoldAvgPriceDisplay = Direction == Direction.Ask
  181. // ? (currentHolding.BuyCurHolderAmount / currentHolding.BuyCurPositionQty / currentHolding.AgreeUnit).ToString(CurrentGoods.FormatPrice)
  182. // : (currentHolding.SellCurHolderAmount / currentHolding.SellCurPositionQty / currentHolding.AgreeUnit).ToString(CurrentGoods.FormatPrice);
  183. HoldAvgPriceDisplay = Direction == Direction.Ask
  184. ? (currentHolding.BuyCurHolderAmount / currentHolding.BuyCurPositionQty / agreeUnit).ToString(CurrentGoods.FormatPrice)
  185. : (currentHolding.SellCurHolderAmount / currentHolding.SellCurPositionQty / agreeUnit).ToString(CurrentGoods.FormatPrice);
  186. DirectionDisplay = Direction == Direction.Ask ? Client_Resource.Delivery_BuyIn : Client_Resource.Delivery_SellOut;
  187. HoldTotalQtyDisplay = Direction == Direction.Ask
  188. ? currentHolding.BuyCurPositionQty.ToString()
  189. : currentHolding.SellCurPositionQty.ToString();
  190. QuerySpslOrderByFundAccountId();
  191. }
  192. }
  193. #endregion
  194. #region 界面项目是否显示控制
  195. /// <summary>
  196. /// 是否显示报价牌
  197. /// </summary>
  198. public override bool IsBidAskVsb
  199. {
  200. get { return false; }
  201. }
  202. /// <summary>
  203. /// 是否可按商品平仓 (不可按商品平仓)
  204. /// </summary>
  205. public override bool IsCanGoodsEdit
  206. {
  207. get
  208. {
  209. return true;//MTP2.0 NWE[只能按头寸平仓]
  210. }
  211. }
  212. /// <summary>
  213. /// 是否显示价格类型可见 默认显示
  214. /// </summary>
  215. public override bool IsPriceModeVisible
  216. {
  217. get { return false; }
  218. }
  219. /// <summary>
  220. /// 是否通道交易模式
  221. /// </summary>
  222. public override bool IsVisibilityChannelTrade
  223. {
  224. get { return true; }
  225. }
  226. #endregion
  227. }
  228. }