Exemplo n.º 1
0
        /// <summary>
        /// 计算持仓损益。
        /// </summary>
        /// <param name="posItem"></param>
        /// <returns></returns>
        private decimal CalcHoldingProfit(PositionDetailCalcItem posItem)
        {
            int           volumeMultiple = posItem.InstrumentDetail.VolumeMultiple;
            USeMarketData marketData     = posItem.MarketData;

            decimal holdProfit = 0m;   // 持仓盈亏

            if (posItem.RemainQty > 0) // 有持仓
            {
                if (posItem.PositionType == USePositionType.Today && posItem.Direction == USeDirection.Long)
                {
                    // 当日多头持仓盈亏 = (最新价 - 开仓价) × 多头合约持仓量 × 合约乘数
                    // 当日多头持仓保证金 = 开仓价 × 合约乘数 × 持仓手数 × 多头保证金率
                    if (marketData.LastPrice > 0)
                    {
                        holdProfit += (marketData.LastPrice - posItem.OpenPrice) * posItem.RemainQty * volumeMultiple;
                    }
                }
                else if (posItem.PositionType == USePositionType.Today && posItem.Direction == USeDirection.Short)
                {
                    // 当日空头持仓盈亏 = (开仓价 - 最新价) × 空头合约持仓量 × 合约乘数
                    // 当日持仓保证金 = 开仓价 × 合约乘数 × 持仓手数 × 空头保证金率
                    if (marketData.LastPrice > 0)
                    {
                        holdProfit += (posItem.OpenPrice - marketData.LastPrice) * posItem.RemainQty * volumeMultiple;
                    }
                }
                else if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Long)
                {
                    // 历史多头持仓盈亏 = (最新价 - 上日结算价) × 多头合约持仓量× 合约乘数
                    // 历史多头持仓保证金 = 上日结算价 × 合约乘数 × 持仓手数 × 交易所多头保证金率
                    if (marketData.LastPrice > 0)
                    {
                        holdProfit += (marketData.LastPrice - marketData.PreSettlementPrice) * posItem.RemainQty * volumeMultiple;
                    }
                }
                else if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Short)
                {
                    // 历史空头持仓盈亏 = (上日结算价-最新价)× 空头合约持仓量 × 合约乘数
                    // 历史空头持仓保证金 = 上日结算价 × 合约乘数 × 持仓手数 × 交易所空头保证金率
                    if (marketData.LastPrice > 0)
                    {
                        holdProfit += (marketData.PreSettlementPrice - marketData.LastPrice) * posItem.RemainQty * volumeMultiple;
                    }
                }
                else
                {
                    Debug.Assert(false);
                }
            }
            return(holdProfit);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 计算持仓保证金。
        /// </summary>
        /// <param name="posItem"></param>
        /// <returns></returns>
        private decimal CalcHoldingPositonMargin(PositionDetailCalcItem posItem)
        {
            int           volumeMultiple = posItem.InstrumentDetail.VolumeMultiple;
            USeMarketData marketData     = posItem.MarketData;

            decimal margin = 0m;

            if (posItem.RemainQty > 0) // 有持仓
            {
                if (posItem.PositionType == USePositionType.Today && posItem.Direction == USeDirection.Long)
                {
                    margin = posItem.OpenPrice * volumeMultiple * posItem.RemainQty * posItem.Margin.BrokerLongMarginRatioByMoney +
                             posItem.RemainQty * posItem.Margin.BrokerLongMarginRatioByVolume;
                    //holdTodayMargin += margin;
                }
                else if (posItem.PositionType == USePositionType.Today && posItem.Direction == USeDirection.Short)
                {
                    margin = (posItem.OpenPrice * volumeMultiple * posItem.RemainQty * posItem.Margin.BrokerShortMarginRatioByMoney) +
                             (posItem.RemainQty * posItem.Margin.BrokerShortMarginRatioByVolume);
                    //holdTodayMargin += margin;
                }
                else if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Long)
                {
                    margin = (marketData.PreSettlementPrice * volumeMultiple * posItem.RemainQty * posItem.Margin.BrokerLongMarginRatioByMoney) +
                             (posItem.RemainQty * posItem.Margin.BrokerLongMarginRatioByVolume);
                    //holdHistoryMargin += margin;
                }
                else if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Short)
                {
                    margin = (marketData.PreSettlementPrice * volumeMultiple * posItem.RemainQty * posItem.Margin.BrokerShortMarginRatioByMoney) +
                             (posItem.RemainQty * posItem.Margin.BrokerLongMarginRatioByVolume);
                    //holdHistoryMargin += margin;
                }
                else
                {
                    Debug.Assert(false);
                }
            }
            return(margin);
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 计算平仓损益。
        /// </summary>
        /// <param name="posItem"></param>
        /// <returns></returns>
        private decimal CalcCloseProfit(PositionDetailCalcItem posItem)
        {
            int           volumeMultiple = posItem.InstrumentDetail.VolumeMultiple;
            USeMarketData marketData     = posItem.MarketData;

            decimal closeProfit = 0m; // 平仓盈亏

            if (posItem.CloseQty > 0) // 有平仓
            {
                if (posItem.PositionType == USePositionType.Today && posItem.Direction == USeDirection.Long)
                {
                    // 平当日多头仓盈亏 = (平仓价 - 开仓价)× 多头合约持仓量 × 合约乘数
                    closeProfit += ((posItem.ClosePrice - posItem.OpenPrice) * posItem.CloseQty * volumeMultiple);
                }
                else if (posItem.PositionType == USePositionType.Today && posItem.Direction == USeDirection.Short)
                {
                    // 平当日空头仓盈亏 = (开仓价 - 平仓价)× 空头合约平仓量 × 合约乘数
                    closeProfit += ((posItem.OpenPrice - posItem.ClosePrice) * posItem.CloseQty * volumeMultiple);
                }
                else if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Long)
                {
                    // 平历史仓多头盈亏 = (平仓价 - 上日结算价) × 多头合约平仓量 × 合约乘数
                    closeProfit += ((posItem.ClosePrice - posItem.OpenPrice) * posItem.CloseQty * volumeMultiple);
                }
                else if (posItem.PositionType == USePositionType.Yestorday && posItem.Direction == USeDirection.Short)
                {
                    // 平历史仓空头盈亏 = (上日结算价 - 平仓价) × 空头合约平仓量 × 合约乘数
                    closeProfit += ((posItem.OpenPrice - posItem.ClosePrice) * posItem.CloseQty * volumeMultiple);
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            return(closeProfit);
        }