[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting EndOfDayTimerHandler.cs,1.1
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-04-26 19:02:35
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8284/b7_Scripts/TickerSelectionTesting Modified Files: EndOfDayTimerHandler.cs EndOfDayTimerHandlerCTC.cs EndOfDayTimerHandlerCTO.cs GenomeManagerForEfficientCTCPortfolio.cs GenomeManagerForEfficientCTOPortfolio.cs GenomeManagerForEfficientPortfolio.cs PortfolioType.cs RunEfficientPortfolio.cs Log Message: Updated files for the EfficientPortfolio script: - Fixed bug: now co-variance is computed; - It is now possible to find efficient portfolios in which short or long positions can be taken on the behalf of genetic optimization's results . Index: PortfolioType.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/PortfolioType.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PortfolioType.cs 11 Apr 2005 18:52:32 -0000 1.1 --- PortfolioType.cs 26 Apr 2005 19:02:14 -0000 1.2 *************** *** 32,36 **** { OnlyLong, ! OnlyShort } } --- 32,37 ---- { OnlyLong, ! OnlyShort, ! ShortAndLong } } Index: EndOfDayTimerHandlerCTO.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTO.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** EndOfDayTimerHandlerCTO.cs 14 Apr 2005 18:38:28 -0000 1.8 --- EndOfDayTimerHandlerCTO.cs 26 Apr 2005 19:02:12 -0000 1.9 *************** *** 71,75 **** { this.addOrderForTicker( ticker ); ! this.lastChosenTickers[idx] = ticker; } idx++; --- 71,76 ---- { this.addOrderForTicker( ticker ); ! this.lastChosenTickers[idx] = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); } idx++; *************** *** 145,159 **** DataTable tickersByPrice = selectorByOpenPrice.GetTableOfSelectedTickers(); */ ! SelectorByWinningOpenToClose selectorByWinningOpenToClose = new SelectorByWinningOpenToClose(this.tickerGroupID, false, currentDate, currentDate, this.numberOfEligibleTickers); DataTable winningOpenToCloseTickers = selectorByWinningOpenToClose.GetTableOfSelectedTickers(); ! SelectorByLiquidity mostLiquid = new SelectorByLiquidity(winningOpenToCloseTickers, false, ! currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers); ! this.eligibleTickers = mostLiquid.GetTableOfSelectedTickers(); SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay( this.eligibleTickers, false, currentDate.AddDays(-this.numDaysForLiquidity), ! currentDate, this.numberOfEligibleTickers, this.benchmark); return quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); } --- 146,168 ---- DataTable tickersByPrice = selectorByOpenPrice.GetTableOfSelectedTickers(); */ ! /*SelectorByWinningOpenToClose selectorByWinningOpenToClose = new SelectorByWinningOpenToClose(this.tickerGroupID, false, currentDate, currentDate, this.numberOfEligibleTickers); DataTable winningOpenToCloseTickers = selectorByWinningOpenToClose.GetTableOfSelectedTickers(); ! */ ! SelectorByLiquidity mostLiquid = new SelectorByLiquidity(this.tickerGroupID, false, ! currentDate.AddDays(-this.numDaysForLiquidity), currentDate, ! this.numberOfEligibleTickers/2); ! SelectorByOpenToCloseVolatility lessVolatile = ! new SelectorByOpenToCloseVolatility(mostLiquid.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForLiquidity), ! currentDate, ! this.numberOfEligibleTickers/4); ! ! this.eligibleTickers = lessVolatile.GetTableOfSelectedTickers(); SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay( this.eligibleTickers, false, currentDate.AddDays(-this.numDaysForLiquidity), ! currentDate, this.numberOfEligibleTickers/4, this.benchmark); return quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); } Index: GenomeManagerForEfficientCTOPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientCTOPortfolio.cs,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** GenomeManagerForEfficientCTOPortfolio.cs 11 Apr 2005 18:52:31 -0000 1.7 --- GenomeManagerForEfficientCTOPortfolio.cs 26 Apr 2005 19:02:13 -0000 1.8 *************** *** 87,98 **** { float[] returnValue; ! Quotes tickerQuotes = new Quotes(ticker, this.firstQuoteDate, this.lastQuoteDate); returnValue = ExtendedDataTable.GetArrayOfFloatFromRatioOfColumns(tickerQuotes, "quClose", "quOpen"); for(int idx = 0; idx!= returnValue.Length; idx++) { ! returnValue[idx] = returnValue[idx] - 1; } return returnValue; } } --- 87,102 ---- { 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; } + } Index: RunEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/RunEfficientPortfolio.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RunEfficientPortfolio.cs 17 Apr 2005 13:36:37 -0000 1.4 --- RunEfficientPortfolio.cs 26 Apr 2005 19:02:14 -0000 1.5 *************** *** 53,57 **** public class RunEfficientPorfolio { ! protected string tickerGroupID; protected int numberOfEligibleTickers; --- 53,59 ---- public class RunEfficientPorfolio { ! public static double MaxNumberOfHoursForScript = 10; ! //if MaxNumberOfHoursForScript has elapsed and the script ! //is still running, it will be stopped. protected string tickerGroupID; protected int numberOfEligibleTickers; *************** *** 83,86 **** --- 85,90 ---- protected PortfolioType portfolioType; + + protected DateTime startingTimeForScript; public virtual string ScriptName *************** *** 115,118 **** --- 119,123 ---- this.targetReturn = targetReturn; this.portfolioType = portfolioType; + this.startingTimeForScript = DateTime.Now; //this.numIntervalDays = 3; } *************** *** 157,164 **** if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime || ! File.Exists(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf('\\')) ! + @"\StopScript.txt")) ! //If you want to stop timer and save script until now, create ! //in bin dir a file named "StopScript.txt" { this.endOfDayTimer.Stop(); --- 162,168 ---- if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime || ! DateTime.Now >= this.startingTimeForScript.AddHours(RunEfficientPorfolio.MaxNumberOfHoursForScript)) ! //last date is reached by the timer or MaxNumberOfHoursForScript hours ! //are elapsed from the time script started { this.endOfDayTimer.Stop(); Index: EndOfDayTimerHandlerCTC.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTC.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** EndOfDayTimerHandlerCTC.cs 14 Apr 2005 18:38:27 -0000 1.4 --- EndOfDayTimerHandlerCTC.cs 26 Apr 2005 19:02:11 -0000 1.5 *************** *** 134,138 **** foreach ( string ticker in this.chosenTickers ) { ! this.lastChosenTickers[idx] = ticker; if(ticker != null) this.addOrderForTicker( ticker ); --- 134,139 ---- foreach ( string ticker in this.chosenTickers ) { ! this.lastChosenTickers[idx] = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); if(ticker != null) this.addOrderForTicker( ticker ); Index: GenomeManagerForEfficientCTCPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientCTCPortfolio.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** GenomeManagerForEfficientCTCPortfolio.cs 11 Apr 2005 18:52:31 -0000 1.2 --- GenomeManagerForEfficientCTCPortfolio.cs 26 Apr 2005 19:02:12 -0000 1.3 *************** *** 79,83 **** protected override 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/this.numDaysOfPortfolioLife + 1]; --- 79,85 ---- 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.numDaysOfPortfolioLife + 1]; *************** *** 86,91 **** for(int idx = 0; idx + this.numDaysOfPortfolioLife < allAdjValues.Length; idx += this.numDaysOfPortfolioLife ) { ! ratesOfReturns[i] = allAdjValues[idx+this.numDaysOfPortfolioLife]/ ! allAdjValues[idx] - 1; i++; } --- 88,93 ---- for(int idx = 0; idx + this.numDaysOfPortfolioLife < allAdjValues.Length; idx += this.numDaysOfPortfolioLife ) { ! ratesOfReturns[i] = (allAdjValues[idx+this.numDaysOfPortfolioLife]/ ! allAdjValues[idx] - 1)*coefficient; i++; } Index: GenomeManagerForEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientPortfolio.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** GenomeManagerForEfficientPortfolio.cs 19 Apr 2005 18:33:43 -0000 1.6 --- GenomeManagerForEfficientPortfolio.cs 26 Apr 2005 19:02:13 -0000 1.7 *************** *** 28,32 **** using QuantProject.Data; using QuantProject.Data.DataTables; ! namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios --- 28,32 ---- using QuantProject.Data; using QuantProject.Data.DataTables; ! using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios *************** *** 43,46 **** --- 43,47 ---- protected DataTable setOfTickers; + protected int originalNumOfTickers; protected DateTime firstQuoteDate; protected DateTime lastQuoteDate; *************** *** 50,53 **** --- 51,66 ---- protected PortfolioType portfolioType; + static public string GetCleanTickerCode(string tickerModifiedCode) + { + if(tickerModifiedCode.StartsWith("-")) + //if the first char is "-" + //each element of the array of rates of return is + //multiplied by -1 + return tickerModifiedCode.Substring(1,tickerModifiedCode.Length -1); + else + return tickerModifiedCode; + + } + //IGenomeManager implementation for properties public int GenomeSize *************** *** 92,96 **** { this.setOfTickers = setOfInitialTickers; ! if(!this.setOfTickers.Columns.Contains("ArrayOfRatesOfReturn")) this.setOfTickers.Columns.Add("ArrayOfRatesOfReturn", System.Type.GetType("System.Array")); --- 105,109 ---- { this.setOfTickers = setOfInitialTickers; ! this.originalNumOfTickers = setOfInitialTickers.Rows.Count; if(!this.setOfTickers.Columns.Contains("ArrayOfRatesOfReturn")) this.setOfTickers.Columns.Add("ArrayOfRatesOfReturn", System.Type.GetType("System.Array")); *************** *** 99,108 **** this.targetPerformance = targetPerformance; this.genomeSize = numberOfTickersInPortfolio; //each genes is the index for the setOfTickers table this.minValueForGenes = 0; ! this.maxValueForGenes = this.setOfTickers.Rows.Count - 1; ! this.portfolioType = portfolioType; } public virtual double GetFitnessValue(Genome genome) { --- 112,167 ---- this.targetPerformance = targetPerformance; this.genomeSize = numberOfTickersInPortfolio; + this.portfolioType = portfolioType; + this.setMinAndMaxValueForGenes(); + this.set_SetOfInitialTickers(); + + } + + + private void set_SetOfInitialTickers() + { + + if(this.portfolioType == PortfolioType.ShortAndLong) + { + for(int i = 0;i<this.originalNumOfTickers;i++) + { + string ticker = (string)this.setOfTickers.Rows[i][0]; + DataRow newRow = this.setOfTickers.NewRow(); + newRow[0] = "-" + ticker; + this.setOfTickers.Rows.Add(newRow); + //so, if row[i][0]="TICKER" + //row[i+originalNumOfTickers][0]="-TICKER" + } + } + } + + private void setMinAndMaxValueForGenes() + { //each genes is the index for the setOfTickers table this.minValueForGenes = 0; ! ! if(this.portfolioType == PortfolioType.OnlyLong || ! this.portfolioType == PortfolioType.OnlyShort) ! this.maxValueForGenes = this.setOfTickers.Rows.Count - 1; ! else//ShortAndLong ! this.maxValueForGenes = this.setOfTickers.Rows.Count*2 - 1; ! } ! ! protected float getCoefficient(string ticker) ! { ! float returnValue; ! if(ticker.StartsWith("-")) ! //if the first char is "-" ! //each element of the array of rates of return is ! //multiplied by -1 ! returnValue = -1; ! else ! returnValue = 1; ! ! return returnValue; ! } + public virtual double GetFitnessValue(Genome genome) { *************** *** 121,126 **** this.rateOfReturn = portfolioRateOfReturn; NormalDistribution normal = new NormalDistribution(portfolioRateOfReturn, Math.Sqrt(portfolioVariance)); ! if(this.portfolioType == PortfolioType.OnlyLong) ! //returnValue = normal.GetProbability(this.targetPerformance*0.75,this.targetPerformance*1.25); returnValue = 1.0 - normal.GetProbability(this.targetPerformance); else//only short orders are permitted --- 180,188 ---- this.rateOfReturn = portfolioRateOfReturn; NormalDistribution normal = new NormalDistribution(portfolioRateOfReturn, Math.Sqrt(portfolioVariance)); ! if(this.portfolioType == PortfolioType.OnlyLong || ! this.portfolioType == PortfolioType.ShortAndLong) ! //the genome fitness is evaluated as if ! //the portfolio was long ! //ALTERNATIVE: returnValue = normal.GetProbability(this.targetPerformance*0.75,this.targetPerformance*1.25); returnValue = 1.0 - normal.GetProbability(this.targetPerformance); else//only short orders are permitted *************** *** 142,146 **** int returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, genome.MaxValueForGenes + 1); ! while(genome.HasGene(returnValue)) { returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, --- 204,211 ---- int returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, genome.MaxValueForGenes + 1); ! while(genome.HasGene(returnValue) || ! genome.HasGene(returnValue + this.originalNumOfTickers)) ! //the portfolio can't have a long position and a short position ! // for the same ticker { returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, *************** *** 208,212 **** float[] ticker_i; float[] ticker_j; ! double tickerCoeff = 1/this.genomeSize; for(int i = 0; i<this.genomeSize ; i++) { --- 273,277 ---- float[] ticker_i; float[] ticker_j; ! double tickerCoeff = 1.0/this.genomeSize; for(int i = 0; i<this.genomeSize ; i++) { Index: EndOfDayTimerHandler.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandler.cs,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** EndOfDayTimerHandler.cs 14 Apr 2005 18:38:27 -0000 1.11 --- EndOfDayTimerHandler.cs 26 Apr 2005 19:02:11 -0000 1.12 *************** *** 102,114 **** protected virtual void addOrderForTicker(string ticker ) { ! double cashForSinglePosition = this.account.CashAmount / this.numberOfTickersToBeChosen; long quantity = ! Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( ticker ) ) ); Order order; ! if(this.portfolioType == PortfolioType.OnlyLong) ! order = new Order( OrderType.MarketBuy, new Instrument( ticker ) , quantity ); ! else// only short orders are permitted ! order = new Order( OrderType.MarketSellShort, new Instrument( ticker ) , quantity ); this.orders.Add(order); --- 102,117 ---- protected virtual void addOrderForTicker(string ticker ) { ! string tickerCode = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); double cashForSinglePosition = this.account.CashAmount / this.numberOfTickersToBeChosen; long quantity = ! Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( tickerCode ) ) ); Order order; ! if(this.portfolioType == PortfolioType.OnlyShort || ! (this.portfolioType == PortfolioType.ShortAndLong && ! ticker != tickerCode)) ! order = new Order( OrderType.MarketSellShort, new Instrument( tickerCode ) , quantity ); ! else ! order = new Order( OrderType.MarketBuy, new Instrument( tickerCode ) , quantity ); this.orders.Add(order); |