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
///
/// Initializes a new instance of the class.
///
/// The goods.
/// The direction.
public ChannelTradeViewMode(QuoteGoods goods, Direction direction)
: base(goods, direction)
{
ChannelMarketInit(direction);
SetMinMaxQtyVaule();
}
///
/// 平仓ty
///
/// The goods.
/// The order base.
public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase)
: base(goods, orderBase)
{
OpenCloseMode = OpenCloseMode.BUILDTYPE_CLOSE;
ChannelMarketInit(this.Direction);
SetCloseQty();
}
///
/// Initializes a new instance of the class.
///
/// The goods.
/// The order base.
/// The price mode.
public ChannelTradeViewMode(QuoteGoods goods, OrderBase orderBase, ePriceMode priceMode)
: this(goods, orderBase)
{
PriceMode = priceMode;
}
///
/// Initializes a new instance of the class.
///
/// The goods.
/// The order base.
/// The order model.
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>(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 successAction, Action 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
///
/// 设置建仓最大最小数量
///
private void SetOpenQty()
{
MinLot = defaut_minLot;
MaxLot = defaut_maxLot;
}
///
/// 设置平仓最大最小数量
///
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 界面项目是否显示控制
///
/// 是否显示报价牌
///
public override bool IsBidAskVsb
{
get { return false; }
}
///
/// 是否可按商品平仓 (不可按商品平仓)
///
public override bool IsCanGoodsEdit
{
get
{
return true;//MTP2.0 NWE[只能按头寸平仓]
}
}
///
/// 是否显示价格类型可见 默认显示
///
public override bool IsPriceModeVisible
{
get { return false; }
}
///
/// 是否通道交易模式
///
public override bool IsVisibilityChannelTrade
{
get { return true; }
}
#endregion
}
}