Esempio n. 1
0
        public override async Task <ExecutionReport> CancelOrderAndWaitExecution(TradingSignal signal, TranslatedSignalTableEntity translatedSignal, TimeSpan timeout)
        {
            var result = await privateData.CancelOrder(signal.OrderId, translatedSignal);

            var executedTrade = new ExecutionReport(signal.Instrument,
                                                    DateTime.UtcNow,
                                                    signal.Price ?? 0,
                                                    signal.Volume,
                                                    signal.TradeType,
                                                    signal.OrderId,
                                                    result.Pending ? OrderExecutionStatus.Pending : OrderExecutionStatus.Cancelled);

            translatedSignal.SetExecutionResult(executedTrade);

            return(executedTrade);
        }
Esempio n. 2
0
        public async Task <ExecutionReport> Post([FromBody] OrderModel orderModel)
        {
            try
            {
                if (orderModel == null)
                {
                    throw new StatusCodeException(HttpStatusCode.BadRequest, "Order has to be specified");
                }
                if (string.IsNullOrEmpty(orderModel.ExchangeName))
                {
                    ModelState.AddModelError(nameof(orderModel.ExchangeName), "Exchange cannot be null");
                }

                if (Math.Abs((orderModel.DateTime - DateTime.UtcNow).TotalMilliseconds) >=
                    TimeSpan.FromMinutes(5).TotalMilliseconds)
                {
                    ModelState.AddModelError(nameof(orderModel.DateTime),
                                             "Date and time must be in 5 minutes threshold from UTC now");
                }

                if (orderModel.Price == 0 && orderModel.OrderType != OrderType.Market)
                {
                    ModelState.AddModelError(nameof(orderModel.Price),
                                             "Price have to be declared for non-market orders");
                }

                if (!ModelState.IsValid)
                {
                    throw new StatusCodeException(HttpStatusCode.BadRequest)
                          {
                              Model = new SerializableError(ModelState)
                          }
                }
                ;


                var instrument    = new Instrument(orderModel.ExchangeName, orderModel.Instrument);
                var tradingSignal = new TradingSignal(instrument, GetUniqueOrderId(orderModel), OrderCommand.Create, orderModel.TradeType,
                                                      orderModel.Price, orderModel.Volume, DateTime.UtcNow,
                                                      orderModel.OrderType, orderModel.TimeInForce);

                var translatedSignal = new TranslatedSignalTableEntity(SignalSource.RestApi, tradingSignal)
                {
                    ClientIP = HttpContext.Connection.RemoteIpAddress.ToString()
                };

                try
                {
                    var result = await Application.GetExchange(orderModel.ExchangeName)
                                 .AddOrderAndWaitExecution(tradingSignal, translatedSignal, _timeout);

                    translatedSignal.SetExecutionResult(result);

                    return(result);
                }
                catch (Exception e)
                {
                    translatedSignal.Failure(e);
                    throw;
                }
                finally
                {
                    await _translatedSignalsRepository.SaveAsync(translatedSignal);
                }
            }
            catch (StatusCodeException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new StatusCodeException(HttpStatusCode.InternalServerError, e.Message, e);
            }
        }