コード例 #1
0
        public IList<TradeInfo> Filter(FilterData filterData)
        {
            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, null);
            var dict = new List<TradeInfo>();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;
            foreach (var info in infos)
            {
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                // if (!filterData.LiveDate.IsNull && !info.IsActive(filterData.LiveDate)) continue; //Skip Inactive Swaps

                dict.Add(info);
            }
            if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<TradeInfo>();
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            return dict;
        }
コード例 #2
0
 public IList<TradeInfo> Filter(FilterData filterData)
 {
     var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, null);
     var dict = new List<TradeInfo>();
     if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
     int count = 0;
     //int skipped = 0;
     foreach (var info in infos)
     {
         if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<TradeInfo>();
         count++;
         if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
         if (FilterInfo(info)) continue;
         dict.Add(info);
     }
     if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<TradeInfo>();
     if (filterData.Progress != null) filterData.Progress.AnalysisDone();
     return dict;
 }
コード例 #3
0
        virtual public IList<Trade> Import(Market market, Feed feed, StringBuilder sb, FilterData filterData)
        {
            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, sb);
            var trades = new List<Trade>();
            var dict = new Dictionary<TradeInfo, Trade>();
            var symbols = new Dictionary<string, Product>();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;
            foreach (var info in infos)
            {
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                if (FilterInfo(info)) continue;
                var trade = new Trade();

                if (!TradeImportHelper.InitTrade(trade, info, filterData, sb)) continue;
                dict[info] = trade;
                //trades.Add(trade);
                var tokens = info.Symbol.Split('-');
                var symbol = tokens.Length > 1 ? tokens[1] : info.Symbol;           // symbol is either cusip/isin, or r-symbol
                info.Symbol = symbol;
                if (symbol != null) symbol = symbol.Trim();
                if (!string.IsNullOrEmpty(symbol))
                {
                    var p = Env.Current.Trade.GetProductByCode(Product.CusipProductCode, symbol) ??
                        Env.Current.Trade.GetProductByCode(Product.IsinProductCode, symbol);
                    if (p == null)
                    {
                        if (symbols.Count < MaxBonds)
                        {
                            symbols[symbol] = null; // bond;
                        }
                        else dict[info] = null;//Remove it
                    }
                    else info.Security = p;
                }
                else //sb.Append("[Not Imported] Symbol is null HoldingID = " + info.SourceID + " " + info.Instrument + "\n");
                    sb.Append(TradeImportHelper.FormatErrorMessage("Symbol is null", info.SourceID, info.Instrument));
            }
            if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            //Now Get Bonds by Cusips and by Isins
            TradeImportHelper.SnapSecurities(feed, symbols, "Bond.FeedMapping.xml", sb);

            var tempProvider = new RepoTemplateProvider();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(dict.Count, "Initializing Trades");
            count = 0;
            foreach (var kvp in dict)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                var trade = kvp.Value;
                var info = kvp.Key;
                if (trade == null) continue;
                if (info.Security == null)
                {
                    if (!string.IsNullOrEmpty(info.Symbol) && symbols.ContainsKey(info.Symbol))
                    {
                        info.Security = symbols[info.Symbol];
                    }
                }
                if (info.Security != null)
                {
                    if (info.Security.Currency == null)
                    {
                        //sb.Append("[Not Imported] Bad Security for HoldingID = " + info.SourceID + " " + info.Instrument + " Currency is NULL \n");
                        sb.Append(TradeImportHelper.FormatErrorMessage("Bad Security currency is NULL", info.SourceID, info.Instrument));
                        continue;
                    }
                    if (filterData.Currencies != null && filterData.Currencies.Count > 0 && !filterData.Currencies.Contains(info.Security.Currency))
                        continue;
                    var copy = trade.Id > 0 ? (Trade)trade.Clone() : null;
                    var repo = trade.Product as Repo ?? new Repo();
                    trade.Product = repo;
                    repo.ResetEventSchedule();
                    if (copy != null)
                        TradeImportHelper.CopyTrade(copy, trade); 
                    TradeImportHelper.SetParties(trade, info);
                    var colSec = repo.CollateralList.FirstOrDefault();
                    if (colSec == null)
                    {
                        colSec = new CollateralSecurity();
                        repo.CollateralList = new List<CollateralSecurity> { colSec };
                    }
                    try
                    {
                        var bond = info.Security;
                        colSec.UnderlyingProductId = bond != null ? bond.Id : 0;
                        // Add repo details here...
                        // ----------------------------------------------------------------------------------------------------
                        // startDate, endDate, repo.Cash.Leg.StartDate, repoCash.Leg.EndDate, colSec.StartDate, colSec.EndDate
                        // repo.Duration
                        // repo.Cash.Leg.PaymentFrequency
                        // repo.Cash.Leg.IsFixedRate
                        // repo.SubType
                        // repo.Cash.IsBorrow
                        // repo.RepoCollateralType
                        // repo.TransactionType
                        repo.Cash.Leg.NotionalCurrency = info.Currency1;
                        var instrCcy = Env.Current.StaticData.GetCurrency(info.Currency1);
                        if (instrCcy == null)
                        {
                            //sb.Append("[Not Imported] HoldingID = " + info.SourceID + " No Currency Static Defined for " + info.Currency1 + "\n");
                            sb.Append(TradeImportHelper.FormatErrorMessage("No Currency Static Defined for " + info.Currency1, info.SourceID, info.Instrument));
                            continue;
                        }
                        repo.Cash.Leg.Daycount = OrchestradeCommon.RefData.Currency.FloatDaycount(info.Currency1);
                        // need to set a default daycount
                        repo.Cash.Leg.PaymentMarketPlaces = instrCcy.DefaultMarketPlace;
                        repo.NoticePeriodMarketPlace = instrCcy.DefaultMarketPlace;
                        // ---------------------------------------------------------------------------------------------------------
                        // colSec.HairCut
                        // colSec.HairCutType
                        // colSec.HairCutConvention
                        // colSec.IsCollateralTaken
                        // colSec.CouponPassThrough
                        colSec.NominalCollateral = info.Nominal1;
                        colSec.PriceType = bond.QuoteName.QuoteType;                        
                        
                        var bd = bond as Bond;
                        if (bd != null)
                        {
                            colSec.PoolFactor = bd.GetPoolFactor(colSec.StartDate);
                            colSec.InflationFactor = bd.GetIndexationFactor(colSec.StartDate, market);
                        }
                        if (colSec.PoolFactor == 0) colSec.PoolFactor = 1;
                        if (colSec.InflationFactor == 0) colSec.InflationFactor = 1;
                        colSec.SettleCurrency = info.Currency1;
                        // colSec.CouponReinvestmentRate = 0;
                        // colSec.DividendReturnRate = 1;
                        CollateralHelper.SetAccrual(trade, colSec, market);
                        var securityPrice = info.Price;
                        // price ...
                        tempProvider.FillTrade(trade, market);
                        trades.Add(trade);
                        // rerate...
                        // interest amount...

                    }
                    catch(NullReferenceException x)
                    {
                        //sb.Append("[Not Imported] Fill Trade HoldingID = " + info.SourceID + " Symbol " + info.Symbol + " " + x.Message + "\n");    
                        sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message + " " + x.StackTrace, info.SourceID, info.Instrument));
                    }
                    catch (Exception x)
                    {
                        //sb.Append("[Not Imported] Fill Trade HoldingID = " + info.SourceID + " Symbol " + info.Symbol + " " + x.Message + "\n");
                        sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message + " " + x.StackTrace, info.SourceID, info.Instrument));
                    }
                }
                else
                {
                    //sb.Append("[Not Imported] HoldingID = " + info.SourceID + " Security not found (" + info.Instrument + ") " + "\n");
                    sb.Append(TradeImportHelper.FormatErrorMessage(" Security not found", info.SourceID, info.Instrument));
                }
            }
            return trades;
        }
