예제 #1
0
 private async Task HandleCommonStuff(OrderBase order)
 {
     var id = await _identityGenerator.GenerateNewIdAsync();
     order.Id = id;
     order.Status = OrderStatus.Registered;
     order.Registered = DateTime.UtcNow;
 }
예제 #2
0
        public static OrderEntity Create(OrderBase src)
        {
            var result = new OrderEntity
            {
                PartitionKey = GeneratePartitionAsAllOrders(src.Status),
                RowKey = GenerateRowKey(src.Id)
            };

            result.SetOrder(src);

            return result;
        }
예제 #3
0
        public void SetOrder(OrderBase order)
        {
            foreach (var orderType in OrderTypes)
                if (orderType.Value == order.GetType())
                {
                    Type = orderType.Key;
                    OrderData = Newtonsoft.Json.JsonConvert.SerializeObject(order);
                    return;
                }

            throw new Exception("Can not set order. Unknown order type: "+order.GetType());
        }
예제 #4
0
        public async Task RegisterOrderAsync(OrderBase order)
        {
            var newEntity = OrderEntity.Create(order);
            await _tableStorage.InsertAsync(newEntity);

            newEntity.PartitionKey = OrderEntity.GeneratePartitionKey(order.TraderId);
            await _tableStorage.InsertAsync(newEntity);
        }
예제 #5
0
        public async Task UpdateOrderAsync(OrderBase updatedOrder)
        {
            var partitionKey = OrderEntity.GeneratePartitionKey(updatedOrder.TraderId);
            var rowKey = OrderEntity.GenerateRowKey(updatedOrder.Id);


            var oldStatus = OrderStatus.Registered;

            await _tableStorage.ReplaceAsync(partitionKey, rowKey, itm =>
            {
                oldStatus = itm.GetOrder().Status;
                itm.SetOrder(updatedOrder);
                return itm;
            });



            if (oldStatus == updatedOrder.Status)
            {
                await _tableStorage.ReplaceAsync(OrderEntity.GeneratePartitionAsAllOrders(oldStatus), rowKey, itm =>
                {
                    itm.SetOrder(updatedOrder);
                    return itm;
                });
                return;
            }


            partitionKey = OrderEntity.GeneratePartitionAsAllOrders(oldStatus);
            await _tableStorage.DeleteAsync(partitionKey, rowKey);

            var newEntity = OrderEntity.Create(updatedOrder);
            await _tableStorage.InsertOrReplaceAsync(newEntity);



        }
예제 #6
0
        private static OrderList SeparateOrders(OrderBase[] orders)
        {

            var result = new OrderList();

            foreach (var orderBase in orders)
            {
                var marketOrder = orderBase as MarketOrder;
                if (marketOrder != null)
                    result.MarketOrders.Add(marketOrder);

                var limitOrder = orderBase as LimitOrder;
                if (limitOrder != null)
                {
                    if (limitOrder.Action == OrderAction.Buy)
                        result.LimitOrdersBuy.Add(OrderWrapper.Create(limitOrder)); 

                    if (limitOrder.Action == OrderAction.Sell)
                        result.LimitOrdersSell.Add(OrderWrapper.Create(limitOrder));
                }
            }

            result.MarketOrders = result.MarketOrders.OrderBy(itm => itm.Id).ToList();
            result.LimitOrdersBuy = result.LimitOrdersBuy.OrderByDescending(itm => itm.Order.Price).ToList();
            result.LimitOrdersSell = result.LimitOrdersSell.OrderBy(itm => itm.Order.Price).ToList();

            return result;
        }