예제 #1
0
 private static bool IsFokTransaction(long orderId, string message)
 {
     return(orderId != 0 && TransactionHelper.IfFOKCancelMessage(message));
 }
예제 #2
0
        /// <summary>
        /// Зарегистрировать транзакцию.
        /// </summary>
        /// <param name="transaction">Транзакция.</param>
        public void RegisterTransaction(Transaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            if (transaction.GetInstruction <long>(Transaction.TransactionId) != 0)
            {
                throw new ArgumentException();
            }

            var transactionId = transaction.TryGetTransactionId();

            if (transactionId == 0)
            {
                transactionId = TransactionIdGenerator.GetNextId();
            }

            if (transactionId <= 0 || transactionId > uint.MaxValue)
            {
                throw new InvalidOperationException(LocalizedStrings.Str1700Params.Put(transactionId));
            }

            FormatTransaction?.Invoke(transaction.SetTransactionId(transactionId));

            _transactions.Add(transactionId, transaction);

            if (IsAsyncMode)
            {
                Api.SendAsyncTransaction(transaction.ToString());
            }
            else
            {
                Exception error = null;

                // http://stocksharp.com/forum/yaf_postst2247_Oshibka-pri-kotirovanii--sinkhronnyie-tranzaktsii.aspx

                var execution = transaction.Message.CreateReply();

                if (execution == null)
                {
                    throw new ArgumentException(LocalizedStrings.Str1835, nameof(transaction));
                }

                var isReRegistering = transaction.TransactionType == TransactionTypes.ReRegister;
                var isRegistering   = transaction.TransactionType == TransactionTypes.Register || isReRegistering;

                var apiMessage = "";

                try
                {
                    long        orderId;
                    uint        transId;
                    OrderStatus status;

                    var transactionTxt = transaction.ToString();

                    Api.SendSyncTransaction(transactionTxt, out status, out transId, out orderId, out apiMessage);

                    var isMatchOrCancel = (transaction.Message.Type == MessageTypes.OrderRegister || transaction.Message.Type == MessageTypes.OrderReplace) &&
                                          ((OrderRegisterMessage)transaction.Message).TimeInForce == TimeInForce.MatchOrCancel;

                    if ((!isMatchOrCancel && status != OrderStatus.Accepted) || (isMatchOrCancel && !TransactionHelper.IfFOKCancelMessage(apiMessage) && orderId == 0))
                    {
                        throw new InvalidOperationException(LocalizedStrings.Str1836Params.Put(transactionTxt, apiMessage));
                    }

                    execution.OrderStatus   = status;
                    execution.SystemComment = apiMessage;

                    if (isRegistering)
                    {
                        ProcessTransactionReply(execution, transaction, orderId, apiMessage, Codes.Success, null);
                    }
                }
                catch (Exception ex)
                {
                    var apiEx = ex as ApiException;

                    if (isRegistering)
                    {
                        ProcessTransactionReply(execution, transaction, 0, apiMessage, apiEx?.Code ?? Codes.Failed, ex);
                    }
                    else
                    {
                        execution.OrderState = OrderStates.Failed;
                        execution.Error      = apiEx ?? new ApiException(Codes.Failed, apiMessage);
                        SendOutMessage(execution);
                    }

                    error = ex;
                }

                error?.Throw();
            }
        }