[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOs
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2007-08-29 09:48:02
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/BiasedPVO In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv12371/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/BiasedPVO Modified Files: EndOfDayTimerHandlerBiasedPVO.cs Log Message: Changed inherited class: now this class derives from EndOfDayTimerHandlerBiasedPVONoThresholds (in place of EndOfDayTimerHandlerPVO) Code has been re-organized in a more logical and Object Oriented way: - WeightedPositions class has been used. - Some static methods have been deleted from SignedTicker class. They have been moved to WeightedPositions. - Basic orders'management has been moved to the primitive base class EndOfDayTimerHandler and to AccountManager class Index: EndOfDayTimerHandlerBiasedPVO.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/BiasedPVO/EndOfDayTimerHandlerBiasedPVO.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** EndOfDayTimerHandlerBiasedPVO.cs 9 Apr 2007 18:14:09 -0000 1.2 --- EndOfDayTimerHandlerBiasedPVO.cs 29 Aug 2007 09:47:54 -0000 1.3 *************** *** 39,42 **** --- 39,43 ---- using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator; using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.WeightedPVO.WeightedBalancedPVO; + using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.BiasedPVO.BiasedPVONoThresholds; namespace QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.BiasedPVO *************** *** 48,66 **** /// </summary> [Serializable] ! public class EndOfDayTimerHandlerBiasedPVO : EndOfDayTimerHandlerPVO { - protected int numOfDifferentGenomesToEvaluateOutOfSample; - protected double minimumAcceptableGain; - protected int currentGenomeIndex = 0; - protected double currentTickersGainOrLoss = 0.0; - protected Hashtable genomesCollector; - protected bool takeProfitConditionReached; - protected string[,] bestGenomesChosenTickers; - protected double[,] bestGenomesChosenTickersPortfolioWeights; - - new protected double[] currentOversoldThreshold; new protected double[] currentOverboughtThreshold; ! ! public EndOfDayTimerHandlerBiasedPVO(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, --- 49,57 ---- /// </summary> [Serializable] ! public class EndOfDayTimerHandlerBiasedPVO : EndOfDayTimerHandlerBiasedPVONoThresholds { new protected double[] currentOverboughtThreshold; ! new protected double[] currentOversoldThreshold; ! public EndOfDayTimerHandlerBiasedPVO(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, *************** *** 81,109 **** PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown, double minimumAcceptableGain): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, ! account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, ! numDaysForOscillatingPeriod, ! minLevelForOversoldThreshold, ! maxLevelForOversoldThreshold, ! minLevelForOverboughtThreshold, ! maxLevelForOverboughtThreshold, ! divisorForThresholdComputation, ! symmetricalThresholds, ! overboughtMoreThanOversoldForFixedPortfolio, ! numDaysBetweenEachOptimization, ! portfolioType, maxAcceptableCloseToCloseDrawdown) { ! ! this.numOfDifferentGenomesToEvaluateOutOfSample = numOfDifferentGenomesToEvaluateOutOfSample; ! this.minimumAcceptableGain = minimumAcceptableGain; ! this.bestGenomesChosenTickers = new string[numOfDifferentGenomesToEvaluateOutOfSample, numberOfTickersToBeChosen]; ! this.bestGenomesChosenTickersPortfolioWeights = new double[numOfDifferentGenomesToEvaluateOutOfSample, numberOfTickersToBeChosen]; ! this.currentOversoldThreshold = new double[numOfDifferentGenomesToEvaluateOutOfSample]; ! this.currentOverboughtThreshold = new double[numOfDifferentGenomesToEvaluateOutOfSample]; ! this.genomesCollector = new Hashtable(); } --- 72,94 ---- PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown, double minimumAcceptableGain): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, ! account, generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, benchmark, ! numOfDifferentGenomesToEvaluateOutOfSample, ! numDaysBetweenEachOptimization, ! portfolioType, maxAcceptableCloseToCloseDrawdown, ! minimumAcceptableGain) { ! this.currentOverboughtThreshold = new double[numOfDifferentGenomesToEvaluateOutOfSample]; ! this.currentOversoldThreshold = new double[numOfDifferentGenomesToEvaluateOutOfSample]; ! this.numDaysForOscillatingPeriod = numDaysForOscillatingPeriod; ! this.minLevelForOversoldThreshold = minLevelForOversoldThreshold; ! this.maxLevelForOversoldThreshold = maxLevelForOversoldThreshold; ! this.minLevelForOverboughtThreshold = minLevelForOverboughtThreshold; ! this.maxLevelForOverboughtThreshold = maxLevelForOverboughtThreshold; ! this.divisorForThresholdComputation = divisorForThresholdComputation; ! this.symmetricalThresholds =symmetricalThresholds; ! this.overboughtMoreThanOversoldForFixedPortfolio = overboughtMoreThanOversoldForFixedPortfolio; } *************** *** 111,207 **** #region MarketCloseEventHandler ! protected virtual double getCurrentChosenTickersGainOrLoss(IndexBasedEndOfDayTimer timer, ! int indexForChosenTickers) ! { ! double returnValue = 999.0; ! try ! { ! DateTime initialDate = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForOscillatingPeriod + 2]["quDate"]; ! //so to replicate exactly in sample scheme, where only numOscillatingDay - 1 returns ! //are computed ! DateTime finalDate = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! string[] tickers = new string[this.numberOfTickersToBeChosen]; ! double[] tickerWeights = new double[this.numberOfTickersToBeChosen]; ! for(int i = 0; i < this.numberOfTickersToBeChosen; i++) ! { ! tickers[i] = this.bestGenomesChosenTickers[indexForChosenTickers,i]; ! tickerWeights[i] = this.bestGenomesChosenTickersPortfolioWeights[indexForChosenTickers,i]; ! } ! returnValue = ! SignedTicker.GetCloseToClosePortfolioReturn( ! tickers, tickerWeights, ! initialDate,finalDate) + 1.0; ! } ! catch(MissingQuotesException ex) ! { ! ex = ex; ! } ! return returnValue; ! } ! ! private void marketCloseEventHandler_reverseOrClose(IndexBasedEndOfDayTimer timer) ! { ! double currentChosenTickersGainOrLoss = ! this.getCurrentChosenTickersGainOrLoss(timer, this.currentGenomeIndex); ! this.marketCloseEventHandler_updateStopLossAndTakeProfitConditions(); ! if(currentChosenTickersGainOrLoss != 999.0) ! //currentChosenTickersValue has been properly computed ! { ! if(currentChosenTickersGainOrLoss >= 1.0 + currentOverboughtThreshold[this.currentGenomeIndex] && ! this.portfolioHasBeenOversold) ! //open positions derive from an overSold period but now ! //the overbought threshold has been reached ! { ! this.reversePositions(); ! this.portfolioHasBeenOversold = false; ! this.portfolioHasBeenOverbought = true; ! } ! else if(currentChosenTickersGainOrLoss <= 1.0 - currentOversoldThreshold[this.currentGenomeIndex] && ! this.portfolioHasBeenOverbought) ! //open positions derive from an overBought period but now ! //the overSold threshold has been reached ! { ! this.reversePositions(); ! this.portfolioHasBeenOversold = true; ! this.portfolioHasBeenOverbought = false; ! } ! else if(this.stopLossConditionReached || ! this.takeProfitConditionReached || ! this.numDaysElapsedSinceLastOptimization + 1 == this.numDaysBetweenEachOptimization ) ! //reversal conditions have not been reached but ! //stop loss or take profit conditions yes ! //or after the close it is necessary to run ! //another optimization ! { ! base.closePositions(); ! this.orders.Clear(); ! this.portfolioHasBeenOverbought = false; ! this.portfolioHasBeenOversold = false; ! } ! } ! } ! ! private void marketCloseEventHandler_closeIfItIsTimeToClose() ! { ! this.marketCloseEventHandler_updateStopLossAndTakeProfitConditions(); ! if(this.stopLossConditionReached || ! this.takeProfitConditionReached || ! this.numDaysElapsedSinceLastOptimization + 1 == this.numDaysBetweenEachOptimization ) ! //stop loss or take profit conditions yes ! //or after the next close it is necessary to run ! //another optimization ! { ! base.closePositions(); ! this.orders.Clear(); ! this.portfolioHasBeenOverbought = false; ! this.portfolioHasBeenOversold = false; ! } ! } ! //sets currentGenomeIndex with the genome's index that crosses an overbought/oversold threshold with the //highest degree and sets currentTickersGainOrLoss accordingly ! private void marketCloseEventHandler_openPositions_chooseBestGenome(IndexBasedEndOfDayTimer timer) { //default index is the first --- 96,103 ---- #region MarketCloseEventHandler ! //sets currentGenomeIndex with the genome's index that crosses an overbought/oversold threshold with the //highest degree and sets currentTickersGainOrLoss accordingly ! protected override void openPositions_chooseBestGenome(IndexBasedEndOfDayTimer timer) { //default index is the first *************** *** 212,232 **** for(int i = 0; i < this.numOfDifferentGenomesToEvaluateOutOfSample; i++) { ! double currentChosenTickersGainOrLoss = ! this.getCurrentChosenTickersGainOrLoss(timer, i); ! if(currentChosenTickersGainOrLoss != 999.0) //currentChosenTickersValue has been properly computed { //computing degree of crossing threshold ! if(currentChosenTickersGainOrLoss >= 1.0 + this.currentOverboughtThreshold[i]) { currentDegreeOfCrossingThreshold = ! (currentChosenTickersGainOrLoss - 1.0 - this.currentOverboughtThreshold[i])/ (1 + this.currentOverboughtThreshold[i]); } ! else if (currentChosenTickersGainOrLoss <= 1.0 - this.currentOversoldThreshold[i]) { currentDegreeOfCrossingThreshold = ! (1.0 - this.currentOversoldThreshold[i] - currentChosenTickersGainOrLoss)/ (1.0 - this.currentOversoldThreshold[i]); } --- 108,128 ---- for(int i = 0; i < this.numOfDifferentGenomesToEvaluateOutOfSample; i++) { ! double currentChosenWeightedPositionsGainOrLoss = ! this.getCurrentWeightedPositionsGainOrLoss(timer, i); ! if(currentChosenWeightedPositionsGainOrLoss != 999.0) //currentChosenTickersValue has been properly computed { //computing degree of crossing threshold ! if(currentChosenWeightedPositionsGainOrLoss >= 1.0 + this.currentOverboughtThreshold[i]) { currentDegreeOfCrossingThreshold = ! (currentChosenWeightedPositionsGainOrLoss - 1.0 - this.currentOverboughtThreshold[i])/ (1 + this.currentOverboughtThreshold[i]); } ! else if (currentChosenWeightedPositionsGainOrLoss <= 1.0 - this.currentOversoldThreshold[i]) { currentDegreeOfCrossingThreshold = ! (1.0 - this.currentOversoldThreshold[i] - currentChosenWeightedPositionsGainOrLoss)/ (1.0 - this.currentOversoldThreshold[i]); } *************** *** 235,270 **** currentMaxDegreeOfCrossingThreshold = currentDegreeOfCrossingThreshold; this.currentGenomeIndex = i; ! this.currentTickersGainOrLoss = currentChosenTickersGainOrLoss; } } } ! } ! ! private void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) { ! this.currentTickersGainOrLoss = 999.0; // ! this.marketCloseEventHandler_openPositions_chooseBestGenome(timer); ! if(this.currentTickersGainOrLoss != 999.0) ! //currentChosenTickersValue has been properly computed { ! string[] tickers = new string[this.numberOfTickersToBeChosen]; ! double[] tickersWeights = new double[this.numberOfTickersToBeChosen]; ! for(int i = 0; i < this.numberOfTickersToBeChosen; i++) ! { ! tickers[i] = this.bestGenomesChosenTickers[this.currentGenomeIndex,i]; ! tickersWeights[i] = this.bestGenomesChosenTickersPortfolioWeights[this.currentGenomeIndex,i]; ! } ! if(this.currentTickersGainOrLoss >= 1.0 + currentOverboughtThreshold[this.currentGenomeIndex] && this.portfolioType == PortfolioType.ShortAndLong) { ! SignedTicker.ChangeSignOfEachTicker(tickers); ! base.openPositions(tickers, tickersWeights); ! this.portfolioHasBeenOverbought = true; ! this.portfolioHasBeenOversold = false; } ! else if (this.currentTickersGainOrLoss <= 1.0 - currentOversoldThreshold[this.currentGenomeIndex]) { ! base.openPositions(tickers, tickersWeights); this.portfolioHasBeenOverbought = false; this.portfolioHasBeenOversold = true; --- 131,168 ---- currentMaxDegreeOfCrossingThreshold = currentDegreeOfCrossingThreshold; this.currentGenomeIndex = i; ! this.currentWeightedPositionsGainOrLoss = currentChosenWeightedPositionsGainOrLoss; } } } ! } ! protected override void openPositions(IndexBasedEndOfDayTimer timer) { ! this.currentWeightedPositionsGainOrLoss = 999.0; // ! this.openPositions_chooseBestGenome(timer); ! if(this.currentWeightedPositionsGainOrLoss != 999.0) ! //currentWeightedPositionsGainOrLoss has been properly computed { ! if(this.currentWeightedPositionsGainOrLoss >= 1.0 + currentOverboughtThreshold[this.currentGenomeIndex] && this.portfolioType == PortfolioType.ShortAndLong) { ! this.weightedPositionsToEvaluateOutOfSample[this.currentGenomeIndex].Reverse(); ! try{ ! AccountManager.OpenPositions(this.weightedPositionsToEvaluateOutOfSample[this.currentGenomeIndex], ! this.account); ! this.portfolioHasBeenOverbought = true; ! this.portfolioHasBeenOversold = false; ! } ! catch(Exception ex){ ! ex=ex; ! } ! finally{ ! this.weightedPositionsToEvaluateOutOfSample[this.currentGenomeIndex].Reverse(); ! } } ! else if (this.currentWeightedPositionsGainOrLoss <= 1.0 - currentOversoldThreshold[this.currentGenomeIndex]) { ! AccountManager.OpenPositions(this.weightedPositionsToEvaluateOutOfSample[this.currentGenomeIndex], ! this.account); this.portfolioHasBeenOverbought = false; this.portfolioHasBeenOversold = true; *************** *** 273,312 **** } } ! ! private void marketCloseEventHandler_updateStopLossAndTakeProfitConditions() ! { ! this.previousAccountValue = this.currentAccountValue; ! this.currentAccountValue = this.account.GetMarketValue(); ! double portfolioGainOrLoss = (this.currentAccountValue - this.previousAccountValue) ! /this.previousAccountValue; ! ! if( portfolioGainOrLoss < -this.maxAcceptableCloseToCloseDrawdown ) ! { ! this.stopLossConditionReached = true; ! this.takeProfitConditionReached = false; ! } ! else if (portfolioGainOrLoss >= this.minimumAcceptableGain) ! ! { ! this.stopLossConditionReached = false; ! this.takeProfitConditionReached = true; ! } ! else ! { ! this.stopLossConditionReached = false; ! this.takeProfitConditionReached = false; ! } ! } ! public override void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if(this.account.Portfolio.Count > 0) - //this.marketCloseEventHandler_reverseOrClose((IndexBasedEndOfDayTimer)sender); this.marketCloseEventHandler_closeIfItIsTimeToClose(); else if ( this.account.Portfolio.Count == 0 && ! this.bestGenomesChosenTickers[0,0] != null ) //portfolio is empty and optimization has been already launched ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); } --- 171,184 ---- } } ! public override void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if(this.account.Portfolio.Count > 0) this.marketCloseEventHandler_closeIfItIsTimeToClose(); else if ( this.account.Portfolio.Count == 0 && ! this.weightedPositionsToEvaluateOutOfSample != null ) //portfolio is empty and optimization has been already launched ! this.openPositions((IndexBasedEndOfDayTimer)sender); } *************** *** 334,349 **** // fitness has been added to the hashtable yet { ! for(int i = 0; i<this.numberOfTickersToBeChosen; i++) ! { ! this.bestGenomesChosenTickers[addedGenomes,i] = ! ((GenomeMeaningPVO)currentGenome.Meaning).Tickers[i]; ! this.bestGenomesChosenTickersPortfolioWeights[addedGenomes,i] = ! ((GenomeMeaningPVO)currentGenome.Meaning).TickersPortfolioWeights[i]; ! } this.currentOversoldThreshold[addedGenomes] = ((GenomeMeaningPVO)currentGenome.Meaning).OversoldThreshold; this.currentOverboughtThreshold[addedGenomes] = ((GenomeMeaningPVO)currentGenome.Meaning).OverboughtThreshold; - this.genomesCollector.Add( ( (GenomeMeaning)currentGenome.Meaning ).HashCodeForTickerComposition, null); --- 206,216 ---- // fitness has been added to the hashtable yet { ! this.weightedPositionsToEvaluateOutOfSample[addedGenomes] = new WeightedPositions( ! ((GenomeMeaningPVO)currentGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaningPVO)currentGenome.Meaning).Tickers ) ); this.currentOversoldThreshold[addedGenomes] = ((GenomeMeaningPVO)currentGenome.Meaning).OversoldThreshold; this.currentOverboughtThreshold[addedGenomes] = ((GenomeMeaningPVO)currentGenome.Meaning).OverboughtThreshold; this.genomesCollector.Add( ( (GenomeMeaning)currentGenome.Meaning ).HashCodeForTickerComposition, null); |