| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Data.Model;
- using Muchinfo.MTPClient.Data.Model.Account;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.Infrastructure.Utilities;
- using System.Windows;
- using Muchinfo.MTPClient.Infrastructure.Cache;
- using Muchinfo.MTPClient.Service;
- using Muchinfo.MTPClient.Data;
- using Muchinfo.MTPClient.Resources;
- namespace Muchinfo.MTPClient.Trade.ViewModels
- {
- public class ChannelTradeViewMode : TradeBaseViewModel
- {
- #region Implement Methods
- /// <summary>
- /// Initializes a new instance of the <see cref="ChannelTradeViewMode"/> class.
- /// </summary>
- /// <param name="goods">The goods.</param>
- /// <param name="direction">The direction.</param>
- public ChannelTradeViewMode(QuoteGoods goods, Direction direction)
- : base(goods, direction)
- {
- ChannelMarketInit(direction);
- SetMinMaxQtyVaule();
- }
- /// <summary>
- /// 平仓ty
- /// </summary>
- /// <param name="goods">The goods.</param>
- /// <param name="orderBase">The order base.</param>
- public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase)
- : base(goods, orderBase)
- {
- OpenCloseMode = OpenCloseMode.BUILDTYPE_CLOSE;
- ChannelMarketInit(this.Direction);
- SetCloseQty();
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="ChannelTradeViewMode"/> class.
- /// </summary>
- /// <param name="goods">The goods.</param>
- /// <param name="orderBase">The order base.</param>
- /// <param name="priceMode">The price mode.</param>
- public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase, ePriceMode priceMode)
- : this(goods, orderBase)
- {
- PriceMode = priceMode;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="ChannelTradeViewMode"/> class.
- /// </summary>
- /// <param name="goods">The goods.</param>
- /// <param name="orderBase">The order base.</param>
- /// <param name="orderModel">The order model.</param>
- public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase, OrderModel orderModel)
- : this(goods, orderBase)
- {
- OrderModel = orderModel;
- OpenCloseMode = OpenCloseMode.BUILDTYPE_CLOSE;
- ChannelMarketInit(this.Direction);
- SetCloseQty();
- }
- private void ChannelMarketInit(Direction direction)
- {
- //默认使用市价 fixme 没有市价,就默认显示超价
- //CurrentChannelPriceMode = ApplicationParameter.CanMarketPrice(CurrentGoods) ? ChannelPriceMode.MarketPrice : ChannelPriceMode.SuperPrice;
- CurrentChannelPriceMode = ChannelPriceMode.MatchPrice;
- CurrentSPSLOrderType = SPSLOrderType.SLOrder;
- //SetEntrustPriceRange();
- DefautEntrustPrice();
- ////注册实时行情消息
- MessengerHelper.QuoteRegister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- if (_currentGoods != null)
- {
- if (quoteList.FirstOrDefault((item) => item.Symbol == _currentGoods.Symbol) != null)
- {
- //SetEntrustPriceRange();
- DefautEntrustPrice(false);
- }
- }
- }));
- });
- }
- #endregion
- #region Override Methods
- protected override void SetMinMaxQtyVaule()
- {
- if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_OPEN)
- {
- SetOpenQty();
- }
- else if (this.OpenCloseMode == OpenCloseMode.BUILDTYPE_CLOSE) //平仓设置最大最小数量
- {
- SetCloseQty();
- }
- }
- public override void PostOrder(Action<OrderDetail> successAction, Action<ErrorEntity> errorAction)
- {
- var entrustOrder = BuildEntrustOrder();
- entrustOrder.CurtQuotePrice = Direction == Direction.Ask ? _currentGoods.BidPrice : _currentGoods.AskPrice;
- if (OrderModel == OrderModel.Normal)
- {
- _orderService.ChannelEntrustOrder(entrustOrder, successAction, errorAction);
- }
- else if (OrderModel == OrderModel.SPSL) // 止盈止损下单
- {
- entrustOrder.ChannelPriceMode = this.CurrentChannelPriceMode;
- if (this.CanUseSlPrice && !this.CanUseSpPrice)// 只有止损价
- {
- entrustOrder.SPSLOrderType = SPSLOrderType.SLOrder;// 传入止盈止损
- entrustOrder.SLPrice = (decimal)this.SlPrice;
- }
- else if (this.CanUseSpPrice && !this.CanUseSlPrice)// 只有止盈价
- {
- entrustOrder.SPSLOrderType = SPSLOrderType.SPOrder;// 传入止盈止损
- entrustOrder.SPPrice = (decimal)this.SpPrice;
- }
- else if (this.CanUseSlPrice && this.CanUseSlPrice)// 有止盈止损价
- {
- entrustOrder.SPSLOrderType = SPSLOrderType.SPSL;// 传入止盈止损
- entrustOrder.SLPrice = (decimal)this.SlPrice;
- entrustOrder.SPPrice = (decimal)this.SpPrice;
- }
- /*entrustOrder.TriggerPrice = this.SPSLTriggerPrice;*/
- // 本身就是止盈止损价,应该就不需要上传止盈止损点数了。
- // 资管下止损止盈的方向应该是持仓方向
- entrustOrder.BuyOrSell = this.Direction == Direction.Ask ? Direction.Bid : Direction.Ask;
- _orderService.ChannelSPSLOrder(entrustOrder, successAction, errorAction);
- }
- //通知更新资金
- //MessengerHelper.DefaultSend(string.Empty, MessengerTokens.MoneyNoticeToken);
- }
- #endregion
- #region Private Methods
- /// <summary>
- /// 设置建仓最大最小数量
- /// </summary>
- private void SetOpenQty()
- {
- MinLot = defaut_minLot;
- MaxLot = defaut_maxLot;
- }
- /// <summary>
- /// 设置平仓最大最小数量
- /// </summary>
- private void SetCloseQty()
- {
- var currentAccount = UserManager.CurrentTradeAccount.FundsAccountId;// == 0 ? CacheManager.FundsAccountId : UserManager.CurrentTradeAccount.FundsAccountId;
- if (currentAccount == 0) return;
- // 需要判断集合中是否有数据。
- if (CacheManager.HoldingSummaries.Count == 0 || !CacheManager.HoldingSummaries.ContainsKey(currentAccount)/*有数据,但是没有这个资金账户的东西,那也返回,不做处理*/) return;
- var currentHolding = CacheManager.HoldingSummaries[currentAccount].FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
- if (currentHolding == null) return;
- var agreeUnit = currentHolding.AgreeUnit;
- if (agreeUnit == 0)
- {
- // 如果持仓汇总中拿不到合约单位,则从商品信息里面拿
- var goods = CacheManager.CacheGoodsBaseInfos.FirstOrDefault(p => p.GoodsId == (uint)CurrentGoods.GoodsParameters.GoodsId);
- if (goods != null)
- {
- agreeUnit = goods.AgreeUnit;
- }
- }
- var qty = Direction == Direction.Ask ? currentHolding.AvailableBuyHolderQty() : currentHolding.AvailableSellHolderQty();
- //最小平仓数
- MinLot = Math.Min(defaut_minLot, qty);
- //按寸头平仓
- MaxLot = Math.Max(0, qty);
- if (this.OrderModel == OrderModel.SPSL)
- {
- //初始化止损止盈相关字段
- //HoldAvgPriceDisplay = Direction == Direction.Ask
- // ? (currentHolding.BuyCurHolderAmount / currentHolding.BuyCurPositionQty / currentHolding.AgreeUnit).ToString(CurrentGoods.FormatPrice)
- // : (currentHolding.SellCurHolderAmount / currentHolding.SellCurPositionQty / currentHolding.AgreeUnit).ToString(CurrentGoods.FormatPrice);
- HoldAvgPriceDisplay = Direction == Direction.Ask
- ? (currentHolding.BuyCurHolderAmount / currentHolding.BuyCurPositionQty / agreeUnit).ToString(CurrentGoods.FormatPrice)
- : (currentHolding.SellCurHolderAmount / currentHolding.SellCurPositionQty / agreeUnit).ToString(CurrentGoods.FormatPrice);
- DirectionDisplay = Direction == Direction.Ask ? Client_Resource.Delivery_BuyIn : Client_Resource.Delivery_SellOut;
- HoldTotalQtyDisplay = Direction == Direction.Ask
- ? currentHolding.BuyCurPositionQty.ToString()
- : currentHolding.SellCurPositionQty.ToString();
- QuerySpslOrderByFundAccountId();
- }
- }
- #endregion
- #region 界面项目是否显示控制
- /// <summary>
- /// 是否显示报价牌
- /// </summary>
- public override bool IsBidAskVsb
- {
- get { return false; }
- }
- /// <summary>
- /// 是否可按商品平仓 (不可按商品平仓)
- /// </summary>
- public override bool IsCanGoodsEdit
- {
- get
- {
- return true;//MTP2.0 NWE[只能按头寸平仓]
- }
- }
- /// <summary>
- /// 是否显示价格类型可见 默认显示
- /// </summary>
- public override bool IsPriceModeVisible
- {
- get { return false; }
- }
- /// <summary>
- /// 是否通道交易模式
- /// </summary>
- public override bool IsVisibilityChannelTrade
- {
- get { return true; }
- }
- #endregion
- }
- }
|