/// <summary> /// 评估创建套利单需要保证金。 /// </summary> /// <param name="openArg"></param> /// <returns></returns> public decimal EvaluateMargin(ArbitrageOpenArgument openArg) { USeOrderDriver orderDriver = USeManager.Instance.OrderDriver; USeQuoteDriver quoteDriver = USeManager.Instance.QuoteDriver; USeInstrumentDetail buyInstrumentDetail = orderDriver.QueryInstrumentDetail(openArg.BuyInstrument); USeInstrumentDetail sellInstrumentDetail = orderDriver.QueryInstrumentDetail(openArg.SellInstrument); USeMarketData buyMarketData = quoteDriver.Query(openArg.BuyInstrument); USeMarketData sellMarketData = quoteDriver.Query(openArg.SellInstrument); USeMargin buyMarginRate = orderDriver.QueryInstrumentMargin(openArg.BuyInstrument); USeMargin sellMarginRate = orderDriver.QueryInstrumentMargin(openArg.SellInstrument); decimal buyMargin = (openArg.TotalOrderQty * buyMarginRate.BrokerLongMarginRatioByVolume) + (buyMarketData.LastPrice * openArg.TotalOrderQty * buyInstrumentDetail.VolumeMultiple * buyMarginRate.BrokerLongMarginRatioByMoney); decimal sellMargin = (openArg.TotalOrderQty * sellMarginRate.BrokerShortMarginRatioByVolume) + (sellMarketData.LastPrice * openArg.TotalOrderQty * sellInstrumentDetail.VolumeMultiple * sellMarginRate.BrokerShortMarginRatioByMoney); if (openArg.BuyInstrument.Market == USeMarket.SHFE && openArg.SellInstrument.Market == USeMarket.SHFE) { return(Math.Max(buyMargin, sellMargin)); } else { return(buyMargin + sellMargin); } }
/// <summary> /// 计算当前套利单占用的保证金。 /// </summary> /// <returns></returns> public decimal CalculatUseMargin() { ArbitrageOpenArgument openArg = null; lock (m_syncObj) { if (m_arbitrageOrder.State == ArbitrageOrderState.Finish || m_arbitrageOrder.State == ArbitrageOrderState.Closed) { //平仓完成套利单不占用保证金 return(0m); } openArg = m_arbitrageOrder.OpenArgument.Clone(); } USeOrderDriver orderDriver = USeManager.Instance.OrderDriver; USeQuoteDriver quoteDriver = USeManager.Instance.QuoteDriver; USeInstrumentDetail buyInstrumentDetail = orderDriver.QueryInstrumentDetail(openArg.BuyInstrument); USeInstrumentDetail sellInstrumentDetail = orderDriver.QueryInstrumentDetail(openArg.SellInstrument); USeMarketData buyMarketData = quoteDriver.Query(openArg.BuyInstrument); USeMarketData sellMarketData = quoteDriver.Query(openArg.SellInstrument); USeMargin buyMarginRate = orderDriver.QueryInstrumentMargin(openArg.BuyInstrument); USeMargin sellMarginRate = orderDriver.QueryInstrumentMargin(openArg.SellInstrument); decimal buyMargin = (openArg.TotalOrderQty * buyMarginRate.BrokerLongMarginRatioByVolume) + (buyMarketData.LastPrice * openArg.TotalOrderQty * buyInstrumentDetail.VolumeMultiple * buyMarginRate.BrokerLongMarginRatioByMoney); decimal sellMargin = (openArg.TotalOrderQty * sellMarginRate.BrokerShortMarginRatioByVolume) + (sellMarketData.LastPrice * openArg.TotalOrderQty * sellInstrumentDetail.VolumeMultiple * sellMarginRate.BrokerShortMarginRatioByMoney); if (openArg.BuyInstrument.Market == USeMarket.SHFE && openArg.SellInstrument.Market == USeMarket.SHFE) { return(Math.Max(buyMargin, sellMargin)); } else { return(buyMargin + sellMargin); } }
/// <summary> /// 查询委托回报。 /// </summary> /// <returns></returns> private List <OrderBookCalcItem> QueryOrderBooks() { USeOrderDriver orderDriver = USeManager.Instance.OrderDriver; List <USeOrderBook> orderBookList = orderDriver.QueryOrderBooks(); List <OrderBookCalcItem> list = new List <OrderBookCalcItem>(); if (orderBookList == null) { return(list); } foreach (USeOrderBook orderBookItem in orderBookList) { USeInstrumentDetail detail = orderDriver.QueryInstrumentDetail(orderBookItem.Instrument); USeMargin productMargin = orderDriver.QueryInstrumentMargin(orderBookItem.Instrument); USeFee productFee = orderDriver.QueryInstrumentFee(orderBookItem.Instrument); OrderBookCalcItem calcItem = new OrderBookCalcItem() { InstrumentDetail = detail, MarginRate = productMargin, FeeRate = productFee, OrderNum = orderBookItem.OrderNum, Account = orderBookItem.Account, Instrument = orderBookItem.Instrument, OrderQty = orderBookItem.OrderQty, OrderPrice = orderBookItem.OrderPrice, TradeQty = orderBookItem.TradeQty, TradeAmount = orderBookItem.TradeAmount, TradePrice = orderBookItem.TradePrice, TradeFee = orderBookItem.TradeFee, OrderStatus = orderBookItem.OrderStatus, CancelQty = orderBookItem.CancelQty, OrderSide = orderBookItem.OrderSide, OffsetType = orderBookItem.OffsetType, Memo = orderBookItem.Memo, OrderTime = orderBookItem.OrderTime }; list.Add(calcItem); } return(list); }
/// <summary> /// 查询持仓明细。 /// </summary> /// <returns></returns> private List <PositionDetailCalcItem> QueryPositonDetails() { USeOrderDriver orderDriver = USeManager.Instance.OrderDriver; USeQuoteDriver quoteDriver = USeManager.Instance.QuoteDriver; List <USePositionDetail> positonList = orderDriver.QueryPositionDetail(); List <PositionDetailCalcItem> list = new List <PositionDetailCalcItem>(); if (positonList == null) { return(list); } foreach (USePositionDetail posItem in positonList) { USeInstrumentDetail detail = orderDriver.QueryInstrumentDetail(posItem.Instrument); USeMargin productMargin = orderDriver.QueryInstrumentMargin(posItem.Instrument); USeMarketData marketData = quoteDriver.Query(posItem.Instrument); PositionDetailCalcItem calcItem = new PositionDetailCalcItem() { InstrumentDetail = detail, Margin = productMargin, MarketData = marketData, ID = posItem.ID, Instrument = posItem.Instrument.Clone(), Direction = posItem.Direction, PositionType = posItem.PositionType, OpenQty = posItem.OpenQty, OpenPrice = posItem.OpenPrice, OpenTime = posItem.OpenTime, CloseQty = posItem.CloseQty, CloseAmount = posItem.CloseAmount, ClosePrice = posItem.ClosePrice }; list.Add(calcItem); } return(list); }
private USeFundDetail CalculFoundBeforeTrade() { USeOrderDriver orderDriver = USeManager.Instance.OrderDriver; USeQuoteDriver quoteDriver = USeManager.Instance.QuoteDriver; try { List <USeTradeBook> tradeBookList = orderDriver.QueryTradeBooks(); List <USePositionDetail> tradeAccountPosList = orderDriver.QueryPositionDetail(); List <USeOrderBook> orderBookList = orderDriver.QueryOrderBooks(); USeFund accountInfo = orderDriver.QueryFundInfo(); if (tradeBookList == null || tradeAccountPosList == null || orderBookList == null || accountInfo == null) { return(null); } #region 读取 decimal preBalance = accountInfo.PreBalance; //上日客户权益(读取)即上日结算准备金 decimal preCredit = accountInfo.PreCredit; // 上日信用额度 decimal preMortgage = accountInfo.PreMortgage; // 上次质押金额 decimal mortgage = accountInfo.Mortgage; // 质押金额 decimal withDraw = accountInfo.WithDraw; // 今日出金 decimal deposit = accountInfo.Deposit; // 今日入金 decimal deliveryMargin = accountInfo.DeliveryMargin; //交割保证金 #endregion 读取 // 静态权益 = 上日结存 - 上次信用额度 - 上次质押金额 // + 质押金额(可能有延时)- 今日出金(可能有延时) + 今日入金(可能有延时) decimal staticBenefit = preBalance - preCredit - preMortgage + mortgage - withDraw + deposit; #region 成交回报推算 decimal holdHistoryMargin = 0m; decimal holdMargin = 0m; foreach (USePositionDetail posItem in tradeAccountPosList) { USeInstrumentDetail detail = orderDriver.QueryInstrumentDetail(posItem.Instrument); int volumeMultiple = detail.VolumeMultiple; USeMargin productMargin = orderDriver.QueryInstrumentMargin(posItem.Instrument); USeMarketData marketData = quoteDriver.Query(posItem.Instrument); if (posItem.RemainQty > 0) // 有持仓 { if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Long) { // 历史多头持仓保证金 = 上日结算价 × 合约乘数 × 持仓手数 × 交易所多头保证金率 decimal margin = (marketData.PreSettlementPrice * volumeMultiple * posItem.RemainQty * productMargin.BrokerLongMarginRatioByMoney) + (posItem.RemainQty * productMargin.BrokerLongMarginRatioByVolume); holdHistoryMargin += margin; } else if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Short) { decimal margin = (marketData.PreSettlementPrice * volumeMultiple * posItem.RemainQty * productMargin.BrokerShortMarginRatioByMoney) + (posItem.RemainQty * productMargin.BrokerLongMarginRatioByVolume); holdHistoryMargin += margin; } else { Debug.Assert(false); } } } holdMargin = holdHistoryMargin; #endregion // 动态权益 = 静态权益 decimal dynamicBenefit = staticBenefit; decimal frozon = 0; decimal closeProfit = 0m; decimal holdProfit = 0m; decimal tradeFee = 0m; decimal available = dynamicBenefit - holdMargin - frozon - deliveryMargin; //风险度 = (占用保证金 + 交割保证金) / 动态权益 decimal risk = decimal.Divide((holdMargin + deliveryMargin), dynamicBenefit); decimal preferCash = available; if (tradeBookList.Count != 0 || tradeAccountPosList.Count != 0) { preferCash = (decimal)(preferCash * 7 / 10); } USeFundDetail fundDetail = new USeFundDetail(); fundDetail.AccountID = string.Empty; fundDetail.Available = available; fundDetail.Deposit = deposit; fundDetail.Mortgage = mortgage; fundDetail.PreBalance = preBalance; fundDetail.PreCredit = preCredit; fundDetail.PreMortgage = preMortgage; fundDetail.WithDraw = withDraw; fundDetail.StaticBenefit = staticBenefit; fundDetail.CloseProfit = closeProfit; fundDetail.TradeFee = tradeFee; fundDetail.HoldProfit = holdProfit; fundDetail.HoldMargin = holdMargin; fundDetail.DynamicBenefit = dynamicBenefit; fundDetail.FrozonMargin = 0; fundDetail.AccountID = string.Empty; fundDetail.Available = available; fundDetail.Deposit = deposit; fundDetail.Mortgage = mortgage; fundDetail.PreBalance = preBalance; fundDetail.PreCredit = preCredit; fundDetail.PreMortgage = preMortgage; fundDetail.WithDraw = withDraw; fundDetail.StaticBenefit = staticBenefit; fundDetail.CloseProfit = closeProfit; fundDetail.TradeFee = tradeFee; fundDetail.HoldProfit = holdProfit; fundDetail.HoldMargin = holdMargin; fundDetail.DynamicBenefit = dynamicBenefit; fundDetail.FrozonMargin = 0; fundDetail.FrozonFee = 0; fundDetail.Fronzon = frozon; fundDetail.Risk = risk; fundDetail.PreferCash = preferCash; return(fundDetail); } catch (Exception ex) { Debug.Assert(false, ex.Message); return(null); } }