[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOs
Brought to you by:
glauco_1
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv16104/Oscillators/FixedLevelOscillators/PortfolioValueOscillator Modified Files: EndOfDayTimerHandlerPVO.cs GenomeManagerPVO.cs GenomeManagerPVO_OTC.cs Log Message: The new revision moves toward an intraday enabled framework. EndOfDayDate time has been removed, DateTime is used now. The code has been changed accordingly. Index: GenomeManagerPVO.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/GenomeManagerPVO.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** GenomeManagerPVO.cs 14 Jan 2008 23:07:02 -0000 1.4 --- GenomeManagerPVO.cs 29 Sep 2008 21:17:25 -0000 1.5 *************** *** 3,7 **** GenomeManagerPVO.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- GenomeManagerPVO.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; *************** *** 46,170 **** /// </summary> [Serializable] ! public class GenomeManagerPVO : GenomeManagerForEfficientPortfolio ! { ! protected int minLevelForOversoldThreshold; ! protected int maxLevelForOversoldThreshold; ! protected int minLevelForOverboughtThreshold; ! protected int maxLevelForOverboughtThreshold; ! protected int divisorForThresholdComputation; ! protected bool symmetricalThresholds = false; ! protected bool overboughtMoreThanOversoldForFixedPortfolio = false; ! protected int numOfGenesDedicatedToThresholds; ! protected double currentOversoldThreshold = 0.0; ! protected double currentOverboughtThreshold = 0.0; ! protected int numDaysForOscillatingPeriod; ! protected ReturnsManager returnsManager; ! protected CorrelationProvider correlationProvider;//used for experimental ! //tests using 2 tickers and PearsonCorrelationCoefficient as fitness ! ! private void genomeManagerPVO_checkParametersForThresholdsComputation() ! { ! if(this.maxLevelForOverboughtThreshold < this.minLevelForOverboughtThreshold || ! this.maxLevelForOversoldThreshold < this.minLevelForOversoldThreshold || ! this.divisorForThresholdComputation < this.maxLevelForOverboughtThreshold || ! this.divisorForThresholdComputation < this.maxLevelForOversoldThreshold || ! (this.symmetricalThresholds && (this.minLevelForOversoldThreshold != this.minLevelForOverboughtThreshold || ! this.maxLevelForOversoldThreshold != this.maxLevelForOverboughtThreshold) ) || ! (this.overboughtMoreThanOversoldForFixedPortfolio && ! (this.minLevelForOverboughtThreshold > Convert.ToInt32(Convert.ToDouble(this.minLevelForOversoldThreshold)* Convert.ToDouble(this.divisorForThresholdComputation) / ! (Convert.ToDouble(this.divisorForThresholdComputation) - Convert.ToDouble(this.minLevelForOversoldThreshold) ) ) || ! this.maxLevelForOverboughtThreshold < Convert.ToInt32(Convert.ToDouble(this.maxLevelForOversoldThreshold) * Convert.ToDouble(this.divisorForThresholdComputation) / ! (Convert.ToDouble(this.divisorForThresholdComputation) - Convert.ToDouble(this.maxLevelForOversoldThreshold) ) ) ) ) ) ! throw new Exception("Bad parameters for thresholds computation!"); ! } ! public GenomeManagerPVO(DataTable setOfInitialTickers, ! DateTime firstQuoteDate, ! DateTime lastQuoteDate, ! int numberOfTickersInPortfolio, ! int numDaysForOscillatingPeriod, ! int minLevelForOversoldThreshold, ! int maxLevelForOversoldThreshold, ! int minLevelForOverboughtThreshold, ! int maxLevelForOverboughtThreshold, ! int divisorForThresholdComputation, ! bool symmetricalThresholds, ! bool overboughtMoreThanOversoldForFixedPortfolio, ! PortfolioType inSamplePortfolioType, ! string benchmark) ! : ! base(setOfInitialTickers, ! firstQuoteDate, ! lastQuoteDate, ! numberOfTickersInPortfolio, ! 0.0, ! inSamplePortfolioType, ! benchmark) ! ! ! { ! this.numDaysForOscillatingPeriod = numDaysForOscillatingPeriod; ! this.divisorForThresholdComputation = divisorForThresholdComputation; this.minLevelForOversoldThreshold = minLevelForOversoldThreshold; ! this.maxLevelForOversoldThreshold = maxLevelForOversoldThreshold; ! this.minLevelForOverboughtThreshold = minLevelForOverboughtThreshold; ! this.maxLevelForOverboughtThreshold = maxLevelForOverboughtThreshold; ! this.symmetricalThresholds = symmetricalThresholds; ! this.overboughtMoreThanOversoldForFixedPortfolio = overboughtMoreThanOversoldForFixedPortfolio; ! if(this.symmetricalThresholds)//value for thresholds must be unique ! numOfGenesDedicatedToThresholds = 1; ! else ! numOfGenesDedicatedToThresholds = 2; ! this.genomeManagerPVO_checkParametersForThresholdsComputation(); ! this.setReturnsManager(firstQuoteDate , lastQuoteDate); ! } ! ! protected virtual void setReturnsManager(DateTime firstQuoteDate, ! DateTime lastQuoteDate) ! { ! EndOfDayDateTime firstEndOfDayDateTime = ! new EndOfDayDateTime(firstQuoteDate, ! EndOfDaySpecificTime.MarketClose); ! EndOfDayDateTime lastEndOfDayDateTime = ! new EndOfDayDateTime(lastQuoteDate, ! EndOfDaySpecificTime.MarketClose); ! this.returnsManager = ! new ReturnsManager( new CloseToCloseIntervals( ! firstEndOfDayDateTime, ! lastEndOfDayDateTime, ! this.benchmark, ! this.numDaysForOscillatingPeriod), ! new HistoricalAdjustedQuoteProvider() ); ! } ! ! public override int GenomeSize ! { ! get{return this.genomeSize + this.numOfGenesDedicatedToThresholds;} ! } ! #region Get Min and Max Value ! private int getMinValueForGenes_getMinValueForTicker() ! { ! int returnValue; ! switch (this.portfolioType) ! { ! case PortfolioType.OnlyLong : ! returnValue = 0; ! break; ! default://For ShortAndLong or OnlyShort portfolios ! returnValue = - this.originalNumOfTickers; ! break; ! } ! return returnValue; ! } ! public override int GetMinValueForGenes(int genePosition) ! { ! int returnValue; ! switch (genePosition) ! { ! case 0 ://gene for oversold threshold returnValue = this.minLevelForOversoldThreshold; break; --- 46,172 ---- /// </summary> [Serializable] ! public class GenomeManagerPVO : GenomeManagerForEfficientPortfolio ! { ! protected int minLevelForOversoldThreshold; ! protected int maxLevelForOversoldThreshold; ! protected int minLevelForOverboughtThreshold; ! protected int maxLevelForOverboughtThreshold; ! protected int divisorForThresholdComputation; ! protected bool symmetricalThresholds = false; ! protected bool overboughtMoreThanOversoldForFixedPortfolio = false; ! protected int numOfGenesDedicatedToThresholds; ! protected double currentOversoldThreshold = 0.0; ! protected double currentOverboughtThreshold = 0.0; ! protected int numDaysForOscillatingPeriod; ! protected ReturnsManager returnsManager; ! protected CorrelationProvider correlationProvider;//used for experimental ! //tests using 2 tickers and PearsonCorrelationCoefficient as fitness ! ! private void genomeManagerPVO_checkParametersForThresholdsComputation() ! { ! if(this.maxLevelForOverboughtThreshold < this.minLevelForOverboughtThreshold || ! this.maxLevelForOversoldThreshold < this.minLevelForOversoldThreshold || ! this.divisorForThresholdComputation < this.maxLevelForOverboughtThreshold || ! this.divisorForThresholdComputation < this.maxLevelForOversoldThreshold || ! (this.symmetricalThresholds && (this.minLevelForOversoldThreshold != this.minLevelForOverboughtThreshold || ! this.maxLevelForOversoldThreshold != this.maxLevelForOverboughtThreshold) ) || ! (this.overboughtMoreThanOversoldForFixedPortfolio && ! (this.minLevelForOverboughtThreshold > Convert.ToInt32(Convert.ToDouble(this.minLevelForOversoldThreshold)* Convert.ToDouble(this.divisorForThresholdComputation) / ! (Convert.ToDouble(this.divisorForThresholdComputation) - Convert.ToDouble(this.minLevelForOversoldThreshold) ) ) || ! this.maxLevelForOverboughtThreshold < Convert.ToInt32(Convert.ToDouble(this.maxLevelForOversoldThreshold) * Convert.ToDouble(this.divisorForThresholdComputation) / ! (Convert.ToDouble(this.divisorForThresholdComputation) - Convert.ToDouble(this.maxLevelForOversoldThreshold) ) ) ) ) ) ! throw new Exception("Bad parameters for thresholds computation!"); ! } ! public GenomeManagerPVO(DataTable setOfInitialTickers, ! DateTime firstQuoteDate, ! DateTime lastQuoteDate, ! int numberOfTickersInPortfolio, ! int numDaysForOscillatingPeriod, ! int minLevelForOversoldThreshold, ! int maxLevelForOversoldThreshold, ! int minLevelForOverboughtThreshold, ! int maxLevelForOverboughtThreshold, ! int divisorForThresholdComputation, ! bool symmetricalThresholds, ! bool overboughtMoreThanOversoldForFixedPortfolio, ! PortfolioType inSamplePortfolioType, ! string benchmark) ! : ! base(setOfInitialTickers, ! firstQuoteDate, ! lastQuoteDate, ! numberOfTickersInPortfolio, ! 0.0, ! inSamplePortfolioType, ! benchmark) ! ! ! { ! this.numDaysForOscillatingPeriod = numDaysForOscillatingPeriod; ! this.divisorForThresholdComputation = divisorForThresholdComputation; this.minLevelForOversoldThreshold = minLevelForOversoldThreshold; ! this.maxLevelForOversoldThreshold = maxLevelForOversoldThreshold; ! this.minLevelForOverboughtThreshold = minLevelForOverboughtThreshold; ! this.maxLevelForOverboughtThreshold = maxLevelForOverboughtThreshold; ! this.symmetricalThresholds = symmetricalThresholds; ! this.overboughtMoreThanOversoldForFixedPortfolio = overboughtMoreThanOversoldForFixedPortfolio; ! if(this.symmetricalThresholds)//value for thresholds must be unique ! numOfGenesDedicatedToThresholds = 1; ! else ! numOfGenesDedicatedToThresholds = 2; ! this.genomeManagerPVO_checkParametersForThresholdsComputation(); ! this.setReturnsManager(firstQuoteDate , lastQuoteDate); ! } ! ! protected virtual void setReturnsManager(DateTime firstQuoteDate, ! DateTime lastQuoteDate) ! { ! DateTime firstDateTime = ! HistoricalEndOfDayTimer.GetMarketClose( firstQuoteDate ); ! // new EndOfDayDateTime(firstQuoteDate, ! // EndOfDaySpecificTime.MarketClose); ! DateTime lastDateTime = ! HistoricalEndOfDayTimer.GetMarketClose( lastQuoteDate ); ! // new EndOfDayDateTime(lastQuoteDate, ! // EndOfDaySpecificTime.MarketClose); ! this.returnsManager = ! new ReturnsManager( new CloseToCloseIntervals( ! firstDateTime, ! lastDateTime, ! this.benchmark, ! this.numDaysForOscillatingPeriod), ! new HistoricalAdjustedQuoteProvider() ); ! } ! ! public override int GenomeSize ! { ! get{return this.genomeSize + this.numOfGenesDedicatedToThresholds;} ! } ! #region Get Min and Max Value ! private int getMinValueForGenes_getMinValueForTicker() ! { ! int returnValue; ! switch (this.portfolioType) ! { ! case PortfolioType.OnlyLong : ! returnValue = 0; ! break; ! default://For ShortAndLong or OnlyShort portfolios ! returnValue = - this.originalNumOfTickers; ! break; ! } ! return returnValue; ! } ! public override int GetMinValueForGenes(int genePosition) ! { ! int returnValue; ! switch (genePosition) ! { ! case 0 ://gene for oversold threshold returnValue = this.minLevelForOversoldThreshold; break; *************** *** 173,206 **** returnValue = this.minLevelForOverboughtThreshold; else ! returnValue = this.getMinValueForGenes_getMinValueForTicker(); ! break; default://gene for ticker returnValue = this.getMinValueForGenes_getMinValueForTicker(); break; ! } ! return returnValue; ! } ! ! private int getMaxValueForGenes_getMaxValueForTicker() ! { ! int returnValue; ! switch (this.portfolioType) ! { ! case PortfolioType.OnlyShort : ! returnValue = - 1; ! break; ! default ://For ShortAndLong or OnlyLong portfolios ! returnValue = this.originalNumOfTickers - 1; ! break; ! } ! return returnValue; ! } ! public override int GetMaxValueForGenes(int genePosition) ! { ! int returnValue; ! switch (genePosition) ! { ! case 0 ://gene for oversold threshold returnValue = this.maxLevelForOversoldThreshold; break; --- 175,208 ---- returnValue = this.minLevelForOverboughtThreshold; else ! returnValue = this.getMinValueForGenes_getMinValueForTicker(); ! break; default://gene for ticker returnValue = this.getMinValueForGenes_getMinValueForTicker(); break; ! } ! return returnValue; ! } ! ! private int getMaxValueForGenes_getMaxValueForTicker() ! { ! int returnValue; ! switch (this.portfolioType) ! { ! case PortfolioType.OnlyShort : ! returnValue = - 1; ! break; ! default ://For ShortAndLong or OnlyLong portfolios ! returnValue = this.originalNumOfTickers - 1; ! break; ! } ! return returnValue; ! } ! public override int GetMaxValueForGenes(int genePosition) ! { ! int returnValue; ! switch (genePosition) ! { ! case 0 ://gene for oversold threshold returnValue = this.maxLevelForOversoldThreshold; break; *************** *** 214,228 **** returnValue = this.getMaxValueForGenes_getMaxValueForTicker(); break; ! } ! return returnValue; ! } ! ! #endregion ! ! #region getStrategyReturns ! ! private float[] getStrategyReturns_getReturnsActually( ! float[] plainReturns) { --- 216,230 ---- returnValue = this.getMaxValueForGenes_getMaxValueForTicker(); break; ! } ! return returnValue; ! } ! #endregion ! ! ! #region getStrategyReturns ! ! private float[] getStrategyReturns_getReturnsActually( ! float[] plainReturns) { *************** *** 232,326 **** //has been entered float coefficient = 0; ! for(int i = 0; i < returnValue.Length - 1; i++) ! { ! if( plainReturns[i] >= (float)this.currentOverboughtThreshold ) ! //portfolio has been overbought ! coefficient = -1; ! else if( plainReturns[i] <= - (float)this.currentOversoldThreshold ) ! //portfolio has been oversold ! coefficient = 1; ! //else the previous coeff is kept or, if no threshold has been ! //reached, then no positions will be opened (coefficient = 0) ! returnValue[i + 1] = coefficient * plainReturns[i + 1]; ! } ! return returnValue; //return plainReturns; } ! ! protected override float[] getStrategyReturns() { float[] plainReturns = this.weightedPositionsFromGenome.GetReturns( ! this.returnsManager); return this.getStrategyReturns_getReturnsActually(plainReturns); } ! ! #endregion ! ! private void getFitnessValue_setCurrentThresholds(Genome genome) ! { ! this.currentOversoldThreshold = Convert.ToDouble(genome.Genes()[0])/ ! Convert.ToDouble(this.divisorForThresholdComputation); ! ! if(this.symmetricalThresholds) ! this.currentOverboughtThreshold = this.currentOversoldThreshold; ! else ! this.currentOverboughtThreshold = Convert.ToDouble(genome.Genes()[1])/ ! Convert.ToDouble(this.divisorForThresholdComputation); ! } ! ! protected int getFitnessValue_getDaysOnTheMarket() ! { ! int returnValue = 0; ! foreach(float strategyReturn in this.strategyReturns) ! if(strategyReturn != 0) ! //the applied strategy gets positions on the market ! returnValue++; ! return returnValue; ! } ! private double getFitnessValue_calculate_calculateActually() ! { ! return this.AverageOfStrategyReturns/Math.Sqrt(this.VarianceOfStrategyReturns); //return Math.Sqrt(this.VarianceOfStrategyReturns); ! } ! ! ! protected override double getFitnessValue_calculate() ! { double returnValue = -1.0; ! if(this.getFitnessValue_getDaysOnTheMarket() > ! this.strategyReturns.Length / 2) ! //if the genome represents a portfolio that stays on the market ! //at least half of the theoretical days ! returnValue = ! this.getFitnessValue_calculate_calculateActually(); return returnValue; ! } ! ! protected string getFitnessValue_getFirstTickerFromGenome(Genome genome) ! { ! GenomeMeaningPVO genomeMeaning = (GenomeMeaningPVO)genome.Meaning; ! return genomeMeaning.Tickers[0]; ! } ! protected string getFitnessValue_getSecondTickerFromGenome(Genome genome) ! { ! GenomeMeaningPVO genomeMeaning = (GenomeMeaningPVO)genome.Meaning; ! return genomeMeaning.Tickers[1]; ! } ! //fitness is a sharpe-ratio based indicator for the equity line resulting //from applying the strategy ! public override double GetFitnessValue(Genome genome) ! { ! //OLD CLASSICAL IMPLEMENTATION (sharpeRatio applied to strategyReturns) ! // this.getFitnessValue_setCurrentThresholds(genome); ! // return base.GetFitnessValue(genome); ! //NEW implementation: fitness is just the pearson correlation ! //applied to two tickers. This kind of fitness is only valid ! //for experimental tests with 2-tickers portfolios double returnValue = -2.0; if(this.correlationProvider == null) --- 234,328 ---- //has been entered float coefficient = 0; ! for(int i = 0; i < returnValue.Length - 1; i++) ! { ! if( plainReturns[i] >= (float)this.currentOverboughtThreshold ) ! //portfolio has been overbought ! coefficient = -1; ! else if( plainReturns[i] <= - (float)this.currentOversoldThreshold ) ! //portfolio has been oversold ! coefficient = 1; ! //else the previous coeff is kept or, if no threshold has been ! //reached, then no positions will be opened (coefficient = 0) ! returnValue[i + 1] = coefficient * plainReturns[i + 1]; ! } ! return returnValue; //return plainReturns; } ! ! protected override float[] getStrategyReturns() { float[] plainReturns = this.weightedPositionsFromGenome.GetReturns( ! this.returnsManager); return this.getStrategyReturns_getReturnsActually(plainReturns); } ! ! #endregion ! ! private void getFitnessValue_setCurrentThresholds(Genome genome) ! { ! this.currentOversoldThreshold = Convert.ToDouble(genome.Genes()[0])/ ! Convert.ToDouble(this.divisorForThresholdComputation); ! ! if(this.symmetricalThresholds) ! this.currentOverboughtThreshold = this.currentOversoldThreshold; ! else ! this.currentOverboughtThreshold = Convert.ToDouble(genome.Genes()[1])/ ! Convert.ToDouble(this.divisorForThresholdComputation); ! } ! ! protected int getFitnessValue_getDaysOnTheMarket() ! { ! int returnValue = 0; ! foreach(float strategyReturn in this.strategyReturns) ! if(strategyReturn != 0) ! //the applied strategy gets positions on the market ! returnValue++; ! return returnValue; ! } ! private double getFitnessValue_calculate_calculateActually() ! { ! return this.AverageOfStrategyReturns/Math.Sqrt(this.VarianceOfStrategyReturns); //return Math.Sqrt(this.VarianceOfStrategyReturns); ! } ! ! ! protected override double getFitnessValue_calculate() ! { double returnValue = -1.0; ! if(this.getFitnessValue_getDaysOnTheMarket() > ! this.strategyReturns.Length / 2) ! //if the genome represents a portfolio that stays on the market ! //at least half of the theoretical days ! returnValue = ! this.getFitnessValue_calculate_calculateActually(); return returnValue; ! } ! ! protected string getFitnessValue_getFirstTickerFromGenome(Genome genome) ! { ! GenomeMeaningPVO genomeMeaning = (GenomeMeaningPVO)genome.Meaning; ! return genomeMeaning.Tickers[0]; ! } ! protected string getFitnessValue_getSecondTickerFromGenome(Genome genome) ! { ! GenomeMeaningPVO genomeMeaning = (GenomeMeaningPVO)genome.Meaning; ! return genomeMeaning.Tickers[1]; ! } ! //fitness is a sharpe-ratio based indicator for the equity line resulting //from applying the strategy ! public override double GetFitnessValue(Genome genome) ! { ! //OLD CLASSICAL IMPLEMENTATION (sharpeRatio applied to strategyReturns) ! // this.getFitnessValue_setCurrentThresholds(genome); ! // return base.GetFitnessValue(genome); ! //NEW implementation: fitness is just the pearson correlation ! //applied to two tickers. This kind of fitness is only valid ! //for experimental tests with 2-tickers portfolios double returnValue = -2.0; if(this.correlationProvider == null) *************** *** 331,336 **** string secondTicker = this.getFitnessValue_getSecondTickerFromGenome(genome); if( ( firstTicker.StartsWith("-") && !secondTicker.StartsWith("-") ) || ! ( secondTicker.StartsWith("-") && !firstTicker.StartsWith("-") ) ) ! //tickers have to be opposite in sign { double correlationIndex = correlationProvider.GetPearsonCorrelation( --- 333,338 ---- string secondTicker = this.getFitnessValue_getSecondTickerFromGenome(genome); if( ( firstTicker.StartsWith("-") && !secondTicker.StartsWith("-") ) || ! ( secondTicker.StartsWith("-") && !firstTicker.StartsWith("-") ) ) ! //tickers have to be opposite in sign { double correlationIndex = correlationProvider.GetPearsonCorrelation( *************** *** 338,463 **** SignedTicker.GetTicker(secondTicker) ); if(correlationIndex < 0.96) ! // if correlation index is not too high to be ! // probably originated by the same instrument returnValue = correlationIndex; } return returnValue; ! } ! ! public override object Decode(Genome genome) ! { ! string[] arrayOfTickers = ! new string[genome.Genes().Length - this.numOfGenesDedicatedToThresholds]; ! int geneForTicker; ! GenomeMeaningPVO meaning; ! for(int genePosition = this.numOfGenesDedicatedToThresholds; ! genePosition < genome.Genes().Length; ! genePosition++) ! { ! geneForTicker = (int)genome.Genes().GetValue(genePosition); ! arrayOfTickers[genePosition - this.numOfGenesDedicatedToThresholds] = ! this.decode_getTickerCodeForLongOrShortTrade(geneForTicker); ! } ! ! double[] arrayOfWeights = ExtendedMath.ArrayOfAbs( WeightedPositions.GetBalancedWeights(new SignedTickers(arrayOfTickers), ! this.returnsManager) ); ! ! if(this.symmetricalThresholds) ! meaning = new GenomeMeaningPVO(arrayOfTickers, ! arrayOfWeights, ! Convert.ToDouble(genome.Genes()[0])/Convert.ToDouble(this.divisorForThresholdComputation), ! Convert.ToDouble(genome.Genes()[0])/Convert.ToDouble(this.divisorForThresholdComputation), ! this.numDaysForOscillatingPeriod); ! else ! meaning = new GenomeMeaningPVO(arrayOfTickers, ! arrayOfWeights, ! Convert.ToDouble(genome.Genes()[0])/Convert.ToDouble(this.divisorForThresholdComputation), ! Convert.ToDouble(genome.Genes()[1])/Convert.ToDouble(this.divisorForThresholdComputation), ! this.numDaysForOscillatingPeriod); ! return meaning; ! } ! public override Genome[] GetChilds(Genome parent1, Genome parent2) ! { ! //in this simple implementation ! //child have the tickers of one parent ! //and the thresholds of the other ! Genome[] childs = new Genome[2]; ! childs[0] = parent1.Clone(); ! childs[1] = parent2.Clone(); ! //exchange of genes coding thresholds ! ! if(this.symmetricalThresholds)//unique value for thresholds ! { ! childs[0].SetGeneValue(parent2.GetGeneValue(0),0); ! childs[1].SetGeneValue(parent1.GetGeneValue(0),0); ! } ! else//two different values for thresholds ! { ! childs[0].SetGeneValue(parent2.GetGeneValue(0),0); ! childs[1].SetGeneValue(parent1.GetGeneValue(0),0); ! childs[0].SetGeneValue(parent2.GetGeneValue(1),1); ! childs[1].SetGeneValue(parent1.GetGeneValue(1),1); ! } ! return childs; ! } ! public override int GetNewGeneValue(Genome genome, int genePosition) ! { ! // in this implementation only new gene values pointing to tickers ! // must be different from the others already stored ! int minValueForGene = genome.GetMinValueForGenes(genePosition); ! int maxValueForGene = genome.GetMaxValueForGenes(genePosition); ! int returnValue = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene + 1); ! ! if(this.numOfGenesDedicatedToThresholds == 2 && ! this.overboughtMoreThanOversoldForFixedPortfolio && genePosition == 1) ! //genePosition points to overbought threshold, ! //dipendent from the oversold one such that the portfolio tends to be fix ! returnValue = Convert.ToInt32(Convert.ToDouble(genome.GetGeneValue(0)) * Convert.ToDouble(this.divisorForThresholdComputation) / ! (Convert.ToDouble(this.divisorForThresholdComputation) - Convert.ToDouble(genome.GetGeneValue(0)))); ! ! while(genePosition > this.numOfGenesDedicatedToThresholds - 1 ! && GenomeManipulator.IsTickerContainedInGenome(returnValue, ! genome, ! this.numOfGenesDedicatedToThresholds, ! genome.Size - 1)) ! //while in the given position has to be stored ! //a new gene pointing to a ticker and ! //the proposed returnValue points to a ticker ! //already stored in the given genome ! returnValue = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene + 1); ! ! return returnValue; ! } ! ! public override void Mutate(Genome genome) ! { ! // in this implementation only one gene is mutated ! int genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); ! int minValueForGene = genome.GetMinValueForGenes(genePositionToBeMutated); ! int maxValueForGene = genome.GetMaxValueForGenes(genePositionToBeMutated); ! int newValueForGene = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene +1); ! while(genePositionToBeMutated > this.numOfGenesDedicatedToThresholds - 1 && ! GenomeManipulator.IsTickerContainedInGenome(newValueForGene, ! genome, ! this.numOfGenesDedicatedToThresholds, ! genome.Size - 1)) ! //while in the proposed genePositionToBeMutated has to be stored ! //a new gene pointing to a ticker and ! //the proposed newValueForGene points to a ticker ! //already stored in the given genome ! newValueForGene = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene +1); ! //TODO add if when it is mutated a threshold ! //(just a single threshold or the pair of thresholds) ! if(genePositionToBeMutated > this.numOfGenesDedicatedToThresholds - 1) ! GenomeManagement.MutateOneGene(genome, ! genePositionToBeMutated, newValueForGene); ! } ! } } --- 340,465 ---- SignedTicker.GetTicker(secondTicker) ); if(correlationIndex < 0.96) ! // if correlation index is not too high to be ! // probably originated by the same instrument returnValue = correlationIndex; } return returnValue; ! } ! ! public override object Decode(Genome genome) ! { ! string[] arrayOfTickers = ! new string[genome.Genes().Length - this.numOfGenesDedicatedToThresholds]; ! int geneForTicker; ! GenomeMeaningPVO meaning; ! for(int genePosition = this.numOfGenesDedicatedToThresholds; ! genePosition < genome.Genes().Length; ! genePosition++) ! { ! geneForTicker = (int)genome.Genes().GetValue(genePosition); ! arrayOfTickers[genePosition - this.numOfGenesDedicatedToThresholds] = ! this.decode_getTickerCodeForLongOrShortTrade(geneForTicker); ! } ! ! double[] arrayOfWeights = ExtendedMath.ArrayOfAbs( WeightedPositions.GetBalancedWeights(new SignedTickers(arrayOfTickers), ! this.returnsManager) ); ! ! if(this.symmetricalThresholds) ! meaning = new GenomeMeaningPVO(arrayOfTickers, ! arrayOfWeights, ! Convert.ToDouble(genome.Genes()[0])/Convert.ToDouble(this.divisorForThresholdComputation), ! Convert.ToDouble(genome.Genes()[0])/Convert.ToDouble(this.divisorForThresholdComputation), ! this.numDaysForOscillatingPeriod); ! else ! meaning = new GenomeMeaningPVO(arrayOfTickers, ! arrayOfWeights, ! Convert.ToDouble(genome.Genes()[0])/Convert.ToDouble(this.divisorForThresholdComputation), ! Convert.ToDouble(genome.Genes()[1])/Convert.ToDouble(this.divisorForThresholdComputation), ! this.numDaysForOscillatingPeriod); ! return meaning; ! } ! public override Genome[] GetChilds(Genome parent1, Genome parent2) ! { ! //in this simple implementation ! //child have the tickers of one parent ! //and the thresholds of the other ! Genome[] childs = new Genome[2]; ! childs[0] = parent1.Clone(); ! childs[1] = parent2.Clone(); ! //exchange of genes coding thresholds ! ! if(this.symmetricalThresholds)//unique value for thresholds ! { ! childs[0].SetGeneValue(parent2.GetGeneValue(0),0); ! childs[1].SetGeneValue(parent1.GetGeneValue(0),0); ! } ! else//two different values for thresholds ! { ! childs[0].SetGeneValue(parent2.GetGeneValue(0),0); ! childs[1].SetGeneValue(parent1.GetGeneValue(0),0); ! childs[0].SetGeneValue(parent2.GetGeneValue(1),1); ! childs[1].SetGeneValue(parent1.GetGeneValue(1),1); ! } ! return childs; ! } ! public override int GetNewGeneValue(Genome genome, int genePosition) ! { ! // in this implementation only new gene values pointing to tickers ! // must be different from the others already stored ! int minValueForGene = genome.GetMinValueForGenes(genePosition); ! int maxValueForGene = genome.GetMaxValueForGenes(genePosition); ! int returnValue = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene + 1); ! ! if(this.numOfGenesDedicatedToThresholds == 2 && ! this.overboughtMoreThanOversoldForFixedPortfolio && genePosition == 1) ! //genePosition points to overbought threshold, ! //dipendent from the oversold one such that the portfolio tends to be fix ! returnValue = Convert.ToInt32(Convert.ToDouble(genome.GetGeneValue(0)) * Convert.ToDouble(this.divisorForThresholdComputation) / ! (Convert.ToDouble(this.divisorForThresholdComputation) - Convert.ToDouble(genome.GetGeneValue(0)))); ! ! while(genePosition > this.numOfGenesDedicatedToThresholds - 1 ! && GenomeManipulator.IsTickerContainedInGenome(returnValue, ! genome, ! this.numOfGenesDedicatedToThresholds, ! genome.Size - 1)) ! //while in the given position has to be stored ! //a new gene pointing to a ticker and ! //the proposed returnValue points to a ticker ! //already stored in the given genome ! returnValue = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene + 1); ! ! return returnValue; ! } ! ! public override void Mutate(Genome genome) ! { ! // in this implementation only one gene is mutated ! int genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); ! int minValueForGene = genome.GetMinValueForGenes(genePositionToBeMutated); ! int maxValueForGene = genome.GetMaxValueForGenes(genePositionToBeMutated); ! int newValueForGene = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene +1); ! while(genePositionToBeMutated > this.numOfGenesDedicatedToThresholds - 1 && ! GenomeManipulator.IsTickerContainedInGenome(newValueForGene, ! genome, ! this.numOfGenesDedicatedToThresholds, ! genome.Size - 1)) ! //while in the proposed genePositionToBeMutated has to be stored ! //a new gene pointing to a ticker and ! //the proposed newValueForGene points to a ticker ! //already stored in the given genome ! newValueForGene = GenomeManagement.RandomGenerator.Next(minValueForGene, ! maxValueForGene +1); ! //TODO add if when it is mutated a threshold ! //(just a single threshold or the pair of thresholds) ! if(genePositionToBeMutated > this.numOfGenesDedicatedToThresholds - 1) ! GenomeManagement.MutateOneGene(genome, ! genePositionToBeMutated, newValueForGene); ! } ! } } Index: EndOfDayTimerHandlerPVO.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/EndOfDayTimerHandlerPVO.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** EndOfDayTimerHandlerPVO.cs 19 Aug 2008 17:11:28 -0000 1.10 --- EndOfDayTimerHandlerPVO.cs 29 Sep 2008 21:17:25 -0000 1.11 *************** *** 51,55 **** /// </summary> [Serializable] ! public class EndOfDayTimerHandlerPVO : EndOfDayTimerHandler { protected int minLevelForOversoldThreshold; --- 51,56 ---- /// </summary> [Serializable] ! public class EndOfDayTimerHandlerPVO : ! QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios.EndOfDayTimerHandler { protected int minLevelForOversoldThreshold; *************** *** 116,126 **** } ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } ! #region MarketCloseEventHandler protected virtual double getCurrentChosenWeightedPositionsReturn(IndexBasedEndOfDayTimer timer) --- 117,127 ---- } ! protected override void marketOpenEventHandler( ! Object sender , DateTime dateTime ) { ; } ! #region marketCloseEventHandler protected virtual double getCurrentChosenWeightedPositionsReturn(IndexBasedEndOfDayTimer timer) *************** *** 133,143 **** DateTime today = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! ReturnsManager returnsManager = new ReturnsManager(new CloseToCloseIntervals( ! new EndOfDayDateTime(firstDayOfOscillatingPeriod, ! EndOfDaySpecificTime.MarketClose) , ! new EndOfDayDateTime(today, ! EndOfDaySpecificTime.MarketClose) , ! this.benchmark , this.numDaysForOscillatingPeriod ) , ! this.historicalQuoteProvider ); returnValue = this.chosenWeightedPositions.GetReturn(0,returnsManager); } --- 134,149 ---- DateTime today = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! ReturnsManager returnsManager = new ReturnsManager( ! new CloseToCloseIntervals( ! HistoricalEndOfDayTimer.GetMarketClose( ! firstDayOfOscillatingPeriod ) , ! HistoricalEndOfDayTimer.GetMarketClose( today ) , ! // new EndOfDayDateTime(firstDayOfOscillatingPeriod, ! // EndOfDaySpecificTime.MarketClose) , ! // new EndOfDayDateTime(today, ! // EndOfDaySpecificTime.MarketClose) , ! this.benchmark , ! this.numDaysForOscillatingPeriod ) , ! this.historicalQuoteProvider ); returnValue = this.chosenWeightedPositions.GetReturn(0,returnsManager); } *************** *** 254,259 **** } ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { this.marketCloseEventHandler_updateStopLossAndTakeProfitConditions(); --- 260,265 ---- } ! protected override void marketCloseEventHandler( ! Object sender , DateTime dateTime ) { this.marketCloseEventHandler_updateStopLossAndTakeProfitConditions(); *************** *** 272,276 **** #endregion ! #region OneHourAfterMarketCloseEventHandler protected virtual DataTable getSetOfTickersToBeOptimized(DateTime currentDate) --- 278,282 ---- #endregion ! #region oneHourAfterMarketCloseEventHandler protected virtual DataTable getSetOfTickersToBeOptimized(DateTime currentDate) *************** *** 404,411 **** /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public override void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! this.lastCloseDate = endOfDayTimingEventArgs.EndOfDayDateTime.DateTime; this.seedForRandomGenerator++; this.numDaysElapsedSinceLastOptimization++; --- 410,417 ---- /// <param name="sender"></param> /// <param name="eventArgs"></param> ! protected override void oneHourAfterMarketCloseEventHandler( ! Object sender , DateTime dateTime ) { ! this.lastCloseDate = dateTime; this.seedForRandomGenerator++; this.numDaysElapsedSinceLastOptimization++; *************** *** 414,418 **** //num days without optimization has elapsed { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); //sets tickers to be chosen next Market Close event this.numDaysElapsedSinceLastOptimization = 0; --- 420,424 ---- //num days without optimization has elapsed { ! this.setTickers(dateTime, false); //sets tickers to be chosen next Market Close event this.numDaysElapsedSinceLastOptimization = 0; *************** *** 421,424 **** --- 427,441 ---- } #endregion + + public virtual void NewTimeEventHandler( + Object sender , DateTime dateTime ) + { + if ( HistoricalEndOfDayTimer.IsMarketOpen( dateTime ) ) + this.marketOpenEventHandler( sender , dateTime ); + if ( HistoricalEndOfDayTimer.IsMarketClose( dateTime ) ) + this.marketCloseEventHandler( sender , dateTime ); + if ( HistoricalEndOfDayTimer.IsOneHourAfterMarketClose( dateTime ) ) + this.oneHourAfterMarketCloseEventHandler( sender , dateTime ); + } } } Index: GenomeManagerPVO_OTC.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/GenomeManagerPVO_OTC.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GenomeManagerPVO_OTC.cs 14 Jan 2008 22:58:11 -0000 1.1 --- GenomeManagerPVO_OTC.cs 29 Sep 2008 21:17:25 -0000 1.2 *************** *** 3,7 **** GenomeManagerPVO_OTC.cs ! Copyright (C) 2008 Marco Milletti --- 3,7 ---- GenomeManagerPVO_OTC.cs ! Copyright (C) 2008 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; *************** *** 47,109 **** /// </summary> [Serializable] ! public class GenomeManagerPVO_OTC : GenomeManagerPVO ! { ! ! public GenomeManagerPVO_OTC(DataTable setOfInitialTickers, ! DateTime firstQuoteDate, ! DateTime lastQuoteDate, ! int numberOfTickersInPortfolio, ! int minLevelForOversoldThreshold, ! int maxLevelForOversoldThreshold, ! int minLevelForOverboughtThreshold, ! int maxLevelForOverboughtThreshold, ! int divisorForThresholdComputation, ! bool symmetricalThresholds, ! bool overboughtMoreThanOversoldForFixedPortfolio, ! PortfolioType inSamplePortfolioType, ! string benchmark) ! : ! base(setOfInitialTickers, ! firstQuoteDate, ! lastQuoteDate, ! numberOfTickersInPortfolio, ! 1, ! minLevelForOversoldThreshold, ! maxLevelForOversoldThreshold, ! minLevelForOverboughtThreshold, ! maxLevelForOverboughtThreshold, ! divisorForThresholdComputation, ! symmetricalThresholds, ! overboughtMoreThanOversoldForFixedPortfolio, ! inSamplePortfolioType, ! benchmark) ! ! ! { ! ! } ! ! protected override void setReturnsManager(DateTime firstQuoteDate, ! DateTime lastQuoteDate) ! { ! EndOfDayDateTime firstEndOfDayDateTime = ! new EndOfDayDateTime(firstQuoteDate, ! EndOfDaySpecificTime.MarketOpen); ! EndOfDayDateTime lastEndOfDayDateTime = ! new EndOfDayDateTime(lastQuoteDate, ! EndOfDaySpecificTime.MarketClose); ! this.returnsManager = ! new ReturnsManager( new DailyOpenToCloseIntervals( ! firstEndOfDayDateTime, ! lastEndOfDayDateTime, ! this.benchmark), ! new HistoricalRawQuoteProvider() ); ! } ! ! //fitness is a sharpe-ratio based indicator for the equity line resulting //from applying the strategy ! public override double GetFitnessValue(Genome genome) ! { //NEW implementation: fitness is just the pearson correlation //applied to two tickers. This kind of fitness is only valid --- 47,111 ---- /// </summary> [Serializable] ! public class GenomeManagerPVO_OTC : GenomeManagerPVO ! { ! ! public GenomeManagerPVO_OTC(DataTable setOfInitialTickers, ! DateTime firstQuoteDate, ! DateTime lastQuoteDate, ! int numberOfTickersInPortfolio, ! int minLevelForOversoldThreshold, ! int maxLevelForOversoldThreshold, ! int minLevelForOverboughtThreshold, ! int maxLevelForOverboughtThreshold, ! int divisorForThresholdComputation, ! bool symmetricalThresholds, ! bool overboughtMoreThanOversoldForFixedPortfolio, ! PortfolioType inSamplePortfolioType, ! string benchmark) ! : ! base(setOfInitialTickers, ! firstQuoteDate, ! lastQuoteDate, ! numberOfTickersInPortfolio, ! 1, ! minLevelForOversoldThreshold, ! maxLevelForOversoldThreshold, ! minLevelForOverboughtThreshold, ! maxLevelForOverboughtThreshold, ! divisorForThresholdComputation, ! symmetricalThresholds, ! overboughtMoreThanOversoldForFixedPortfolio, ! inSamplePortfolioType, ! benchmark) ! ! ! { ! ! } ! ! protected override void setReturnsManager(DateTime firstQuoteDate, ! DateTime lastQuoteDate) ! { ! DateTime firstDateTime = ! HistoricalEndOfDayTimer.GetMarketOpen( firstQuoteDate ); ! // new EndOfDayDateTime(firstQuoteDate, ! // EndOfDaySpecificTime.MarketOpen); ! DateTime lastDateTime = ! HistoricalEndOfDayTimer.GetMarketClose( lastQuoteDate ); ! // new EndOfDayDateTime(lastQuoteDate, ! // EndOfDaySpecificTime.MarketClose); ! this.returnsManager = ! new ReturnsManager( new DailyOpenToCloseIntervals( ! firstDateTime, ! lastDateTime, ! this.benchmark), ! new HistoricalRawQuoteProvider() ); ! } ! ! //fitness is a sharpe-ratio based indicator for the equity line resulting //from applying the strategy ! public override double GetFitnessValue(Genome genome) ! { //NEW implementation: fitness is just the pearson correlation //applied to two tickers. This kind of fitness is only valid *************** *** 117,122 **** string secondTicker = this.getFitnessValue_getSecondTickerFromGenome(genome); if( ( firstTicker.StartsWith("-") && !secondTicker.StartsWith("-") ) || ! ( secondTicker.StartsWith("-") && !firstTicker.StartsWith("-") ) ) ! //tickers have to be opposite in sign { double correlationIndex = correlationProvider.GetPearsonCorrelation( --- 119,124 ---- string secondTicker = this.getFitnessValue_getSecondTickerFromGenome(genome); if( ( firstTicker.StartsWith("-") && !secondTicker.StartsWith("-") ) || ! ( secondTicker.StartsWith("-") && !firstTicker.StartsWith("-") ) ) ! //tickers have to be opposite in sign { double correlationIndex = correlationProvider.GetPearsonCorrelation( *************** *** 124,133 **** SignedTicker.GetTicker(secondTicker) ); if(correlationIndex < 0.96) ! // if correlation index is not too high to be ! // probably originated by the same instrument returnValue = correlationIndex; } return returnValue; ! } ! } } --- 126,135 ---- SignedTicker.GetTicker(secondTicker) ); if(correlationIndex < 0.96) ! // if correlation index is not too high to be ! // probably originated by the same instrument returnValue = correlationIndex; } return returnValue; ! } ! } } |