private bool isDisposed = false; // Dispose indicator #endregion Data Members #region Consturctors, Initialization & Destructor public cPriceReturnsHandler(IPortfolioBL cPort, IRepository repository) { m_objPortfolio = cPort; m_objColHandler = m_objPortfolio.ColHandler; m_objErrorHandler = m_objPortfolio.cErrorLog; _repository = repository; }//constructor
private Boolean m_isCovarCalc = true; // Covariance calculation (false = correlation) #endregion Data members #region Constructors, Initialization & Destructor public cCovarCorrelData(ISecurity cSec, ICollectionsHandler cColHandler, IErrorHandler cErrors, Boolean isCovar) { m_objRelevantSec = cSec; m_objColHandler = cColHandler; m_objErrorHandler = cErrors; m_isCovarCalc = isCovar; }//constructor
private bool isDisposed = false; // Dispose indicator #endregion Data Members #region Consturctors, Initialization & Destructor public cPricesHandler(IPortfolioBL cPort, IRepository repository) { m_objPortfolio = cPort; //m_objOleDBConn = m_objPortfolio.OleDBConn; m_objColHandler = m_objPortfolio.ColHandler; m_objErrorHandler = m_objPortfolio.cErrorLog; _repository = repository; }//constructor
private bool isDisposed = false; // indicates if Dispose has already been called // Data variables //private DataTable m_dtSecurityRatesData; // Final rates calculations #endregion Data Members #region Construcors, Initialization & Destructor public cRateHandler(IPortfolioBL cPort) { m_objPortfolio = cPort; m_objErrorHandler = m_objPortfolio.cErrorLog; m_objColHandler = m_objPortfolio.ColHandler; //m_dtSecurityRatesData = getMainRatesTblStruct(); }// constructor
public cConstHandler(cConstraints cConsts, IPortfolioBL cPort) { m_objPortfolio = cPort; m_colConstraints = cConsts; m_objErrorHandler = m_objPortfolio.cErrorLog; m_objColHandler = m_objPortfolio.ColHandler; repository = Resolver.Resolve <IRepository>(); setSeperatedConstraintCols(); }//constructor
private double[,] m_dCovarMatrix = null; // Covariance final matrix #endregion Data Members #region Constructors, Initialization & Destructor public cCovarCorrelHandler(IPortfolioBL cPort) { // Init variables m_objPortfolio = cPort; m_objErrorHandler = m_objPortfolio.cErrorLog; m_objColHandler = m_objPortfolio.ColHandler; m_objRateHandler = m_objPortfolio.Classes.RatesHandler; m_dtCovarOutput = getOutputDataStruct(); // Output data structure }//constructor
} //destructor protected void Dispose(bool disposing) { // clearing class variables if (disposing) { //clean up managed resources m_objErrorHandler = null; m_objPortfolio = null; m_objColHandler = null; } isDisposed = true; }//Dispose
public cOptimizationResults(IPortfolioBL cPort) { m_objPortfolio = cPort; m_objColHandler = m_objPortfolio.ColHandler; //m_colSecurities = m_objPortfolio.ColHandler.ActiveSecs; m_objCovarCalcs = m_objPortfolio.Classes.CovarCorrel; m_objErrorHandler = m_objPortfolio.cErrorLog; m_objConstraints = m_objPortfolio.Classes.ConstHandler; initMainVars(); }//constructor
} //destructor protected void Dispose(bool disposing) { // clearing class variables if (disposing) { //clean up managed resources m_objRateHandler = null; m_objErrorHandler = null; m_objColHandler = null; if (m_dtCovarOutput != null) { m_dtCovarOutput.Dispose(); } } isDisposed = true; }//Dispose
private Boolean m_isDisabled = false; // Whether the system has disabled the current security #endregion Data members #region Consturctors, Initialization & Destructor public cBacktestingSecurity(IPortfolioBL cPort, ISecurity cSec, cDateRange Dates) { m_objPortfolio = cPort; m_objErrorHandler = m_objPortfolio.cErrorLog; m_objColHandler = m_objPortfolio.ColHandler; m_objSecurity = cSec; m_drDateRange = Dates; try { } catch (Exception ex) { m_objErrorHandler.LogInfo(ex); } }//cSecurity constructor
} //destructor public void Dispose(bool disposing) { // Disposing class variables if (disposing) { // Managed code //m_objOleDBConn = null; m_objErrorHandler = null; m_objColHandler = null; if (m_dtAllSecsPrices != null) { m_dtAllSecsPrices.Dispose(); } } isDisposed = true; }//Dispose
} //destructor public void Dispose(bool disposing) { // Disposing class variables if (disposing) { // Managed code m_objErrorHandler = null; m_objColHandler = null; m_colConstraints.Clear(); m_colSingleSecs.Clear(); m_colEquality.Clear(); m_colRange.Clear(); m_colDefaultEquality.Clear(); m_colDefaultRange.Clear(); } isDisposed = true; }//Dispose
}//cSecurity constructor #endregion Consturctors, Initialization & Destructor #region Methods #region Base methods public void Init(IPortfolioBL cPort) { m_objPortfolio = cPort; m_objErrorHandler = m_objPortfolio.cErrorLog; m_objColHandler = m_objPortfolio.ColHandler; m_objSecAnalytics = new cSecAnalytics(this, m_objErrorHandler, m_objColHandler); m_objPrices.Portfolio = cPort; try { //LR: why do we have 5 years of date range here ??? m_sPricesRange = new cDateRange(DateTime.Today.AddYears(-cProperties.DatesInterval).AddDays(-1), DateTime.Today.AddDays(-1)); m_objRates = new cRateData(this, m_objPortfolio, false); m_objCovarData = new cCovarCorrelData(this, m_objColHandler, m_objErrorHandler, true); m_objCorrelData = new cCovarCorrelData(this, m_objColHandler, m_objErrorHandler, false); } catch (Exception ex) { m_objErrorHandler.LogInfo(ex); } }
private List <double> m_colVolumes = new List <double>(); // Collection of traded volumes for each day in full date-range #endregion Data members #region Consturctors, Initialization & Destructor public cSecurity(IPortfolioBL cPort, string secName, string secSymbol) { m_objPortfolio = cPort; m_objErrorHandler = m_objPortfolio.cErrorLog; m_objColHandler = m_objPortfolio.ColHandler; m_objSecProperties = new cSecProperties(m_objErrorHandler, m_objPortfolio); m_objSecAnalytics = new cSecAnalytics(this, m_objErrorHandler, m_objColHandler); m_objSecProperties.SecurityName = m_objSecProperties.getSecName(secName); // No ' signs + trim spaces m_objSecProperties.SecuritySymbol = secSymbol; m_objSecProperties.SecColor = System.Drawing.Color.FromArgb(cProperties.RndGenerator.Next(255), cProperties.RndGenerator.Next(255), cProperties.RndGenerator.Next(255)); m_objPrices = new cPriceData(this, m_objPortfolio); try { m_sPricesRange = new cDateRange(DateTime.Today.AddYears(-cProperties.DatesInterval).AddDays(-1), DateTime.Today.AddDays(-1)); m_objRates = new cRateData(this, m_objPortfolio, false); m_objCovarData = new cCovarCorrelData(this, m_objColHandler, m_objErrorHandler, true); m_objCorrelData = new cCovarCorrelData(this, m_objColHandler, m_objErrorHandler, false); } catch (Exception ex) { m_objErrorHandler.LogInfo(ex); } }//cSecurity constructor
private double m_dSortinoDesiredRate = 0D; // Desired rate value (for Sortino calculation) #endregion Data members #region Constructors, Initialization & Destructor public cSecAnalytics(ISecurity cCurrsec, IErrorHandler cErrors, ICollectionsHandler cColHandler) { m_objErrorHandler = cErrors; m_objColHandler = cColHandler; m_objRelevantSec = cCurrsec; }//constructor
}//getCatItemByID #endregion Load from DB #region Static methods private cSecurity getCurrSecurity(Security security, IPortfolioBL cCurrPort) { // Retrieves an instance of a security based on its datarow info ICollectionsHandler cColHandler = cCurrPort.ColHandler; cSecurity cCurrSec = new cSecurity(cCurrPort, security.strName, security.strSymbol);//111An cCurrSec.Properties.PortSecurityId = security.idSecurity; cCurrSec.Properties.HebName = security.strHebName; //cCurrSec.FAC = Convert.ToDouble(security.FAC); if (cCurrSec.FAC <= 0D) { cCurrSec.FAC = 1D; } cCurrSec.AvgYield = security.AvgYield; //* 52; cCurrSec.StdYield = security.StdYield; // * Math.Sqrt(52); cCurrSec.AvgYieldNIS = security.AvgYieldNIS; // * 52; cCurrSec.StdYieldNIS = security.StdYieldNIS; // * Math.Sqrt(52); cCurrSec.ValueUSA = security.dValueUSA; cCurrSec.ValueNIS = security.dValueNIS; cCurrSec.WeightUSA = security.WeightUSA; cCurrSec.WeightNIS = security.WeightNIS; //cCurrSec.Properties.ISIN = security.strISIN; cCurrSec.IdCurrency = security.idCurrency; cCurrSec.DateRange = new cDateRange(security.dtPriceStart, security.dtPriceEnd); cCurrSec.setSecurityActivity(true); lock (lockObject) { cCurrSec.Properties.Sector = cColHandler.getCatItemByID(enumCatType.Sector, security.idSector, cColHandler.Sectors); cCurrSec.Properties.Market = cColHandler.getCatItemByID(enumCatType.StockMarket, security.idMarket, cColHandler.Markets); cCurrSec.Properties.MarketName = getSecMarketName(security.idMarket); cCurrSec.Properties.SecurityType = cColHandler.getCatItemByID(enumCatType.SecurityType, security.idSecurityType, cColHandler.SecTypes); } //var priceRepository = Resolver.Resolve<IRepository>(); //try //{ // priceRepository.Execute(session => // { // cCurrSec.PriceTable = session.Query<Entities.dbo.Price>().Where(x => x.idSecurity == security.idSecurity).ToList(); //.OrderByDescending(x => x.dDate) // }); //} //catch (Exception ex) //{ // m_objErrorHandler.LogInfo(ex); //} //Resolver.Release(priceRepository); //List<Models.dbo.Price> BMprices = getFullPrices(); //cCurrSec.PriceTable = AutoMapper.Mapper.Map<List<Price>, List<Entities.dbo.Price>>(BMprices.Where(x => x.idSecurity == cCurrSec.Properties.PortSecurityId).OrderByDescending(x => x.dDate).ToList()); return(cCurrSec); }//getCurrSecurity
}//Dispose #endregion Consturctors, Initialization & Destructor #region Methods #region General methods public void InitCollectionObject(string currency, List <int> exchangesPackagees) { m_objPortfolioClasses.CategoryHandler = new Categories.cCategoriesHandler(m_objErrorHandler); m_objColHandler = new cCollectionsHandler(this, currency, exchangesPackagees); }//InitCollectionObject
}//InitCollectionObject // TODO: NEW METHOD public bool instantiateVariablesForPortfolio(Boolean isCreateNew, string sCurrency, List <int> exchangesIds, List <string> securities = null) { // Sets initial values preparing for new / existing portfolio try { // Set categories m_objPortfolioClasses.CategoryHandler = new Categories.cCategoriesHandler(m_objErrorHandler); // Init Securities collection is cPortfolio.Details // TODO: Check if necessary if (securities != null && securities.Count > 0) { // Initializes collection of securities this.Details.SecurityData = new List <SecurityData>(); foreach (var item in securities) { this.Details.SecurityData.Add(new SecurityData { idSecurity = item }); } } // Set collections Boolean isNewInstance = ((m_objColHandler == null) || (m_objColHandler.Securities.Count == 0)); if (isNewInstance && (cProperties.CollectionHandler != null)) { m_objColHandler = cProperties.CollectionHandler; isNewInstance = false; } if (isNewInstance) { // Only if collections are empty (first time) m_objColHandler = new cCollectionsHandler(this, sCurrency, exchangesIds); m_objColHandler.loadSecuritiesCollections(); cProperties.CollectionHandler = m_objColHandler; } else { m_objColHandler.clearSecsCalculatedOptData(); } // TODO: check if necessary cProperties.LastOptimization = DateTime.Today; cProperties.isIsraelOnly = sCurrency == "9999"; // Constraints if (m_objPortfolioClasses.ConstHandler == null) { m_objPortfolioClasses.ConstHandler = new cConstHandler(new cConstraints(), this); } // Init final variables if (m_objPortfolioClasses.PriceHandler == null) { m_objPortfolioClasses.PriceHandler = new cPricesHandler(this, Resolver.Resolve <IRepository>()); // Sets prices handler } if (m_objPortfolioClasses.RatesHandler == null) { m_objPortfolioClasses.RatesHandler = new cRateHandler(this); } if (m_objPortfolioClasses.CovarCorrel == null) { m_objPortfolioClasses.CovarCorrel = new TFI.BusinessLogic.GMath.cCovarCorrelHandler(this); } if (m_objPortfolioClasses.Optimizer == null) { m_objPortfolioClasses.Optimizer = new Optimization.cOptimizationResults(this); } // TODO: in Backtesting m_objColHandler.ActiveSecs is used and it doesn't have .PriceReturns table filled in! if (isNewInstance) { m_objPortfolioClasses.RatesHandler.setSecuritiesPriceReturns(m_objColHandler.Securities, DateTime.Today.AddYears(-3).AddDays(-1), DateTime.Today.AddDays(-1), sCurrency); m_objPortfolioClasses.RatesHandler.setSecuritiesPriceReturns(m_objColHandler.Benchmarks, DateTime.Today.AddYears(-3).AddDays(-1), DateTime.Today.AddDays(-1), sCurrency); } } catch (Exception ex) { m_objErrorHandler.LogInfo(ex); } return(true); }//instantiateVariablesForPortfolio
}//getSecDatarowPosition #endregion Static methods #region Load Benchmarks from SQL Srv private cSecurity getCurrBMSecurity(BMsecurity drSec, IPortfolioBL cCurrPort) { // Retrieves an instance of a security based on its datarow info ICollectionsHandler cColHandler = cCurrPort.ColHandler; cSecurity cCurrSec = new cSecurity(cCurrPort, drSec.strName, drSec.strSymbol); //111An cCurrSec.Properties.PortSecurityId = drSec.idSecurity; cCurrSec.Properties.HebName = drSec.strHebName; cCurrSec.FAC = 1D; cCurrSec.AvgYield = Convert.ToDouble(drSec.AvgYield); cCurrSec.StdYield = Convert.ToDouble(drSec.StdYield); cCurrSec.AvgYieldNIS = Convert.ToDouble(drSec.AvgYieldNIS); cCurrSec.StdYieldNIS = Convert.ToDouble(drSec.StdYieldNIS); //////cCurrSec.ValueUSA = Convert.ToDouble(drSec.dValueUSA); // THEY ARE NULLS for BM sec //////cCurrSec.ValueNIS = Convert.ToDouble(drSec.dValueNIS); //////cCurrSec.WeightUSA = Convert.ToDouble(drSec.WeightUSA); //////cCurrSec.WeightNIS = Convert.ToDouble(drSec.WeightNIS); cCurrSec.DateRange = new cDateRange(DateTime.Today.AddYears(-cProperties.DatesInterval), DateTime.Today.AddDays(-1)); lock (lockObject) { cCurrSec.Properties.Sector = cColHandler.getCatItemByID(enumCatType.Sector, Convert.ToInt32(drSec.idSector), cColHandler.Sectors); // Exchange values cCurrSec.Properties.Market = cColHandler.getCatItemByID(enumCatType.StockMarket, drSec.idMarket, cColHandler.Markets); cCurrSec.Properties.MarketName = getSecMarketName(drSec.idMarket); cCurrSec.Properties.SecurityType = cColHandler.getCatItemByID(enumCatType.SecurityType, drSec.idSecurityType, cColHandler.SecTypes); } //if (drSec["idCurrency"] != DBNull.Value) cCurrSec.IdCurrency = drSec["IdCurrency"].ToString(); cCurrSec.IdCurrency = drSec.idCurrency; // WHY WAS IT HERE????? cProperties.CurrencyId; try { // Only exists in portfolio securities // For Benchmark we assign 'true' //if (drSec["isActiveSecurity"] != DBNull.Value) cCurrSec.setSecurityActivity(Convert.ToBoolean(drSec["isActiveSecurity"])); //else cCurrSec.setSecurityActivity(true); // LR: at this point security already has .PriceTable filled in, so commenting the line, because it crashes here //cCurrSec.PriceTable = drSec.Prices.ToList(); //////foreach (var p in cCurrSec.PriceTable) //////{ ////// p.dAdjPrice = p.fClose; //////} cCurrSec.setSecurityActivity(true); } catch (Exception ex) { } //// Made separate entity for tbl_IndexPrices //var priceRepository = Resolver.Resolve<IRepository>(); //List<Entities.dbo.BMPrice> laura; //try //{ // priceRepository.Execute(session => // { // ////cCurrSec.PriceTable = AutoMapper.Mapper.Map<List<Entities.dbo.Price>>(session.Query<Entities.dbo.BMPrice>().Where(x => x.idSecurity == "'" + drSec.idSecurity + "'").OrderByDescending(x => x.dDate).ToList()); // laura = session.Query<Entities.dbo.BMPrice>().Where(x => x.idSecurity == "'" + drSec.idSecurity + "'").OrderByDescending(x => x.dDate).ToList(); // }); //} //catch (Exception ex) //{ // m_objErrorHandler.LogInfo(ex); //} //Resolver.Release(priceRepository); return(cCurrSec); }//getCurrSecurity
}//refreshConstList private void refreshClassPointers() { // Refreshes the pointer variables of the current class m_objColHandler = m_objPortfolio.ColHandler; }//refreshClassPointers
}//getCurrSecurity private cSecurity getCurrBMSecurity_with_EntitySec(Entities.dbo.Security drSec, IPortfolioBL cCurrPort) { // Retrieves an instance of a security based on its datarow info ICollectionsHandler cColHandler = cCurrPort.ColHandler; cSecurity cCurrSec = new cSecurity(cCurrPort, drSec.strName, drSec.strSymbol); //111An cCurrSec.Properties.PortSecurityId = drSec.idSecurity; cCurrSec.Properties.HebName = drSec.strHebName; cCurrSec.FAC = 1D; ////if (drSec["FAC"] != DBNull.Value) cCurrSec.FAC = Convert.ToDouble(drSec["FAC"]); ////else cCurrSec.FAC = 1D; ////if (cCurrSec.FAC <= 0D) cCurrSec.FAC = 1D; if (drSec.AvgYield.HasValue) { cCurrSec.AvgYield = Convert.ToDouble(drSec.AvgYield); } if (drSec.StdYield.HasValue) { cCurrSec.StdYield = Convert.ToDouble(drSec.StdYield); } if (drSec.AvgYieldNIS.HasValue) { cCurrSec.AvgYieldNIS = Convert.ToDouble(drSec.AvgYieldNIS); } if (drSec.StdYieldNIS.HasValue) { cCurrSec.StdYieldNIS = Convert.ToDouble(drSec.StdYieldNIS); } if (drSec.MonetaryAvg.HasValue) { cCurrSec.ValueUSA = Convert.ToDouble(drSec.MonetaryAvg); } if (drSec.MonetaryAvgNIS.HasValue) { cCurrSec.ValueNIS = Convert.ToDouble(drSec.MonetaryAvgNIS); } if (drSec.WeightUSA.HasValue) { cCurrSec.WeightUSA = Convert.ToDouble(drSec.WeightUSA); } if (drSec.WeightNIS.HasValue) { cCurrSec.WeightNIS = Convert.ToDouble(drSec.WeightNIS); } //cCurrSec.Properties.ISIN = drSec.strISIN; ////if ((drSec["dtPriceStart"] != DBNull.Value) && (drSec["dtPriceEnd"] != DBNull.Value)) //// cCurrSec.DateRange = new cDateRange(Convert.ToDateTime(drSec["dtPriceStart"]), Convert.ToDateTime(drSec["dtPriceEnd"])); cCurrSec.DateRange = new cDateRange(DateTime.Today.AddYears(-cProperties.DatesInterval), DateTime.Today.AddDays(-1)); if (drSec.idSector.HasValue) { cCurrSec.Properties.Sector = cColHandler.getCatItemByID(enumCatType.Sector, Convert.ToInt32(drSec.idSector), cColHandler.Sectors); } if (drSec.idMarket.HasValue) { // Exchange values cCurrSec.Properties.Market = cColHandler.getCatItemByID(enumCatType.StockMarket, drSec.idMarket.Value, cColHandler.Markets); cCurrSec.Properties.MarketName = getSecMarketName(drSec.idMarket.Value); } if (drSec.idSecurityType.HasValue) { cCurrSec.Properties.SecurityType = cColHandler.getCatItemByID(enumCatType.SecurityType, drSec.idSecurityType.Value, cColHandler.SecTypes); } //if (drSec["idCurrency"] != DBNull.Value) cCurrSec.IdCurrency = drSec["IdCurrency"].ToString(); cCurrSec.IdCurrency = cProperties.CurrencyId; try { // Only exists in portfolio securities // For Benchmark we assign 'true' //if (drSec["isActiveSecurity"] != DBNull.Value) cCurrSec.setSecurityActivity(Convert.ToBoolean(drSec["isActiveSecurity"])); //else cCurrSec.setSecurityActivity(true); // LR: at this point security already has .PriceTable filled in, so commenting the line, because it crashes here //cCurrSec.PriceTable = drSec.Prices.ToList(); //////foreach (var p in cCurrSec.PriceTable) //////{ ////// p.dAdjPrice = p.fClose; //////} cCurrSec.setSecurityActivity(true); } catch (Exception ex) { } return(cCurrSec); }//getCurrSecurity