[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting GenomeManagerForEfficientCT
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2004-12-14 21:25:56
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11480/b7_Scripts/TickerSelectionTesting Modified Files: GenomeManagerForEfficientCTOPortfolio.cs RunEfficientCTOPortfolio.cs Log Message: Script based on efficient portfolio theory: modified GetFitnessValue implementation for the IGenomeManager used by this script. Now it is maximized a probability value. Index: GenomeManagerForEfficientCTOPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientCTOPortfolio.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GenomeManagerForEfficientCTOPortfolio.cs 1 Dec 2004 22:40:03 -0000 1.1 --- GenomeManagerForEfficientCTOPortfolio.cs 14 Dec 2004 21:25:45 -0000 1.2 *************** *** 104,115 **** { //parameters used to balance the rate of return against variance ! double a=2.5, b=2.0; double portofolioVariance = this.getPortfolioVariance(genome.Genes()); double portfolioRateOfReturn = this.getPortfolioRateOfReturn(genome.Genes()); this.variance = portofolioVariance; this.rateOfReturn = portfolioRateOfReturn; ! ! double returnValue = System.Math.Pow(((this.targetStdDev*this.targetStdDev)/portofolioVariance),a)* ! System.Math.Pow(System.Math.Max(0.0,(portfolioRateOfReturn/this.targetPerformance)),b); return returnValue; } --- 104,116 ---- { //parameters used to balance the rate of return against variance ! //double a=2.5, b=2.0; double portofolioVariance = this.getPortfolioVariance(genome.Genes()); double portfolioRateOfReturn = this.getPortfolioRateOfReturn(genome.Genes()); this.variance = portofolioVariance; this.rateOfReturn = portfolioRateOfReturn; ! NormalDistribution normal = new NormalDistribution(portfolioRateOfReturn, Math.Sqrt(portofolioVariance)); ! double returnValue = normal.GetProbability(this.targetPerformance*0.5,this.targetPerformance*1.5); ! //double returnValue = System.Math.Pow(((this.targetStdDev*this.targetStdDev)/portofolioVariance),a)* ! //System.Math.Pow(System.Math.Max(0.0,(portfolioRateOfReturn/this.targetPerformance)),b); return returnValue; } *************** *** 154,157 **** --- 155,169 ---- public object Decode(Genome genome) { + + string[] arrayOfTickers = new string[genome.Genes().Length]; + int indexOfTicker; + for(int index = 0; index < genome.Genes().Length; index++) + { + indexOfTicker = (int)genome.Genes().GetValue(index); + arrayOfTickers[index] = (string)this.setOfTickers.Rows[indexOfTicker][0]; + } + return arrayOfTickers; + + /*old implementation, to be used for output to console string sequenceOfTickers = ""; object returnValue; *************** *** 164,167 **** --- 176,181 ---- System.Math.Sqrt(this.Variance) + ")"; return returnValue; + */ + } // end of implementation of IGenomeManager Index: RunEfficientCTOPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/RunEfficientCTOPortfolio.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RunEfficientCTOPortfolio.cs 1 Dec 2004 22:40:03 -0000 1.1 --- RunEfficientCTOPortfolio.cs 14 Dec 2004 21:25:46 -0000 1.2 *************** *** 22,47 **** using System; - using System.Data; - using QuantProject.ADT.Optimizing.Genetic; - - /* using System.Collections; using QuantProject.ADT; using QuantProject.Business.Financial.Accounting.Reporting; using QuantProject.Business.Financial.Instruments; using QuantProject.Business.Strategies; using QuantProject.Business.Testing; ! using QuantProject.Data.DataTables; using QuantProject.Data.DataProviders; - using QuantProject.Presentation.Reporting.WindowsForm; - */ - using QuantProject.Business.Scripting; using QuantProject.Data.Selectors; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { /// <summary> ! /// Script to find the efficient close to open daily portfolio /// </summary> public class RunEfficientCTOPorfolio : Script --- 22,52 ---- using System; using System.Collections; + using System.Data; using QuantProject.ADT; + using QuantProject.ADT.Optimizing.Genetic; + using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Financial.Accounting.Reporting; using QuantProject.Business.Financial.Instruments; + using QuantProject.Business.Financial.Ordering; + using QuantProject.Business.Scripting; using QuantProject.Business.Strategies; using QuantProject.Business.Testing; ! using QuantProject.Business.Timing; using QuantProject.Data.DataProviders; using QuantProject.Data.Selectors; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; + using QuantProject.Presentation.Reporting.WindowsForm; + namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { /// <summary> ! /// Script to buy at open and sell at close ! /// the efficient close to open daily portfolio ! /// The efficient portfolio's generation rules ! /// (contained in the EndOfDayTimerHandler) are: ! /// - choose the most liquid tickers; ! /// - choose the most efficient portfolio among these tickers /// </summary> public class RunEfficientCTOPorfolio : Script *************** *** 49,64 **** //DateTime lastDate = DateTime.Now.Date; //DateTime firstDate = DateTime.Now.Date.AddDays(-60); DateTime lastDate = new DateTime(2004,11,25); DateTime firstDate = new DateTime(2004,9,25); public RunEfficientCTOPorfolio() { ! } #region Run ! public override void Run() { //"STOCKMI" --- 54,89 ---- //DateTime lastDate = DateTime.Now.Date; //DateTime firstDate = DateTime.Now.Date.AddDays(-60); + //these two members are used by the old script DateTime lastDate = new DateTime(2004,11,25); DateTime firstDate = new DateTime(2004,9,25); + // + private ReportTable reportTable; + private EndOfDayDateTime startDateTime; + private EndOfDayDateTime endDateTime; + private int numIntervalDays;// number of days for the equity line graph + + //private ProgressBarForm progressBarForm; + + private EndOfDayTimerHandler endOfDayTimerHandler; + + private Account account; + + private IEndOfDayTimer endOfDayTimer; public RunEfficientCTOPorfolio() { ! //this.progressBarForm = new ProgressBarForm(); ! this.reportTable = new ReportTable( "Summary_Reports" ); ! this.startDateTime = new EndOfDayDateTime( ! new DateTime( 2002 , 1 , 1 ) , EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = new EndOfDayDateTime( ! new DateTime( 2002 , 3 , 31 ) , EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.numIntervalDays = 7; } #region Run ! private void run_FindBestPortfolioForNextTrade() { //"STOCKMI" *************** *** 74,80 **** --- 99,199 ---- GO.MutationRate = 0.05; GO.Run(true); + //it has to be changed the decode implementation for this IGenomeManager System.Console.WriteLine("\n\nThe best solution found is: " + (string)GO.BestGenome.Meaning + " with {0} generations", GO.GenerationCounter); } + + private void run_initializeEndOfDayTimer() + { + this.endOfDayTimer = + new HistoricalEndOfDayTimer( this.startDateTime ); + } + private void run_initializeAccount() + { + this.account = new Account( "EfficientCloseToOpenPortfolio" , this.endOfDayTimer , + new HistoricalEndOfDayDataStreamer( this.endOfDayTimer ) , + new HistoricalEndOfDayOrderExecutor( this.endOfDayTimer ) ); + + } + private void run_initializeEndOfDayTimerHandler() + { + this.endOfDayTimerHandler = new EndOfDayTimerHandler(70,6,this.account ); + } + /* + private void inSampleNewProgressEventHandler( + Object sender , NewProgressEventArgs eventArgs ) + { + this.progressBarForm.ProgressBarInSample.Value = eventArgs.CurrentProgress; + this.progressBarForm.ProgressBarInSample.Refresh(); + } + private void run_initializeProgressHandlers() + { + this.endOfDayTimerHandler.InSampleNewProgress += + new InSampleNewProgressEventHandler( this.inSampleNewProgressEventHandler ); + } + */ + #region oneHourAfterMarketCloseEventHandler + /* + private void oneHourAfterMarketCloseEventHandler_handleProgessBarForm( + IEndOfDayTimer endOfDayTimer ) + { + long elapsedDays = Convert.ToInt64( ((TimeSpan)( endOfDayTimer.GetCurrentTime().DateTime - + this.startDateTime.DateTime )).TotalDays ); + long totalDays = Convert.ToInt64( ((TimeSpan)( this.endDateTime.DateTime - + this.startDateTime.DateTime )).TotalDays ); + if ( Math.Floor( elapsedDays / totalDays * 100 ) > + Math.Floor( ( elapsedDays - 1 ) / totalDays * 100 ) ) + // a new out of sample time percentage point has been elapsed + this.progressBarForm.ProgressBarOutOfSample.Value = + Convert.ToInt16( Math.Floor( elapsedDays / totalDays * 100 ) ); + } + public void oneHourAfterMarketCloseEventHandler( + Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) + { + this.oneHourAfterMarketCloseEventHandler_handleProgessBarForm( + ( IEndOfDayTimer )sender ); + if ( ( ( IEndOfDayTimer )sender ).GetCurrentTime().DateTime > + this.endDateTime.DateTime ) + { + // the simulation has reached the ending date + this.account.EndOfDayTimer.Stop(); + this.progressBarForm.Close(); + } + } + */ + #endregion + + public override void Run() + { + //old script + //this.run_FindBestPortfolioForNextTrade(); + + Report report; + run_initializeEndOfDayTimer(); + run_initializeAccount(); + run_initializeEndOfDayTimerHandler(); + //run_initializeProgressHandlers(); + this.endOfDayTimer.MarketOpen += + new MarketOpenEventHandler( + this.endOfDayTimerHandler.MarketOpenEventHandler); + + this.endOfDayTimer.FiveMinutesBeforeMarketClose += + new FiveMinutesBeforeMarketCloseEventHandler( + this.endOfDayTimerHandler.FiveMinutesBeforeMarketCloseEventHandler ); + + this.endOfDayTimer.OneHourAfterMarketClose += + new OneHourAfterMarketCloseEventHandler( + this.endOfDayTimerHandler.OneHourAfterMarketCloseEventHandler ); + //this.endOfDayTimer.OneHourAfterMarketClose += + //new OneHourAfterMarketCloseEventHandler( + //this.oneHourAfterMarketCloseEventHandler ); + + //this.progressBarForm.Show(); + this.endOfDayTimer.Start(); + report = new Report( this.account ); + report.Show("CTO_Portfolio" , this.numIntervalDays , this.startDateTime , "CTO_Portfolio" ); + + + } #endregion } |