| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //----------------------------------------------------------------
- //Module Name: $safeprojectname$
- //Purpose:
- //CopyRight: Muchinfo
- //History:
- //----------------------------------------------------------------
- //DateTime 2016/7/22 16:33:46
- //Author
- //Description Create
- //----------------------------------------------------------------
- using System.Windows;
- using Muchinfo.MTPClient.Data;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Data.Model;
- using Muchinfo.MTPClient.Data.Model.Account;
- using Muchinfo.MTPClient.Infrastructure.Cache;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.Infrastructure.Utilities;
- using GalaSoft.MvvmLight.Command;
- namespace Muchinfo.MTPClient.Trade.ViewModels
- {
- public class BidTradeViewModel : TradeBaseViewModel
- {
- public BidTradeViewModel(QuoteGoods goods,Direction direction):base(goods,direction)
- {
- BidMarketInit(goods);
- SetMinMaxQtyVaule();
- }
- /// <summary>
- /// 设置默认价格模式[读取数据库配置--设置]
- /// </summary>
- protected override void setDefaultPriceMode()
- {
- PriceMode = ePriceMode.PRICEMODE_LIMIT;//【客户需求】若商品为竞价模式,则默认成交的价格类型为“限价”
- if (ApplicationParameter.BidTradeDefaultPriceMode == 0)
- {
- PriceMode = ePriceMode.PRICEMODE_MARKET;
- }
- }
- public BidTradeViewModel(QuoteGoods goods, OrderBase orderBase)
- : base(goods, orderBase)
- {
- BidMarketInit(goods);
- OpenCloseMode = OpenCloseMode.BUILDTYPE_CLOSE;
- if (this.HoldDetails != null&&orderBase is HoldingOrder)
- {
- GoodsOrderMode = GoodsOrderMode.Order;
- SelectOrder =
- this.HoldDetails.FirstOrDefault((item) => item.OrderID == (orderBase as HoldingOrder).OrderID);
- }
- }
- private void BidMarketInit(QuoteGoods goods)
- {
- AskCommissions = SortCommissions(false, goods.AskList, goods.FormatPrice);
- BidCommissions = SortCommissions(true, goods.BidList, goods.FormatPrice);
- MessengerHelper.QuoteRegister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- if (_currentGoods != null)
- {
- foreach (var goodItem in quoteList)
- {
- if (goodItem.GoodsHqCode.ToLower() == _currentGoods.GoodsHqCode.ToLower())
- {
- SetEntrustPriceRange();
- AskCommissions = SortCommissions(false, goodItem.AskList, goodItem.FormatPrice);
- BidCommissions = SortCommissions(true, goodItem.BidList, goodItem.FormatPrice);
- RaisePropertyChanged(() => CurrentPrice);
- }
- }
- }
- }));
- });
- }
- #region 竞价内容
- /// <summary>
- /// 跌停
- /// </summary>
- public decimal LowPrice
- {
- get
- {
- if (this._currentGoods != null )
- {
- return _currentGoods.FallsPrice;
- }
- return 0m;
- }
- }
- /// <summary>
- /// 涨停
- /// </summary>
- public decimal UpPrice
- {
- get
- {
- if (this._currentGoods != null )
- {
- return _currentGoods.RaisesPrice;
- }
- return 0m;
- }
- }
- /// <summary>
- /// 价格格式化
- /// </summary>
- public string PriceFormat
- {
- get
- {
- if (this._currentGoods != null)
- {
- return _currentGoods.FormatPrice;
- }
- else
- {
- return "F0";
- }
- }
- }
- /// <summary>
- ///最新价
- /// </summary>
- public decimal CurrentPrice
- {
- get
- {
- if (this._currentGoods != null)
- {
- return _currentGoods.CurrentPrice;
- }
- else
- {
- return default(decimal);
- }
- }
- }
- /// <summary>
- ///最新价
- /// </summary>
- public decimal LastClose
- {
- get
- {
- if (this._currentGoods != null)
- {
- return _currentGoods.LastClose;
- }
- else
- {
- return default(decimal);
- }
- }
- }
- /// <summary>
- /// 格式化涨停
- /// </summary>
- public string UpPriceDisplay
- {
- get
- {
- return UpPrice.ToString(PriceFormat);
- }
- }
- /// <summary>
- /// 格式化跌停
- /// </summary>
- public string LowPriceDisplay
- {
- get
- {
- return LowPrice.ToString(PriceFormat);
- }
- }
- #endregion
- /// <summary>
- /// 是否显示买卖五档
- /// </summary>
- public override bool IsBidQueueVsb
- {
- get { return true; }
- }
- #region 五档报价
-
- private List<Commission> _bidCommissions;
- /// <summary>
- /// 显示买入档
- /// </summary>
- public List<Commission> BidCommissions
- {
- get { return _bidCommissions; }
- set { Set(() => BidCommissions, ref _bidCommissions, value); }
- }
- private List<Commission> _askCommissions;
- /// <summary>
- /// 显示卖出档
- /// </summary>
- public List<Commission> AskCommissions
- {
- get { return _askCommissions; }
- set { Set(() => AskCommissions, ref _askCommissions, value); }
- }
- /// <summary>
- /// 设置买卖档
- /// </summary>
- /// <param name="bid">if set to <c>true</c> [bid].</param>
- /// <param name="commissions">The commissions.</param>
- /// <returns>List{Commission}.</returns>
- private List<Commission> SortCommissions(bool bid, Commission[] commissions, string formatStr)
- {
- var commissionLsit = new List<Commission>();
- int index = 1;
- foreach (var commission in commissions)
- {
- commission.Index = index;
- commission.FormatString = formatStr;
- commissionLsit.Add(commission);
- index++;
- }
- if (!bid)
- {
- commissionLsit = commissionLsit.OrderByDescending((item) => item.Index).ToList();
- }
- return commissionLsit;
- }
- private Commission _currentCommission;
- /// <summary>
- /// 当前选择的五档价格,设置限价价格
- /// </summary>
- public Commission CurrentCommission
- {
- get { return _currentCommission; }
- set
- {
- Set(() => CurrentCommission, ref _currentCommission, value);
- SetLimitOrderPirce(value);
- }
- }
- private RelayCommand<Commission> _commissionsListBoxCommand;
- /// <summary>
- /// 多个ListBox中改变当前选中的CurrentCommission
- /// </summary>
- public RelayCommand<Commission> CommissionsListBoxCommand
- {
- get
- {
- return _commissionsListBoxCommand
- ?? (_commissionsListBoxCommand = new RelayCommand<Commission>(
- p =>
- {
- if (p != null)
- {
- //CurrentCommission = p;
- }
- }));
- }
- }
- #endregion
- public override bool Validated(ref string msg)
- {
- if (!base.Validated(ref msg))
- {
- return false;
- }
-
- return true;
- }
- public override bool IsShowRaiseFall
- {
- get { return true; }
- }
- /// <summary>
- /// 用户点五档时,显示相应的价格
- /// </summary>
- /// <param name="commission"></param>
- protected void SetLimitOrderPirce(Commission commission)
- {
- if (PriceMode == ePriceMode.PRICEMODE_LIMIT)
- {
- if (commission != null && commission.Price>0)
- {
- if (ListingSelectModel == eListingSelectType.LISTINGSELECTTYPE_DELISTING && DelistingModel == eDelistingType.DELISTINGTYPE_SELECTED)
- { }
- else
- {
- ExecutePrice = commission.Price;
- }
-
- }
- }
- }
- protected override void SetMinMaxQtyVaule()
- {
-
- if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_OPEN)
- {
- SetOpenQty();
- }
- else if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_CLOSE) //平仓设置最大最小数量
- {
- SetCloseQty();
- }
- //else if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_CLOSETHENOPEN)
- //{
- // SetCloseThenOpen();
- //}
- }
- /// <summary>
- /// 设置建仓最大最小数量
- /// </summary>
- protected void SetOpenQty()
- {
- var maxQty = _goodsService.GetGoodsParamerRule((int) _currentGoods.GoodsId,
- _currentGoods.TradeMode, GoodsTradeConts.MAXOPENTRADEQTY);
- var minQty = _goodsService.GetGoodsParamerRule((int) _currentGoods.GoodsId,
- _currentGoods.TradeMode, GoodsTradeConts.MINOPENTRADEQTY);
- MinLot = minQty == null ? defaut_minLot : minQty.FeeValue;
- // Lot = MinLot;
- MaxLot = maxQty == null ? defaut_maxLot : maxQty.FeeValue;
- var enableOpenNum = CalcAmountQty();
- MaxLot = Math.Min(enableOpenNum, MaxLot);
- }
- /// <summary>
- /// 当前可用资金还能下多少单
- /// </summary>
- /// <returns></returns>
- private decimal CalcAmountQty()
- {
- var price = OrderPriceSetting();
- var enableOpenNum = GetEnableNum(price); //根据资金算出可建仓数
- enableOpenNum = MinLot == 0 ? enableOpenNum : Math.Floor(enableOpenNum / MinLot) * MinLot; ////下单数量应该为最小的整数倍
- enableOpenNum = Math.Max(0, enableOpenNum); ////是大于等0
- return enableOpenNum;
- }
- /// <summary>
- /// 先平后建设置最大可建仓数量
- /// </summary>
- protected void SetCloseThenOpen()
- {
- var maxQty = _goodsService.GetGoodsParamerRule((int)_currentGoods.GoodsId,
- _currentGoods.TradeMode, GoodsTradeConts.MAXOPENTRADEQTY);
- var minQty = _goodsService.GetGoodsParamerRule((int)_currentGoods.GoodsId,
- _currentGoods.TradeMode, GoodsTradeConts.MINOPENTRADEQTY);
- MinLot = minQty == null ? defaut_minLot : minQty.FeeValue;
- // Lot = MinLot;
- MaxLot = maxQty == null ? defaut_maxLot : maxQty.FeeValue;
-
- decimal tempMaxLot = CalcAvailCloseQty();
- tempMaxLot += CalcAmountQty();
- MaxLot = Math.Min(tempMaxLot, MaxLot);
- }
- /// <summary>
- /// 计算汇总可平数量
- /// </summary>
- /// <returns></returns>
- protected decimal CalcAvailCloseQty()
- {
- decimal tempMaxLot = 0; ///可平数量
- try
- {
- var currentAccount = UserManager.CurrentTradeAccount.FundsAccountId;// == 0 ? CacheManager.FundsAccountId : UserManager.CurrentTradeAccount.FundsAccountId;
- if (currentAccount == 0)
- {
- return 0;
- }
- var currentHolding = CacheManager.HoldingSummaries[currentAccount].FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
- if (Direction == Direction.Ask)
- {
- tempMaxLot = currentHolding.AvailableBuyHolderQty();
- }
- else
- {
- tempMaxLot = currentHolding.AvailableSellHolderQty();
- }
- return tempMaxLot;
- }
- catch
- {
- }
- return 0;
- }
- /// <summary>
- /// /设置计算数量下单价格.
- /// </summary>
- /// <returns></returns>
- protected virtual decimal OrderPriceSetting()
- {
- var price = ExecutePrice;
- //竞价市场-》-》市价-》保证金OR全额-》默认价格 按涨跌停计算可购买数量
- if (PriceMode == ePriceMode.PRICEMODE_MARKET)
- //&& CurrentGoods.GoodsParameters.MoneyMode == eMoneyMode.MONEYMODE_MARGIN
- {
- #region 市价:买一价卖一价
- //price = decimal.Zero;
- //if (BidCommissions != null && Direction == Direction.Ask)
- //{
- // price = BidCommissions.Find(x=>x.Index==1).Price; //卖跌停价//买一价
- //}
- //if (AskCommissions != null && Direction == Direction.Bid)
- //{
- // price = AskCommissions.Find(x => x.Index == 1).Price; //卖跌停价//卖一价
- //}
- #endregion
- if (Direction == Direction.Ask)
- {
- price = this.LowPrice; //卖跌停价
- }
- else
- {
- price = this.UpPrice; //买涨停价
- }
- }
- return price;
- }
- /// <summary>
- /// 设置平仓最大最小数量
- /// </summary>
- protected virtual void SetCloseQty()
- {
- var minQty = _goodsService.GetGoodsParamerRule((int)_currentGoods.GoodsId, _currentGoods.TradeMode, GoodsTradeConts.MINOPENTRADEQTY);
- MinLot = minQty == null ? defaut_minLot : minQty.FeeValue;
- decimal tempMaxLot = CalcAvailCloseQty();
- //按寸头平仓
- MaxLot = tempMaxLot;
- }
- public override void PostOrder(Action<OrderDetail> successAction, Action<ErrorEntity> errorAction)
- {
- var entrustOrder = BuildEntrustOrder();
- // _orderService.BidMarketEntrustOrder(entrustOrder, successAction, errorAction);
- _orderService.MakeMarketEntrustOrder(entrustOrder, successAction, errorAction);
- }
-
-
- }
- }
|