Example #1
0
        internal static void CalculatePreCheckNecessary(Transaction tran, ref CalculateUnfillMarginParameters unfillParams, decimal?effectiveLot = null)
        {
            Price   buy     = null;
            Price   sell    = null;
            Account account = tran.Owner;

            Settings.Instrument     settingInstrument = tran.SettingInstrument;
            AccountClass.Instrument accountInstrument = account.GetOrCreateInstrument(settingInstrument.Id);
            if (tran.OrderType == OrderType.Market || tran.OrderType == OrderType.MarketOnOpen ||
                tran.OrderType == OrderType.MarketOnClose || settingInstrument.MarginFormula == MarginFormula.CSiMarketPrice ||
                settingInstrument.MarginFormula == MarginFormula.CSxMarketPrice)
            {
                Quotation quotation = QuotationProvider.GetLatestQuotation(tran.InstrumentId, account);
                buy  = quotation.BuyOnCustomerSide;
                sell = quotation.SellOnCustomerSide;
            }
            foreach (Order order in tran.Orders)
            {
                if (order.Phase == OrderPhase.Placed || order.Phase == OrderPhase.Placing)
                {
                    decimal contractSize = tran.ContractSize == 0 ? accountInstrument.TradePolicyDetail.ContractSize : tran.ContractSize;
                    var     price        = order.IsBuy ? sell : buy;
                    order.CalculatePreCheckNecessary(settingInstrument.MarginFormula, tran.CurrencyRate, contractSize, price, effectiveLot);
                }
            }

            foreach (Order order in tran.Orders)
            {
                if (tran.Type == TransactionType.OneCancelOther && tran.OrderType == OrderType.Limit && order.TradeOption == TradeOption.Better)
                {
                    continue;
                }
                Collect(order, tran.ContractSize, effectiveLot, ref unfillParams);
            }
        }
        internal static decimal CalculatePreCheckBalance(this Instrument instrument)
        {
            if (!instrument.IsPhysical)
            {
                return(0);
            }
            Price     buy       = null;
            Account   account   = instrument.Owner;
            Quotation quotation = QuotationProvider.GetLatestQuotation(instrument.Id, account);

            Debug.Assert(quotation != null);
            buy = quotation.BuyOnCustomerSide;
            decimal           preCheckBalance   = 0m;
            TradePolicyDetail tradePolicyDetail = instrument.TradePolicyDetail;

            foreach (Transaction tran in account.GetTransactions(instrument.Id))
            {
                foreach (PhysicalOrder order in tran.Orders)
                {
                    if (order.PhysicalTradeSide == PhysicalTradeSide.Buy && order.IsOpen)
                    {
                        if (order.Phase == OrderPhase.Placed || order.Phase == OrderPhase.Placing)
                        {
                            var     price       = order.SetPrice == null ? buy : order.SetPrice;
                            decimal marketValue = MarketValueCalculator.CalculateValue(instrument.Setting.TradePLFormula, order.Lot, price,
                                                                                       tradePolicyDetail.DiscountOfOdd, tradePolicyDetail.ContractSize);

                            if (order.IsInstalment)
                            {
                                decimal instalmentAdministrationFee = order.CalculateInstalmentAdministrationFee(marketValue);
                                decimal downPayment = order.CalculatePaidAmount(marketValue);
                                preCheckBalance += (instalmentAdministrationFee + downPayment);
                            }
                            else
                            {
                                preCheckBalance += marketValue;
                            }
                        }
                    }
                }
            }
            return(preCheckBalance);
        }