Ejemplo n.º 1
0
        internal DeliveryRequest(Account owner, Protocal.Physical.DeliveryRequestData node)
            : base(BusinessRecordNames.DeliveryRequest, DEFAULT_ITEMS_FACTOR)
        {
            this._owner = owner;
            var constructParams = this.Parse(node);

            this.Initialize(constructParams);
            owner.AddDeliveryRequest(this, OperationType.AsNewRecord);
        }
Ejemplo n.º 2
0
        private static void ParseDeliveryRequestSpecification(this Protocal.Physical.DeliveryRequestData deliveryRequest, XmlNode node)
        {
            XmlAttributeCollection attributes = node.Attributes;
            int     quantity = XmlConvert.ToInt32(attributes["Quantity"].Value);
            decimal size     = XmlConvert.ToDecimal(attributes["Size"].Value);

            deliveryRequest.Specifications.Add(new Protocal.Physical.DeliveryRequestSpecificationData {
                Size = size, Quantity = quantity
            });
        }
Ejemplo n.º 3
0
        private static void ParseDeliveryRequestOrderRelation(this Protocal.Physical.DeliveryRequestData deliveryRequest, XmlNode node)
        {
            var deliveryRequestOrderRelation  = new Protocal.Physical.DeliveryRequestOrderRelationData();
            XmlAttributeCollection attributes = node.Attributes;

            deliveryRequestOrderRelation.DeliveryRequestId = deliveryRequest.Id;
            deliveryRequestOrderRelation.OpenOrderId       = XmlConvert.ToGuid(attributes["OpenOrderId"].Value);
            deliveryRequestOrderRelation.DeliveryQuantity  = XmlConvert.ToDecimal(attributes["DeliveryQuantity"].Value);
            deliveryRequestOrderRelation.DeliveryLot       = XmlConvert.ToDecimal(attributes["DeliveryLot"].Value);
            deliveryRequest.OrderRelations.Add(deliveryRequestOrderRelation);
        }
Ejemplo n.º 4
0
 public iExchange.Common.TransactionError ApplyDelivery(Protocal.Physical.DeliveryRequestData data, out string code, out string balance, out string usableMargin)
 {
     code = balance = usableMargin = null;
     try
     {
         return(this.Service.ApplyDelivery(data, out code, out balance, out usableMargin));
     }
     catch (Exception ex)
     {
         this.RecoverConnection(ex);
         return(TransactionError.RuntimeError);
     }
 }
Ejemplo n.º 5
0
        private static void VerifyDeliveryLot(Protocal.Physical.DeliveryRequestData requestData)
        {
            decimal totalDeliveryLot = 0m;

            foreach (var eachRelation in requestData.OrderRelations)
            {
                totalDeliveryLot += eachRelation.DeliveryLot;
            }
            if (totalDeliveryLot != requestData.RequireLot)
            {
                string errorInfo = string.Format("totalDeliveryLotInRelation = {0}, requireLot = {1}, deliveryId = {2}",
                                                 totalDeliveryLot, requestData.RequireLot, requestData.Id);
                throw new TransactionServerException(TransactionError.InvalidRelation, errorInfo);
            }
        }