コード例 #4
0
        public IList<Trade> Import(Market market, Feed feed, StringBuilder sb, FilterData filterData)
        {
            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, sb);
            var trades = new List<Trade>();
            var dict = new Dictionary<TradeInfo, Trade>();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;
            foreach (var info in infos)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                if (FilterInfo(info)) continue;
                if(filterData.Currencies !=null && filterData.Currencies.Count > 0 && !filterData.Currencies.Contains(info.Currency1)) continue; 
                if (info.Otc == null || !(info.Otc is SwapInfo) || info.Otc.IsEmpty())
                {
                    sb.Append(TradeImportHelper.FormatErrorMessage("Swap info not found or not complete", info.HoldingID, info.TradeID, info.Instrument));
                    continue;
                }
                var prodCurrency = Env.Current.StaticData.GetCurrency(info.Currency1);
                string prodtype, aliasSource;
                if (info.SecurityType.Equals(SwapInfo.IRSwap))
                {
                    prodtype = prodCurrency.IsNonDeliverable ? "NDS" : "Swap";
                    aliasSource = SwapInfo.AliasSourceIrs;
                    
                }
                else if (info.SecurityType.Equals(SwapInfo.CcySwap))
                {

                    prodtype = info.Instrument.Contains("MTM") ? "MTMCurrencySwap" : "CurrencySwap";
                    aliasSource = SwapInfo.AliasSourceCcy;
                }
                else
                {
                    prodtype = "FRA";
                    aliasSource = SwapInfo.AliasSourceFra;
                }
                var alias = Env.Current.StaticData.GetAlias(new Alias
                            {
                                AliasType = "Template",
                                EntityId = 0,
                                Source = aliasSource,
                                SourceReference = info.Instrument
                            });
                if (alias != null)
                {
                    var swapInfo = info.Otc as SwapInfo;
                    var template = Env.Current.StaticData.GetTemplate(Template.Shared, prodtype, alias.Name);
                    if (template == null && prodtype == "Swap")
                    {
                        // some swap is booked as NDS
                        prodtype = "NDS";
                        template = Env.Current.StaticData.GetTemplate(Template.Shared, prodtype, alias.Name);
                        prodtype = "Swap/NDS";
                    }
                    swapInfo.Generator = template;
                    if (swapInfo.Generator == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("Generator template for " + prodtype + " " + alias.Name + " not found", info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                }
                else
                {
                    sb.Append(TradeImportHelper.FormatErrorMessage("Alias for Generator not found", info.HoldingID, info.TradeID, info.Instrument));
                    continue;
                }
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                var trade = new Trade();
                if (FilterInfo(info)) continue;
                if (!TradeImportHelper.InitTrade(trade, info, filterData, sb)) continue;
                dict[info] = trade;
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            count = 0;
            foreach (var kvp in dict)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null && count%5==0) filterData.Progress.AnalysisProgress(count, "");
                var info = kvp.Key;
                var trade = kvp.Value;
                var sinfo= info.Otc as SwapInfo;
                try
                {
                    var copy = trade.Id > 0 ? (Trade)trade.Clone() : null;
                    var tradeType = trade.TradeType;
                    sinfo.Generator.Apply(trade, new SimpleDate(sinfo.TradeDate), market);
                    trade.TradeType = tradeType;
                    if (trade.Product == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("No product generated after applying Generator", info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                    if (trade.Product  == null) continue;
                    if (copy != null)
                    {
                        TradeImportHelper.CopyTrade(copy, trade);
                        trade.MirrorBookId = copy.MirrorBookId;
                        trade.MirrorTradeId = copy.MirrorTradeId;
                    }
                    //if (sinfo.ClearingHouse != null)
                    //    info.ClearingHouse = sinfo.ClearingHouse;
                    trade.SettlementDate = sinfo.SwapStartDate1;
                    TradeImportHelper.SetParties(trade, info);

 
                    if (info.SecurityType.Equals(SwapInfo.IRSwap))
                    {
                        var swap = trade.Product as Swap;
                        var payLeg = swap.PayLeg;
                        var recLeg = swap.ReceiveLeg;
                        Leg leg1 = payLeg, leg2 = recLeg;
                        if (!payLeg.IsFixedRate && !recLeg.IsFixedRate)
                        {
                            // basis swap
                            var r1 = payLeg.RateIndex;        // leg1 index
                            var r2 = recLeg.RateIndex;        // leg2 index
                            if (r1.Equals(r2))
                            {
                                r1 = payLeg.RateIndexTenor.Count.ToString();
                                r2 = recLeg.RateIndexTenor.Count.ToString();
                            }
                            var idx1 = sinfo.Generator.Name.IndexOf(r1);
                            var idx2 = sinfo.Generator.Name.IndexOf(r2);
                            if ((idx1 < idx2 && info.TradeAction.Equals("B")) || (idx1 > idx2 && (info.TradeAction.Equals("S"))))                               
                            {
                                swap.SwitchLegs();
                            }
                            if (idx2 < idx1)
                            {
                                leg1 = recLeg;
                                leg2 = payLeg;
                            }
                            InitLegs2(leg1, leg2, info);
                        }
                        else
                        {
                            if ((info.TradeAction.Equals("S") && !swap.PayLeg.IsFixedRate) ||
                                (info.TradeAction.Equals("B") && swap.PayLeg.IsFixedRate))
                                swap.SwitchLegs();
                            InitLegs(swap.PayLeg, swap.ReceiveLeg, info);
                        }                        
                        if (trade.CcpId != 0)
                        {
                            //Nothing to do let the EventScheduleHelper insert the StepIn
                        }
                        else
                        {
                            if (swap.PayLeg.StartDate < trade.GetTradingDay())
                            {
                                swap.PayLeg.FirstFullCalculationPeriod = true;
                                swap.PayLeg.FirstCalculationPeriodStart = sinfo.SwapStartDate1;
                                swap.ReceiveLeg.FirstFullCalculationPeriod = true;
                                swap.ReceiveLeg.FirstCalculationPeriodStart = sinfo.SwapStartDate1;
                                swap.PayLeg.StartDate = trade.GetTradingDay();
                                swap.ReceiveLeg.StartDate = swap.PayLeg.StartDate;
                            }
                        }
                    }
                    else if (info.SecurityType.Equals(SwapInfo.CcySwap))
                    {

                        var swap = trade.Product as CurrencySwap;
                        var mtm = trade.Product as MTMCurrencySwap;
                        Leg payLeg, recLeg, leg1, leg2;
                        payLeg = swap != null ? swap.PayLeg : mtm.PayLeg;
                        recLeg = swap != null ? swap.ReceiveLeg : mtm.ReceiveLeg;
                        if (info.TradeAction.Equals("B") && recLeg.NotionalCurrency.Equals(info.Currency2) ||
                            (info.TradeAction.Equals("S") && recLeg.NotionalCurrency.Equals(info.Currency1)))
                        {
                            if (swap != null)
                                swap.SwitchLegs();
                            else
                                mtm.SwitchLegs(); 
                        }
                        if (payLeg.NotionalCurrency.Equals(info.Currency1))
                        {
                            leg1 = payLeg;
                            leg2 = recLeg;
                        }
                        else
                        {
                            leg2 = payLeg;
                            leg1 = recLeg;
                        }
                        InitLegs2(leg1, leg2, info);

                    }
                    else if (info.SecurityType.Equals(SwapInfo.Fras))
                    {
                        var fra = trade.Product as FRA;
                        InitFra(fra, info);
                    }
                    sinfo.Generator.Provider.FillTrade(trade, market);
                    trade.SetProperty("TemplateName", sinfo.Generator.Name);
                    trade.SetProperty(TradeImportHelper.GeneratorProp, info.Instrument);
                    //Add UpFront Fee
                    if (Math.Abs(sinfo.FeeAmount) > Utilities.Epsilon)
                    {
                        var fee = new Fee
                        {
                            Date = sinfo.FeeDate,
                            FeeType = FeeType.Upfront,
                            PartyId = trade.PartyId,
                            Currency = sinfo.FeeCurrency,
                            Amount = Math.Abs(sinfo.FeeAmount),
                            PayReceive = sinfo.FeePayRecv.Equals("Pay") ? PayReceive.Pay : PayReceive.Receive     // how about None??
                        };
                        if (trade.Fees == null) trade.Fees = new List<Fee>();
                        trade.Fees.Add(fee);
                    }
                    trades.Add(trade);
                }
                catch (Exception x)
                {
                    Logger.Error("Import Swap/FRA/CurrencySwap/NDS", x);
                    sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message + " " + x.StackTrace, info.HoldingID, info.TradeID, info.Instrument));
                }
            }
            sb.Append("Imported " + trades.Count + " Swaps/Fras/CurrencySwap/NDS\n");
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            return trades;
        }
コード例 #5
0
        internal static IList<PartyInfo> ReadPartyData(FilterData filterData, StringBuilder sb)
        {
            var fileName = filterData.FileName;

            if (!File.Exists(fileName))
            {
                if (sb != null) sb.Append("File " + fileName + " not found\n");
                return new List<PartyInfo>();
            }

            var list = new List<PartyInfo>();
            using (var stream = File.OpenRead(fileName))
            {
                using (var reader = new StreamReader(stream))
                {
                    string line;
                    var header = reader.ReadLine();
                    if (string.IsNullOrEmpty(header))
                    {
                        Logger.Error("[Error Read File] Empty Headers ... ");
                        return new List<PartyInfo>();
                    }
                    var headers = new Dictionary<string, int>();
                    using (var csvParser = new TextFieldParser(new StringReader(header)))
                    {
                        csvParser.SetDelimiters(new[] { "," });
                        var hss = csvParser.ReadFields();
                        int idx = 0;
                        foreach (var s in hss)
                        {
                            headers[s.Trim()] = idx++;
                        }
                    }

                    while ((line = reader.ReadLine()) != null)
                    {
                        //Parse Line here
                        if (string.IsNullOrEmpty(line)) break; //we are at end
                        var pinfo = new PartyInfo(line, headers);
                        list.Add(pinfo);
                    }
                }
            }
            return list;
        }
