Exemple #1
0
        /// <summary>
        /// 查询成交回报。
        /// </summary>
        /// <returns></returns>
        private List <TradeBookCalcItem> QueryTradeBooks()
        {
            USeOrderDriver      orderDriver   = USeManager.Instance.OrderDriver;
            List <USeTradeBook> tradeBookList = orderDriver.QueryTradeBooks();

            List <TradeBookCalcItem> list = new List <TradeBookCalcItem>();

            if (tradeBookList == null)
            {
                return(list);
            }
            foreach (USeTradeBook tradeItem in tradeBookList)
            {
                USeInstrumentDetail detail = orderDriver.QueryInstrumentDetail(tradeItem.Instrument);
                //USeMargin productMargin = orderDriver.QueryInstrumentMargin(tradeItem.Instrument);

                TradeBookCalcItem calcItem = new TradeBookCalcItem()
                {
                    InstrumentDetail = detail,

                    TradeNum   = tradeItem.TradeNum,
                    Instrument = tradeItem.Instrument,
                    OrderNum   = tradeItem.OrderNum,
                    OrderSide  = tradeItem.OrderSide,
                    OffsetType = tradeItem.OffsetType,
                    Price      = tradeItem.Price,
                    Qty        = tradeItem.Qty,
                    Amount     = tradeItem.Amount,
                    Fee        = tradeItem.Fee,
                    TradeTime  = tradeItem.TradeTime,
                    Account    = tradeItem.Account
                };
                list.Add(calcItem);
            }

            return(list);
        }
Exemple #2
0
        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);
            }
        }