示例#1
0
 public void Add(DeliveryRequest deliveryRequest)
 {
     lock (_mutex)
     {
         this._deliveryRequestDict.Add(deliveryRequest.Id, deliveryRequest);
     }
 }
示例#2
0
 internal void Remove(DeliveryRequest deliveryRequest)
 {
     lock (_mutex)
     {
         this._deliveryRequestDict.Remove(deliveryRequest.Id);
     }
 }
示例#3
0
 internal bool TryGet(Guid deliveryRequestId, out DeliveryRequest deliveryRequest)
 {
     lock (_mutex)
     {
         return(this._deliveryRequestDict.TryGetValue(deliveryRequestId, out deliveryRequest));
     }
 }
示例#4
0
        private static bool CancelDeliveryWithShortSellOrders(DeliveryRequest deliveryRequest)
        {
            List <Transaction> executedTransactions = new List <Transaction>();
            List <Transaction> transactions         = new List <Transaction>();

            Account          account           = TradingSetting.Default.GetAccount(deliveryRequest.AccountId);
            var              instrument        = Settings.Setting.Default.GetInstrument(deliveryRequest.InstrumentId);
            var              tradePolicyDetail = Settings.Setting.Default.GetTradePolicy(account.Setting().TradePolicyId)[instrument.Id, null];
            TransactionError error             = TransactionError.OK;

            foreach (DeliveryRequestOrderRelation relation in deliveryRequest.DeliveryRequestOrderRelations)
            {
                Order openOrder = account.GetOrder(relation.OpenOrderId);
                transactions.Add(account.CreateCloseTranAndOrder(openOrder, relation, deliveryRequest));

                transactions.Add(account.CreateOpenTranAndOrder(openOrder, relation, deliveryRequest));
            }

            foreach (var eachTran in transactions)
            {
                eachTran.ExecuteDirectly(new Engine.ExecuteContext(account.Id, eachTran.Id, Engine.ExecuteStatus.Filled, eachTran.CreateOrderPriceInfo())
                {
                    IsFreeFee = true, IsFreeValidation = true
                });
            }

            return(error == TransactionError.OK);
        }
示例#5
0
 internal DeliveryRequestSpecification(DeliveryRequest owner, Protocal.Physical.DeliveryRequestSpecificationData specification)
     : base("DeliveryRequestSpecification", 2)
 {
     owner.AddDeliveryRequestSpecification(this, OperationType.AsNewRecord);
     _quantity = BusinessItemFactory.Create("Quantity", specification.Quantity, PermissionFeature.ReadOnly, this);
     _size     = BusinessItemFactory.Create("Size", specification.Size, PermissionFeature.ReadOnly, this);
     _unitId   = BusinessItemFactory.Create("UnitId", specification.UnitId, PermissionFeature.ReadOnly, this);
 }
示例#6
0
        internal DeliveryRequestOrderRelation(DeliveryRequest request, Protocal.Physical.DeliveryRequestOrderRelationData orderRelationData)
            : base(BusinessRecordNames.DeliveryRequestOrderRelation, DEFAULT_ITEMS_FACTOR)
        {
            this._owner = request;
            var key = orderRelationData.OpenOrderId;

            this._deliveryRequestId = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.DeliveryRequestId, request.Id, PermissionFeature.Key, this);
            this._openOrderId       = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.OpenOrderId, key, PermissionFeature.Key, this);
            this._deliveryQuantity  = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.DeliveryQuantity, orderRelationData.DeliveryQuantity, PermissionFeature.Dumb, this);
            this._deliveryLot       = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.DeliveryLot, orderRelationData.DeliveryLot, PermissionFeature.Dumb, this);
            request.AddDeliveryRequestOrderRelation(this, OperationType.AsNewRecord);
        }
示例#7
0
        internal DeliveryRequestOrderRelation(DeliveryRequest request, IDBRow dataRow)
            : base(BusinessRecordNames.DeliveryRequestOrderRelation, DEFAULT_ITEMS_FACTOR)
        {
            this._owner = request;
            var key = (Guid)dataRow["OpenOrderId"];

            this._deliveryRequestId = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.DeliveryRequestId, (Guid)dataRow["DeliveryRequestId"], PermissionFeature.Key, this);
            this._openOrderId       = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.OpenOrderId, key, PermissionFeature.Key, this);
            this._deliveryQuantity  = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.DeliveryQuantity, (decimal)dataRow["DeliveryQuantity"], PermissionFeature.Dumb, this);
            this._deliveryLot       = BusinessItemFactory.Create(DeliveryRequestRelationBusinessItemNames.DeliveryLot, (decimal)dataRow["DeliveryLot"], PermissionFeature.Dumb, this);
            request.AddDeliveryRequestOrderRelation(this, OperationType.None);
        }