コード例 #6
0
        //Init Trade Date SettleDateS Strategy CounterParty ....
        internal static bool InitTrade(Trade trade, TradeInfo info, FilterData filterData, StringBuilder sb)
        {

            Trade existingTrade = null;
            if (info.HoldingID != null)
            {
                if (filterData.ExistingTransIds != null && filterData.ExistingTransIds.Count > 0)
                {
                    if (filterData.ExistingTransIds.Contains(info.HoldingID))
                        existingTrade = Env.Current.Trade.GetTradeByProperty(SymmetryTranId, info.HoldingID);
                }
                else existingTrade = Env.Current.Trade.GetTradeByProperty(SymmetryTranId, info.HoldingID);
            }
            if (existingTrade != null)
            {
                if (filterData.OnlyNewTrades) return false;
                trade.Id = existingTrade.Id;
                trade.Status = existingTrade.Status;
                trade.Action = UpdateAction;
                trade.Version = existingTrade.Version;
                trade.Product = existingTrade.Product;
                trade.InputUserId = existingTrade.InputUserId;
                trade.InputTime = existingTrade.InputTime;
                if (existingTrade.BackToBackBookId != 0)
                {
                    trade.BackToBackBookId = existingTrade.BackToBackBookId;
                    trade.SetProperty(Trade.BackToBackInternalTradeId, existingTrade.GetProperty(Trade.BackToBackInternalTradeId));
                }
            }
            else
            {
                trade.Id = 0; // _id--;
                trade.Status = "New";
                trade.Action = "Create";
            }
            if (info.HoldingID != null)
                trade.SetProperty(SymmetryTranId, info.HoldingID);
            if (info.TradeID != null && !info.TradeID.Equals("0"))
                trade.SetProperty(MurexTradeId, info.TradeID);

            // set parties: entity, book, counterparty
            // need to book trade under block entity
            var s1 = ManagedAccountCode + ":" + info.Strategy;
            var s2 = MasterFundCode + ":" + info.Strategy;
            var s3 = info.Strategy;
            var p1 = ManagedAccountCode;
            var p2 = MasterFundCode;            
            var p3 = BlockEntityCode;       
            var entity1 = SetAndLoadParty(trade, p1, Party.Entity);
            var entity2 = SetAndLoadParty(trade, p2, Party.Entity);
            var entity3 = SetAndLoadParty(trade, p3, Party.Entity + ":" + Party.Block);        // must be block entity

            var book1 = SetAndLoadParty(trade, s1, Party.Book, entity1.Id);  
            var book2 = SetAndLoadParty(trade, s2, Party.Book, entity2.Id);
            var book3 = SetAndLoadParty(trade, s3, Party.Book, entity3.Id, true);         // book trade under book3        
            SetUpAllocaitonGrid(AllocName, entity3.Id, entity1.Id, info.ReportDate.AddYears(-2), 1);

            if (info.Portfolio != null)
            {
                SetBookPortfolio(book1, info.Portfolio);
                SetBookPortfolio(book2, info.Portfolio);
                SetBookPortfolio(book3, info.Portfolio);
            }
            // set counterParty   
            var counterparty = Env.Current.StaticData.GetPartyByProperty(Party.CounterParty, PartyDisplay, info.Counterparty);
            if (counterparty == null)
            {
                if (!info.Counterparty.Equals(TradeInfo.DefaultParty))
                    Logger.Warn(FormatReadFileMessage("CounterParty does not exist by display name, will create or load one by code", info.Counterparty, info.HoldingID, info.TradeID, false));
                SetAndLoadParty(trade, info.Counterparty, Party.CounterParty);
            }
            else
            {
                trade.PartyId = counterparty.Id;
                trade.InitialPartyId = counterparty.Id;
            }
            

            var tradeDate = (info.Otc != null && !info.Otc.TradeDate.IsNull) ? info.Otc.TradeDate : info.ReportDate;
            trade.TradeTime = new DateTime(tradeDate.Year, tradeDate.Month, tradeDate.Day);
            trade.SettlementDate = info.ReportDate;
            trade.Price = info.Price;
            if (!double.IsNaN(info.Quantity))
                trade.Quantity = info.Quantity;
            else
                trade.Quantity = info.Nominal1;
            trade.SettleCurrency = info.Currency1;
            trade.Source = Source;
            trade.SourceReference = info.HoldingID;
            trade.SetProperty(SourceSys, info.FileSource);
            if (info.TradeID != null && !info.TradeID.Equals("0"))
                trade.SetProperty(info.FileSource + "TradeId", info.TradeID);

            //Set Clearer and PrimeBroker
            if (info.PrimeBroker != null)
            {
                var clearer = Env.Current.StaticData.GetPartyByCode(info.PrimeBroker);
                if (clearer == null)
                {
                    //Should create SSI Automatically ??
                    SetAndLoadParty(trade, info.PrimeBroker, Party.Clearer + ":" + Party.PrimeBroker);

                }
                else
                {
                    trade.ClearerId = clearer.Id;
                    trade.PrimeBrokerId = clearer.Id;
                }
            }
                /*
            else if (info.Account != null)
            {
                var clearer = Env.Current.StaticData.GetPartyByCode(info.Account);
                if (clearer == null)
                {
                    //Should create SSI Automatically ??
                    SetAndLoadParty(trade, info.Account, Party.Clearer);
                   
                }
                else
                    trade.ClearerId = clearer.Id;
            }*/
            return true;
        }
