Example #1
0
        internal static decimal CalculateTotalAutoCloseLot(AccountClass.Instrument instrument, bool isBuy, decimal unfilledLot, Dictionary <Guid, decimal> remainLotsDict)
        {
            decimal result            = 0m;
            decimal remainUnfilledLot = unfilledLot;

            foreach (Transaction eachTran in instrument.GetTransactions())
            {
                if (remainUnfilledLot <= 0)
                {
                    break;
                }
                foreach (Order eachOrder in eachTran.Orders)
                {
                    if (eachOrder.IsBuy != isBuy && eachOrder.IsOpen && eachOrder.Phase == OrderPhase.Executed)
                    {
                        var     items        = FilledCalculator.CalculateOrderCanCloseLot(eachOrder, unfilledLot, remainLotsDict);
                        decimal canClosedLot = items.Item1;
                        decimal remainLot    = items.Item2;
                        remainLotsDict[eachOrder.Id] = remainLot - canClosedLot;
                        remainUnfilledLot           -= canClosedLot;
                        result += canClosedLot;
                        if (remainUnfilledLot <= 0)
                        {
                            break;
                        }
                    }
                }
            }
            return(result);
        }
Example #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));
        }