Ejemplo n.º 6
0
        private static void Verify(Account account, Protocal.Physical.DeliveryRequestData requestData)
        {
            if (requestData.OrderRelations == null || requestData.OrderRelations.Count == 0)
            {
                throw new TransactionServerException(TransactionError.InvalidOrderRelation, "Order relation is null or count = 0");
            }

            if ((account.Setting().TradePolicy()[requestData.InstrumentId, null].AllowedPhysicalTradeSides & PhysicalTradeSide.Delivery) != PhysicalTradeSide.Delivery)
            {
                throw new TransactionServerException(TransactionError.OrderTypeIsNotAcceptable);
            }
            VerifyDeliveryLot(requestData);
            foreach (var eachOrderRelation in requestData.OrderRelations)
            {
                var order = (PhysicalOrder)account.GetOrder(eachOrderRelation.OpenOrderId);
                if (order == null || !order.IsBuy || !order.IsOpen ||
                    (order.PhysicalTradeSide != PhysicalTradeSide.Buy && order.PhysicalTradeSide != PhysicalTradeSide.Deposit) ||
                    order.Instrument().Id != requestData.InstrumentId ||
                    (order.IsInstalment && !order.IsPayoff))
                {
                    throw new TransactionServerException(TransactionError.InvalidRelation);
                }

                Logger.InfoFormat("order id = {0}, lotBalance = {1}, lockLot = {2}, deliveryLot = {3}", order.Id, order.LotBalance, order.DeliveryLockLot, eachOrderRelation.DeliveryLot);

                if (order.LotBalance - order.DeliveryLockLot < eachOrderRelation.DeliveryLot)
                {
                    throw new TransactionServerException(TransactionError.ExceedOpenLotBalance);
                }
            }
            var tradingInstrument = account.GetOrCreateInstrument(requestData.InstrumentId);

            if (!account.HasEnoughMoneyToDelivery(tradingInstrument))
            {
                throw new TransactionServerException(TransactionError.MarginIsNotEnough);
            }

            if (requestData.Charge > 0)
            {
                account.AddBalance(requestData.ChargeCurrencyId, -requestData.Charge, null);
                decimal usableMargin = account.SumFund.Equity - account.SumFund.Necessary;
                if (account.SumFund.Balance < 0 || usableMargin < 0)
                {
                    throw new TransactionServerException(TransactionError.BalanceOrEquityIsShort);
                }
            }
        }
Ejemplo n.º 7
0
        private static void ParserRequestAttrs(this Protocal.Physical.DeliveryRequestData deliveryRequest, XmlNode node)
        {
            XmlAttributeCollection attributes = node.Attributes;

            deliveryRequest.Id               = XmlConvert.ToGuid(attributes["Id"].Value);
            deliveryRequest.AccountId        = XmlConvert.ToGuid(attributes["AccountId"].Value);
            deliveryRequest.InstrumentId     = XmlConvert.ToGuid(attributes["InstrumentId"].Value);
            deliveryRequest.RequireQuantity  = XmlConvert.ToDecimal(attributes["RequireQuantity"].Value);
            deliveryRequest.RequireLot       = XmlConvert.ToDecimal(attributes["RequireLot"].Value);
            deliveryRequest.DeliveryTime     = attributes["DeliveryTime"] == null ? DateTime.Now : XmlConvert.ToDateTime(attributes["DeliveryTime"].Value);
            deliveryRequest.Charge           = XmlConvert.ToDecimal(attributes["Charge"].Value);
            deliveryRequest.ChargeCurrencyId = XmlConvert.ToGuid(attributes["ChargeCurrencyId"].Value);
            if (attributes["DeliveryAddressId"] != null && !string.IsNullOrEmpty(attributes["DeliveryAddressId"].Value))
            {
                deliveryRequest.DeliveryAddressId = XmlConvert.ToGuid(attributes["DeliveryAddressId"].Value);
            }
        }
Ejemplo n.º 8
0
        internal DeliveryRequest Create(Account account, Protocal.Physical.DeliveryRequestData requestNode)
        {
            DeliveryRequest deliveryRequest = new DeliveryRequest(account, requestNode);

            if (requestNode.OrderRelations != null)
            {
                foreach (var eachOrderRelation in requestNode.OrderRelations)
                {
                    new DeliveryRequestOrderRelation(deliveryRequest, eachOrderRelation);
                }
            }

            if (requestNode.Specifications != null)
            {
                foreach (var eachSpecification in requestNode.Specifications)
                {
                    new DeliveryRequestSpecification(deliveryRequest, eachSpecification);
                }
            }
            return(deliveryRequest);
        }
Ejemplo n.º 9
0
        private DeliveryRequestConstructParams Parse(Protocal.Physical.DeliveryRequestData node)
        {
            var codeAndPritingCode = DeliveryCodeGenerator.Default.Create();
            DeliveryRequestConstructParams result = new DeliveryRequestConstructParams();

            result.Id                    = node.Id;
            result.Code                  = codeAndPritingCode.Item1;
            result.PrintingCode          = codeAndPritingCode.Item2;
            result.AccountId             = node.AccountId;
            result.InstrumentId          = node.InstrumentId;
            result.RequireQuantity       = node.RequireQuantity;
            result.RequireLot            = node.RequireLot;
            result.SubmitTime            = DateTime.Now;
            result.SubmitorId            = node.SubmitorId;
            result.DeliveryTime          = node.DeliveryTime;
            result.DeliveryRequestStatus = DeliveryRequestStatus.Accepted;
            result.Charge                = node.Charge;
            result.ChargeCurrencyId      = node.ChargeCurrencyId;
            result.DeliveryAddressId     = node.DeliveryAddressId;
            return(result);
        }
