/// <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);
            }
        }
예제 #2
0
        /// <summary>
        /// 计算开仓损益。
        /// </summary>
        /// <param name="arbitrageOrder"></param>
        /// <returns></returns>
        private ProfitResult CalculatOpenProfit(USeArbitrageOrder arbitrageOrder)
        {
            USeOrderDriver orderDriver = USeManager.Instance.OrderDriver;
            USeQuoteDriver quoteDriver = USeManager.Instance.QuoteDriver;

            Debug.Assert(orderDriver != null);
            Debug.Assert(quoteDriver != null);

            ArbitrageTaskGroup  taskGroup     = arbitrageOrder.OpenTaskGroup;
            List <USeOrderBook> orderBookList = arbitrageOrder.OpenTaskGroup.GetAllOrderBooks();

            USeMarketData       buyMarketData        = quoteDriver.Query(taskGroup.BuyInstrument);
            USeMarketData       sellMarketData       = quoteDriver.Query(taskGroup.SellInstrument);
            USeInstrumentDetail buyInstrumentDetail  = orderDriver.QueryInstrumentDetail(taskGroup.BuyInstrument);
            USeInstrumentDetail sellInstrumentDetail = orderDriver.QueryInstrumentDetail(taskGroup.SellInstrument);

            decimal buyProfit   = CalculateProfitByOrderBook(orderBookList, buyInstrumentDetail, buyMarketData);
            decimal sellProfit  = CalculateProfitByOrderBook(orderBookList, sellInstrumentDetail, sellMarketData);
            decimal totalProfit = buyProfit + sellProfit;

            ProfitResult result = new ProfitResult()
            {
                BuyProfit  = buyProfit,
                SellProfit = sellProfit
            };

            return(result);
        }
예제 #3
0
        /// <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);
            }
        }
예제 #4
0
        /// <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);
        }
예제 #5
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);
        }
예제 #6
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);
        }
예제 #7
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);
            }
        }
예제 #8
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            List <ErrorUSeOrderBook> result = new List <ErrorUSeOrderBook>();

            try
            {
                //如果存在未被处理的委托单,提示处理
                foreach (DataRow row in m_dataTable.Rows)
                {
                    string errorMessage = string.Empty;
                    if (IsMatchRule(row, out errorMessage) == false)
                    {
                        USeFuturesSpiritUtility.ShowErrrorMessageBox(this, "请处理异常委托单");
                        return;
                    }
                }

                USeOrderDriver orderDriver = USeManager.Instance.OrderDriver;
                Debug.Assert(orderDriver != null);

                //根据界面生成结果保存到Result
                foreach (DataRow row in m_dataTable.Rows)
                {
                    USeInstrument instrument = row["Instrument"] as USeInstrument;
                    Debug.Assert(instrument != null);

                    USeInstrumentDetail instrumentDetail = orderDriver.QueryInstrumentDetail(instrument);
                    //[yangming]合约过期后可能查不到了,此处可以考虑用品种信息获取合约乘数
                    Debug.Assert(instrumentDetail != null);
                    int orderQty  = Convert.ToInt32(row["OrderQty"]);
                    int tradeQty  = Convert.ToInt32(row["TradeQty"]);
                    int cancelQty = orderQty - tradeQty;
                    Debug.Assert(cancelQty >= 0);

                    decimal tradePrice  = Convert.ToDecimal(row["TradePrice"]);
                    decimal tradeFee    = Convert.ToDecimal(row["TradeFee"]);
                    decimal tradeAmount = tradePrice * tradeQty * instrumentDetail.VolumeMultiple;

                    USeOrderStatus selectedOrderStatus = (USeOrderStatus)row["SelectedOrderStatus"];
                    USeOrderStatus status = USeOrderStatus.Unknown;
                    if (selectedOrderStatus == USeOrderStatus.AllTraded)
                    {
                        status = USeOrderStatus.AllTraded;
                    }
                    else if (selectedOrderStatus == USeOrderStatus.AllCanceled)
                    {
                        if (cancelQty == orderQty)
                        {
                            status = USeOrderStatus.AllCanceled;
                        }
                        else
                        {
                            status = USeOrderStatus.PartCanceled;
                        }
                    }

                    USeOrderNum       orderNum      = row["OrderNum"] as USeOrderNum;
                    ErrorUSeOrderBook erroOrderBook = row["ErrorUSeOrderBook"] as ErrorUSeOrderBook;
                    erroOrderBook.OrderBook.OrderStatus = status;
                    erroOrderBook.OrderBook.CancelQty   = cancelQty;
                    erroOrderBook.OrderBook.TradeQty    = tradeQty;
                    erroOrderBook.OrderBook.TradePrice  = tradePrice;
                    erroOrderBook.OrderBook.TradeAmount = tradeAmount;
                    erroOrderBook.OrderBook.TradeFee    = tradeFee;

                    result.Add(erroOrderBook);
                }

                this.Result = result;
            }
            catch (Exception ex)
            {
                USeFuturesSpiritUtility.ShowWarningMessageBox(this, ex.Message);
                return;
            }

            this.DialogResult = DialogResult.Yes;
            this.Close();
        }