Esempio n. 1
0
        internal static MarginAndQuantityResult CalculateFilledMarginAndQuantity(this Order order, bool isBuy, Dictionary <Guid, decimal> remainFilledLotPerOrderDict)
        {
            var     result = new MarginAndQuantityResult();
            decimal margin, quantity;
            var     physicalOrder = order as PhysicalOrder;
            bool    isPartialPaymentPhysicalOrder = physicalOrder != null && physicalOrder.IsPartialPaymentPhysicalOrder;

            order.CalculateMarginAndQuantity(isPartialPaymentPhysicalOrder, remainFilledLotPerOrderDict, out margin, out quantity);
            result.AddMarginAndQuantity(order.IsBuy, isPartialPaymentPhysicalOrder, margin, quantity);
            return(result);
        }
Esempio n. 2
0
        internal static MarginAndQuantityResult CalculateUnfilledMarginArgsForPlacePendingOrder(AccountClass.Instrument instrument, Transaction tran, bool isBuy, Dictionary <Guid, decimal> remainFilledLotPerOrderDict)
        {
            MarginAndQuantityResult result = new MarginAndQuantityResult();
            bool    isAutoClose            = tran.Owner.IsAutoClose || instrument.IsPhysical;
            decimal canAutoCloseLot        = CalculateUnfilledAutoCloseLot(instrument, tran, isBuy, isAutoClose, remainFilledLotPerOrderDict);
            Dictionary <Guid, decimal> unfilledLotPerTran = null;

            if (isAutoClose && canAutoCloseLot > 0)
            {
                decimal totalAutoCloseLot = FilledCalculator.CalculateTotalAutoCloseLot(instrument, isBuy, canAutoCloseLot, remainFilledLotPerOrderDict);
                unfilledLotPerTran = CalculateRemainUnfilledLotPerTransactionInSameDirection(instrument, isBuy, totalAutoCloseLot);
            }
            result = CalculateUnfilledMarginAndQuantity(instrument, isBuy, unfilledLotPerTran);
            return(result);
        }
        private static decimal InnerCalculatePreCheckNecessary(AccountClass.Instrument instrument, Transaction tran, bool isBuy)
        {
            MarginAndQuantityResult    unfilledArgs = new MarginAndQuantityResult();
            Dictionary <Guid, decimal> remainFilledLotPerOrderDict = new Dictionary <Guid, decimal>();

            if (tran.IsPending)
            {
                unfilledArgs = UnfilledCalculator.CalculateUnfilledMarginArgsForPlacePendingOrder(instrument, tran, isBuy, remainFilledLotPerOrderDict);
            }
            MarginAndQuantityResult filledArgs = new MarginAndQuantityResult();
            MarginAndQuantityResult marginArgs = new MarginAndQuantityResult();

            instrument.InitializeFilledAndMarginArgs(isBuy, unfilledArgs, filledArgs, marginArgs);
            filledArgs += FilledCalculator.CalculateFillMarginAndQuantity(instrument, isBuy, remainFilledLotPerOrderDict);
            return(CalculateNecessary(instrument, isBuy, marginArgs, filledArgs));
        }
Esempio n. 4
0
        internal static MarginAndQuantityResult CalculateFillMarginAndQuantity(AccountClass.Instrument instrument, bool isBuy, Dictionary <Guid, decimal> remainFilledLotPerOrderDict)
        {
            var result = new MarginAndQuantityResult();

            foreach (Transaction eachTran in instrument.GetTransactions())
            {
                foreach (Order eachOrder in eachTran.Orders)
                {
                    if (eachOrder.ShouldCalculateFilledMarginAndQuantity(isBuy))
                    {
                        result += eachOrder.CalculateFilledMarginAndQuantity(isBuy, remainFilledLotPerOrderDict);
                    }
                }
            }
            return(result);
        }
Esempio n. 5
0
        private static MarginAndQuantityResult CalculateUnfilledMarginAndQuantity(AccountClass.Instrument instrument, bool isBuy, Dictionary <Guid, decimal> unfilledLotsPerTran)
        {
            MarginAndQuantityResult result = new MarginAndQuantityResult();

            foreach (Transaction eachTran in instrument.GetTransactions())
            {
                if (eachTran.OrderCount == 0)
                {
                    continue;
                }
                decimal?unfilledLot;
                if (eachTran.ShouldCalculatePreCheckNecessary(instrument, isBuy, unfilledLotsPerTran, out unfilledLot))
                {
                    result += eachTran.CalculateUnfilledMarginAndQuantity(unfilledLot);
                }
            }
            return(result);
        }