コード例 #7
0
ファイル: FXImporter.cs プロジェクト: heimanhon/researchwork
        public IList<Trade> Import(Market market, Feed feed, StringBuilder sb, FilterData filterData)
        {
            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, sb);
            var trades = new List<Trade>();
            var dict = new Dictionary<TradeInfo, Trade>();
            var exceptions = new List<Exception>();
            
            
            
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;
            foreach (var info in infos)
            {
                count++;
                if (FilterInfo(info)) continue;
                if (info.IsMurex && (info.Otc == null || info.Otc.IsEmpty()))
                {
                    sb.Append(TradeImportHelper.FormatErrorMessage("Fx info not found or not complete", info.HoldingID, info.TradeID, info.Instrument));
                    continue;
                }
                var ccy = info.Currency1;
                if (ccy == null && info.Otc != null && info.Otc is FxInfo)
                    ccy = ((FxInfo)info.Otc).PrimaryCcy;               
                if (filterData.Currencies != null && filterData.Currencies.Count > 0 && !filterData.Currencies.Contains(ccy)) continue; //Skip filtered ccy

                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                
                var trade = new Trade();
                if (!TradeImportHelper.InitTrade(trade, info, filterData, sb)) continue;
                dict[info] = trade;
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            count = 0;
            foreach (var kvp in dict)
            {
                count++;
                if (filterData.Progress != null && count % 5 == 0) filterData.Progress.AnalysisProgress(count, "");
                var info = kvp.Key;
                var trade = kvp.Value;
                var fxInfo = info.Otc as FxInfo;
                var fxoInfo = info.Otc as FxoInfo;
                try
                {
                    var copy = trade.Id > 0 ? (Trade)trade.Clone() : null;
                    if (copy != null)
                        TradeImportHelper.CopyTrade(copy, trade);
                    CurrencyPair cp;
                    string c1, c2;
                    if (fxInfo != null)
                    {
                        c1 = fxInfo.PrimaryCcy;
                        c2 = fxInfo.QuotingCcy;
                    }
                    else
                    {
                        c1 = info.Currency1;
                        c2 = info.Currency2;
                    }                    
                    cp = Env.Current.Trade.GetCurrencyPair(c1, c2);
                    if (cp == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("Undefined Currency Pair " + c1 + ":" + c2, info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                    var ccy1 = Env.Current.StaticData.GetCurrency(cp.Primary);
                    var ccy2 = Env.Current.StaticData.GetCurrency(cp.Quoting);
                    var isNdf = false;
                    if (ccy1.IsNonDeliverable || ccy2.IsNonDeliverable)
                        isNdf = true;
                    if (info.SecurityType.Equals(FxInfo.SpotForward) || info.SecurityType.Equals(TradeInfo.CrossCurrency))
                    {  
                        // FX or NDF
                        var fx = trade.Product as FX ?? (isNdf ? new NDF() : new FX());
                        var fxTempProvider = new FXTemplateProvider();
                        var ndfTemplateProvider = new NDFTemplateProvider();        
                                                                       
                        fx.IsForward = false;
                        
                        fx.Primary = cp.Primary;
                        fx.Quoting = cp.Quoting;
                        var sd = fxInfo != null ? fxInfo.SettleDate : info.Maturity;
                        fx.PrimarySettleDate = sd;
                        fx.QuotingSettleDate = sd;
                        if (!info.IsMurex)
                        {
                            // handling imagine fx info
                            if (fx.Primary == info.Currency1)
                            {
                                fx.PrimaryAmount = Math.Abs(info.Quantity);
                                fx.QuotingAmount = Math.Abs(info.Nominal1);
                                trade.Quantity = info.Quantity;
                            }
                            else
                            {
                                fx.PrimaryAmount = Math.Abs(info.Nominal1);
                                fx.QuotingAmount = Math.Abs(info.Quantity);
                                trade.Quantity = info.Nominal1;
                            }
                            fx.SpotRate = Math.Abs(fx.Primary.Equals("USD") ? info.Price : 1 / info.Price);
                        }
                        else if (fxInfo != null)
                        {
                            
                            if (fx.Primary == fxInfo.PrimaryCcy)
                            {
                                fx.PrimaryAmount = Math.Abs(fxInfo.PrimaryAmount);
                                fx.QuotingAmount = Math.Abs(fxInfo.QuotingAmount);
                                //trade.Quantity = info.TradeAction.Equals("B") ? -1 : 1;  
                            }
                            else
                            {
                                fx.PrimaryAmount = Math.Abs(fxInfo.QuotingAmount);
                                fx.QuotingAmount = Math.Abs(fxInfo.PrimaryAmount);
                                //trade.Quantity = info.TradeAction.Equals("B") ? 1 : -1;
                            }
                            if (info.TradeAction.Equals("B")) //It means the Buy the M_XPQTYCUR fxinfo.QuotingCcy 
                            {
                                if (fx.Primary == fxInfo.QuotingCcy)
                                {
                                    trade.Quantity = 1;
                                }
                                else trade.Quantity = -1;
                            }
                            else
                            {
                                if (fx.Primary == fxInfo.QuotingCcy)
                                {
                                    trade.Quantity = -1;
                                }
                                else trade.Quantity = 1;

                            }
                            
                            

                            fx.SpotRate = Math.Abs(info.Price);
                            
                        }
                        
                        double amt = fx.PrimaryAmount * fx.SpotRate;
                        if (Math.Abs(amt - fx.QuotingAmount) > 1)
                        {
                            var diff1 = Math.Abs(amt - fx.QuotingAmount);
                            //Check the other way round
                            amt = fx.QuotingAmount * fx.SpotRate;
                            if (Math.Abs(amt - fx.PrimaryAmount) > 1)
                            {
                                var diff2 = amt - fx.PrimaryAmount;
                                var diff = Math.Min(diff1, diff2);
                                sb.Append("Warning Incorrect Amounts for Trade Spot-Forward: TradeID = " + info.TradeID + " Diff = " + diff + "\n");
                            }
                        }

                        var spotDate = fxInfo != null ? cp.SpotDate(fxInfo.TradeDate) : cp.SpotDate(new SimpleDate(trade.TradeTime));            
                        if (spotDate < sd) fx.IsForward = true;
                        trade.Price = fx.SpotRate;
                        trade.Product = fx;
                        if (!isNdf)
                            fxTempProvider.FillTrade(trade, market);
                        else if (fxInfo != null)
                        {
                            var ndCcy = ccy1.IsNonDeliverable ? cp.Primary : cp.Quoting;
                            var ndfixing = cp.Quoting + "/" + cp.Primary;
                            var ndf = fx as NDF;
                            if (ndf == null)
                            {
                                sb.Append(TradeImportHelper.FormatErrorMessage("Instrument was saved as FX while it's NDF", info.HoldingID, info.TradeID, info.Instrument));
                                continue;
                            }
                            ndf.NonDeliverableCurrency = ndCcy;
                            var fix = Env.Current.StaticData.GetFxFixing(ndfixing);
                            if (fix == null)
                            {
                                sb.Append(TradeImportHelper.FormatErrorMessage("FX Fixing Missing for  " + ndfixing, info.HoldingID, info.TradeID, info.Instrument));
                                continue;
                            }
                            ndf.FXFixingName = fix.Name;
                            ndf.FXFixingOffset = fix.FixingLag;
                            ndf.FXFixingDate = fxInfo.FixingDate;
                            if (!double.IsNaN(fxInfo.FixingRate) && fxInfo.FixingRate != 0)
                                ndf.FixingValue = fxInfo.FixingRate;
                            ndfTemplateProvider.FillTrade(trade, market);
                        }
                        
                    }     
                    else if (fxoInfo != null)
                    {
                        // fx option  
                        var fxo = trade.Product as FXOption ?? new FXOption();
                        var optionInfo = fxo.OptionInfo;
                        var fxoTempProvider = new FXOptionTemplateProvider();
                        trade.Product = fxo;                        
                        fxo.ExpiryDate = info.Maturity;
                        fxo.Primary = cp.Primary;
                        fxo.Quoting = cp.Quoting;
                        fxo.IsBuy = info.TradeAction.Equals("B") ? true : false;
                        if (fxo.Primary.Equals(info.Currency1))
                        {
                            fxo.PrimaryAmount = Math.Abs(info.Nominal1);
                            fxo.QuotingAmount = Math.Abs(info.Nominal2);
                            
                        }
                        else
                        {
                            fxo.PrimaryAmount = Math.Abs(info.Nominal2);
                            fxo.QuotingAmount = Math.Abs(info.Nominal1);
                        }
                        fxo.OptionStrike = Math.Abs(info.Strike);
                        fxo.ExerciseSettlement = fxoInfo.SettlementType;
                        if (fxo.ExerciseSettlement == ExerciseSettlement.None)
                        {
                            sb.Append("Warning SettlementType is None: TradeID=" + info.TradeID + " " + info.Instrument + "\n");
                        }
                        fxo.InitialPremiumAmount = fxoInfo.OptionPremium;
                        fxo.InitialPremiumDate = fxoInfo.PremiumDate;
                        fxo.InitialPremiumCurrency = fxoInfo.PremiumCcy;
                        fxo.InitialPremiumPayReceive = info.TradeAction.Equals("B") ? PayReceive.Pay : PayReceive.Receive;                        
                        fxo.OptionType = info.PutCall == "P" ? OptionType.Put : OptionType.Call;
                        fxo.SettleCurrency = info.Currency1;
                        fxo.SettlementMarketPlaces = cp.DefaultMarketPlaces;
                        fxo.SettlementLag = cp.SpotLag;
                        fxo.ExpireCity = MapExpireCity(fxoInfo.CutOff);
                        double amt = fxo.PrimaryAmount * fxo.OptionStrike;
                        if (Math.Abs(amt - fxo.QuotingAmount) > 1)
                        {
                            sb.Append("Warning Incorrect Amounts for Trade FXOption: TradeID=" + info.TradeID + " Diff = " + (amt - fxo.PrimaryAmount) + "\n");
                        }

                        fxo.ExerciseType = OptionExerciseType.European;
                        fxoTempProvider.FillTrade(trade, market);
                    }
                    TradeImportHelper.SetParties(trade, info);
                    trades.Add(trade);
                }
                catch (NullReferenceException x)
                {
                    Logger.Error("Import FX", x);
                    sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message + " " + x.StackTrace, info.HoldingID, info.TradeID, info.Instrument));
                }
                catch (Exception x)
                {
                    Logger.Error("Import FX", x);
                    sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message + " " + x.StackTrace, info.HoldingID, info.TradeID, info.Instrument));
                }
                
            }
            sb.Append("Imported " + trades.Count + " FX Trades\n");
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            return trades;
        }
コード例 #8
0
        internal static IList<TradeInfo> ReadData(FilterTrade filterTrade, FilterData filterData, StringBuilder sb)
        {
            var fileName= filterData.FolderName;

            if (!File.Exists(fileName))
            {
                if (sb != null) sb.Append("File " + fileName + " not found\n");
                return new List<TradeInfo>();
            }
            
            var list = new List<TradeInfo>();
            using (var stream = File.OpenRead(fileName))
            {
                using (var reader = new StreamReader(stream))
                {
                    string line;
                    var header = reader.ReadLine();
                    if (string.IsNullOrEmpty(header))
                    {
                        Logger.Error("[Error Read File] Empty Headers ... ");
                        return new List<TradeInfo>();
                    }
                    var headers = new Dictionary<string, int>();
                    using (var csvParser = new TextFieldParser(new StringReader(header)))
                    {
                        csvParser.SetDelimiters(new[] { "," });
                        var hss = csvParser.ReadFields();
                        int idx = 0;
                        foreach (var s in hss)
                        {
                            headers[s.Trim()] = idx++;
                        }
                    }
                    //var hss = header.Split(Separators, StringSplitOptions.None);
                    int skipped1 = 0, skipped2 = 0, skipped3 = 0, skipped4 = 0, skipped5 = 0;
                    var count = 0;
                    while ((line = reader.ReadLine()) != null)
                    {
                        count++;
                        //Parse Line here
                        if (string.IsNullOrEmpty(line)) break; //we are at end
                        var tinfo = new TradeInfo(line, headers);

                        // start filtering out bad entries
                        // 1. Symbol not found
                        // 2. Duplicated entries
                        // 3. Trade has expired
                        // 4. Quantity/Nominal is 0
                        // 5. Security type not support

                        var isblankedOut = tinfo.IsMurex && tinfo.SecurityType.Equals(FxInfo.SpotForward);  // murex spotforward info is blanked out
                        var isSymbolBlankedOut = isblankedOut || (tinfo.IsMurex && tinfo.SecurityType.Equals(SwapInfo.CcySwap));
                        if (tinfo.ReportDate.IsNull) tinfo.ReportDate = filterData.LiveDate;
                        if (!isSymbolBlankedOut && tinfo.Symbol == null)
                        {
                            var msg = FormatReadFileMessage("Product Symbol not found", tinfo.Instrument, tinfo.HoldingID, tinfo.TradeID);
                            Logger.Warn("Trade Import " + msg);
                            //sb.Append(msg);
                            skipped1++;
                            continue;
                        }
                        if (tinfo.IsMurex && tinfo.HoldingID != null)
                        {
                            // right now only Murex has id for each entry
                            if (filterData.ExistingIds.Contains(tinfo.HoldingID))
                            {
                                var msg = FormatReadFileMessage("Duplicated HoldingID found", tinfo.Instrument, tinfo.HoldingID, tinfo.TradeID);
                                //sb.Append(msg);
                                Logger.Warn("Trade Import " + msg);
                                skipped2++;
                                continue;
                            }
                            else
                                filterData.ExistingIds.Add(tinfo.HoldingID);
                        }
                        if (!tinfo.Maturity.IsNull && filterData.LiveDate > tinfo.Maturity)
                        {
                            if (tinfo.IsMurex || tinfo.Maturity.Month != filterData.LiveDate.Month)
                            {
                                var msg = FormatReadFileMessage("Trade has matured before live date", tinfo.Instrument, tinfo.HoldingID, tinfo.TradeID);
                                //sb.Append(msg);
                                Logger.Warn("Trade Import " + msg);
                                skipped3++;
                                continue;
                            }
                        }
                        if (tinfo.Nominal1 == 0 && tinfo.Nominal2 == 0)
                        {
                            var msg = FormatReadFileMessage("Trade's quantity/nominal is 0", tinfo.Instrument, tinfo.HoldingID, tinfo.TradeID);
                            //sb.Append(msg);
                            Logger.Warn("Trade Import " + msg);
                            skipped4++;
                            continue;
                        }
                        if (!tinfo.IsMurex && !SecurityTypeSupported(tinfo.SecurityType))
                        {
                            var msg = FormatReadFileMessage("Security type(" + (tinfo.SecurityType ?? string.Empty) + ") is not supported by the importer",
                                tinfo.Instrument, tinfo.HoldingID, tinfo.TradeID);
                            Logger.Warn("Trade Import " + msg);
                            skipped5++;
                            continue;
                        }
                        list.Add(tinfo);
                    }
                    var skipped = skipped1 + skipped2 + skipped3 + skipped4 + skipped5;
                    sb.Append(skipped + "/" + count + " entries are skipped at file loading\n");
                    if (skipped1 > 0) sb.Append("Skip symbol is null " + skipped1 + "\n");
                    if (skipped2 > 0) sb.Append("Skip duplicated entries " + skipped2 + "\n");
                    if (skipped3 > 0) sb.Append("Skip trade has expired " + skipped3 + "\n");
                    if (skipped4 > 0) sb.Append("Skip 0 quality/norminal " + skipped4 + "\n");
                    if (skipped5 > 0) sb.Append("Skip security type not identified " + skipped5 + "\n");
                }
            }
            filterData.Data = list;
            return list;
        }
