示例#1
0
        public async Task <IEnumerable <OperationResult> > CancelOrders(string accountId, string instrument, int numOrders)
        {
            var operations          = new List <OperationResult>();
            var resGetOpenPositions = await GetOpenPositionsFromDemo();

            operations.Add(resGetOpenPositions);

            var orders = ((OrderClientContract[])resGetOpenPositions.Result)
                         .Where(x => x.AccountId == accountId && x.Instrument == instrument && x.Status == 0).ToList();
            var processed = 0;

            foreach (var order in orders)
            {
                if (processed >= numOrders)
                {
                    break;
                }
                try
                {
                    LogInfo($"Canceling order {processed + 1}/{numOrders}: [{instrument}] Id={order.Id} Fpl={order.Fpl}");
                    var res = new OperationResult
                    {
                        Operation = "CancelOrders",
                        StartDate = DateTime.UtcNow
                    };
                    var request = new CloseOrderRpcClientRequest
                    {
                        OrderId   = order.Id,
                        AccountId = order.AccountId,
                        Token     = _token
                    };
                    var ordercanceled = await _service.CancelOrder(request.ToJson());

                    res.EndDate = DateTime.UtcNow;
                    res.Result  = ordercanceled;
                    operations.Add(res);

                    LogInfo(ordercanceled.Result
                        ? $";{res.Duration};Order Canceled Id={order.Id}"
                        : $";{res.Duration};Order Cancel Failed Id={order.Id} Message:{ordercanceled.Message}");
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
                processed++;
                // Sleep TransactionFrequency
                Thread.Sleep(GetRandomTransactionInterval());
            }

            if (processed < numOrders)
            {
                LogWarning($"Not enough orders to close requested amount {numOrders}");
            }

            return(operations);
        }
示例#2
0
文件: MtClient.cs 项目: wildbunny/MT
        public async Task CancelOrder()
        {
            var subscription = _realmProxy.Services.GetSubject <NotifyResponse>($"user.updates.{_notificationId}")
                               .Subscribe(info =>
            {
                if (info.Order != null)
                {
                    Console.WriteLine($"Order status: {info.Order.Status}");
                }
            });

            while (true)
            {
                var orders = await _service.GetOpenPositions(_token);

                if (orders.Demo.Any())
                {
                    var order = orders.Demo.First();

                    var request = new CloseOrderRpcClientRequest
                    {
                        OrderId = order.Id,
                        Token   = _token
                    };

                    var result = _service.CancelOrder(request.ToJson());
                    break;
                }

                Thread.Sleep(200);
            }

            Console.WriteLine("Press enter");
            Console.ReadLine();
            subscription.Dispose();
        }