Esempio n. 6
0
 internal void Add(bool isBuy, MarginAndQuantityResult unfilledResult, MarginAndQuantityResult filledResult)
 {
     if (isBuy)
     {
         var normalMargin    = new BuySellPair(unfilledResult.Normal.Margin.Buy, filledResult.Normal.Margin.Sell);
         var normalQuantity  = new BuySellPair(unfilledResult.Normal.Quantity.Buy, filledResult.Normal.Quantity.Sell);
         var partialMargin   = new BuySellPair(unfilledResult.Partial.Margin.Buy, filledResult.Partial.Margin.Sell);
         var partialQuantity = new BuySellPair(unfilledResult.Partial.Quantity.Buy, filledResult.Partial.Quantity.Sell);
         this.Normal  += new MarginAndQunatity(normalMargin, normalQuantity);
         this.Partial += new MarginAndQunatity(partialMargin, partialQuantity);
     }
     else
     {
         var margin          = new BuySellPair(filledResult.Normal.Margin.Buy, unfilledResult.Normal.Margin.Sell);
         var quantity        = new BuySellPair(filledResult.Normal.Quantity.Buy, unfilledResult.Normal.Margin.Sell);
         var partialMargin   = new BuySellPair(filledResult.Partial.Margin.Buy, unfilledResult.Partial.Margin.Sell);
         var partialQuantity = new BuySellPair(filledResult.Partial.Quantity.Buy, unfilledResult.Partial.Quantity.Sell);
         this.Normal  += new MarginAndQunatity(margin, quantity);
         this.Partial += new MarginAndQunatity(partialMargin, partialQuantity);
     }
 }
        private static decimal CalculateNecessary(AccountClass.Instrument instrument, bool isBuy, MarginAndQuantityResult marginArgs, MarginAndQuantityResult filledArgs)
        {
            MarginAndQuantityResult necessaryParams = new MarginAndQuantityResult();

            necessaryParams.Add(isBuy, marginArgs, filledArgs);
            decimal netNecessary   = 0m;
            decimal hedgeNecessary = 0m;
            decimal partialPaymentPhysicalNecessary = 0m;

            if (necessaryParams.PartialQuantity.Sell > 0)
            {
                partialPaymentPhysicalNecessary = necessaryParams.PartialMargin.Sell;
            }
            else if (necessaryParams.PartialQuantity.Buy > 0)
            {
                partialPaymentPhysicalNecessary = necessaryParams.PartialMargin.Buy;
            }
            instrument.CalculateNetAndHedgeNecessary(necessaryParams.Margin.Buy, necessaryParams.Margin.Sell,
                                                     necessaryParams.Quantity.Buy, necessaryParams.Quantity.Sell, partialPaymentPhysicalNecessary,
                                                     out netNecessary, out hedgeNecessary);
            return(netNecessary + hedgeNecessary);
        }
        private static void InitializeFilledAndMarginArgs(this AccountClass.Instrument instrument, bool isBuy, MarginAndQuantityResult unfilledArgs, MarginAndQuantityResult filledArgs, MarginAndQuantityResult marginArgs)
        {
            var         physicalInstrument = instrument as Physical.PhysicalInstrument;
            BuySellPair margin, quantity, partialMargin, partialQuantity;

            margin   = new BuySellPair(instrument.TotalBuyMargin, instrument.TotalSellMargin);
            quantity = new BuySellPair(instrument.TotalBuyQuantity, instrument.TotalSellQuantity);
            if (physicalInstrument != null)
            {
                partialMargin   = new BuySellPair(physicalInstrument.TotalBuyMarginForPartialPaymentPhysicalOrder, physicalInstrument.TotalSellMarginForPartialPaymentPhysicalOrder);
                partialQuantity = new BuySellPair(physicalInstrument.TotalBuyLotBalanceForPartialPaymentPhysicalOrder, physicalInstrument.TotalSellLotBalanceForPartialPaymentPhysicalOrder);
            }
            else
            {
                partialMargin   = BuySellPair.Empty;
                partialQuantity = BuySellPair.Empty;
            }
            filledArgs.Add(isBuy, margin, quantity, partialMargin, partialQuantity);
            marginArgs.Add(isBuy, unfilledArgs);
            marginArgs.Add(isBuy, margin, quantity, partialMargin, partialQuantity);
        }
Esempio n. 9
0
 internal void Add(MarginAndQuantityResult other)
 {
     this.Normal  += other.Normal;
     this.Partial += other.Partial;
 }
Esempio n. 10
0
 internal void Add(bool isBuy, MarginAndQuantityResult other)
 {
     this.Normal  = this.Normal.Add(isBuy, other.Normal);
     this.Partial = this.Partial.Add(isBuy, other.Partial);
 }