TradeOrderBase.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Ioc;
  3. using Muchinfo.MTPClient.Data;
  4. using Muchinfo.MTPClient.Data.Enums;
  5. using Muchinfo.MTPClient.Data.Helper;
  6. using Muchinfo.MTPClient.Data.Model;
  7. using Muchinfo.MTPClient.Data.Model.Account;
  8. using Muchinfo.MTPClient.Infrastructure.Utilities;
  9. using Muchinfo.MTPClient.IService;
  10. using Muchinfo.MTPClient.Resources;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. //----------------------------------------------------------------
  15. //Module Name: $safeprojectname$
  16. //Purpose:
  17. //CopyRight: Muchinfo
  18. //History:
  19. //----------------------------------------------------------------
  20. //DateTime 2016/1/16 10:06:39
  21. //Author
  22. //Description Create
  23. //----------------------------------------------------------------
  24. using System.Windows;
  25. using System.Windows.Media.Imaging;
  26. namespace Muchinfo.MTPClient.Trade.ViewModels
  27. {
  28. public class TradeOrderBase : ViewModelBase
  29. {
  30. private QuoteGoods _currentGoods; ////当前商品
  31. private bool _isBidDirection = true;
  32. private ExpirationType _currentExpirationType = ExpirationType.Today;
  33. private OrderType _currentOrderType; ////订单类型
  34. private decimal _pips = 5; //// 成交点差
  35. protected IOrderService _orderService;
  36. /// LimitOrderVisibility
  37. /// </summary>
  38. private Visibility _limitOrderGridVisibility;
  39. private decimal _lot;
  40. /// <summary>
  41. /// MarketOrderVisibility
  42. /// </summary>
  43. private Visibility _marketOrderGridVisibility;
  44. public TradeOrderBase(QuoteGoods currentGoods, OrderType orderType)
  45. {
  46. _currentGoods = currentGoods;
  47. CurrentOrderType = orderType;
  48. _orderService = SimpleIoc.Default.GetInstance<IOrderService>();
  49. IsBidDirection = true;
  50. }
  51. /// <summary>
  52. /// 价格格式化
  53. /// </summary>
  54. public string PriceFormat
  55. {
  56. get
  57. {
  58. if (this._currentGoods != null)
  59. {
  60. return _currentGoods.FormatPrice;
  61. }
  62. else
  63. {
  64. return "F0";
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 表单买卖方向
  70. /// </summary>
  71. public bool IsBidDirection
  72. {
  73. get
  74. {
  75. return _isBidDirection;
  76. }
  77. set
  78. {
  79. Set(() => IsBidDirection, ref _isBidDirection, value);
  80. SetQuotePrice();
  81. }
  82. }
  83. /// <summary>
  84. /// 当前商品
  85. /// </summary>
  86. public QuoteGoods CurrentGoods
  87. {
  88. get
  89. {
  90. return _currentGoods;
  91. }
  92. }
  93. /// <summary>
  94. /// Sets and gets the CurrentOrderType property.
  95. /// Changes to that property's value raise the PropertyChanged event.
  96. /// </summary>
  97. public OrderType CurrentOrderType
  98. {
  99. get
  100. {
  101. return _currentOrderType;
  102. }
  103. set
  104. {
  105. Set(() => CurrentOrderType, ref _currentOrderType, value);
  106. if (value == OrderType.MarketOpenOrder || value == OrderType.BidMarketOpen)
  107. {
  108. MarketOrderGridVisibility = Visibility.Visible;
  109. LimitOrderGridVisibility = Visibility.Collapsed;
  110. }
  111. else if (value == OrderType.LimitOpenOrder || value == OrderType.BidLimitOpen)
  112. {
  113. MarketOrderGridVisibility = Visibility.Collapsed;
  114. LimitOrderGridVisibility = Visibility.Visible;
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Sets and gets the LimitOrderVisibility property.
  120. /// Changes to that property's value raise the PropertyChanged event.
  121. /// </summary>
  122. public Visibility LimitOrderGridVisibility
  123. {
  124. get
  125. {
  126. return _limitOrderGridVisibility;
  127. }
  128. set
  129. {
  130. Set(() => LimitOrderGridVisibility, ref _limitOrderGridVisibility, value);
  131. }
  132. }
  133. /// <summary>
  134. /// Sets and gets the Lot property.
  135. /// Changes to that property's value raise the PropertyChanged event.
  136. /// </summary>
  137. public decimal Lot
  138. {
  139. get
  140. {
  141. return _lot;
  142. }
  143. set
  144. {
  145. Set(() => Lot, ref _lot, value);
  146. // RaisePropertyChanged(() => LotVailedString);
  147. }
  148. }
  149. /// <summary>
  150. /// Sets and gets the MarketOrderVisibility property.
  151. /// Changes to that property's value raise the PropertyChanged event.
  152. /// </summary>
  153. public Visibility MarketOrderGridVisibility
  154. {
  155. get
  156. {
  157. return _marketOrderGridVisibility;
  158. }
  159. set
  160. {
  161. //if (_marketOrderGridVisibility == Visibility.Visible)
  162. //{
  163. // WindowHeight = 580;
  164. //}
  165. //else
  166. //{
  167. // WindowHeight = 580;
  168. //}
  169. Set(() => MarketOrderGridVisibility, ref _marketOrderGridVisibility, value);
  170. }
  171. }
  172. /// <summary>
  173. /// 商品图片资源
  174. /// </summary>
  175. public BitmapImage ImageSource
  176. {
  177. get
  178. {
  179. if (File.Exists(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension))
  180. {
  181. return new BitmapImage(new Uri(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension));
  182. }
  183. else
  184. {
  185. return null;
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// 图片可见性
  191. /// </summary>
  192. public Visibility ImageVisibility
  193. {
  194. get
  195. {
  196. if (File.Exists(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension))
  197. {
  198. return Visibility.Visible;
  199. }
  200. else
  201. {
  202. return Visibility.Collapsed;
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// Sets and gets the Pips property.
  208. /// Changes to that property's value raise the PropertyChanged event.
  209. /// </summary>
  210. public decimal Pips
  211. {
  212. get
  213. {
  214. return _pips;
  215. }
  216. set
  217. {
  218. Set(() => Pips, ref _pips, value);
  219. }
  220. }
  221. /// <summary>
  222. /// 是否允许点差
  223. /// </summary>
  224. private bool _isAllowPips;
  225. /// <summary>
  226. /// 是否允许点差
  227. /// </summary>
  228. public bool IsAllowPips
  229. {
  230. get
  231. {
  232. return _isAllowPips;
  233. }
  234. set
  235. {
  236. Set(() => IsAllowPips, ref _isAllowPips, value);
  237. }
  238. }
  239. private bool _isBidMarket;
  240. /// <summary>
  241. /// 是否是竞价商品
  242. /// </summary>
  243. public bool IsBidMarket
  244. {
  245. get { return _isBidMarket; }
  246. set { Set(() => IsBidMarket, ref _isBidMarket, value); }
  247. }
  248. /// <summary>
  249. /// 获取和设置the expiration types
  250. /// </summary>
  251. public Dictionary<ExpirationType, string> ExpirationTypes
  252. {
  253. get
  254. {
  255. var types = new Dictionary<ExpirationType, string>();
  256. ExpirationType expirationType;
  257. try
  258. {
  259. string[] expirationData = ApplicationParameter.ExpirationType.Split(',');
  260. for (int i = 0; i < expirationData.Length; i++)
  261. {
  262. expirationType = (ExpirationType)System.Enum.Parse(typeof(ExpirationType), expirationData[i], true);
  263. types.Add(expirationType, expirationType.Discription());
  264. }
  265. }
  266. catch (Exception e)
  267. {
  268. }
  269. //types.Add(ExpirationType.SomeTime, "指定时间有效");
  270. //types.Add(ExpirationType.Weekday, ExpirationType.Weekday.Discription());
  271. return types;
  272. }
  273. }
  274. /// <summary>
  275. /// Sets and gets the CurrentExpirationType property.
  276. /// Changes to that property's value raise the PropertyChanged event.
  277. /// </summary>
  278. public ExpirationType CurrentExpirationType
  279. {
  280. get
  281. {
  282. return _currentExpirationType;
  283. }
  284. set
  285. {
  286. Set(() => CurrentExpirationType, ref _currentExpirationType, value);
  287. }
  288. }
  289. private decimal _executePrice;
  290. /// <summary>
  291. /// 限价建仓单-执行价格
  292. /// </summary>
  293. public decimal ExecutePrice
  294. {
  295. get
  296. {
  297. return _executePrice;
  298. }
  299. set
  300. {
  301. Set(() => ExecutePrice, ref _executePrice, value);
  302. }
  303. }
  304. private decimal _quotePrice; //当前行情价
  305. /// <summary>
  306. /// 当前行情价
  307. /// </summary>
  308. public decimal QuotePrice
  309. {
  310. get
  311. {
  312. return _quotePrice;
  313. }
  314. set
  315. {
  316. Set(() => QuotePrice, ref _quotePrice, value);
  317. }
  318. }
  319. #region 虚函数属性
  320. /// <summary>
  321. /// 验证信息
  322. /// </summary>
  323. /// <returns></returns>
  324. public virtual bool Validated()
  325. {
  326. return true;
  327. }
  328. /// <summary>
  329. /// 获取和设置the order types
  330. /// </summary>
  331. public virtual Dictionary<OrderType, string> OrderTypes
  332. {
  333. get
  334. {
  335. var types = new Dictionary<OrderType, string>
  336. {
  337. {OrderType.MarketOpenOrder, Muchinfo_Resource.OrderType_Enum_MarketOpen},
  338. {OrderType.LimitOpenOrder, Muchinfo_Resource.OrderType_Enum_LimitOpen}
  339. };
  340. return types;
  341. }
  342. }
  343. /// <summary>
  344. /// 创建下单委托对象
  345. /// </summary>
  346. /// <returns>下单委托对象</returns>
  347. public virtual NewEntrustOrder BuildEntrustOrder()
  348. {
  349. var entrustOrder = new NewEntrustOrder()
  350. {
  351. GoodsCode = this.CurrentGoods.GoodsCode,
  352. MarketId = (uint)this.CurrentGoods.GoodsParameters.MarketId,
  353. GoodId = (uint)this.CurrentGoods.GoodsParameters.GoodsId,
  354. AccountType = UserManager.CurrentTradeAccount.AccountType,
  355. BuyOrSell = IsBidDirection ? Direction.Bid : Direction.Ask,
  356. EntrustQuantity = Lot,
  357. AccountId = UserManager.CurrentTradeAccount.AccountId,
  358. OpenType = OpenClose.Open,
  359. EntrurstTime = ApplicationParameter.ServerTimeNow,
  360. MemberId = (uint)UserManager.CurrentTradeAccount.MemberAreaId,
  361. };
  362. entrustOrder.OrderMode = OrderMode.InValid;
  363. // entrustOrder.TradeCloseMode = TradeCloseMode.IsVailed;
  364. entrustOrder.ValidType = CurrentExpirationType;
  365. return entrustOrder;
  366. }
  367. /// <summary>
  368. /// 提交表单
  369. /// </summary>
  370. /// <param name="successAction">成功回调</param>
  371. /// <param name="errorAction">错误返回</param>
  372. public virtual void PostOrder(Action<OrderDetail> successAction, Action<ErrorEntity> errorAction)
  373. {
  374. }
  375. /// <summary>
  376. /// 设置当前下单价格
  377. /// </summary>
  378. protected virtual void SetQuotePrice()
  379. {
  380. //todo:暂时设置默认值
  381. if (CurrentGoods != null)
  382. {
  383. ExecutePrice = IsBidDirection ? CurrentGoods.BidPrice : CurrentGoods.AskPrice;
  384. QuotePrice = ExecutePrice;
  385. }
  386. }
  387. #endregion
  388. /// <summary>
  389. /// Cleanups this instance.
  390. /// </summary>
  391. public override void Cleanup()
  392. {
  393. ////取消注册消息
  394. base.Cleanup();
  395. ApplicationParameter.QuotationMessenger.Unregister(this);
  396. }
  397. }
  398. }