Example #1
0
        private static decimal CalculateUnfilledAutoCloseLot(AccountClass.Instrument instrument, Transaction tran, bool isBuy, bool isAutoClose, Dictionary <Guid, decimal> remainFilledLotPerOrderDict)
        {
            decimal result = 0m;

            foreach (Transaction eachTran in instrument.GetTransactions())
            {
                if (!eachTran.ShouldSumPlaceMargin())
                {
                    continue;
                }
                bool isHandledOCOTran = false;
                foreach (Order eachOrder in eachTran.Orders)
                {
                    if (isHandledOCOTran)
                    {
                        continue;
                    }
                    isHandledOCOTran = eachTran.Type == TransactionType.OneCancelOther;
                    if (eachOrder.IsBuy != isBuy)
                    {
                        continue;
                    }
                    if (!eachOrder.IsOpen)
                    {
                        CalculateRemainFilledLotPerOpenOrder(eachOrder, remainFilledLotPerOrderDict);
                    }
                    else if (isAutoClose)
                    {
                        result += eachOrder.Lot;
                    }
                }
            }
            return(result);
        }
Example #2
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);
        }
 internal static IEnumerable <ResetOrder> GetOrders(this AccountClass.Instrument instrument, DateTime tradeDay)
 {
     foreach (var eachTran in instrument.GetTransactions())
     {
         foreach (var eachOrder in eachTran.Orders)
         {
             var orderDayHistory = ResetManager.Default.GetOrderDayHistory(eachOrder.Id, tradeDay);
             if (eachOrder.IsOpen && eachOrder.LotBalance > 0 && eachOrder.Phase == OrderPhase.Executed && orderDayHistory != null && orderDayHistory.LotBalance > 0)
             {
                 yield return(new ResetOrder(eachOrder));
             }
         }
     }
 }
Example #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);
        }
Example #5
0
        protected virtual BuySellLot CalculateSameDirectionLotsOfPendingOrders()
        {
            if (!_tran.IsPending)
            {
                return(BuySellLot.Empty);
            }
            BuySellLot result = BuySellLot.Empty;

            foreach (Transaction eachTran in _instrument.GetTransactions())
            {
                if (this.ShouldSumPlaceMargin(eachTran))
                {
                    result += this.CalculateLotBalanceForPreCheck();
                }
            }
            return(result);
        }
Example #6
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);
        }
Example #7
0
        internal static TradePolicyDetail Get(AccountClass.Instrument instrument)
        {
            var result = instrument.TradePolicyDetail();

            if (result != null)
            {
                return(result);
            }
            var trans = instrument.GetTransactions();

            if (trans.Count == 0)
            {
                throw new NullReferenceException(string.Format("tradePolicyDetail not found, because instrument's tranCount = 0, instrumentId = {0}, accountId = {1}", instrument.Id, instrument.Owner.Id));
            }
            var order = trans[0].FirstOrder;

            return(GetCommon(order.Id, instrument.Owner));
        }
Example #8
0
        private static Dictionary <Guid, decimal> CalculateRemainUnfilledLotPerTransactionInSameDirection(AccountClass.Instrument instrument, bool isBuy, decimal totalAutoCloseLot)
        {
            Dictionary <Guid, decimal> result = null;

            foreach (Transaction eachTran in instrument.GetTransactions())
            {
                if (totalAutoCloseLot <= 0)
                {
                    break;
                }
                if (!eachTran.ShouldSumPlaceMargin())
                {
                    continue;
                }
                bool isHandledOCOTran = false;
                foreach (Order eachOrder in eachTran.Orders)
                {
                    if (isHandledOCOTran)
                    {
                        continue;
                    }
                    isHandledOCOTran = eachTran.Type == TransactionType.OneCancelOther;
                    if (eachOrder.IsBuy == isBuy && eachOrder.IsOpen)
                    {
                        decimal canFilledLot = Math.Min(totalAutoCloseLot, eachOrder.Lot);
                        decimal unfilledLot  = eachOrder.Lot - canFilledLot;
                        if (result == null)
                        {
                            result = new Dictionary <Guid, decimal>();
                        }
                        result[eachTran.Id] = unfilledLot;
                        totalAutoCloseLot  -= canFilledLot;
                        if (totalAutoCloseLot <= 0)
                        {
                            break;
                        }
                    }
                }
            }
            return(result);
        }