Пример #1
0
        public async Task <TradeDataObject> ConvertTradeAsync(OwnTrade m7Trade, string apiKey)
        {
            try
            {
                var tradeDateTime      = m7Trade.execTimeUTC;
                var tradeId            = m7Trade.tradeId;
                var accountId          = m7Trade.acctId;
                var userCode           = m7Trade.usrCode;
                var quantity           = m7Trade.qty;
                var price              = m7Trade.px;
                var side               = m7Trade.side;
                var deliveryAreaId     = m7Trade.dlvryAreaId;
                var deliveryStart      = m7Trade.Contract.dlvryStartUTC;
                var deliveryEnd        = m7Trade.Contract.dlvryEndUTC;
                var exchangeId         = m7Trade.Product.exchangeId;
                var aggressorIndicator = m7Trade.aggressorIndicator?.ToUpper();

                var allMappings = (await mappingService.GetMappingsViaApiKeyAsync(apiKey)).Data.ToDictionary(k => k.MappingKey, v => v);
                var allSettings = (await settingService.GetSettingsViaApiKeyAsync(apiKey)).Data.ToDictionary(k => k.SettingName, v => v);

                var tradeStatus  = "Live";
                var quantityType = "Fixed";
                var priceType    = "Fixed";
                var buySell      = MapSideToBuySell(side);

                var commodityMapping  = MapDeliveryAreaToCommodityAsync(deliveryAreaId, allMappings);
                var fingerprintTask   = MapCommodityToProductAsync(commodityMapping?.MappingTo, deliveryStart, deliveryEnd, apiKey);
                var internalPartyTask = im7PartyService.MapInternalPartyAsync(accountId, allMappings, allSettings, apiKey);
                var counterpartyTask  = im7PartyService.MapCounterpartyAsync(exchangeId, allMappings, allSettings, apiKey);

                var fingerprint   = await fingerprintTask;
                var internalParty = await internalPartyTask;
                var counterparty  = await counterpartyTask;

                var tradingBook = await tradingBookService.MapTradingBookAsync(deliveryAreaId, allMappings, allSettings, apiKey);

                var internalTrader = await contactService.MapInternalTraderAsync(userCode, allMappings, allSettings, apiKey);

                var venue = await venueService.MapVenueAsync(exchangeId, allMappings, allSettings, apiKey);

                if (internalParty is null)
                {
                    throw new DataException("Could not map InternalParty");
                }

                if (counterparty is null)
                {
                    throw new DataException("Could not map Counterparty");
                }

                if (tradingBook is null)
                {
                    throw new DataException("Could not map TradingBook");
                }

                if (internalTrader is null)
                {
                    throw new DataException("Could not map InternalTrader");
                }

                if (venue is null)
                {
                    throw new DataException("Could not map Venue");
                }

                var trade = new TradeDataObject
                {
                    TradeDateTime         = tradeDateTime,
                    TradeStatus           = tradeStatus,
                    BuySell               = buySell,
                    Product               = fingerprint?.ProductDataObject,
                    TradingBook           = tradingBook,
                    InternalParty         = internalParty,
                    InternalTrader        = internalTrader,
                    Counterparty          = counterparty,
                    CounterpartyReference = tradeId.ToString(),
                    Buyer = buySell == "Buy"
                        ? internalParty
                        : counterparty,
                    Seller = buySell == "Sell"
                        ? internalParty
                        : counterparty,
                    Exchange          = counterparty,
                    ExchangeReference = tradeId.ToString(),
                    Initiator         = aggressorIndicator == "Y"
                        ? counterparty
                        : internalParty,
                    Aggressor = aggressorIndicator == "Y"
                        ? internalParty
                        : counterparty,
                    Venue    = venue,
                    Quantity = new QuantityDataObject
                    {
                        Quantity     = quantity,
                        QuantityType = quantityType,
                        QuantityUnit = fingerprint?.ProductDataObject?.QuantityUnit
                    },
                    Price = new PriceDataObject
                    {
                        Price     = price,
                        PriceType = priceType,
                        PriceUnit = fingerprint?.ProductDataObject?.PriceUnit
                    }
                };

                return(trade);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error creating trade from M7 ({Message})", ex.Message);
                throw;
            }
        }