Example #1
0
        public async Task <OrderEntity> InsertAsync(OrderEntity item)
        {
            try
            {
                await _trader.GetByIdAsync(item.UserId);
            }
            catch (NotFoundException ex)
            {
                await _trader.CreateAsync(new TraderInfoEntity()
                {
                    Id = item.UserId
                });
            }
            MarketUpdateDto marketPrice = await _updatesManager.GetLastUpdateAsync(item.MarketId);

            if (marketPrice == null)
            {
                throw new Exception($"Cant get price of Market with id: {item.MarketId}");
            }
            item.Price     = item.Action == OrderAction.Buy ? marketPrice.AskPrice : marketPrice.BidPrice;
            item.Timestamp = DateTime.UtcNow;
            var entity = await _order.InsertAsync(item);

            if (entity == null)
            {
                throw new InvalidDataException(ErrorCode.CannotCreateOrder);
            }
            await _order.UnitOfWork.SaveChangesAsync();

            _messageBus.Publish(entity, "exc.Order", RabbitExchangeType.DirectExchange, $"Add:{entity.UserId}");
            await _positionManager.PlaceOrderAsync(entity);

            return(entity);
        }
Example #2
0
 private void RecalculateUPL(PositionEntity position, MarketUpdateDto marketUpdate)
 {
     if (position.Kind == STP.Interfaces.Enums.PositionKind.Short)
     {
         position.UnrealizedProfitLoss = (position.AveragePrice - marketUpdate.AskPrice) * position.Quantity;
     }
     if (position.Kind == STP.Interfaces.Enums.PositionKind.Long)
     {
         position.UnrealizedProfitLoss = (marketUpdate.BidPrice - position.AveragePrice) * position.Quantity;
     }
 }
Example #3
0
        public List <UserUPL> UpdatePositionsUPL(MarketUpdateDto marketUpdate)
        {
            UpdateMarketPrice(marketUpdate);
            var usersUPLs = new List <UserUPL>();

            if (_marketsPositions.TryGetValue(marketUpdate.MarketId, out List <PositionEntity> marketPositions))
            {
                RecalculateMultiplePositionsUPL(marketPositions, marketUpdate);
                CollectUpdatedUPL(marketPositions, usersUPLs);
            }
            return(usersUPLs);
        }
Example #4
0
        /// <summary>
        /// Notify update
        /// </summary>
        public void NotifyUpdate(object sender, EventArgs <MarketUpdate> marketUpdate)
        {
            LastMarketUpdates.AddOrUpdate(marketUpdate.Value.MarketId, marketUpdate.Value, (key, oldValue) => oldValue = marketUpdate.Value);

            HashSet <string> item;

            if (_subscribers.TryGetValue(marketUpdate.Value.MarketId, out item))
            {
                if (item.Any())
                {
                    MarketUpdateDto marketUpdateDto = _mapper.Map <MarketUpdateDto>(marketUpdate.Value);
                    _messageBus.Publish(marketUpdateDto, "exc.Datafeed", Interfaces.Enums.RabbitExchangeType.DirectExchange, $"Update:{marketUpdate.Value.MarketId}");

                    //_messageBus.Publish(marketUpdateDto, "exc.Datafeed", Interfaces.Enums.RabbitExchangeType.DirectExchange, $"PL:{marketUpdate.Value.MarketId}");
                    Debug.WriteLine($"asdfg:\n " + marketUpdate.Value.AskPrice + " / " + marketUpdate.Value.BidPrice + "\n" +
                                    marketUpdate.Value.LastPrice + " / " + marketUpdate.Value.LastDailyPrice + "\n" +
                                    marketUpdate.Value.Change + " / " + marketUpdate.Value.PercentChange + "\n" + marketUpdate.Value.MarketId + " / " + marketUpdate.Value.Timestamp + "\n");
                }
            }
        }
Example #5
0
 private void RecalculateMultiplePositionsUPL(List <PositionEntity> positions, MarketUpdateDto marketUpdate)
 {
     lock (list_root)
     {
         foreach (var position in positions)
         {
             RecalculateUPL(position, marketUpdate);
         }
     }
 }
Example #6
0
 private void UpdateMarketPrice(MarketUpdateDto marketUpdate)
 {
     MarketPrice.AddOrUpdate(marketUpdate.MarketId, marketUpdate, (k, v) => v = marketUpdate);
 }