Ejemplo n.º 10
0
        internal static void ApplyDelivery(Account account, Protocal.Physical.DeliveryRequestData requestData)
        {
            var instrument = Settings.Setting.Default.GetInstrument(requestData.InstrumentId);

            if (string.IsNullOrEmpty(instrument.DeliveryPrice))
            {
                throw new TransactionServerException(TransactionError.InvalidPrice);
            }
            Verify(account, requestData);
            DeliveryRequest request = DeliveryRequestManager.Default.Create(account, requestData);

            request.InitPrice(instrument.DeliveryPrice);
            foreach (var eachRelation in requestData.OrderRelations)
            {
                var order = (PhysicalOrder)account.GetOrder(eachRelation.OpenOrderId);
                order.LockForDelivery(eachRelation.DeliveryLot);
            }
            var tradingInstrument = account.GetOrCreateInstrument(requestData.InstrumentId);

            request.DeliveryRequestStatus = DeliveryRequestStatus.Accepted;
            DeliveryRequestManager.Default.Add(request);
        }
Ejemplo n.º 11
0
 internal TransactionError ApplyDelivery(Protocal.Physical.DeliveryRequestData requestData)
 {
     _readWriteLock.EnterReadLock();
     try
     {
         var account          = GetAccount(requestData.AccountId);
         var transactionError = account.ApplyDelivery(requestData);
         if (transactionError != TransactionError.OK)
         {
             account.RejectChanges();
         }
         return(transactionError);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         return(TransactionError.RuntimeError);
     }
     finally
     {
         _readWriteLock.ExitReadLock();
     }
 }
Ejemplo n.º 12
0
        internal static Protocal.Physical.DeliveryRequestData Parser(XmlNode node)
        {
            var result = new Protocal.Physical.DeliveryRequestData();

            result.OrderRelations = new List <Protocal.Physical.DeliveryRequestOrderRelationData>();
            result.Specifications = new List <Protocal.Physical.DeliveryRequestSpecificationData>();
            result.ParserRequestAttrs(node);
            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (childNode.Name.Equals("DeliveryRequestOrderRelation", StringComparison.InvariantCultureIgnoreCase))
                {
                    result.ParseDeliveryRequestOrderRelation(childNode);
                }
                if (childNode.Name.Equals("DeliveryRequestSpecifications", StringComparison.InvariantCultureIgnoreCase))
                {
                    foreach (XmlNode deliveryRequestSpecificationNode in childNode.ChildNodes)
                    {
                        result.ParseDeliveryRequestSpecification(deliveryRequestSpecificationNode);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 13
0
 internal TransactionError ApplyDelivery(Protocal.Physical.DeliveryRequestData requestData, out string code, out string balance, out string usableMargin)
 {
     _readWriteLock.EnterReadLock();
     code = balance = usableMargin = null;
     try
     {
         var account          = GetAccount(requestData.AccountId);
         var transactionError = account.ApplyDelivery(requestData, out code, out balance, out usableMargin);
         if (transactionError != TransactionError.OK)
         {
             account.RejectChanges();
         }
         return(transactionError);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         return(TransactionError.RuntimeError);
     }
     finally
     {
         _readWriteLock.ExitReadLock();
     }
 }
Ejemplo n.º 14
0
 public TransactionError ApplyDelivery(Protocal.Physical.DeliveryRequestData requestData)
 {
     return(ServerFacade.Default.Server.ApplyDelivery(requestData));
 }
Ejemplo n.º 15
0
 public iExchange.Common.TransactionError ApplyDelivery(Protocal.Physical.DeliveryRequestData data)
 {
     return(this.Call <TransactionError>(() => this.Service.ApplyDelivery(data)));
 }