[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting RunEfficientPortfolio.cs,
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-08-19 17:13:41
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv3611/b7_Scripts/TickerSelectionTesting Modified Files: RunEfficientPortfolio.cs Log Message: - the code has been cleaned up to avoid warnings - standard indentation has been applied Index: RunEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/RunEfficientPortfolio.cs,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** RunEfficientPortfolio.cs 29 Aug 2007 10:04:30 -0000 1.21 --- RunEfficientPortfolio.cs 19 Aug 2008 17:13:06 -0000 1.22 *************** *** 3,7 **** RunEfficientPortfolio.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- RunEfficientPortfolio.cs ! Copyright (C) 2003 Marco Milletti *************** *** 19,23 **** along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ! */ using System; --- 19,23 ---- along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ! */ using System; *************** *** 40,44 **** using QuantProject.Business.Financial.Accounting.Commissions; using QuantProject.Data.DataProviders; ! using QuantProject.Data.Selectors; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; using QuantProject.Presentation.Reporting.WindowsForm; --- 40,44 ---- using QuantProject.Business.Financial.Accounting.Commissions; using QuantProject.Data.DataProviders; ! using QuantProject.Data.Selectors; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; using QuantProject.Presentation.Reporting.WindowsForm; *************** *** 52,296 **** /// </summary> [Serializable] ! public class RunEfficientPortfolio { ! protected string tickerGroupID; ! protected int numberOfEligibleTickers; ! protected int numberOfTickersToBeChosen; ! protected int numDaysForOptimizationPeriod; ! protected int generationNumberForGeneticOptimizer; ! protected int populationSizeForGeneticOptimizer; ! protected ReportTable reportTable; ! protected EndOfDayDateTime startDateTime; ! protected EndOfDayDateTime endDateTime; ! //protected int numIntervalDays;// number of days for the equity line graph protected IHistoricalQuoteProvider historicalQuoteProvider; ! //protected ProgressBarForm progressBarForm; ! protected EndOfDayTimerHandler endOfDayTimerHandler; ! protected Account account; ! protected IEndOfDayTimer endOfDayTimer; ! protected string benchmark; ! ! protected string scriptName; ! ! protected double targetReturn; ! ! protected PortfolioType portfolioType; ! ! protected DateTime startingTimeForScript; ! protected double maxRunningHours; ! //if MaxNumberOfHoursForScript has elapsed and the script ! //is still running, it will be stopped. ! ! ! public PortfolioType TypeOfPortfolio ! { ! get { return this.portfolioType; } ! } ! ! public virtual string ScriptName ! { ! get{return this.scriptName;} ! set{this.scriptName = value;} ! } ! ! public DateTime TimerLastDate ! { ! get{return this.endOfDayTimer.GetCurrentTime().DateTime ;} ! } ! public RunEfficientPortfolio(string benchmark, ! DateTime startDate, DateTime endDate, ! PortfolioType portfolioType, ! double maxRunningHours) { ! ! this.startDateTime = new EndOfDayDateTime( ! startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = new EndOfDayDateTime( ! endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.benchmark = benchmark; ! this.ScriptName = "EfficientGeneric"; ! this.portfolioType = portfolioType; ! this.startingTimeForScript = DateTime.Now; ! this.maxRunningHours = maxRunningHours; ! //this.numIntervalDays = 3; } ! ! public RunEfficientPortfolio(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, string benchmark, ! DateTime startDate, DateTime endDate, ! double targetReturn, ! PortfolioType portfolioType, ! double maxRunningHours) { ! //this.progressBarForm = new ProgressBarForm(); ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; ! this.numDaysForOptimizationPeriod = numDaysForOptimizationPeriod; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.reportTable = new ReportTable( "Summary_Reports" ); ! this.startDateTime = new EndOfDayDateTime( ! startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = new EndOfDayDateTime( ! endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.benchmark = benchmark; ! this.ScriptName = "EfficientGeneric"; ! this.targetReturn = targetReturn; ! this.portfolioType = portfolioType; ! this.startingTimeForScript = DateTime.Now; ! this.maxRunningHours = maxRunningHours; ! //this.numIntervalDays = 3; } - - protected string getGenomeCounterInfo() - { - string returnValue = ""; - if(this.endOfDayTimerHandler.GenomeCounter != null) - returnValue = "Total generated genomes: " + - this.endOfDayTimerHandler.GenomeCounter.TotalEvaluatedGenomes.ToString() + - "; Current fitness: " + - this.endOfDayTimerHandler.GenomeCounter.BestFitness.ToString(); - return returnValue; - } ! #region Run ! ! protected virtual void run_initializeEndOfDayTimer() ! { ! //default endOfDayTimer ! this.endOfDayTimer = ! new IndexBasedEndOfDayTimer( this.startDateTime, this.endDateTime, ! this.benchmark ); ! ! } ! ! protected virtual void run_initializeAccount() ! { ! //default account with no commissions and no slippage calculation ! this.account = new Account( this.scriptName , this.endOfDayTimer , ! new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , ! this.historicalQuoteProvider ) , ! new HistoricalEndOfDayOrderExecutor( this.endOfDayTimer , ! this.historicalQuoteProvider )); ! ! } ! protected virtual void run_initializeEndOfDayTimerHandler() ! { ! //always needs specific implementation in inherited classes; ! } ! ! protected virtual void run_initializeHistoricalQuoteProvider() ! { ! //always needs specific implementation in inherited classes; ! } ! ! protected void checkDateForReport_createDirIfNotPresent(string dirPath) ! { ! if(!Directory.Exists(dirPath)) ! Directory.CreateDirectory(dirPath); ! } ! ! protected virtual void checkDateForReport(Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime || ! DateTime.Now >= this.startingTimeForScript.AddHours(this.maxRunningHours)) ! //last date is reached by the timer or maxRunning hours ! //are elapsed from the time script started ! this.SaveScriptResults(); ! } ! ! public virtual string SaveScriptResults_CreateFileName() ! { ! return DateTime.Now.Hour.ToString().PadLeft(2,'0') + "_" + ! DateTime.Now.Minute.ToString().PadLeft(2,'0') + "_" + ! "From_" + this.tickerGroupID + "_" + ! + this.numberOfEligibleTickers + ! "_OptDays" + this.numDaysForOptimizationPeriod + "_Port" + ! this.numberOfTickersToBeChosen + "GenNum" + ! this.generationNumberForGeneticOptimizer + ! "PopSize" + this.populationSizeForGeneticOptimizer + ! Convert.ToString(this.portfolioType); ! } ! ! ! public virtual void SaveScriptResults() ! { ! string fileName = this.SaveScriptResults_CreateFileName(); ! string dirNameWhereToSaveReports = System.Configuration.ConfigurationSettings.AppSettings["ReportsArchive"] + ! "\\" + this.ScriptName + "\\"; ! string dirNameWhereToSaveTransactions = System.Configuration.ConfigurationSettings.AppSettings["TransactionsArchive"] + ! "\\" + this.ScriptName + "\\"; ! string dirNameWhereToSaveBestGenomes = System.Configuration.ConfigurationSettings.AppSettings["GenomesArchive"] + ! "\\" + this.ScriptName + "\\"; ! //default report with numIntervalDays = 1 ! AccountReport accountReport = this.account.CreateReport(fileName,1, ! this.endOfDayTimer.GetCurrentTime(), ! this.benchmark, ! new HistoricalAdjustedQuoteProvider()); ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveReports); ! ObjectArchiver.Archive(accountReport, ! dirNameWhereToSaveReports + ! fileName + ".qPr"); ! // ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveTransactions); ! ObjectArchiver.Archive(this.account.Transactions, ! dirNameWhereToSaveTransactions + ! fileName + ".qPt"); ! // ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveBestGenomes); ! OptimizationOutput optimizationOutput = new OptimizationOutput(); ! foreach(GenomeRepresentation genomeRepresentation in this.endOfDayTimerHandler.BestGenomes) ! optimizationOutput.Add(genomeRepresentation); ! ObjectArchiver.Archive(optimizationOutput, ! dirNameWhereToSaveBestGenomes + ! fileName + ".bgn"); ! this.endOfDayTimer.Stop(); ! ! } ! ! protected virtual void run_initialize() ! { ! run_initializeHistoricalQuoteProvider(); ! run_initializeEndOfDayTimer(); ! run_initializeAccount(); ! run_initializeEndOfDayTimerHandler(); ! //run_initializeProgressHandlers(); ! } ! protected virtual void run_addEventHandlers() ! { ! this.endOfDayTimer.MarketClose += ! new MarketCloseEventHandler( ! this.checkDateForReport); ! ! //in inherited classes'override method: ! //add here TimerHandler's handlers to timer's events ! //example ! //this.endOfDayTimer.EVENT_NAME += ! // new EVENT_NAMEEventHandler( ! // this.endOfDayTimerHandler.EVENT_NAMEEventHandler); ! } ! ! ! public virtual void Run() ! { ! this.run_initialize(); ! this.run_addEventHandlers(); ! //this.progressBarForm.Show(); ! this.endOfDayTimer.Start(); ! } ! ! #endregion ! } } --- 52,299 ---- /// </summary> [Serializable] ! public class RunEfficientPortfolio { ! protected string tickerGroupID; ! protected int numberOfEligibleTickers; ! protected int numberOfTickersToBeChosen; ! protected int numDaysForOptimizationPeriod; ! protected int generationNumberForGeneticOptimizer; ! protected int populationSizeForGeneticOptimizer; ! protected ReportTable reportTable; ! protected EndOfDayDateTime startDateTime; ! protected EndOfDayDateTime endDateTime; ! //protected int numIntervalDays;// number of days for the equity line graph protected IHistoricalQuoteProvider historicalQuoteProvider; ! //protected ProgressBarForm progressBarForm; ! protected EndOfDayTimerHandler endOfDayTimerHandler; ! protected Account account; ! protected IEndOfDayTimer endOfDayTimer; ! protected string benchmark; ! ! protected string scriptName; ! ! protected double targetReturn; ! ! protected PortfolioType portfolioType; ! ! protected DateTime startingTimeForScript; ! protected double maxRunningHours; ! //if MaxNumberOfHoursForScript has elapsed and the script ! //is still running, it will be stopped. ! ! ! public PortfolioType TypeOfPortfolio ! { ! get { return this.portfolioType; } ! } ! ! public virtual string ScriptName ! { ! get{return this.scriptName;} ! set{this.scriptName = value;} ! } ! ! public DateTime TimerLastDate ! { ! get{return this.endOfDayTimer.GetCurrentTime().DateTime ;} ! } ! public RunEfficientPortfolio(string benchmark, ! DateTime startDate, DateTime endDate, ! PortfolioType portfolioType, ! double maxRunningHours) { ! ! this.startDateTime = new EndOfDayDateTime( ! startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = new EndOfDayDateTime( ! endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.benchmark = benchmark; ! this.ScriptName = "EfficientGeneric"; ! this.portfolioType = portfolioType; ! this.startingTimeForScript = DateTime.Now; ! this.maxRunningHours = maxRunningHours; ! //this.numIntervalDays = 3; } ! ! public RunEfficientPortfolio(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, string benchmark, ! DateTime startDate, DateTime endDate, ! double targetReturn, ! PortfolioType portfolioType, ! double maxRunningHours) { ! //this.progressBarForm = new ProgressBarForm(); ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; ! this.numDaysForOptimizationPeriod = numDaysForOptimizationPeriod; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.reportTable = new ReportTable( "Summary_Reports" ); ! this.startDateTime = new EndOfDayDateTime( ! startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = new EndOfDayDateTime( ! endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.benchmark = benchmark; ! this.ScriptName = "EfficientGeneric"; ! this.targetReturn = targetReturn; ! this.portfolioType = portfolioType; ! this.startingTimeForScript = DateTime.Now; ! this.maxRunningHours = maxRunningHours; ! //this.numIntervalDays = 3; ! } ! ! protected string getGenomeCounterInfo() ! { ! string returnValue = ""; ! if(this.endOfDayTimerHandler.GenomeCounter != null) ! returnValue = "Total generated genomes: " + ! this.endOfDayTimerHandler.GenomeCounter.TotalEvaluatedGenomes.ToString() + ! "; Current fitness: " + ! this.endOfDayTimerHandler.GenomeCounter.BestFitness.ToString(); ! return returnValue; } ! #region Run ! ! protected virtual void run_initializeEndOfDayTimer() ! { ! //default endOfDayTimer ! this.endOfDayTimer = ! new IndexBasedEndOfDayTimer( this.startDateTime, this.endDateTime, ! this.benchmark ); ! ! } ! ! protected virtual void run_initializeAccount() ! { ! //default account with no commissions and no slippage calculation ! this.account = new Account( this.scriptName , this.endOfDayTimer , ! new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , ! this.historicalQuoteProvider ) , ! new HistoricalEndOfDayOrderExecutor( this.endOfDayTimer , ! this.historicalQuoteProvider )); ! ! } ! protected virtual void run_initializeEndOfDayTimerHandler() ! { ! //always needs specific implementation in inherited classes; ! } ! ! protected virtual void run_initializeHistoricalQuoteProvider() ! { ! //always needs specific implementation in inherited classes; ! } ! ! protected void checkDateForReport_createDirIfNotPresent(string dirPath) ! { ! if(!Directory.Exists(dirPath)) ! Directory.CreateDirectory(dirPath); ! } ! ! protected virtual void checkDateForReport(Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime || ! DateTime.Now >= this.startingTimeForScript.AddHours(this.maxRunningHours)) ! //last date is reached by the timer or maxRunning hours ! //are elapsed from the time script started ! this.SaveScriptResults(); ! } ! ! public virtual string SaveScriptResults_CreateFileName() ! { ! return DateTime.Now.Hour.ToString().PadLeft(2,'0') + "_" + ! DateTime.Now.Minute.ToString().PadLeft(2,'0') + "_" + ! "From_" + this.tickerGroupID + "_" + ! + this.numberOfEligibleTickers + ! "_OptDays" + this.numDaysForOptimizationPeriod + "_Port" + ! this.numberOfTickersToBeChosen + "GenNum" + ! this.generationNumberForGeneticOptimizer + ! "PopSize" + this.populationSizeForGeneticOptimizer + ! Convert.ToString(this.portfolioType); ! } ! ! ! public virtual void SaveScriptResults() ! { ! string fileName = this.SaveScriptResults_CreateFileName(); ! string dirNameWhereToSaveReports = ! System.Configuration.ConfigurationManager.AppSettings["ReportsArchive"] + ! "\\" + this.ScriptName + "\\"; ! string dirNameWhereToSaveTransactions = ! System.Configuration.ConfigurationManager.AppSettings["TransactionsArchive"] + ! "\\" + this.ScriptName + "\\"; ! string dirNameWhereToSaveBestGenomes = ! System.Configuration.ConfigurationManager.AppSettings["GenomesArchive"] + ! "\\" + this.ScriptName + "\\"; ! //default report with numIntervalDays = 1 ! AccountReport accountReport = this.account.CreateReport(fileName,1, ! this.endOfDayTimer.GetCurrentTime(), ! this.benchmark, ! new HistoricalAdjustedQuoteProvider()); ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveReports); ! ObjectArchiver.Archive(accountReport, ! dirNameWhereToSaveReports + ! fileName + ".qPr"); ! // ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveTransactions); ! ObjectArchiver.Archive(this.account.Transactions, ! dirNameWhereToSaveTransactions + ! fileName + ".qPt"); ! // ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveBestGenomes); ! OptimizationOutput optimizationOutput = new OptimizationOutput(); ! foreach(GenomeRepresentation genomeRepresentation in this.endOfDayTimerHandler.BestGenomes) ! optimizationOutput.Add(genomeRepresentation); ! ObjectArchiver.Archive(optimizationOutput, ! dirNameWhereToSaveBestGenomes + ! fileName + ".bgn"); ! this.endOfDayTimer.Stop(); ! ! } ! ! protected virtual void run_initialize() ! { ! run_initializeHistoricalQuoteProvider(); ! run_initializeEndOfDayTimer(); ! run_initializeAccount(); ! run_initializeEndOfDayTimerHandler(); ! //run_initializeProgressHandlers(); ! } ! protected virtual void run_addEventHandlers() ! { ! this.endOfDayTimer.MarketClose += ! new MarketCloseEventHandler( ! this.checkDateForReport); ! ! //in inherited classes'override method: ! //add here TimerHandler's handlers to timer's events ! //example ! //this.endOfDayTimer.EVENT_NAME += ! // new EVENT_NAMEEventHandler( ! // this.endOfDayTimerHandler.EVENT_NAMEEventHandler); ! } ! ! ! public virtual void Run() ! { ! this.run_initialize(); ! this.run_addEventHandlers(); ! //this.progressBarForm.Show(); ! this.endOfDayTimer.Start(); ! } ! ! #endregion ! } } |