[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting EndOfDayTimerHandlerLastCho
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-06-25 09:51:00
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14463/b7_Scripts/TickerSelectionTesting Modified Files: EndOfDayTimerHandlerLastChosenPortfolio.cs GenomeManagerForEfficientCTCPortfolio.cs GenomeManagerForEfficientCTOPortfolio.cs GenomeManagerForEfficientPortfolio.cs RunEfficientPortfolio.cs RunLastChosenPortfolioOutOfSample.cs Log Message: (Sorry: for the last commit I was too confident. I didn't diff files before committing) Updated files for the EfficientPortfolio script: - new classes for testing a given array of tickers (normally, for testing out of sample an optimization's result); - reorganized code in a more object oriented way (now code duplication has been completely erased); - now, the optimization process for the shortAndLong portfolio should be more efficient (memory is now used properly) Index: GenomeManagerForEfficientCTOPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientCTOPortfolio.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** GenomeManagerForEfficientCTOPortfolio.cs 24 Jun 2005 22:52:25 -0000 1.10 --- GenomeManagerForEfficientCTOPortfolio.cs 25 Jun 2005 09:50:44 -0000 1.11 *************** *** 86,100 **** protected override float[] getArrayOfRatesOfReturn(string ticker) { ! float[] returnValue; ! float coefficient = this.getCoefficient(ticker); ! string tickerCode = GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); ! Quotes tickerQuotes = new Quotes(tickerCode, this.firstQuoteDate, this.lastQuoteDate); ! returnValue = ExtendedDataTable.GetArrayOfFloatFromRatioOfColumns(tickerQuotes, "quClose", "quOpen"); ! ! for(int idx = 0; idx!= returnValue.Length; idx++) { ! returnValue[idx] = coefficient*(returnValue[idx] - 1); } ! return returnValue; } /* --- 86,98 ---- protected override float[] getArrayOfRatesOfReturn(string ticker) { ! float[] returnValue = null; ! if(!ticker.StartsWith("-")) { ! string tickerCode = GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); ! Quotes tickerQuotes = new Quotes(tickerCode, this.firstQuoteDate, this.lastQuoteDate); ! returnValue = ExtendedDataTable.GetRatesOfReturnsFromColumns(tickerQuotes, "quClose", "quOpen"); ! this.numberOfExaminedReturns = returnValue.Length; } ! return returnValue; } /* Index: RunEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/RunEfficientPortfolio.cs,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RunEfficientPortfolio.cs 24 Jun 2005 22:52:25 -0000 1.9 --- RunEfficientPortfolio.cs 25 Jun 2005 09:50:44 -0000 1.10 *************** *** 96,104 **** get { return this.portfolioType; } } ! ! public IHistoricalQuoteProvider HistoricalQuoteProvider ! { ! get { return this.historicalQuoteProvider; } ! } public virtual string ScriptName { --- 96,100 ---- get { return this.portfolioType; } } ! public virtual string ScriptName { Index: GenomeManagerForEfficientCTCPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientCTCPortfolio.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** GenomeManagerForEfficientCTCPortfolio.cs 24 Jun 2005 22:52:25 -0000 1.5 --- GenomeManagerForEfficientCTCPortfolio.cs 25 Jun 2005 09:50:44 -0000 1.6 *************** *** 82,100 **** protected override float[] getArrayOfRatesOfReturn(string ticker) { ! float coefficient = this.getCoefficient(ticker); ! string tickerCode = GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); ! Quotes tickerQuotes = new Quotes(tickerCode, this.firstQuoteDate, this.lastQuoteDate); ! float[] allAdjValues = ExtendedDataTable.GetArrayOfFloatFromColumn(tickerQuotes, "quAdjustedClose"); ! float[] ratesOfReturns = new float[allAdjValues.Length/this.numDaysForReturnCalculation + 1]; ! int i = 0; //index for ratesOfReturns array ! ! for(int idx = 0; idx + this.numDaysForReturnCalculation < allAdjValues.Length; idx += this.numDaysForReturnCalculation ) { ! ratesOfReturns[i] = (allAdjValues[idx+this.numDaysForReturnCalculation]/ ! allAdjValues[idx] - 1)*coefficient; ! i++; } ! ! return ratesOfReturns; } /* --- 82,104 ---- protected override float[] getArrayOfRatesOfReturn(string ticker) { ! float[] returnValue = null; ! if(!ticker.StartsWith("-")) { ! string tickerCode = GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); ! Quotes tickerQuotes = new Quotes(tickerCode, this.firstQuoteDate, this.lastQuoteDate); ! float[] allAdjValues = ExtendedDataTable.GetArrayOfFloatFromColumn(tickerQuotes, "quAdjustedClose"); ! returnValue = new float[allAdjValues.Length/this.numDaysForReturnCalculation + 1]; ! int i = 0; //index for ratesOfReturns array ! ! for(int idx = 0; idx + this.numDaysForReturnCalculation < allAdjValues.Length; idx += this.numDaysForReturnCalculation ) ! { ! returnValue[i] = (allAdjValues[idx+this.numDaysForReturnCalculation]/ ! allAdjValues[idx] - 1); ! i++; ! } ! this.numberOfExaminedReturns = returnValue.Length; } ! return returnValue; ! } /* Index: EndOfDayTimerHandlerLastChosenPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerLastChosenPortfolio.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** EndOfDayTimerHandlerLastChosenPortfolio.cs 24 Jun 2005 22:52:25 -0000 1.1 --- EndOfDayTimerHandlerLastChosenPortfolio.cs 25 Jun 2005 09:50:44 -0000 1.2 *************** *** 81,85 **** //if(endOfDayTimingEventArgs.EndOfDayDateTime.CompareTo(this.lastDate) == 0) // this.closePositions(); ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime.CompareTo(this.firstDate.DateTime) == 0) this.closePositions(); } --- 81,85 ---- //if(endOfDayTimingEventArgs.EndOfDayDateTime.CompareTo(this.lastDate) == 0) // this.closePositions(); ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime.CompareTo(this.lastDate.DateTime.AddDays(-1)) == 0) this.closePositions(); } Index: GenomeManagerForEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientPortfolio.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** GenomeManagerForEfficientPortfolio.cs 24 Jun 2005 22:52:25 -0000 1.10 --- GenomeManagerForEfficientPortfolio.cs 25 Jun 2005 09:50:44 -0000 1.11 *************** *** 52,55 **** --- 52,56 ---- protected PortfolioType portfolioType; protected double[] portfolioRatesOfReturn; + protected int numberOfExaminedReturns; static public string GetCleanTickerCode(string tickerModifiedCode) *************** *** 62,66 **** else return tickerModifiedCode; - } --- 63,66 ---- *************** *** 150,153 **** --- 150,155 ---- } + + protected float getCoefficient(string ticker) { *************** *** 189,194 **** { double returnValue = 0; - //OLD IMPLEMENTATION double portfolioRateOfReturn = this.getPortfolioRateOfReturn(genome.Genes()); - //OLD IMPLEMENTATION double portfolioVariance = this.getPortfolioVariance(genome.Genes()); this.portfolioRatesOfReturn = this.getPortfolioRatesOfReturn(genome.Genes()); double averagePortfolioRateOfReturn = --- 191,194 ---- *************** *** 278,281 **** --- 278,283 ---- // end of implementation of IGenomeManager + #region old implementation for variance computation + protected double getPortfolioVariance(int[] tickerIdx) { *************** *** 320,323 **** --- 322,328 ---- } + #endregion + + protected void retrieveData() { *************** *** 326,379 **** // float[] arrayOfRatesOfReturn = this.getArrayOfRatesOfReturn((string)row[0]); ! row["ArrayOfRatesOfReturn"] = arrayOfRatesOfReturn; } } ! //this protected method can be overriden by inherited classes ! //which can specify the type of rates of return ! ! //in this basic implementation rates of returns ! //are based on daily close to close protected virtual float[] getArrayOfRatesOfReturn(string ticker) { ! Quotes tickerQuotes = new Quotes(ticker, this.firstQuoteDate, this.lastQuoteDate); ! float[] allAdjValues = ExtendedDataTable.GetArrayOfFloatFromColumn(tickerQuotes, "quAdjustedClose"); ! float[] ratesOfReturns = new float[allAdjValues.Length]; ! int i = 0; //index for ratesOfReturns array ! ! for(int idx = 0; idx < allAdjValues.Length; idx++) ! { ! ratesOfReturns[i] = allAdjValues[idx+1]/ ! allAdjValues[idx] - 1; ! i++; ! } ! ! return ratesOfReturns; } ! protected double getPortfolioRateOfReturn(int[] tickerIdx) ! { ! double returnValue = 0; ! foreach(int idx in tickerIdx) ! { ! returnValue += ! BasicFunctions.SimpleAverage((float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]); ! } ! //the investment is assumed to be equally divided ! return (returnValue/this.GenomeSize); ! ! } ! protected double[] getPortfolioRatesOfReturn(int[] tickerIdx) { ! int numberOfExaminedReturns = ! ((float[])this.setOfTickers.Rows[tickerIdx[0]]["ArrayOfRatesOfReturn"]).Length; ! double[] returnValue = new double[numberOfExaminedReturns]; for(int i = 0; i<returnValue.Length; i++) { foreach(int idx in tickerIdx) { ! float[] tickerRatesOfReturn = (float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]; ! returnValue[i] += tickerRatesOfReturn[i]/this.genomeSize; //the investment is assumed to be equally divided for each ticker } --- 331,371 ---- // float[] arrayOfRatesOfReturn = this.getArrayOfRatesOfReturn((string)row[0]); ! if(arrayOfRatesOfReturn == null) ! row["ArrayOfRatesOfReturn"] = DBNull.Value; ! else ! row["ArrayOfRatesOfReturn"] = arrayOfRatesOfReturn; } } ! //this protected method must be overriden by inherited classes ! //specifing the type of rates of return that have to ! //be analyzed ! protected virtual float[] getArrayOfRatesOfReturn(string ticker) { ! float[] returnValue = null; ! return returnValue; } ! protected double[] getPortfolioRatesOfReturn(int[] tickerIdx) { ! double[] returnValue = new double[this.numberOfExaminedReturns]; ! float[] tickerRatesOfReturn; for(int i = 0; i<returnValue.Length; i++) { foreach(int idx in tickerIdx) { ! if(this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"] is System.DBNull) ! //the idx points to a ticker for which short returns are ! //to be examined ! tickerRatesOfReturn = ! (float[])this.setOfTickers.Rows[idx - this.originalNumOfTickers]["ArrayOfRatesOfReturn"]; ! else ! tickerRatesOfReturn = (float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]; ! ! returnValue[i] += ! this.getCoefficient((string)this.setOfTickers.Rows[idx][0])* ! tickerRatesOfReturn[i]/this.genomeSize; //the investment is assumed to be equally divided for each ticker } Index: RunLastChosenPortfolioOutOfSample.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/RunLastChosenPortfolioOutOfSample.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RunLastChosenPortfolioOutOfSample.cs 24 Jun 2005 22:52:25 -0000 1.1 --- RunLastChosenPortfolioOutOfSample.cs 25 Jun 2005 09:50:44 -0000 1.2 *************** *** 49,60 **** /// <summary> /// Class for running a test based on last chosen tickers of a given ! /// RunEfficientPortfolio - normally, one with optimization purposes - ! /// , after it has been executed /// </summary> [Serializable] public class RunLastChosenPortfolioOutOfSample : RunEfficientPorfolio { ! RunEfficientPorfolio testEfficientPortfolio; ! public RunLastChosenPortfolioOutOfSample(RunEfficientPorfolio testEfficientPortfolio, string benchmark, DateTime startDate, --- 49,61 ---- /// <summary> /// Class for running a test based on last chosen tickers of a given ! /// PortfolioType /// </summary> [Serializable] public class RunLastChosenPortfolioOutOfSample : RunEfficientPorfolio { ! private string[] tickers; ! private PortfolioType typeOfPortfolio; ! public RunLastChosenPortfolioOutOfSample(string[] chosenTickers, ! PortfolioType typeOfPortfolio, string benchmark, DateTime startDate, *************** *** 64,77 **** startDate, endDate, ! testEfficientPortfolio.TypeOfPortfolio, maxRunningHours) { ! this.startDateTime = new EndOfDayDateTime( startDate, EndOfDaySpecificTime.MarketOpen ); this.endDateTime = new EndOfDayDateTime( endDate, EndOfDaySpecificTime.MarketClose ); this.ScriptName = "LastChosenPortfolioOutOfSample"; ! this.testEfficientPortfolio = testEfficientPortfolio; ! } #region Run --- 65,79 ---- startDate, endDate, ! typeOfPortfolio, maxRunningHours) { ! this.tickers = chosenTickers; ! this.typeOfPortfolio = typeOfPortfolio; ! this.startDateTime = new EndOfDayDateTime( startDate, EndOfDaySpecificTime.MarketOpen ); this.endDateTime = new EndOfDayDateTime( endDate, EndOfDaySpecificTime.MarketClose ); this.ScriptName = "LastChosenPortfolioOutOfSample"; ! } #region Run *************** *** 80,85 **** { this.endOfDayTimerHandler = ! new EndOfDayTimerHandlerLastChosenPortfolio(this.testEfficientPortfolio.LastChosenTickers, ! this.testEfficientPortfolio.TypeOfPortfolio, this.account, this.benchmark, --- 82,87 ---- { this.endOfDayTimerHandler = ! new EndOfDayTimerHandlerLastChosenPortfolio(this.tickers, ! this.typeOfPortfolio, this.account, this.benchmark, *************** *** 96,100 **** protected override void run_initializeHistoricalQuoteProvider() { ! this.historicalQuoteProvider = this.testEfficientPortfolio.HistoricalQuoteProvider; } protected override void run_initializeAccount() --- 98,103 ---- protected override void run_initializeHistoricalQuoteProvider() { ! this.historicalQuoteProvider = new HistoricalAdjustedQuoteProvider(); ! } protected override void run_initializeAccount() |