示例#8
0
        internal static bool CancelDelivery(Guid deliveryRequestId, Account account, out int status)
        {
            Logger.InfoFormat("CancelDelivery deliveryRequestId = {0} accountId = {1}", deliveryRequestId, account.Id);

            status = (int)DeliveryRequestStatus.Cancelled;
            try
            {
                bool            result                   = false;
                DeliveryRequest deliveryRequest          = DeliveryRequestManager.Default[deliveryRequestId];
                bool            hasFilledShortSellOrders = account.HasFilledShortSellOrders(deliveryRequest.InstrumentId);
                if (hasFilledShortSellOrders)
                {
                    result = CancelDeliveryWithShortSellOrders(deliveryRequest);
                }
                else
                {
                    result = deliveryRequest.Cancel();
                }

                status = (int)deliveryRequest.DeliveryRequestStatus;
                if (result)
                {
                    if (!hasFilledShortSellOrders)
                    {
                        foreach (DeliveryRequestOrderRelation deliveryRequestOrderRelation in deliveryRequest.DeliveryRequestOrderRelations)
                        {
                            Physical.PhysicalOrder order = (Physical.PhysicalOrder)account.GetOrder(deliveryRequestOrderRelation.OpenOrderId);
                            order.LockForDelivery(-deliveryRequestOrderRelation.DeliveryLot);
                        }

                        if (deliveryRequest.Charge != 0)
                        {
                            account.AddBalance(deliveryRequest.ChargeCurrencyId.Value, deliveryRequest.Charge, null);
                            Broadcaster.Default.Add(BroadcastBLL.CommandFactory.CreateUpdateBalanceCommand(account.Id, deliveryRequest.ChargeCurrencyId.Value, deliveryRequest.Charge, Protocal.ModifyType.Add));
                        }
                    }

                    DeliveryRequestManager.Default.Remove(deliveryRequest);
                }
                return(result);
            }
            catch (Exception e)
            {
                Logger.ErrorFormat(deliveryRequestId.ToString() + " failed:\r\n" + e.ToString());
                return(false);
            }
        }
示例#9
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);
        }
示例#10
0
        private static Transaction CreateOpenTranAndOrder(this Account account, Order openOrder, DeliveryRequestOrderRelation relation, DeliveryRequest deliveryRequest)
        {
            Transaction transaction = TransactionFacade.CreateCancelDeliveryWithShortSellTran(account, deliveryRequest.InstrumentId, openOrder.Owner.ContractSize(null));
            CancelDeliveryWithShortSellOrderParam param = new CancelDeliveryWithShortSellOrderParam
            {
                IsBuy             = true,
                IsOpen            = true,
                SetPrice          = openOrder.SetPrice,
                ExecutePrice      = openOrder.ExecutePrice,
                Lot               = relation.DeliveryLot,
                LotBalance        = relation.DeliveryLot,
                PhysicalRequestId = deliveryRequest.Id,
                TradeOption       = openOrder.TradeOption
            };
            Order order = OrderFacade.Default.CreateCancelDeliveryWithShortSellOrder(transaction, param);

            return(transaction);
        }
示例#11
0
        private static Transaction CreateCloseTranAndOrder(this Account account, Order openOrder, DeliveryRequestOrderRelation relation, DeliveryRequest deliveryRequest)
        {
            Transaction transaction                     = TransactionFacade.CreateCancelDeliveryWithShortSellTran(account, deliveryRequest.InstrumentId, openOrder.Owner.ContractSize(null));
            var         orderRelationRecord             = new OrderRelationRecord(account.GetOrder(relation.OpenOrderId), relation.DeliveryLot);
            CancelDeliveryWithShortSellOrderParam param = new CancelDeliveryWithShortSellOrderParam
            {
                IsBuy             = false,
                IsOpen            = false,
                SetPrice          = openOrder.SetPrice,
                ExecutePrice      = openOrder.ExecutePrice,
                Lot               = relation.DeliveryLot,
                LotBalance        = 0m,
                PhysicalRequestId = deliveryRequest.Id,
                TradeOption       = iExchange.Common.TradeOption.Invalid,
                OrderRelations    = new List <OrderRelationRecord>()
                {
                    orderRelationRecord
                }
            };
            Order order = OrderFacade.Default.CreateCancelDeliveryWithShortSellOrder(transaction, param);

            return(transaction);
        }