Exemplo n.º 1
0
        internal static List <Physical.PhysicalOrder> GetInstalmentOrders(this Account account)
        {
            var result = new List <Physical.PhysicalOrder>(4);

            foreach (var eachTran in account.Transactions)
            {
                if (!eachTran.IsPhysical)
                {
                    continue;
                }
                foreach (var eachOrder in eachTran.Orders)
                {
                    Physical.PhysicalOrder physicalOrder = eachOrder as Physical.PhysicalOrder;
                    if (physicalOrder == null)
                    {
                        throw new OrderConvertException(account.Id, eachOrder.Instrument().Id, eachOrder.Id, string.Format("tranId={0}, tran.instrumentId={1}", eachTran.Id, eachTran.InstrumentId));
                    }
                    if (physicalOrder.Instalment != null && physicalOrder.Instalment.InstalmentType != InstalmentType.FullAmount && Math.Abs(physicalOrder.PaidAmount) < physicalOrder.PhysicalOriginValue)
                    {
                        result.Add(physicalOrder);
                    }
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        private OrderResetResult CreateResetResult(Order order, Dictionary <Guid, TradeDayCommonResult> commonDict, Dictionary <Guid, OpenOrderPLOfCurrentDay> openOrderPLOfCurrentDayDict, Dictionary <Guid, InterestStorage> notValuedPLDict, Dictionary <Guid, InterestStorage> valuedPLDict)
        {
            OrderResetResult     resetResult  = new OrderResetResult();
            TradeDayCommonResult commonResult = null;

            if (commonDict != null)
            {
                commonDict.TryGetValue(order.Id, out commonResult);
            }

            OpenOrderPLOfCurrentDay openOrderPLOfCurrentDay = null;

            if (openOrderPLOfCurrentDayDict != null)
            {
                openOrderPLOfCurrentDayDict.TryGetValue(order.Id, out openOrderPLOfCurrentDay);
            }

            InterestStorage interestStoragePLNotValued = InterestStorage.Empty;

            if (notValuedPLDict != null)
            {
                notValuedPLDict.TryGetValue(order.Id, out interestStoragePLNotValued);
            }

            InterestStorage interestStoragePLValued = InterestStorage.Empty;

            if (valuedPLDict != null)
            {
                valuedPLDict.TryGetValue(order.Id, out interestStoragePLValued);
            }
            resetResult.TradeDay   = _info.TradeDay;
            resetResult.OrderId    = order.Id;
            resetResult.LotBalance = order.LotBalance;
            resetResult.CurrencyId = _info.RateSetting.CurrencyId;
            Price livePrice = order.IsBuy ? _info.Settings.SellPrice : _info.Settings.BuyPrice;

            resetResult.Margin = Calculator.MarginCalculator.CalculateMargin((int)_info.Instrument.MarginFormula, order.LotBalance, order.Owner.ContractSize(_info.TradeDay), order.ExecutePrice, livePrice, _info.RateSetting.RateIn, _info.RateSetting.RateOut, _info.RateSetting.RoundDecimals.Common, _info.RateSetting.RoundDecimals.Instrument);

            resetResult.PerLot = commonResult == null ? InterestStorage.Empty : new InterestStorage(commonResult.InterestPerLot, commonResult.StoragePerLot);

            resetResult.FloatPL = commonResult == null ? InterestStorage.Empty : new InterestStorage(_exchanger.ExchangeByCommonWithInstrumentSourceDecimals(order.LotBalance * commonResult.InterestPerLot), _exchanger.ExchangeByCommonWithInstrumentSourceDecimals(order.LotBalance * commonResult.StoragePerLot));

            resetResult.DayNotValuedPL = openOrderPLOfCurrentDay == null ? InterestStorage.Empty : new InterestStorage(openOrderPLOfCurrentDay.DayNotValued);

            resetResult.NotValuedPL = interestStoragePLNotValued + (openOrderPLOfCurrentDay == null ? InterestStorage.Empty : openOrderPLOfCurrentDay.NotValued);

            resetResult.ValuedPL = interestStoragePLValued + (openOrderPLOfCurrentDay == null ? InterestStorage.Empty : openOrderPLOfCurrentDay.Valued);

            if (_info.Instrument.Category == InstrumentCategory.Physical)
            {
                Physical.PhysicalOrder physicalOrder = (Physical.PhysicalOrder)order;
                resetResult.PhysicalPaidAmount         = physicalOrder.PaidAmount;
                resetResult.PaidPledgeBalance          = physicalOrder.PaidPledgeBalance;
                resetResult.PhysicalOriginValueBalance = physicalOrder.PhysicalOriginValueBalance;
                resetResult.InstalmentInterest         = commonResult == null ? 0m : commonResult.InstalmentInterest;
            }
            return(resetResult);
        }
Exemplo n.º 3
0
 internal static void VerifyInstalmentPolicy(Physical.PhysicalOrder order)
 {
     if (order.Instalment != null)
     {
         InstalmentPolicyDetail instalmentPolicyDetail = order.Instalment.InstalmentPolicyDetail(null);
         if (!instalmentPolicyDetail.IsActive)
         {
             throw new TransactionServerException(TransactionError.OrderTypeIsNotAcceptable, "Instalment policy is not active");
         }
     }
 }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
        private static void UpdateInstalmentOrderDebitInterest(Transaction tran, Settings.Setting setting, DateTime?tradeDay)
        {
            if (!tran.IsPhysical)
            {
                return;
            }
            DateTime lastTradeDay = DateTime.Now.Date;
            int      currencyDecimals;

            if (tran.Owner.IsMultiCurrency)
            {
                var currency = setting.GetCurrency(tran.CurrencyId);
                currencyDecimals = currency.Decimals;
            }
            else
            {
                currencyDecimals = tran.Owner.Setting(tradeDay).Currency(tradeDay).Decimals;
            }
            foreach (var eachOrder in tran.Orders)
            {
                Physical.PhysicalOrder physicalOrder = (Physical.PhysicalOrder)eachOrder;
                if (physicalOrder.Instalment == null)
                {
                    return;
                }
                var instalmentPolicyDetail = physicalOrder.Instalment.InstalmentPolicyDetail(null);
                foreach (var eachInstalmentDetail in physicalOrder.Instalment.InstalmentDetails)
                {
                    if (eachInstalmentDetail.PaymentDateTimeOnPlan >= lastTradeDay)
                    {
                        continue;
                    }
                    eachInstalmentDetail.DebitInterest = InstalmentManager.CalculateDebitInterest(eachInstalmentDetail.Principal,
                                                                                                  eachInstalmentDetail.Interest, (lastTradeDay - eachInstalmentDetail.PaymentDateTimeOnPlan.Value).Days,
                                                                                                  instalmentPolicyDetail.InterestRate, instalmentPolicyDetail.DebitInterestType,
                                                                                                  instalmentPolicyDetail.DebitInterestRatio, instalmentPolicyDetail.DebitFreeDays, tran.SettingInstrument().InterestYearDays, currencyDecimals);
                }
            }
        }
Exemplo n.º 6
0
 internal AddPhysicalInstalmentTransactionCommand(Account account, Transaction oldTran, Guid sourceOrderId, Physical.PhysicalOrder oldOrder, bool isBuy, bool isOpen, decimal lot)
     : base(account, Visitors.AddPhysicalInstalmentTransactionCommandVisitor.Default, AddPhysicalTransactionCommandService.Default)
 {
     this.OldTran       = oldTran;
     this.SourceOrderId = sourceOrderId;
     this.OldOrder      = oldOrder;
     this.IsBuy         = isBuy;
     this.IsOpen        = isOpen;
     this.Lot           = lot;
 }
Exemplo n.º 7
0
 internal AddBookPhysicalOrderRelationCommand(Physical.PhysicalOrder closeOrder, Protocal.OrderRelationBookData orderRelationData)
     : base(closeOrder, AddPhysicalOrderRelationCommandService.Default, orderRelationData)
 {
 }
Exemplo n.º 8
0
 internal AddPhysicalDoneOrderCommand(Transaction tran, Physical.PhysicalOrder openOrder, Price closePrice, TradeOption tradeOption)
     : base(tran, AddPhysicalOrderCommandService.Default, openOrder, closePrice, tradeOption)
 {
 }
 internal AddGeneralPhysicalOrderRelationCommand(Physical.PhysicalOrder closeOrder, Physical.PhysicalOrder openOrder, decimal closedLot)
     : base(closeOrder, AddPhysicalOrderRelationCommandService.Default, openOrder, closedLot)
 {
 }
Exemplo n.º 10
0
 internal AddDataRowPhysicalOrderRelationCommand(Physical.PhysicalOrder closeOrder, IDBRow dr)
     : base(closeOrder, AddPhysicalOrderRelationCommandService.Default, dr)
 {
 }
Exemplo n.º 11
0
 internal PhysicalOrderPreCheckService(Physical.PhysicalOrder order)
     : base(order)
 {
 }
Exemplo n.º 12
0
 internal Commands.AddTranCommandBase CreateInstalmentTransaction(Account account, Transaction oldTran, Guid sourceOrderId, Physical.PhysicalOrder oldOrder, bool isBuy, bool isOpen, decimal lot)
 {
     return(new Commands.AddPhysicalInstalmentTransactionCommand(account, oldTran, sourceOrderId, oldOrder, isBuy, isOpen, lot));
 }