[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOs
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2008-01-14 23:07:38
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/BiasedPVO/BiasedPVONoThresholds In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv7110/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/BiasedPVO/BiasedPVONoThresholds Modified Files: EndOfDayTimerHandlerBiasedPVONoThresholds.cs GenomeManagerBiasedPVONoThresholds.cs Log Message: Several changes applied to the classes implementing the Portfolio Value Oscillator strategy (the most important one relates to the use of ReturnsManager) Index: GenomeManagerBiasedPVONoThresholds.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/BiasedPVO/BiasedPVONoThresholds/GenomeManagerBiasedPVONoThresholds.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GenomeManagerBiasedPVONoThresholds.cs 27 Feb 2007 22:52:40 -0000 1.1 --- GenomeManagerBiasedPVONoThresholds.cs 14 Jan 2008 23:07:04 -0000 1.2 *************** *** 24,27 **** --- 24,28 ---- using System.Data; using System.Collections; + using QuantProject.ADT; using QuantProject.ADT.Statistics; *************** *** 29,32 **** --- 30,38 ---- using QuantProject.Data; using QuantProject.Data.DataTables; + using QuantProject.Business.DataProviders; + using QuantProject.Business.Timing; + using QuantProject.Business.Strategies; + using QuantProject.Business.Strategies.ReturnsManagement; + using QuantProject.Business.Strategies.ReturnsManagement.Time; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; *************** *** 41,50 **** public class GenomeManagerBiasedPVONoThresholds : GenomeManagerForEfficientPortfolio { ! public GenomeManagerBiasedPVONoThresholds(DataTable setOfInitialTickers, DateTime firstQuoteDate, DateTime lastQuoteDate, int numberOfTickersInPortfolio, ! PortfolioType inSamplePortfolioType) : base(setOfInitialTickers, --- 47,58 ---- public class GenomeManagerBiasedPVONoThresholds : GenomeManagerForEfficientPortfolio { ! private ReturnsManager returnsManager; ! public GenomeManagerBiasedPVONoThresholds(DataTable setOfInitialTickers, DateTime firstQuoteDate, DateTime lastQuoteDate, int numberOfTickersInPortfolio, ! PortfolioType inSamplePortfolioType, ! string benchmark) : base(setOfInitialTickers, *************** *** 53,142 **** numberOfTickersInPortfolio, 0.0, ! inSamplePortfolioType) { ! this.retrieveData(); } ! #region Get Min and Max Value ! ! public override int GetMinValueForGenes(int genePosition) ! { ! 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 GetMaxValueForGenes(int genePosition) ! { ! int returnValue; ! switch (this.portfolioType) { ! case PortfolioType.OnlyShort : ! returnValue = - 1; ! break; ! default ://For ShortAndLong or OnlyLong portfolios ! returnValue = this.originalNumOfTickers - 1; ! break; } - return returnValue; - } - - #endregion - - protected override float[] getArrayOfRatesOfReturn(string ticker) - { - float[] returnValue = null; - Quotes tickerQuotes = new Quotes(ticker, this.firstQuoteDate, this.lastQuoteDate); - tickerQuotes.RecalculateCloseToCloseRatios(); - returnValue = QuantProject.ADT.ExtendedDataTable.GetArrayOfFloatFromColumn(tickerQuotes, - Quotes.AdjustedCloseToCloseRatio); - for(int i = 0; i<returnValue.Length; i++) - returnValue[i] = returnValue[i] - 1.0f; - - this.numberOfExaminedReturns = returnValue.Length; - - return returnValue; - } - - //fitness is a number that indicates how much the portfolio - //tends to preserve equity (no gain and no loss), with a low std dev - public override double GetFitnessValue(Genome genome) - { - double returnValue = -1.0; - this.portfolioRatesOfReturn = this.getPortfolioRatesOfReturn( genome.Genes() ); - //double[] asbolutePortfolioRatesOfReturns = new double[this.portfolioRatesOfReturn.Length]; - // for( int i = 0; i<asbolutePortfolioRatesOfReturns.Length; i++ ) - // asbolutePortfolioRatesOfReturns[i] = Math.Abs(this.portfolioRatesOfReturn[i]); - returnValue = 1.0/ - //( BasicFunctions.SimpleAverage(asbolutePortfolioRatesOfReturns) * - ( - Math.Abs( BasicFunctions.SimpleAverage(this.portfolioRatesOfReturn) ) * - BasicFunctions.StdDev(this.portfolioRatesOfReturn) ); //); return returnValue; ! } ! public override 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] = this.decode_getTickerCodeForLongOrShortTrade(indexOfTicker); ! } ! GenomeMeaningPVO meaning = new GenomeMeaningPVO(arrayOfTickers, ! 0.0, 0.0, 2); ! return meaning; ! } } } --- 61,134 ---- numberOfTickersInPortfolio, 0.0, ! inSamplePortfolioType, ! benchmark) { ! this.setReturnsManager(); } ! private void setReturnsManager() ! { ! EndOfDayDateTime firstEndOfDayDateTime = ! new EndOfDayDateTime(firstQuoteDate, EndOfDaySpecificTime.MarketClose); ! EndOfDayDateTime lastEndOfDayDateTime = ! new EndOfDayDateTime(lastQuoteDate, EndOfDaySpecificTime.MarketClose); ! this.returnsManager = ! new ReturnsManager( new CloseToCloseIntervals( ! firstEndOfDayDateTime, ! lastEndOfDayDateTime, ! this.benchmark), ! new HistoricalAdjustedQuoteProvider() ); ! } ! ! private float[] getStrategyReturns_getReturnsActually( ! float[] plainReturns) ! { ! float[] returnValue = new float[plainReturns.Length]; ! returnValue[0] = 0; //at the very first day the ! //first strategy return is equal to 0 because no position ! //has been entered ! float coefficient = 0; ! for(int i = 0; i < returnValue.Length - 1; i++) { ! if( plainReturns[i] >= 0 ) ! //portfolio is overbought ! coefficient = -1; ! else if( plainReturns[i] <= 0 ) ! //portfolio is oversold ! coefficient = 1; ! //else ! // coefficient = coefficient; the previous coeff is kept ! returnValue[i + 1] = coefficient * plainReturns[i + 1]; } return returnValue; ! } ! protected override float[] getStrategyReturns() ! { ! EndOfDayDateTime firstEndOfDayDateTime = ! new EndOfDayDateTime(firstQuoteDate, EndOfDaySpecificTime.MarketClose); ! EndOfDayDateTime lastEndOfDayDateTime = ! new EndOfDayDateTime(lastQuoteDate, EndOfDaySpecificTime.MarketClose); ! float[] plainReturns = this.weightedPositionsFromGenome.GetReturns( ! this.returnsManager); ! return this.getStrategyReturns_getReturnsActually(plainReturns); ! } } + + // public override 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] = this.decode_getTickerCodeForLongOrShortTrade(indexOfTicker); + // } + // GenomeMeaningPVO meaning = new GenomeMeaningPVO(arrayOfTickers, + // 0.0, 0.0, 2); + // return meaning; + // } + } Index: EndOfDayTimerHandlerBiasedPVONoThresholds.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/BiasedPVO/BiasedPVONoThresholds/EndOfDayTimerHandlerBiasedPVONoThresholds.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** EndOfDayTimerHandlerBiasedPVONoThresholds.cs 29 Aug 2007 09:48:18 -0000 1.2 --- EndOfDayTimerHandlerBiasedPVONoThresholds.cs 14 Jan 2008 23:07:04 -0000 1.3 *************** *** 30,33 **** --- 30,36 ---- using QuantProject.Business.Timing; using QuantProject.Business.Strategies; + using QuantProject.Business.Strategies.ReturnsManagement; + using QuantProject.Business.Strategies.ReturnsManagement.Time; + using QuantProject.Business.DataProviders; using QuantProject.Data; using QuantProject.Data.DataProviders; *************** *** 78,94 **** #region MarketCloseEventHandler ! protected override double getCurrentWeightedPositionsGainOrLoss(IndexBasedEndOfDayTimer timer, ! int indexForChosenWeightedPositions) { double returnValue = 999.0; try { DateTime today = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! DateTime lastMarketDay = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - 1]["quDate"]; ! returnValue = ! this.weightedPositionsToEvaluateOutOfSample[indexForChosenWeightedPositions].GetCloseToCloseReturn( ! lastMarketDay, today); } catch(MissingQuotesException ex) --- 81,104 ---- #region MarketCloseEventHandler ! protected override double getCurrentWeightedPositionsGainOrLoss( ! IndexBasedEndOfDayTimer timer, ! ReturnsManager returnsManager, ! int indexForChosenWeightedPositions) { double returnValue = 999.0; try { + DateTime firstDayOfOscillatingPeriod = + (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForOscillatingPeriod]["quDate"]; DateTime today = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! ReturnsManager returnsManager = new ReturnsManager(new CloseToCloseIntervals( ! new EndOfDayDateTime(firstDayOfOscillatingPeriod, ! EndOfDaySpecificTime.MarketClose) , ! new EndOfDayDateTime(finalDateForHalfPeriod, ! EndOfDaySpecificTime.MarketClose) , ! this.benchmark , this.numDaysForReturnCalculation ) , ! new HistoricalAdjustedQuoteProvider() ); ! returnValue = this.chosenWeightedPositions.GetReturn(0,returnsManager); } catch(MissingQuotesException ex) *************** *** 106,110 **** /this.previousAccountValue; ! if( portfolioGainOrLoss < -this.maxAcceptableCloseToCloseDrawdown ) { this.stopLossConditionReached = true; --- 116,120 ---- /this.previousAccountValue; ! if( portfolioGainOrLoss <= -this.maxAcceptableCloseToCloseDrawdown ) { this.stopLossConditionReached = true; *************** *** 130,135 **** 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 --- 140,144 ---- this.takeProfitConditionReached || this.numDaysElapsedSinceLastOptimization + 1 == this.numDaysBetweenEachOptimization ) ! //stop loss or take profit conditions have been reached //or after the close it is necessary to run //another optimization *************** *** 146,150 **** this.marketCloseEventHandler_closeIfItIsTimeToClose(); else if ( this.account.Portfolio.Count == 0 && ! this.weightedPositionsToEvaluateOutOfSample != null ) //portfolio is empty and optimization has been already launched this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); --- 155,159 ---- this.marketCloseEventHandler_closeIfItIsTimeToClose(); else if ( this.account.Portfolio.Count == 0 && ! this.weightedPositionsToEvaluateOutOfSample[0] != null ) //portfolio is empty and optimization has been already launched this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); *************** *** 165,169 **** currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, this.numberOfTickersToBeChosen, ! this.portfolioType); GeneticOptimizer GO = new GeneticOptimizer(this.iGenomeManager, this.populationSizeForGeneticOptimizer, --- 174,178 ---- currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, this.numberOfTickersToBeChosen, ! this.portfolioType,this.benchmark); GeneticOptimizer GO = new GeneticOptimizer(this.iGenomeManager, this.populationSizeForGeneticOptimizer, |