BidTradeViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //----------------------------------------------------------------
  6. //Module Name: $safeprojectname$
  7. //Purpose:
  8. //CopyRight: Muchinfo
  9. //History:
  10. //----------------------------------------------------------------
  11. //DateTime 2016/7/22 16:33:46
  12. //Author
  13. //Description Create
  14. //----------------------------------------------------------------
  15. using System.Windows;
  16. using Muchinfo.MTPClient.Data;
  17. using Muchinfo.MTPClient.Data.Enums;
  18. using Muchinfo.MTPClient.Data.Model;
  19. using Muchinfo.MTPClient.Data.Model.Account;
  20. using Muchinfo.MTPClient.Infrastructure.Cache;
  21. using Muchinfo.MTPClient.Infrastructure.Helpers;
  22. using Muchinfo.MTPClient.Infrastructure.Utilities;
  23. using GalaSoft.MvvmLight.Command;
  24. namespace Muchinfo.MTPClient.Trade.ViewModels
  25. {
  26. public class BidTradeViewModel : TradeBaseViewModel
  27. {
  28. public BidTradeViewModel(QuoteGoods goods,Direction direction):base(goods,direction)
  29. {
  30. BidMarketInit(goods);
  31. SetMinMaxQtyVaule();
  32. }
  33. /// <summary>
  34. /// 设置默认价格模式[读取数据库配置--设置]
  35. /// </summary>
  36. protected override void setDefaultPriceMode()
  37. {
  38. PriceMode = ePriceMode.PRICEMODE_LIMIT;//【客户需求】若商品为竞价模式,则默认成交的价格类型为“限价”
  39. if (ApplicationParameter.BidTradeDefaultPriceMode == 0)
  40. {
  41. PriceMode = ePriceMode.PRICEMODE_MARKET;
  42. }
  43. }
  44. public BidTradeViewModel(QuoteGoods goods, OrderBase orderBase)
  45. : base(goods, orderBase)
  46. {
  47. BidMarketInit(goods);
  48. OpenCloseMode = OpenCloseMode.BUILDTYPE_CLOSE;
  49. if (this.HoldDetails != null&&orderBase is HoldingOrder)
  50. {
  51. GoodsOrderMode = GoodsOrderMode.Order;
  52. SelectOrder =
  53. this.HoldDetails.FirstOrDefault((item) => item.OrderID == (orderBase as HoldingOrder).OrderID);
  54. }
  55. }
  56. private void BidMarketInit(QuoteGoods goods)
  57. {
  58. AskCommissions = SortCommissions(false, goods.AskList, goods.FormatPrice);
  59. BidCommissions = SortCommissions(true, goods.BidList, goods.FormatPrice);
  60. MessengerHelper.QuoteRegister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
  61. {
  62. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  63. {
  64. if (_currentGoods != null)
  65. {
  66. foreach (var goodItem in quoteList)
  67. {
  68. if (goodItem.GoodsHqCode.ToLower() == _currentGoods.GoodsHqCode.ToLower())
  69. {
  70. SetEntrustPriceRange();
  71. AskCommissions = SortCommissions(false, goodItem.AskList, goodItem.FormatPrice);
  72. BidCommissions = SortCommissions(true, goodItem.BidList, goodItem.FormatPrice);
  73. RaisePropertyChanged(() => CurrentPrice);
  74. }
  75. }
  76. }
  77. }));
  78. });
  79. }
  80. #region 竞价内容
  81. /// <summary>
  82. /// 跌停
  83. /// </summary>
  84. public decimal LowPrice
  85. {
  86. get
  87. {
  88. if (this._currentGoods != null )
  89. {
  90. return _currentGoods.FallsPrice;
  91. }
  92. return 0m;
  93. }
  94. }
  95. /// <summary>
  96. /// 涨停
  97. /// </summary>
  98. public decimal UpPrice
  99. {
  100. get
  101. {
  102. if (this._currentGoods != null )
  103. {
  104. return _currentGoods.RaisesPrice;
  105. }
  106. return 0m;
  107. }
  108. }
  109. /// <summary>
  110. /// 价格格式化
  111. /// </summary>
  112. public string PriceFormat
  113. {
  114. get
  115. {
  116. if (this._currentGoods != null)
  117. {
  118. return _currentGoods.FormatPrice;
  119. }
  120. else
  121. {
  122. return "F0";
  123. }
  124. }
  125. }
  126. /// <summary>
  127. ///最新价
  128. /// </summary>
  129. public decimal CurrentPrice
  130. {
  131. get
  132. {
  133. if (this._currentGoods != null)
  134. {
  135. return _currentGoods.CurrentPrice;
  136. }
  137. else
  138. {
  139. return default(decimal);
  140. }
  141. }
  142. }
  143. /// <summary>
  144. ///最新价
  145. /// </summary>
  146. public decimal LastClose
  147. {
  148. get
  149. {
  150. if (this._currentGoods != null)
  151. {
  152. return _currentGoods.LastClose;
  153. }
  154. else
  155. {
  156. return default(decimal);
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// 格式化涨停
  162. /// </summary>
  163. public string UpPriceDisplay
  164. {
  165. get
  166. {
  167. return UpPrice.ToString(PriceFormat);
  168. }
  169. }
  170. /// <summary>
  171. /// 格式化跌停
  172. /// </summary>
  173. public string LowPriceDisplay
  174. {
  175. get
  176. {
  177. return LowPrice.ToString(PriceFormat);
  178. }
  179. }
  180. #endregion
  181. /// <summary>
  182. /// 是否显示买卖五档
  183. /// </summary>
  184. public override bool IsBidQueueVsb
  185. {
  186. get { return true; }
  187. }
  188. #region 五档报价
  189. private List<Commission> _bidCommissions;
  190. /// <summary>
  191. /// 显示买入档
  192. /// </summary>
  193. public List<Commission> BidCommissions
  194. {
  195. get { return _bidCommissions; }
  196. set { Set(() => BidCommissions, ref _bidCommissions, value); }
  197. }
  198. private List<Commission> _askCommissions;
  199. /// <summary>
  200. /// 显示卖出档
  201. /// </summary>
  202. public List<Commission> AskCommissions
  203. {
  204. get { return _askCommissions; }
  205. set { Set(() => AskCommissions, ref _askCommissions, value); }
  206. }
  207. /// <summary>
  208. /// 设置买卖档
  209. /// </summary>
  210. /// <param name="bid">if set to <c>true</c> [bid].</param>
  211. /// <param name="commissions">The commissions.</param>
  212. /// <returns>List{Commission}.</returns>
  213. private List<Commission> SortCommissions(bool bid, Commission[] commissions, string formatStr)
  214. {
  215. var commissionLsit = new List<Commission>();
  216. int index = 1;
  217. foreach (var commission in commissions)
  218. {
  219. commission.Index = index;
  220. commission.FormatString = formatStr;
  221. commissionLsit.Add(commission);
  222. index++;
  223. }
  224. if (!bid)
  225. {
  226. commissionLsit = commissionLsit.OrderByDescending((item) => item.Index).ToList();
  227. }
  228. return commissionLsit;
  229. }
  230. private Commission _currentCommission;
  231. /// <summary>
  232. /// 当前选择的五档价格,设置限价价格
  233. /// </summary>
  234. public Commission CurrentCommission
  235. {
  236. get { return _currentCommission; }
  237. set
  238. {
  239. Set(() => CurrentCommission, ref _currentCommission, value);
  240. SetLimitOrderPirce(value);
  241. }
  242. }
  243. private RelayCommand<Commission> _commissionsListBoxCommand;
  244. /// <summary>
  245. /// 多个ListBox中改变当前选中的CurrentCommission
  246. /// </summary>
  247. public RelayCommand<Commission> CommissionsListBoxCommand
  248. {
  249. get
  250. {
  251. return _commissionsListBoxCommand
  252. ?? (_commissionsListBoxCommand = new RelayCommand<Commission>(
  253. p =>
  254. {
  255. if (p != null)
  256. {
  257. //CurrentCommission = p;
  258. }
  259. }));
  260. }
  261. }
  262. #endregion
  263. public override bool Validated(ref string msg)
  264. {
  265. if (!base.Validated(ref msg))
  266. {
  267. return false;
  268. }
  269. return true;
  270. }
  271. public override bool IsShowRaiseFall
  272. {
  273. get { return true; }
  274. }
  275. /// <summary>
  276. /// 用户点五档时,显示相应的价格
  277. /// </summary>
  278. /// <param name="commission"></param>
  279. protected void SetLimitOrderPirce(Commission commission)
  280. {
  281. if (PriceMode == ePriceMode.PRICEMODE_LIMIT)
  282. {
  283. if (commission != null && commission.Price>0)
  284. {
  285. if (ListingSelectModel == eListingSelectType.LISTINGSELECTTYPE_DELISTING && DelistingModel == eDelistingType.DELISTINGTYPE_SELECTED)
  286. { }
  287. else
  288. {
  289. ExecutePrice = commission.Price;
  290. }
  291. }
  292. }
  293. }
  294. protected override void SetMinMaxQtyVaule()
  295. {
  296. if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_OPEN)
  297. {
  298. SetOpenQty();
  299. }
  300. else if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_CLOSE) //平仓设置最大最小数量
  301. {
  302. SetCloseQty();
  303. }
  304. //else if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_CLOSETHENOPEN)
  305. //{
  306. // SetCloseThenOpen();
  307. //}
  308. }
  309. /// <summary>
  310. /// 设置建仓最大最小数量
  311. /// </summary>
  312. protected void SetOpenQty()
  313. {
  314. var maxQty = _goodsService.GetGoodsParamerRule((int) _currentGoods.GoodsId,
  315. _currentGoods.TradeMode, GoodsTradeConts.MAXOPENTRADEQTY);
  316. var minQty = _goodsService.GetGoodsParamerRule((int) _currentGoods.GoodsId,
  317. _currentGoods.TradeMode, GoodsTradeConts.MINOPENTRADEQTY);
  318. MinLot = minQty == null ? defaut_minLot : minQty.FeeValue;
  319. // Lot = MinLot;
  320. MaxLot = maxQty == null ? defaut_maxLot : maxQty.FeeValue;
  321. var enableOpenNum = CalcAmountQty();
  322. MaxLot = Math.Min(enableOpenNum, MaxLot);
  323. }
  324. /// <summary>
  325. /// 当前可用资金还能下多少单
  326. /// </summary>
  327. /// <returns></returns>
  328. private decimal CalcAmountQty()
  329. {
  330. var price = OrderPriceSetting();
  331. var enableOpenNum = GetEnableNum(price); //根据资金算出可建仓数
  332. enableOpenNum = MinLot == 0 ? enableOpenNum : Math.Floor(enableOpenNum / MinLot) * MinLot; ////下单数量应该为最小的整数倍
  333. enableOpenNum = Math.Max(0, enableOpenNum); ////是大于等0
  334. return enableOpenNum;
  335. }
  336. /// <summary>
  337. /// 先平后建设置最大可建仓数量
  338. /// </summary>
  339. protected void SetCloseThenOpen()
  340. {
  341. var maxQty = _goodsService.GetGoodsParamerRule((int)_currentGoods.GoodsId,
  342. _currentGoods.TradeMode, GoodsTradeConts.MAXOPENTRADEQTY);
  343. var minQty = _goodsService.GetGoodsParamerRule((int)_currentGoods.GoodsId,
  344. _currentGoods.TradeMode, GoodsTradeConts.MINOPENTRADEQTY);
  345. MinLot = minQty == null ? defaut_minLot : minQty.FeeValue;
  346. // Lot = MinLot;
  347. MaxLot = maxQty == null ? defaut_maxLot : maxQty.FeeValue;
  348. decimal tempMaxLot = CalcAvailCloseQty();
  349. tempMaxLot += CalcAmountQty();
  350. MaxLot = Math.Min(tempMaxLot, MaxLot);
  351. }
  352. /// <summary>
  353. /// 计算汇总可平数量
  354. /// </summary>
  355. /// <returns></returns>
  356. protected decimal CalcAvailCloseQty()
  357. {
  358. decimal tempMaxLot = 0; ///可平数量
  359. try
  360. {
  361. var currentAccount = UserManager.CurrentTradeAccount.FundsAccountId;// == 0 ? CacheManager.FundsAccountId : UserManager.CurrentTradeAccount.FundsAccountId;
  362. if (currentAccount == 0)
  363. {
  364. return 0;
  365. }
  366. var currentHolding = CacheManager.HoldingSummaries[currentAccount].FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
  367. if (Direction == Direction.Ask)
  368. {
  369. tempMaxLot = currentHolding.AvailableBuyHolderQty();
  370. }
  371. else
  372. {
  373. tempMaxLot = currentHolding.AvailableSellHolderQty();
  374. }
  375. return tempMaxLot;
  376. }
  377. catch
  378. {
  379. }
  380. return 0;
  381. }
  382. /// <summary>
  383. /// /设置计算数量下单价格.
  384. /// </summary>
  385. /// <returns></returns>
  386. protected virtual decimal OrderPriceSetting()
  387. {
  388. var price = ExecutePrice;
  389. //竞价市场-》-》市价-》保证金OR全额-》默认价格 按涨跌停计算可购买数量
  390. if (PriceMode == ePriceMode.PRICEMODE_MARKET)
  391. //&& CurrentGoods.GoodsParameters.MoneyMode == eMoneyMode.MONEYMODE_MARGIN
  392. {
  393. #region 市价:买一价卖一价
  394. //price = decimal.Zero;
  395. //if (BidCommissions != null && Direction == Direction.Ask)
  396. //{
  397. // price = BidCommissions.Find(x=>x.Index==1).Price; //卖跌停价//买一价
  398. //}
  399. //if (AskCommissions != null && Direction == Direction.Bid)
  400. //{
  401. // price = AskCommissions.Find(x => x.Index == 1).Price; //卖跌停价//卖一价
  402. //}
  403. #endregion
  404. if (Direction == Direction.Ask)
  405. {
  406. price = this.LowPrice; //卖跌停价
  407. }
  408. else
  409. {
  410. price = this.UpPrice; //买涨停价
  411. }
  412. }
  413. return price;
  414. }
  415. /// <summary>
  416. /// 设置平仓最大最小数量
  417. /// </summary>
  418. protected virtual void SetCloseQty()
  419. {
  420. var minQty = _goodsService.GetGoodsParamerRule((int)_currentGoods.GoodsId, _currentGoods.TradeMode, GoodsTradeConts.MINOPENTRADEQTY);
  421. MinLot = minQty == null ? defaut_minLot : minQty.FeeValue;
  422. decimal tempMaxLot = CalcAvailCloseQty();
  423. //按寸头平仓
  424. MaxLot = tempMaxLot;
  425. }
  426. public override void PostOrder(Action<OrderDetail> successAction, Action<ErrorEntity> errorAction)
  427. {
  428. var entrustOrder = BuildEntrustOrder();
  429. // _orderService.BidMarketEntrustOrder(entrustOrder, successAction, errorAction);
  430. _orderService.MakeMarketEntrustOrder(entrustOrder, successAction, errorAction);
  431. }
  432. }
  433. }