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();
}
///
/// 设置默认价格模式[读取数据库配置--设置]
///
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>(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 竞价内容
///
/// 跌停
///
public decimal LowPrice
{
get
{
if (this._currentGoods != null )
{
return _currentGoods.FallsPrice;
}
return 0m;
}
}
///
/// 涨停
///
public decimal UpPrice
{
get
{
if (this._currentGoods != null )
{
return _currentGoods.RaisesPrice;
}
return 0m;
}
}
///
/// 价格格式化
///
public string PriceFormat
{
get
{
if (this._currentGoods != null)
{
return _currentGoods.FormatPrice;
}
else
{
return "F0";
}
}
}
///
///最新价
///
public decimal CurrentPrice
{
get
{
if (this._currentGoods != null)
{
return _currentGoods.CurrentPrice;
}
else
{
return default(decimal);
}
}
}
///
///最新价
///
public decimal LastClose
{
get
{
if (this._currentGoods != null)
{
return _currentGoods.LastClose;
}
else
{
return default(decimal);
}
}
}
///
/// 格式化涨停
///
public string UpPriceDisplay
{
get
{
return UpPrice.ToString(PriceFormat);
}
}
///
/// 格式化跌停
///
public string LowPriceDisplay
{
get
{
return LowPrice.ToString(PriceFormat);
}
}
#endregion
///
/// 是否显示买卖五档
///
public override bool IsBidQueueVsb
{
get { return true; }
}
#region 五档报价
private List _bidCommissions;
///
/// 显示买入档
///
public List BidCommissions
{
get { return _bidCommissions; }
set { Set(() => BidCommissions, ref _bidCommissions, value); }
}
private List _askCommissions;
///
/// 显示卖出档
///
public List AskCommissions
{
get { return _askCommissions; }
set { Set(() => AskCommissions, ref _askCommissions, value); }
}
///
/// 设置买卖档
///
/// if set to true [bid].
/// The commissions.
/// List{Commission}.
private List SortCommissions(bool bid, Commission[] commissions, string formatStr)
{
var commissionLsit = new List();
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;
///
/// 当前选择的五档价格,设置限价价格
///
public Commission CurrentCommission
{
get { return _currentCommission; }
set
{
Set(() => CurrentCommission, ref _currentCommission, value);
SetLimitOrderPirce(value);
}
}
private RelayCommand _commissionsListBoxCommand;
///
/// 多个ListBox中改变当前选中的CurrentCommission
///
public RelayCommand CommissionsListBoxCommand
{
get
{
return _commissionsListBoxCommand
?? (_commissionsListBoxCommand = new RelayCommand(
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; }
}
///
/// 用户点五档时,显示相应的价格
///
///
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();
//}
}
///
/// 设置建仓最大最小数量
///
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);
}
///
/// 当前可用资金还能下多少单
///
///
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;
}
///
/// 先平后建设置最大可建仓数量
///
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);
}
///
/// 计算汇总可平数量
///
///
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;
}
///
/// /设置计算数量下单价格.
///
///
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;
}
///
/// 设置平仓最大最小数量
///
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 successAction, Action errorAction)
{
var entrustOrder = BuildEntrustOrder();
// _orderService.BidMarketEntrustOrder(entrustOrder, successAction, errorAction);
_orderService.MakeMarketEntrustOrder(entrustOrder, successAction, errorAction);
}
}
}