public override IList<Trade> Import(Stream stream, Market market, IList<Exception> exceps) { var trades = new List<Trade>(); //var csvStream = new MemoryStream(); //DocumentConvererHelper.XlsxToExt(stream, csvStream, "csv", 1); //csvStream.Position = 0; var csvStream = stream; var parser = new TextFieldParser(csvStream) { HasFieldsEnclosedInQuotes = true }; parser.SetDelimiters(new string[] { Separator }); //parser.SetDelimiters(","); var titles = parser.ReadFields(); if (titles == null) return trades; var headers = new Dictionary<string, int>(); for (int i = 0; i < titles.Length; i++) headers[titles[i]] = i; while (!parser.EndOfData) { var items = parser.ReadFields(); var info = new MurexInfo(items, headers); // leave out trades not traded on report date if (info.TradeDate != info.ReportDate) continue; var trade = FromString(info, exceps); if (trade != null) { trades.Add(trade); } } parser.Close(); long count = -1; foreach (var tr in trades) { tr.Id = count--; } // aggregate imported trades var aggregatedTrades = PrepareTradesForMatching(trades, DateTime.Now); return aggregatedTrades.Keys.ToList(); }
protected Trade FromString(MurexInfo info, IList<Exception> exceps) { var trade = new Trade(); trade.TradingDay = info.TradeDate; trade.Description = info.SecurityType; trade.Status = info.Status; trade.Accrual = info.AccruedInterest; if (info.Strategy != null) { var strategy = info.Strategy; if (info.Strategy.Equals(TradeInfo.DefaultStrategy) && info.Portfolio != null) { strategy = info.Portfolio + ":" + info.Strategy; } var book = Env.Current.StaticData.GetPartyByCode(EntityPrefix + ":" + strategy); if (book != null) trade.BookId = book.Id; } if (info.Counterparty != null || info.CounterpartyCode != null) { var party = Env.Current.StaticData.GetPartyByProperty(Party.CounterParty, TradeImportHelper.PartyDisplay, info.Counterparty) ?? Env.Current.StaticData.GetPartyByProperty(Party.CounterParty, TradeImportHelper.PartyMlpCode, info.CounterpartyCode); if (party != null) trade.InitialPartyId = party.Id; } if (info.PBAccount != null) { trade.SetProperty(MurexInfo.AccountProp, info.PBAccount); var pbname = Alias.PartyFromAlias(0, "Murex", info.PBAccount); if (pbname != null) { var pb = Env.Current.StaticData.GetPartyByCode(pbname); if (pb != null) trade.PrimeBrokerId = pb.Id; } //} } if (info.ClearingHouse != null) { var ccp = Env.Current.StaticData.GetPartyByCode(info.ClearingHouse + TradeInfo.ClearingHouseTag); if (ccp == null) { var ccpName = Alias.PartyFromAlias(0, "Murex", info.ClearingHouse); if (ccpName != null) { ccp = Env.Current.StaticData.GetPartyByCode(ccpName); } } if (ccp != null) trade.CcpId = ccp.Id; } if (info.TradeId != null && !info.TradeId.Equals("0") && info.IsOtc) trade.SetProperty(TradeImportHelper.MurexTradeId, info.TradeId); if (info.IsListedOption) trade.Product = info.GetProduct(GetContracts(true), Logger); else if (info.IsFuture) trade.Product = info.GetProduct(GetContracts(false), Logger); else trade.Product = info.GetProduct(null, Logger); if (trade.Product is MurexProduct) { trade.SetProperty(TradeImportHelper.GeneratorProp, ((MurexProduct)trade.Product).Generator); } if (!double.IsNaN(info.UpFrontAmount)) trade.SettleAmount = info.UpFrontAmount; if (info.SecurityType.Equals(FxInfo.SpotForward) && trade.Product is FX) { var fx = trade.Product as FX; if (info.BuySell.Equals("S")) { if (fx.Primary == info.QuotingCcy) { trade.Quantity = fx.PrimaryAmount; trade.SettleAmount = -fx.QuotingAmount; } else { trade.Quantity = -fx.PrimaryAmount; trade.SettleAmount = fx.QuotingAmount; } } else { if (fx.Primary == info.QuotingCcy) { trade.Quantity = -fx.PrimaryAmount; trade.SettleAmount = fx.QuotingAmount; } else { trade.Quantity = fx.PrimaryAmount; trade.SettleAmount = -fx.QuotingAmount; } } } else { if (!info.IsOtc) { trade.Quantity = info.Nominal1; if (trade.Product is Bond) { var bond = trade.Product as Bond; trade.Quantity = info.Nominal1 / bond.Nominal; } } else { trade.Quantity = info.BuySell.Equals("B") ? 1 : -1; trade.SetProperty(TradeImportHelper.GeneratorProp, info.Instrument); } } var price = Math.Abs(info.Price); if (trade.Product is Future) { trade.PriceType = ((Future)trade.Product).Contract.Quotation; } else if (trade.Product is Bond) { trade.PriceType = ((Bond)trade.Product).QuotationType; } if (trade.PriceType != QuotationType.None && Utilities.IsPercentageQuotation(trade.PriceType)) price /= 100; trade.Price = price; return trade; }