コード例 #9
0
        virtual  public IList<Trade> Import(Market market, Feed feed, StringBuilder sb, FilterData filterData)
        {
            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, sb);
            var trades = new List<Trade>();
            var dict = new Dictionary<TradeInfo, Trade>();
            var exceptions = new List<Exception>();
            //var futures = new Dictionary<string, Future>();
            var allContracts = Env.Current.MarketData.GetAllContracts(null);
            var contracts = new List<Contract>();
            var isETO = IsListedOption();
            foreach (var c in allContracts)
            {
                if (isETO != c.IsETO) continue;
                if (c.TickerSymbol != null)
                {
                    contracts.Add(c);
                }
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;
            //int skipped = 0;
            foreach (var info in infos)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                if (FilterInfo(info)) continue;
                var trade = new Trade();
                if (!TradeImportHelper.InitTrade(trade, info, filterData, sb)) continue;
                string display;
                var bbgTicker = info.IsMurex ? TryGetTicker(info, out display) : TryGetTicker2(info, out display);
                //info.Identifier2 + info.Symbol.Substring(info.Symbol.Length - 2);
                if (!string.IsNullOrEmpty(bbgTicker))
                {
                    var prod = Env.Current.Trade.GetProductByCode(Product.DefaultTicker, bbgTicker) as Future;
                    if (!info.IsMurex && prod == null)
                        prod = Env.Current.Trade.GetProductByCode(Product.RtDefaultTicker, info.Symbol) as Future;
                    if (prod != null && prod.Contract != null)
                    {
                        if (IsBadMaturity(info.Maturity, prod.ContractMaturity, info.IsMurex))
                        {
                            sb.Append("Warning: " + info.Instrument + " Maturity " + info.Maturity + " is different than System created " + prod.ContractMaturity + "\n");
                            SetSourceProperty(prod, info);
                        }
                        else if (!info.IsMurex)
                        {
                            TradeImportHelper.SetAndSaveRtDefaultTicker(prod, info.Symbol);
                        }
                        info.Security = prod;
                        dict[info] = trade;
                        continue;
                    }
                    if (prod != null && prod.Contract == null)
                    {
                        Logger.Warn("Future Import Bad future found skipping null contract " + bbgTicker + prod.Id);
                    }
                    Contract contract;
                    var fut = FindFuture(feed, info, contracts, bbgTicker, out contract);                   
#if false
                    if (contract == null)
                    {
                        var toSave = FindContracts(feed, bbgTicker, info.ReportDate);
                        if (toSave != null && toSave.Count > 0)
                        {
                            var saved = new List<Contract>();
                            var errors = new List<string>();
                            var warnings = new List<string>();
                            foreach (var c in toSave)
                            {
                                errors.Clear();
                                warnings.Clear();
                                c.IsValid(warnings, errors);
                                if (errors.Count == 0)
                                    try
                                    {
                                        var cc = Env.Current.MarketData.SaveAndLoadContract(c);
                                        
                                        saved.Add(cc);
                                    }
                                    catch
                                    {
                                        sb.Append("Saving Contract fails: " + bbgTicker + "\n");
                                        continue;
                                    }
                                else
                                {
                                    sb.Append("Snapped Contract is not valid: " + bbgTicker + " Error: " + errors[0] + "\n");
                                    continue;
                                }
                            }
                            if(saved.Count > 0) fut = FindFuture(feed, info, saved, bbgTicker);
                        }
                    }
#endif
                    if (fut != null)
                    {
                        if (!info.IsMurex && fut.ContractMaturity < filterData.LiveDate)
                        {
                            sb.Append(TradeImportHelper.FormatErrorMessage("Contract expiry date " + TradeImportHelper.FormatDate(fut.ContractMaturity) 
                                + " is before live date", info.HoldingID, info.TradeID, info.Instrument));
                            continue;
                        }
                        if (fut.Id <= 0)
                        {
                            try
                            {
                                fut = (Future)Env.Current.Trade.SaveAndLoadProduct(fut);
                            }
                            catch 
                            {
                                sb.Append(TradeImportHelper.FormatErrorMessage("Cannot save created " + info.SecurityType, info.HoldingID, info.TradeID, info.Instrument));
                                continue;
                            }
                        }
                        if (IsBadMaturity(info.Maturity, fut.ContractMaturity, info.IsMurex))
                        {
                            sb.Append("Warning: " + info.Instrument + " Maturity " + info.Maturity + " is different than System created " + fut.ContractMaturity + "\n");
                            SetSourceProperty(fut, info);
                        }
                        else if (!info.IsMurex)
                            TradeImportHelper.SetAndSaveRtDefaultTicker(fut, info.Symbol);
                        info.Security = fut;
                        dict[info] = trade;
                        
                    }
                    else
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("Cannot Find " + info.SecurityType + " " + display, info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                }
                else
                {
                    sb.Append(TradeImportHelper.FormatErrorMessage("Symbol not found", info.HoldingID, info.TradeID, info.Instrument));
                }
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            var tempProvider = new FutureTemplateProvider();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(dict.Count, "Initializing Trades");
            count = 0;
            foreach (var kvp in dict)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                var trade = kvp.Value;
                var info = kvp.Key;
                if (info.Security != null)
                {
                    if (filterData.Currencies != null && filterData.Currencies.Count > 0 && info.Security.Currency != null
                            && !filterData.Currencies.Contains(info.Security.Currency))
                        continue;
                    if (((Future)info.Security).Contract.RtTickerSymbol == null && info.Security.GetProperty(Product.RtDefaultTicker) == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("Trade Security found but contract's Rt Ticker Symbol is missing", 
                            info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                    trade.Product = info.Security;
                    try
                    {
                        trade.PriceType = info.Security.QuoteName.QuoteType;
                        if (Utilities.IsPercentageQuotation(trade.PriceType) /*|| trade.PriceType == QuotationType.Cents*/)
                        {
                            trade.Price = trade.Price / 100;
                            info.MarketPrice /= 100;
                        }
                        tempProvider.FillTrade(trade, market);
                        trade.Product = info.Security;
                        if (trade.Fees != null)
                        {
                            if (trade.Product.Currency == null)
                            {
                                sb.Append(TradeImportHelper.FormatErrorMessage("Invalid Trade Product Ccy is NULL", info.HoldingID, info.TradeID, info.Instrument));
                                continue;
                            }
                            foreach (var f in trade.Fees)
                            {
                                f.Currency = trade.Product.Currency;
                                f.Date = trade.SettlementDate;
                            }
                        }
                        
                        trades.Add(kvp.Value);
                    
                    }                   
                    catch (Exception x)
                    {
                        Logger.Error("Future Import", x);
                        sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message + " " + x.StackTrace, info.HoldingID, info.TradeID, info.Instrument));
                    }
#if false
                    if (filterData.SaveQuote)
                    {
                        var q = TradeImportHelper.SaveAndLoadQuote(trade, market, info.ReportDate, info.MarketPrice, exceptions);
                        if (q != null)
                            sb.Append("[Quote Saved] " + q.Name + "\n");
                        else
                            sb.Append("[Quote Not Saved] HoldingID = " + info.SourceID + " Symbol = " + info.Symbol + "\n");
                    }
#endif
                }
                else
                {
                    sb.Append(TradeImportHelper.FormatErrorMessage(" Security not found", info.HoldingID, info.TradeID, info.Instrument));
                }                   
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            var ptype = IsListedOption() ? " ListedOptions\n" : " Futures\n";
            sb.Append("Imported " + trades.Count + ptype);
            return trades;
        }
コード例 #10
0
        public IList<Trade> Import(Market market, Feed feed, StringBuilder sb, FilterData filterData)
        {
            var trades = new List<Trade>();
            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, sb);
            var dict = new Dictionary<TradeInfo, Trade>();
            var symbols = new Dictionary<string, Product>();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;

            foreach (var info in infos)
            {
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                if (FilterInfo(info)) continue;
                var trade = new Trade();
                if (!TradeImportHelper.InitTrade(trade, info, filterData, sb)) continue;
                dict[info] = trade;
                var symbol = info.Symbol;       // symbol is cusip/isin/sedol
                if (symbol != null) symbol = symbol.Trim();
                if (!string.IsNullOrEmpty(symbol))
                {
                    var p = Env.Current.Trade.GetProductByCode(Product.CusipProductCode, symbol) ??
                        Env.Current.Trade.GetProductByCode(Product.IsinProductCode, symbol) ??
                        Env.Current.Trade.GetProductByCode(Product.SedolProductCode, symbol);
                    if (p == null)
                    {
                        if (symbols.Count < MaxEqs)
                        {
                            symbols[symbol] = null;
                        }
                        else dict[info] = null; //Remove it
                    }
                    else info.Security = p;
                }
                else
                    sb.Append(TradeImportHelper.FormatErrorMessage("Symbol is null", info.HoldingID, info.TradeID, info.Instrument));
            }
            if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();

            // snapping equities by cusip, isin or sedol
            var snapped = new List<Product>();
            var exceptions = new List<Exception>();
            if (symbols.Count > 0 && feed != null)
            {
                var prodcs = new List<Product>();
                foreach (var s in symbols.Keys)
                {
                    var eq = new Equity();
                    eq.SetProperty(Product.DefaultTicker, s);
                    prodcs.Add(eq);
                }
                sb.Append("Snapping Equities By Symbol " + symbols.Count + " " + Utilities.ToString(new List<string>(symbols.Keys)) + "\n");
                try
                {
                    FeedImportSecurityTaskExecutor.SnapProducts("Equity.FeedMapping.xml", prodcs, feed, exceptions, true, snapped);
                    // might need other equity type snapping
                }
                catch (Exception x)
                {
                    sb.Append(x.ToString());
                }
            }
            foreach (var p in snapped)
            {
                var isin = p.Isin;
                var cusip = p.Cusip;
                var sedol = p.Sedol;
                if (p.Id > 0)
                {
                    if (symbols.ContainsKey(cusip))
                        symbols[cusip] = p;
                    else if (symbols.ContainsKey(isin))
                        symbols[isin] = p;
                    else if (symbols.ContainsKey(sedol))
                        symbols[sedol] = p;
                }
            }
            if (snapped.Count > 0) sb.Append("Snapped " + snapped.Count + " equities by Symbol\n");
            var tempEquityProvider = new EquityTemplateProvider();

            if (filterData.Progress != null) filterData.Progress.AnalysisInit(dict.Count, "Initializing Trades");
            count = 0;
            foreach (var kvp in dict)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                var info = kvp.Key;
                var trade = kvp.Value;
                if (trade == null) continue;
                if (info.Security == null)
                {
                    if (!string.IsNullOrEmpty(info.Symbol) && symbols.ContainsKey(info.Symbol))
                        info.Security = symbols[info.Symbol];
                }
                if (info.Security != null)
                {
                    if (info.Security.Currency == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("Bad Security currency is NULL", info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                    if (filterData.Currencies != null && filterData.Currencies.Count > 0 && !filterData.Currencies.Contains(info.Security.Currency))
                    {
                        continue;
                    }

                    var equity = info.Security as Equity;
                    if (CreateEquity(trade, info, market, sb, tempEquityProvider))
                    {
                        trades.Add(trade);
                    }
                }
                else
                    sb.Append(TradeImportHelper.FormatErrorMessage("Security not found", info.HoldingID, info.TradeID, info.Symbol));

            }
            if (exceptions.Count > 0)
            {
                sb.Append("Exceptions snapping equities:\n");
                foreach (var ex in exceptions) sb.Append(ex.Message + " " + ex + "\n");
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            sb.Append("Imported " + trades.Count + " Equities\n");

            return trades;

        }
コード例 #11
0
        public IList<Trade> Import(Market market, Feed feed,  StringBuilder sb, FilterData filterData)
        {
            var trades = new List<Trade>();

            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, sb);
          
            var dict = new Dictionary<TradeInfo, Trade>();
            var symbols = new Dictionary<string, Product>();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;
            //int skipped = 0;
            foreach (var info in infos)
            {
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                if (FilterInfo(info)) continue;
                var trade = new Trade();
                
                if (!TradeImportHelper.InitTrade(trade, info, filterData, sb)) continue;
                dict[info] = trade;
                //trades.Add(trade);
                var symbol = info.Symbol;           // symbol is either cusip/isin, or r-symbol
                if (symbol != null) symbol = symbol.Trim();
                if (!string.IsNullOrEmpty(symbol))
                {
                    var p = Env.Current.Trade.GetProductByCode(Product.CusipProductCode, symbol) ??
                        Env.Current.Trade.GetProductByCode(Product.IsinProductCode, symbol);
                    if (p == null)
                    {
                        if (symbols.Count < MaxBonds)
                        {
                            symbols[symbol] = null; // bond;
                        }
                        else dict[info] = null;//Remove it
                    }
                    else info.Security = p;
                }
                else 
                    sb.Append(TradeImportHelper.FormatErrorMessage("Symbol is null", info.HoldingID, info.TradeID, info.Instrument));
            }
            if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            //Now Get Bonds by Cusips and by Isins
            var snapped = new List<Product>();
            var exceptions = new List<Exception>();
            if (symbols.Count > 0 && feed != null)
            {
                var prodcs = new List<Product>();
                foreach (var s in symbols.Keys)
                {
                    var bond = new Bond();
                    bond.SetProperty(Product.DefaultTicker, s);
                    prodcs.Add(bond);
                }
                sb.Append("Snapping Bonds By Symbol " + symbols.Count + " " + Utilities.ToString(new List<string>(symbols.Keys)) + "\n");
                try{
                    FeedImportSecurityTaskExecutor.SnapProducts("Bond.FeedMapping.xml", prodcs, feed, exceptions, true, snapped);
                }
                catch (Exception x)
                {
                    sb.Append(x.ToString());
                }
            }
            foreach (var p in snapped)
            {
                var isin = p.Isin;
                var cusip = p.Cusip;
                if (p.Id > 0)
                {
                    if (symbols.ContainsKey(isin))
                        symbols[isin] = p;
                    else if (symbols.ContainsKey(cusip))
                        symbols[cusip] = p;
                }
            }
            if (snapped.Count > 0) sb.Append("Snapped " + snapped.Count + " bonds by Symbol\n");
            var tempBondProvider = new BondTemplateProvider();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(dict.Count, "Initializing Trades");
            count = 0;
            //int fxbcount = 0;
            foreach (var kvp in dict)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                var info = kvp.Key;
                var trade = kvp.Value;
                if (trade == null)
                {
                    //skipped++;
                    continue;
                }
                if (info.Security == null)
                {
                    if (!string.IsNullOrEmpty(info.Symbol) && symbols.ContainsKey(info.Symbol))
                    {
                        info.Security = symbols[info.Symbol];
                    }
                }
                if (info.Security != null)
                {
                    if (info.Security.Currency == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("Bad Security currency is NULL", info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                    if (filterData.Currencies != null && filterData.Currencies.Count > 0 && !filterData.Currencies.Contains(info.Security.Currency))
                    {
                        continue;
                    }
                    
                    var bond = info.Security as Bond;
                    if (bond != null && bond.IsFixedRate == false)
                    {
                        var rateInex = Env.Current.StaticData.GetRateIndex(bond.Leg.IndexCurrency, bond.Leg.RateIndex);
                        if (rateInex == null)
                        {
                            sb.Append(TradeImportHelper.FormatErrorMessage("Missing RateIndex " + bond.Leg.IndexCurrency + " " + bond.Leg.RateIndex, 
                                info.HoldingID, info.TradeID, info.Instrument));
                            continue;
                        }
                    }
                    
                    if (CreateBond(trade, info, filterData, market, sb, tempBondProvider))
                        trades.Add(trade);
                    
                }
                else
                    sb.Append(TradeImportHelper.FormatErrorMessage("Security not found", info.HoldingID, info.TradeID, info.Symbol));
                

            }
            if (exceptions.Count > 0)
            {
                sb.Append("Exceptions snapping bonds:\n");
                foreach (var ex in exceptions) sb.Append(ex.Message + " " + ex + "\n");
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            sb.Append("Imported " + trades.Count + " Bonds\n");


            return trades;
        }
コード例 #12
0
 static bool CreateBond(Trade trade, TradeInfo info, FilterData filterData, Market market, StringBuilder sb, TemplateProvider provider)
 {
     try
     {
         var bond = (Bond)info.Security;
         trade.Product = info.Security;
         // assuming file has all clean price
         trade.PriceType = QuotationType.CleanPrice;
         if (bond.IssueDate > trade.SettlementDate)
             trade.SettlementDate = bond.IssueDate;
         trade.Price /= 100;
         info.MarketPrice /= 100;
         
         trade.Quantity = info.Nominal1/bond.FaceValue;
         if (!double.IsNaN(info.Quantity))
         {
             if (trade.Quantity != info.Quantity)
                 sb.Append("Warning Bad Quantity for Trade" + " Symbol " + info.Symbol + "\n");
         }
         provider.FillTrade(trade, market);
         if (Double.IsNaN(trade.Accrual))
         {
             trade.Accrual = 0;
             sb.Append("Warning Bad Accrual for Trade" + " Symbol " + info.Symbol + "\n");
         }              
         return true;
     }            
     catch (Exception x)
     {
         Logger.Error("Import Bond", x);
         sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message, info.HoldingID, info.TradeID, info.Instrument));
         return false;
     }
 }
コード例 #13
0
        public IList<Trade> Import(Market market, Feed feed, StringBuilder sb, FilterData filterData)
        {
            var infos = filterData.Data ?? TradeImportHelper.ReadData(FilterInfo, filterData, sb);
            var trades = new List<Trade>();
            var dict = new Dictionary<TradeInfo, Trade>();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            int count = 0;
            foreach (var info in infos)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                if (FilterInfo(info)) continue;
                if (filterData.Currencies != null && filterData.Currencies.Count > 0 && !filterData.Currencies.Contains(info.Currency1)) continue; 
                if (info.Otc == null || !(info.Otc is SwaptionInfo) || info.Otc.IsEmpty())
                {
                    sb.Append(TradeImportHelper.FormatErrorMessage("Swaption info not found or not complete", info.HoldingID, info.TradeID, info.Instrument));
                    continue;
                }
                var alias =
                  Env.Current.StaticData.GetAlias(new Alias
                  {
                      AliasType = "Template",
                      EntityId = 0,
                      Source = SwaptionInfo.AliasSourceSwpt,
                      SourceReference = info.Instrument
                  });
                if (alias != null)
                {
                    var sinfo = info.Otc as SwapInfo;
                    sinfo.Generator = Env.Current.StaticData.GetTemplate(Template.Shared, "Swaption", alias.Name);
                    if (sinfo.Generator == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("Generator Template for Swaption " + alias.Name + " not found", info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                }
                else
                {
                    sb.Append(TradeImportHelper.FormatErrorMessage("Alias for Generator not found", info.HoldingID, info.TradeID, info.Instrument));
                    continue;
                }

                if (filterData.Progress != null) filterData.Progress.AnalysisProgress(count, "");
                var trade = new Trade();
                if (!TradeImportHelper.InitTrade(trade, info, filterData, sb)) continue;                 
                dict[info] = trade;
            }
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            if (filterData.Progress != null) filterData.Progress.AnalysisInit(infos.Count, "Building Products");
            count = 0;
            foreach (var kvp in dict)
            {
                if (filterData.Progress != null && filterData.Progress.IsCanceled) return new List<Trade>();
                count++;
                if (filterData.Progress != null && count % 5 == 0) filterData.Progress.AnalysisProgress(count, "");
                var info = kvp.Key;
                var trade = kvp.Value;
                var swInfo = info.Otc as SwaptionInfo;
                try
                {
                    var copy = trade.Id > 0 ? (Trade)trade.Clone() : null;

                    if (swInfo.Generator != null)
                    {
                        var tradeType = trade.TradeType;
                        swInfo.Generator.Apply(trade, new SimpleDate(swInfo.TradeDate), market);
                        trade.TradeType = tradeType;
                    }
                    if (trade.Product == null)
                    {
                        sb.Append(TradeImportHelper.FormatErrorMessage("No product generated after applying Generator", info.HoldingID, info.TradeID, info.Instrument));
                        continue;
                    }
                    
                    if (copy != null)
                    {
                        TradeImportHelper.CopyTrade(copy, trade);
                        trade.MirrorBookId = copy.MirrorBookId;
                        trade.MirrorTradeId = copy.MirrorTradeId;
                    }
                    TradeImportHelper.SetParties(trade, info);
                    var swaption = (Swaption)trade.Product;
                    trade.SettlementDate = swInfo.SettleDate;
                    swaption.ExpiryDate = swInfo.OptionMaturity;
                    swaption.ExerciseType= info.OptionStyle.Equals("E") ? OptionExerciseType.European :  OptionExerciseType.American;
                    swaption.ExerciseSettlement = swInfo.SettlementType;              
                    swaption.IsBuy = info.TradeAction.Equals("B");
                    swaption.InitialPremiumPayReceive = swaption.IsBuy ? PayReceive.Pay : PayReceive.Receive;
                    swaption.InitialPremiumAmount = Math.Abs(swInfo.FeeAmount);
                    swaption.InitialPremiumCurrency = swInfo.FeeCurrency;
                    swaption.InitialPremiumDate = swInfo.SettleDate;
                    if (swInfo.SettleDate >= swInfo.OptionMaturity)
                        swaption.FirstPremium.IsForwardStart = true;
                    var swap = swaption.Swap;
                    // Assuming trade action tells direction of underlying swap (probably putcall)
                    if (info.PutCall.Equals("C") && swap.ReceiveLeg.IsFixedRate)
                        swap.SwitchLegs();
                    else if (info.PutCall.Equals("P") && !swap.ReceiveLeg.IsFixedRate)
                        swap.SwitchLegs();                    
                    var fixedLeg = swap.PayLeg.IsFixedRate ? swap.PayLeg : swap.ReceiveLeg;
                    var floatLeg = swap.PayLeg.IsFixedRate ? swap.ReceiveLeg : swap.PayLeg;
                    InitLegs(swap.PayLeg, swap.ReceiveLeg, info);

                    if (trade.CcpId != 0)
                    {
                        //Nothing to do let the EventScheduleHelper insert the StepIn

                    }
                    else
                    {
                        if (fixedLeg.StartDate < trade.GetTradingDay())
                        {
                            fixedLeg.FirstFullCalculationPeriod = true;
                            fixedLeg.FirstCalculationPeriodStart = fixedLeg.StartDate;
                            floatLeg.FirstFullCalculationPeriod = true;
                            floatLeg.FirstCalculationPeriodStart = floatLeg.StartDate;
                            fixedLeg.StartDate = trade.GetTradingDay();
                            floatLeg.StartDate = fixedLeg.StartDate;
                        }
                    }

                    if (swInfo.Generator != null)
                        swInfo.Generator.Provider.FillTrade(trade, market);
                    if (swInfo.Generator != null)
                        trade.SetProperty("TemplateName", swInfo.Generator.Name);
                    trade.SetProperty(TradeImportHelper.GeneratorProp, info.Instrument);

                    //Add UpFront Fee  
                    /*
                    if (Math.Abs(info.PremiumAmount) > Utilities.Epsilon)
                    {
                        var fee = new Fee
                        {
                            Date = swInfo.SettleDate,
                            FeeType = FeeType.Upfront,
                            PartyId = trade.PartyId,
                            Currency = trade.Product.Currency,
                            Amount = Math.Abs(info.PremiumAmount),
                            PayReceive = info.PremiumAmount < 0 ? PayReceive.Pay : PayReceive.Receive
                        };
                        if (trade.Fees == null) trade.Fees = new List<Fee>();
                        trade.Fees.Add(fee);
                    }*/

                    trades.Add(trade);
                }
                catch (Exception x)
                {
                    Logger.Error("Import Swaption", x);
                    sb.Append(TradeImportHelper.FormatErrorMessage("Fill Trade " + x.Message + " " + x.StackTrace, info.HoldingID, info.TradeID, info.Instrument));
                }
            }
            sb.Append("Imported " + trades.Count + " Swaptions\n");
            if (filterData.Progress != null) filterData.Progress.AnalysisDone();
            return trades;
        }
コード例 #14
0
        private void ImportProductButtonClick(object sender, EventArgs e)
        {
            var all = checkBoxAllProducts.Checked;
            var sb = new StringBuilder();
            tradeOutputText.Lines = null;
            var trades = new List<Trade>();
            //var excludeTradeIds = new List<long>();
            var folderName = textBoxTradeFolder.Text;
            var ccys = Utilities.ToList(checkedComboBoxEditCurrency.Text);
            //var tranIds = Utilities.ToList(textBoxTransIds.Text);               // ??
            var stgys = Utilities.ToList(checkedComboBoxEditStrategy.Text);     // disabled
            var les = Utilities.ToList(checkedComboBoxEditLegalEntity.Text);    // disabled
            var sources = Utilities.ToList(checkedComboBoxEditSources.Text.ToLower());
            var liveDate = GetLiveDate();
            importProductButton.Enabled = false;
            buttonCancel.Enabled = true;
            bool onlyNewTrade = checkBoxnlyNewTrade.Checked;

            var progress = new ImportProgress { ProgressBar = saveProgressBar, IsCanceled = false };
            importProductButton.Tag = progress;

            var filterData = new FilterData
            {
                Currencies = ccys,
                //TransIds = tranIds,
                OnlyNewTrades = onlyNewTrade,
                LiveDate = liveDate,
                Strategies = stgys,
                Progress = progress,
                FolderName = folderName
            };

            var existIds = Env.Current.Trade.GetTradePropertyValues(TradeImportHelper.SymmetryTranId, false);
            filterData.ExistingTransIds = new List<string>();
            filterData.ExistingIds = new List<string>();
            foreach (var id in existIds.Keys) filterData.ExistingTransIds.Add(id);

            tradeOutputText.Text = "";
            var feed = checkBoxUseFeed.Checked ? GetFeed() : null;
            // if (TradeImportHelper.NewMethod) filterData.Data = TradeImportHelper.LoadFile(fileName, filterData);
            var market = GetMarket();
            var importers = BuildImporters();
            var thread = new Thread(() =>
            {
                try
                {
                    var lst = new List<string>();
                    lst.AddRange(all ? AllProducTypes : Utilities.ToList(checkedComboBoxEditProduct.Text).ToArray());
                    string[] files = Directory.GetFiles(filterData.FolderName, "*.csv");
                    var filesSorted = SortFiles(files, sources);        // remove not used and sort files in the folder
                    foreach (var f in filesSorted)
                    {
                        filterData.FolderName = f;        // replace with file name
                        filterData.Data = null;

                        sb.Append("-------- Importing from " + Path.GetFileName(f) +
                                      " --------\n");
                        foreach (var ci in lst)
                        {
                            if (progress.IsCanceled) return;


                            Invoke(new MethodInvoker(() =>
                            {
                                tradeOutputText.AppendText(" Importing " + ci + "\n");
                            }));
                            ITradeImportHelper importer;
                            if (importers.TryGetValue(ci, out importer))
                            {
                                trades.AddRange(importer.Import(market, feed, sb, filterData));
                            }
                            else
                            {
                                Logger.Error("Cannot Find Importer for " + ci);
                            }
                        }

                    }

                    //Filter Trades
                    //var newtrades = trades.Where(t => t.Id <= 0 || t.Status != "Matured").ToList();
                    //trades = newtrades;
                    /*
                    var ssb = new StringBuilder();
                    foreach (var s in filterData.MissingEntities)
                    {
                        ssb.Append("Missing Entity " + s + "\n");
                        sb.Append("Missing Entity " + s + "\n");
                    }
                    foreach (var s in filterData.MissingStrategies)
                    {
                        ssb.Append("Missing Strategy " + s + "\n");
                        sb.Append("Missing Strategy " + s + "\n");
                    }
                    if (ssb.Length > 0)
                        Invoke(new MethodInvoker(() => MessageBox.Show(ssb.ToString())));
                     */
                    //Filter using IsValidInput
                    {
                        var validTrades = new List<Trade>();
                        var errors = new List<string>();
                        var warnings = new List<string>();
                        sb.Append("-------------------- Validation Check --------------------\n");
                        foreach (var t in trades)
                        {
                            try
                            {
                                errors.Clear();
                                warnings.Clear();
                                bool isValid = t.IsValid(warnings, errors);
                                var id = t.GetProperty(TradeImportHelper.SymmetryTranId) ?? "N/A";
                                var sid = t.GetProperty(TradeImportHelper.MurexTradeId) ?? "N/A";
                                if (!isValid)
                                {
                                    foreach (var err in errors)
                                        sb.Append("[IsValid Error]   Trade= ").Append(t.Id).Append(" HoldingID= ").Append(id).Append(" TradeId= ").Append(sid).
                                            Append(" type= ").Append(t.Product.GetType().Name).Append(" ").Append(err).Append("\n");
                                }
                                else
                                {
                                    foreach (var err in warnings)
                                        sb.Append("[IsValid Warning] Trade= ").Append(t.Id).Append(" HoldingID= ").Append(id).Append(" TradeId= ").Append(sid).
                                            Append(" type= ").Append(t.Product.GetType().Name).Append(" ").Append(err).Append("\n");
                                    validTrades.Add(t);
                                }
                            }
                            catch (Exception xx)
                            {
                                Logger.Error("IsValid Input " + t.Id + " ", xx);
                            }
                        }
                        trades = validTrades;
                    }
                    sb.Append("\nImported Trades Count in all: " + trades.Count + "\n");
                    saveProductButton.Tag = trades;
                    /*
                    if (excludeTradeIds.Count != 0)
                    {
                        foreach (long id in excludeTradeIds)
                        {
                            var trade = Env.Current.Trade.GetTrade(id);
                            if (trade == null) continue;
                            if (trade.Status == "Canceled") continue;
                            trade.Action = "Cancel";
                            trade.SetProperty("InitialId", Convert.ToString(trade.Id));
                            trades.Add(trade);
                        }
                    }*/
                    if (progress.IsCanceled) return;
                    Invoke(new MethodInvoker(() =>
                    {
                        MessageBox.Show(trades.Count +
                                        " trades Imported");
                        tradeOutputText.Lines =
                            sb.ToString().Split('\n');
                        if (trades.Count > 0)
                            ShowTrades(trades, null);
                    }));
                }
                catch (Exception x)
                {
                    Logger.Error(x);
                    Invoke(new MethodInvoker(() => MessageBox.Show(x.ToString())));
                }
                finally
                {
                    Invoke(new MethodInvoker(() =>
                    {
                        importProductButton.Enabled = true;
                        importProductButton.Tag = null;
                        buttonCancel.Enabled = false;
                    }
                                    ));
                }
            });
            thread.Start();
        }
コード例 #15
0
        private void ImportPartyButtonClick(object sender, EventArgs e)
        {
            importPartyButton.Enabled = false;
            var sb = new StringBuilder();
            var progress = new ImportProgress { ProgressBar = saveProgressBar, IsCanceled = false };
            importPartyButton.Tag = progress;
            var filename = textBoxPartyFile.Text;
            var filterData = new FilterData
            {
                FileName = filename
            };
            var thread = new Thread(() =>
            {
                try
                {
                    sb.Append("-------- Importing from " + Path.GetFileName(filename) +
                                      " --------\n");
                    Invoke(new MethodInvoker(() =>
                    {
                        tradeOutputText.AppendText(" Importing parties \n");
                    }));
                    var infos = TradeImportHelper.ReadPartyData(filterData, sb);
                    var infos2 = new List<PartyInfo>();
                    var parties = new List<Party>();
                    var partyCodes = new HashSet<string>();
                    string duplicates = string.Empty;
                    int newCount = 0, updateCount = 0;
                    foreach (var info in infos)
                    {
                        if (partyCodes.Contains(info.Code))
                        {
                            var msg = "[Load Party Error] Duplicated code (" + info.Code + ") found, will be ignored\n";
                            duplicates += msg;
                            sb.Append(msg);
                        }
                        else
                        {
                            partyCodes.Add(info.Code);
                            infos2.Add(info);
                        }
                    }
                    if (duplicates.Length > 0)
                    {
                        if (MessageBox.Show("Duplicates:\n" + duplicates + "Do you want to continue without them?", 
                            "Load Parties", MessageBoxButtons.YesNo) == DialogResult.No)
                            return;
                    }

                    foreach (var info in infos2)
                    {
                        var party = TradeImportHelper.LoadParty(info, sb);
                        if (party != null)
                        {
                            parties.Add(party);
                            if (party.Action.Equals(TradeImportHelper.UpdateAction))
                                updateCount++;
                            else
                                newCount++;

                        }
                    }
                    var count = parties.Count;
                    if (count == 0)
                    {
                        sb.Append("No new parties loaded\n");
                    }
                    else
                    {
                        var msg = newCount + " new and " + updateCount + " updated parties loaded.\n";                        
                        sb.Append(msg);
                        if (MessageBox.Show(msg + " Do you want to Save?", "Save Parties", MessageBoxButtons.YesNo) == DialogResult.No)
                            return;
                        saveProgressBar.Properties.Minimum = 0;
                        saveProgressBar.Properties.Maximum = count;
                        saveProgressBar.EditValue = 0;
                        var thread2 = new Thread(() => SaveParties(parties, importPartyButton));
                        thread2.Start();
                    }

                    tradeOutputText.Lines = sb.ToString().Split('\n');

                }
                catch (Exception x)
                {
                    Logger.Error(x);
                    Invoke(new MethodInvoker(() => MessageBox.Show(x.ToString())));
                }
                finally
                {
                    Invoke(new MethodInvoker(() =>
                        {
                            importPartyButton.Enabled = true;
                        }
                    ));
                }

            });
            thread.Start();
        }