[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination ExtremeCoun
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-08-19 17:11:35
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv2916/b7_Scripts/WalkForwardTesting/LinearCombination Modified Files: ExtremeCounterTrendStrategy.cs FixedLevelOscillatorBiasedPVOStrategy.cs GenomeRepresentation.cs ImmediateTrendFollowerStrategy.cs Log Message: - the code has been cleaned up to avoid warnings - standard indentation has been applied Index: FixedLevelOscillatorBiasedPVOStrategy.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/FixedLevelOscillatorBiasedPVOStrategy.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** FixedLevelOscillatorBiasedPVOStrategy.cs 12 Mar 2008 22:09:04 -0000 1.5 --- FixedLevelOscillatorBiasedPVOStrategy.cs 19 Aug 2008 17:11:29 -0000 1.6 *************** *** 3,7 **** FixedLevelOscillatorBiasedPVOStrategy.cs ! Copyright (C) 2007 Marco Milletti --- 3,7 ---- FixedLevelOscillatorBiasedPVOStrategy.cs ! Copyright (C) 2007 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; *************** *** 36,114 **** { /// <summary> ! /// Fixed Level Oscillators /// Biased Portfolio Value Oscillator strategy: /// from a group of optimized PVO portfolios, the strategy indicates /// to buy the portfolio that presents the highest degree ! /// of moving from a threshold /// </summary> [Serializable] public class FixedLevelOscillatorBiasedPVOStrategy : EndOfDayTimerHandlerBiasedPVO, IEndOfDayStrategy { ! public FixedLevelOscillatorBiasedPVOStrategy( Account accountPVO , ! WeightedPositions[] weightedPositionsToEvaluateOutOfSample, ! double[] oversoldThreshold, ! double[] overboughtThreshold, ! int numOfDifferentGenomesToEvaluateOutOfSample, ! int numDaysForOscillatingPeriod, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain): ! base("", 0, 0, 0, ! weightedPositionsToEvaluateOutOfSample[0].Count, 0, accountPVO, null, 0, 0, ! "^GSPC", numOfDifferentGenomesToEvaluateOutOfSample, ! false,0, ! 1.0, ! 1000.0, ! false, ! false, ! numDaysForOscillatingPeriod,numDaysForOscillatingPeriod,1,1,1,1,1,false,false,0, ! PortfolioType.ShortAndLong,maxAcceptableCloseToCloseDrawdown, ! minimumAcceptableGain) ! { this.weightedPositionsToEvaluateOutOfSample = weightedPositionsToEvaluateOutOfSample; ! this.currentOversoldThreshold = oversoldThreshold; ! this.currentOverboughtThreshold = overboughtThreshold; ! this.numDaysForOscillatingPeriod = numDaysForOscillatingPeriod; ! this.numOfDifferentGenomesToEvaluateOutOfSample = numOfDifferentGenomesToEvaluateOutOfSample; } ! ! public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! } ! ! protected override double getCurrentWeightedPositionsGainOrLoss( IndexBasedEndOfDayTimer timer, ! ReturnsManager returnsManager, ! int indexForChosenWeightedPositions) ! { ! double returnValue = 999.0; ! if(timer.CurrentDateArrayPosition - this.numDaysForOscillatingPeriod >= 0) ! //if there are sufficient data for computing currentChosenTickersValue ! //that's why the method has been overriden ! { try { ! DateTime today = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! DateTime lastMarketDay = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - 1]["quDate"]; returnValue = this.weightedPositionsToEvaluateOutOfSample[indexForChosenWeightedPositions].GetReturn( ! 0, returnsManager); } catch(MissingQuotesException ex) { ! ex = ex; } ! } return returnValue; ! } ! public override void OneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! } } --- 36,114 ---- { /// <summary> ! /// Fixed Level Oscillators /// Biased Portfolio Value Oscillator strategy: /// from a group of optimized PVO portfolios, the strategy indicates /// to buy the portfolio that presents the highest degree ! /// of moving from a threshold /// </summary> [Serializable] public class FixedLevelOscillatorBiasedPVOStrategy : EndOfDayTimerHandlerBiasedPVO, IEndOfDayStrategy { ! public FixedLevelOscillatorBiasedPVOStrategy( Account accountPVO , ! WeightedPositions[] weightedPositionsToEvaluateOutOfSample, ! double[] oversoldThreshold, ! double[] overboughtThreshold, ! int numOfDifferentGenomesToEvaluateOutOfSample, ! int numDaysForOscillatingPeriod, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain): ! base("", 0, 0, 0, ! weightedPositionsToEvaluateOutOfSample[0].Count, 0, accountPVO, null, 0, 0, ! "^GSPC", numOfDifferentGenomesToEvaluateOutOfSample, ! false,0, ! 1.0, ! 1000.0, ! false, ! false, ! numDaysForOscillatingPeriod,numDaysForOscillatingPeriod,1,1,1,1,1,false,false,0, ! PortfolioType.ShortAndLong,maxAcceptableCloseToCloseDrawdown, ! minimumAcceptableGain) ! { this.weightedPositionsToEvaluateOutOfSample = weightedPositionsToEvaluateOutOfSample; ! this.currentOversoldThreshold = oversoldThreshold; ! this.currentOverboughtThreshold = overboughtThreshold; ! this.numDaysForOscillatingPeriod = numDaysForOscillatingPeriod; ! this.numOfDifferentGenomesToEvaluateOutOfSample = numOfDifferentGenomesToEvaluateOutOfSample; } ! ! public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! } ! ! protected override double getCurrentWeightedPositionsGainOrLoss( IndexBasedEndOfDayTimer timer, ! ReturnsManager returnsManager, ! int indexForChosenWeightedPositions) ! { ! double returnValue = 999.0; ! if(timer.CurrentDateArrayPosition - this.numDaysForOscillatingPeriod >= 0) ! //if there are sufficient data for computing currentChosenTickersValue ! //that's why the method has been overriden ! { try { ! DateTime today = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! DateTime lastMarketDay = (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - 1]["quDate"]; returnValue = this.weightedPositionsToEvaluateOutOfSample[indexForChosenWeightedPositions].GetReturn( ! 0, returnsManager); } catch(MissingQuotesException ex) { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; } ! } return returnValue; ! } ! public override void OneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! } } Index: ImmediateTrendFollowerStrategy.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/ImmediateTrendFollowerStrategy.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ImmediateTrendFollowerStrategy.cs 29 Aug 2007 09:43:36 -0000 1.2 --- ImmediateTrendFollowerStrategy.cs 19 Aug 2008 17:11:29 -0000 1.3 *************** *** 3,7 **** ImmediateTrendFollowerStrategy.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- ImmediateTrendFollowerStrategy.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; *************** *** 36,40 **** /// <summary> /// Immediate Trend Follower Strategy (it's just the ! /// reversal of the ExtremeCounterTrend strategy /// </summary> [Serializable] --- 36,40 ---- /// <summary> /// Immediate Trend Follower Strategy (it's just the ! /// reversal of the ExtremeCounterTrend strategy /// </summary> [Serializable] *************** *** 42,135 **** { private int numDaysForReturnCalculation; ! private int numOfClosesElapsed = 0; ! private int numOfDaysWithOpenPosition = 0; public ImmediateTrendFollowerStrategy( Account account , ! WeightedPositions weightedPositions, ! int numDaysForReturnCalculation) : ! base("", 0, ! weightedPositions.Count, 0, account, ! 0, ! 0, ! "^GSPC", 0.0, ! PortfolioType.ShortAndLong) { this.account = account; this.chosenWeightedPositions = weightedPositions; ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; } - - public override void MarketOpenEventHandler( - Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) - { - } ! public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! } ! private double marketCloseEventHandler_openPositions_getLastHalfPeriodGain(IndexBasedEndOfDayTimer timer) ! { ! double returnValue = 999.0; ! try ! { ! DateTime initialDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForReturnCalculation + 1]["quDate"]; ! DateTime finalDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! returnValue = ! this.chosenWeightedPositions.GetCloseToCloseReturn(initialDateForHalfPeriod,finalDateForHalfPeriod); ! } ! catch(MissingQuotesException ex) ! { ! ex = ex; ! } ! return returnValue; ! } ! private void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! double lastHalfPeriodGain = ! this.marketCloseEventHandler_openPositions_getLastHalfPeriodGain(timer); ! if(lastHalfPeriodGain != 999.0) ! //last half period gain has been properly computed ! { ! if(lastHalfPeriodGain > 0.0) ! //the portfolio had a gain for the last half period ! this.openPositions(); ! else//the portfolio had a loss for the last half period ! { ! this.chosenWeightedPositions.Reverse(); ! //short the portfolio ! try{this.openPositions();} ! catch(Exception ex){ex = ex;} ! finally{this.chosenWeightedPositions.Reverse();} ! } ! } ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! if(this.account.Portfolio.Count > 0) ! this.numOfDaysWithOpenPosition++; ! ! if(this.numOfDaysWithOpenPosition == this.numDaysForReturnCalculation) ! AccountManager.ClosePositions(this.account); ! ! if(this.account.Portfolio.Count == 0 && ! (this.numOfClosesElapsed + 1) >= this.numDaysForReturnCalculation) ! //portfolio is empty and previous closes can be now checked ! if(this.account.Portfolio.Count == 0) ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! ! this.numOfClosesElapsed++; ! } ! public override void OneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! } } --- 42,138 ---- { private int numDaysForReturnCalculation; ! private int numOfClosesElapsed = 0; ! private int numOfDaysWithOpenPosition = 0; public ImmediateTrendFollowerStrategy( Account account , ! WeightedPositions weightedPositions, ! int numDaysForReturnCalculation) : ! base("", 0, ! weightedPositions.Count, 0, account, ! 0, ! 0, ! "^GSPC", 0.0, ! PortfolioType.ShortAndLong) { this.account = account; this.chosenWeightedPositions = weightedPositions; ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; } ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! } ! ! public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! } ! private double marketCloseEventHandler_openPositions_getLastHalfPeriodGain(IndexBasedEndOfDayTimer timer) ! { ! double returnValue = 999.0; ! try ! { ! DateTime initialDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForReturnCalculation + 1]["quDate"]; ! DateTime finalDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! returnValue = ! this.chosenWeightedPositions.GetCloseToCloseReturn(initialDateForHalfPeriod,finalDateForHalfPeriod); ! } ! catch(MissingQuotesException ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! return returnValue; ! } ! private void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! double lastHalfPeriodGain = ! this.marketCloseEventHandler_openPositions_getLastHalfPeriodGain(timer); ! if(lastHalfPeriodGain != 999.0) ! //last half period gain has been properly computed ! { ! if(lastHalfPeriodGain > 0.0) ! //the portfolio had a gain for the last half period ! this.openPositions(); ! else//the portfolio had a loss for the last half period ! { ! this.chosenWeightedPositions.Reverse(); ! //short the portfolio ! try{this.openPositions();} ! catch(Exception ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! finally{this.chosenWeightedPositions.Reverse();} ! } ! } ! } ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! if(this.account.Portfolio.Count > 0) ! this.numOfDaysWithOpenPosition++; ! ! if(this.numOfDaysWithOpenPosition == this.numDaysForReturnCalculation) ! AccountManager.ClosePositions(this.account); ! ! if(this.account.Portfolio.Count == 0 && ! (this.numOfClosesElapsed + 1) >= this.numDaysForReturnCalculation) ! //portfolio is empty and previous closes can be now checked ! if(this.account.Portfolio.Count == 0) ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! ! this.numOfClosesElapsed++; ! } ! ! public override void OneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! } } Index: GenomeRepresentation.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/GenomeRepresentation.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** GenomeRepresentation.cs 17 Sep 2006 21:24:53 -0000 1.8 --- GenomeRepresentation.cs 19 Aug 2008 17:11:29 -0000 1.9 *************** *** 3,7 **** GenomeRepresentation.cs ! Copyright (C) 2003 Glauco Siliprandi --- 3,7 ---- GenomeRepresentation.cs ! Copyright (C) 2003 Glauco Siliprandi *************** *** 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; *************** *** 41,54 **** private double fitness; private string signedTickers; ! private string weights; private int generationCounter; private DateTime firstOptimizationDate; private DateTime lastOptimizationDate; private int eligibleTickers = -1; ! private int halfPeriodDays = -1; ! private PortfolioType portfolioType = PortfolioType.ShortAndLong; private int createdGenerations = -1; ! private double oversoldThreshold = 0.0; ! private double overboughtThreshold = 0.0; public string SignedTickers --- 41,54 ---- private double fitness; private string signedTickers; ! private string weights; private int generationCounter; private DateTime firstOptimizationDate; private DateTime lastOptimizationDate; private int eligibleTickers = -1; ! private int halfPeriodDays = -1; ! private PortfolioType portfolioType = PortfolioType.ShortAndLong; private int createdGenerations = -1; ! private double oversoldThreshold = 0.0; ! private double overboughtThreshold = 0.0; public string SignedTickers *************** *** 59,63 **** } } ! public int EligibleTickers { --- 59,63 ---- } } ! public int EligibleTickers { *************** *** 68,78 **** } ! public string WeightsForSignedTickers ! { ! get ! { ! return this.weights; ! } ! } public double Fitness { --- 68,78 ---- } ! public string WeightsForSignedTickers ! { ! get ! { ! return this.weights; ! } ! } public double Fitness { *************** *** 104,138 **** } ! public int HalfPeriodDays ! { ! get ! { ! return this.halfPeriodDays; ! } ! } ! public PortfolioType PortfolioType ! { ! get ! { ! return this.portfolioType; ! } ! } ! public double OversoldThreshold ! { ! get ! { ! return this.oversoldThreshold; ! } ! } ! ! public double OverboughtThreshold ! { ! get ! { ! return this.overboughtThreshold; ! } ! } // public static string[] GetSignedTickers( string signedTickers ) --- 104,138 ---- } ! public int HalfPeriodDays ! { ! get ! { ! return this.halfPeriodDays; ! } ! } ! public PortfolioType PortfolioType ! { ! get ! { ! return this.portfolioType; ! } ! } ! public double OversoldThreshold ! { ! get ! { ! return this.oversoldThreshold; ! } ! } ! ! public double OverboughtThreshold ! { ! get ! { ! return this.overboughtThreshold; ! } ! } // public static string[] GetSignedTickers( string signedTickers ) *************** *** 142,178 **** // } ! public static double[] GetWeightsArray( string weightsWithSeparator ) ! { ! double[] returnValue; ! string[] returnValueString = weightsWithSeparator.Split( ";".ToCharArray()); ! returnValue = new double[returnValueString.Length]; ! for(int i = 0; i<returnValue.Length; i++) ! { ! returnValue[i] = Convert.ToDouble(returnValueString[i]); ! } ! return returnValue; ! } public static string[] GetSignedTickers( string signedTickersWithOrWithoutWeights ) { ! string[] returnValue = ! signedTickersWithOrWithoutWeights.Split( ConstantsProvider.SeparatorForTickers.ToCharArray()); if( signedTickersWithOrWithoutWeights.Split( ConstantsProvider.SeparatorForWeights.ToCharArray() ).Length > 1 ) ! //the separator char for tickers is contained in signedTickersWithWeights: ! //so weights have been saved within tickers, separated by this special char { for(int i = 0; i<returnValue.Length; i++) { ! returnValue[i] = ! returnValue[i].Split( ConstantsProvider.SeparatorForWeights.ToCharArray() )[0]; } ! } return returnValue; } ! public static double[] GetWeightsForSignedTickers( string signedTickersWithOrWithoutWeights ) { ! string[] signedTickersWithOrWithoutWeightsArray = ! signedTickersWithOrWithoutWeights.Split( ConstantsProvider.SeparatorForTickers.ToCharArray() ); double[] returnValue = new double[signedTickersWithOrWithoutWeights.Length]; --- 142,178 ---- // } ! public static double[] GetWeightsArray( string weightsWithSeparator ) ! { ! double[] returnValue; ! string[] returnValueString = weightsWithSeparator.Split( ";".ToCharArray()); ! returnValue = new double[returnValueString.Length]; ! for(int i = 0; i<returnValue.Length; i++) ! { ! returnValue[i] = Convert.ToDouble(returnValueString[i]); ! } ! return returnValue; ! } public static string[] GetSignedTickers( string signedTickersWithOrWithoutWeights ) { ! string[] returnValue = ! signedTickersWithOrWithoutWeights.Split( ConstantsProvider.SeparatorForTickers.ToCharArray()); if( signedTickersWithOrWithoutWeights.Split( ConstantsProvider.SeparatorForWeights.ToCharArray() ).Length > 1 ) ! //the separator char for tickers is contained in signedTickersWithWeights: ! //so weights have been saved within tickers, separated by this special char { for(int i = 0; i<returnValue.Length; i++) { ! returnValue[i] = ! returnValue[i].Split( ConstantsProvider.SeparatorForWeights.ToCharArray() )[0]; } ! } return returnValue; } ! public static double[] GetWeightsForSignedTickers( string signedTickersWithOrWithoutWeights ) { ! string[] signedTickersWithOrWithoutWeightsArray = ! signedTickersWithOrWithoutWeights.Split( ConstantsProvider.SeparatorForTickers.ToCharArray() ); double[] returnValue = new double[signedTickersWithOrWithoutWeights.Length]; *************** *** 181,193 **** if((signedTickersWithOrWithoutWeights.Split(ConstantsProvider.SeparatorForWeights.ToCharArray())).Length > 1) ! //the separator for weights is contained in signedTickersWithWeights: ! //so weights have been saved within tickers, separated by this char { for(int i = 0; i<returnValue.Length; i++) { ! returnValue[i] = Convert.ToDouble( signedTickersWithOrWithoutWeightsArray[i].Split( ConstantsProvider.SeparatorForWeights.ToCharArray() )[1] ); } ! } return returnValue; } --- 181,193 ---- if((signedTickersWithOrWithoutWeights.Split(ConstantsProvider.SeparatorForWeights.ToCharArray())).Length > 1) ! //the separator for weights is contained in signedTickersWithWeights: ! //so weights have been saved within tickers, separated by this char { for(int i = 0; i<returnValue.Length; i++) { ! returnValue[i] = Convert.ToDouble( signedTickersWithOrWithoutWeightsArray[i].Split( ConstantsProvider.SeparatorForWeights.ToCharArray() )[1] ); } ! } return returnValue; } *************** *** 218,235 **** signedTickers += signedTicker + ";"; signedTickers = signedTickers.Substring( 0 , ! signedTickers.Length - 1 ); return signedTickers; } - - private string getWeights( Genome genome ) - { - string weights = ""; - foreach ( double weight in ((GenomeMeaning)genome.Meaning).TickersPortfolioWeights ) - weights += weight.ToString() + ";"; - weights = weights.Substring( 0, weights.Length - 1 ); - return weights; - } ! // private string getSignedTickersWithWeights( Genome genome ) // { // string signedTickersWithWeights = ""; --- 218,235 ---- signedTickers += signedTicker + ";"; signedTickers = signedTickers.Substring( 0 , ! signedTickers.Length - 1 ); return signedTickers; } ! private string getWeights( Genome genome ) ! { ! string weights = ""; ! foreach ( double weight in ((GenomeMeaning)genome.Meaning).TickersPortfolioWeights ) ! weights += weight.ToString() + ";"; ! weights = weights.Substring( 0, weights.Length - 1 ); ! return weights; ! } ! ! // private string getSignedTickersWithWeights( Genome genome ) // { // string signedTickersWithWeights = ""; *************** *** 237,241 **** // { // signedTickersWithWeights += ! // ((GenomeMeaning)genome.Meaning).Tickers[i] + // ConstantsProvider.SeparatorForWeights + // ((GenomeMeaning)genome.Meaning).TickersPortfolioWeights[i] + --- 237,241 ---- // { // signedTickersWithWeights += ! // ((GenomeMeaning)genome.Meaning).Tickers[i] + // ConstantsProvider.SeparatorForWeights + // ((GenomeMeaning)genome.Meaning).TickersPortfolioWeights[i] + *************** *** 244,266 **** // signedTickersWithWeights = signedTickersWithWeights.Substring( 0 , // signedTickersWithWeights.Length - 1 ); ! // // return signedTickersWithWeights; // } ! private void genomeRepresentation_synchronizeOldWithNew() ! { ! if(this.weights == null) ! //for old genomes saved to disk not having "weights" field ! { ! foreach(double weight in GenomeRepresentation.GetWeightsForSignedTickers(this.signedTickers)) ! this.weights += weight.ToString() + ";"; ! this.weights = this.weights.Substring( 0, this.weights.Length - 1 ); ! // ! string newRepresentationForSignedTickers = null; ! foreach(string ticker in GenomeRepresentation.GetSignedTickers(this.signedTickers)) ! newRepresentationForSignedTickers += ticker + ";"; ! this.signedTickers = newRepresentationForSignedTickers.Substring(0, newRepresentationForSignedTickers.Length -1); ! } ! } /// <summary> --- 244,266 ---- // signedTickersWithWeights = signedTickersWithWeights.Substring( 0 , // signedTickersWithWeights.Length - 1 ); ! // // return signedTickersWithWeights; // } ! private void genomeRepresentation_synchronizeOldWithNew() ! { ! if(this.weights == null) ! //for old genomes saved to disk not having "weights" field ! { ! foreach(double weight in GenomeRepresentation.GetWeightsForSignedTickers(this.signedTickers)) ! this.weights += weight.ToString() + ";"; ! this.weights = this.weights.Substring( 0, this.weights.Length - 1 ); ! // ! string newRepresentationForSignedTickers = null; ! foreach(string ticker in GenomeRepresentation.GetSignedTickers(this.signedTickers)) ! newRepresentationForSignedTickers += ticker + ";"; ! this.signedTickers = newRepresentationForSignedTickers.Substring(0, newRepresentationForSignedTickers.Length -1); ! } ! } /// <summary> *************** *** 278,282 **** // deserialize the fields from the info object ! for (Int32 i = 0 ; i < mi.Length; i++) { FieldInfo fieldInfo = (FieldInfo) mi[i]; --- 278,282 ---- // deserialize the fields from the info object ! for (Int32 i = 0 ; i < mi.Length; i++) { FieldInfo fieldInfo = (FieldInfo) mi[i]; *************** *** 284,301 **** // set the field to the deserialized value try{ ! fieldInfo.SetValue( this , ! info.GetValue( fieldInfo.Name, fieldInfo.FieldType ) ); } catch(Exception ex) ! {ex = ex;} } ! this.genomeRepresentation_synchronizeOldWithNew(); } private void genomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, ! int halfPeriodDays, PortfolioType portfolioType, ! int createdGenerations) { this.fitness = genome.Fitness; --- 284,303 ---- // set the field to the deserialized value try{ ! fieldInfo.SetValue( this , ! info.GetValue( fieldInfo.Name, fieldInfo.FieldType ) ); } catch(Exception ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } } ! this.genomeRepresentation_synchronizeOldWithNew(); } private void genomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, ! int halfPeriodDays, PortfolioType portfolioType, ! int createdGenerations) { this.fitness = genome.Fitness; *************** *** 303,328 **** this.signedTickers = this.getSignedTickers( ((GenomeMeaning)genome.Meaning).Tickers ); this.weights = this.getWeights( genome ); ! this.firstOptimizationDate = firstOptimizationDate; this.lastOptimizationDate = lastOptimizationDate; this.generationCounter = generationCounter; this.eligibleTickers = eligibleTickers; ! this.halfPeriodDays = halfPeriodDays; ! this.portfolioType = portfolioType; ! this.createdGenerations = createdGenerations; } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate ) { this.genomeRepresentation( genome , ! firstOptimizationDate , lastOptimizationDate , -1, -1, -1, PortfolioType.ShortAndLong, -1 ); } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter ) { this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, -1, -1, PortfolioType.ShortAndLong, -1); } --- 305,330 ---- this.signedTickers = this.getSignedTickers( ((GenomeMeaning)genome.Meaning).Tickers ); this.weights = this.getWeights( genome ); ! this.firstOptimizationDate = firstOptimizationDate; this.lastOptimizationDate = lastOptimizationDate; this.generationCounter = generationCounter; this.eligibleTickers = eligibleTickers; ! this.halfPeriodDays = halfPeriodDays; ! this.portfolioType = portfolioType; ! this.createdGenerations = createdGenerations; } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate ) { this.genomeRepresentation( genome , ! firstOptimizationDate , lastOptimizationDate , -1, -1, -1, PortfolioType.ShortAndLong, -1 ); } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter ) { this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, -1, -1, PortfolioType.ShortAndLong, -1); } *************** *** 330,334 **** string[] signedTickersArray, DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int eligibleTickers ) { this.fitness = BFOptimizableParamaters.Fitness; --- 332,336 ---- string[] signedTickersArray, DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int eligibleTickers ) { this.fitness = BFOptimizableParamaters.Fitness; *************** *** 336,394 **** this.signedTickers = this.getSignedTickers(signedTickersArray); this.weights = null; ! this.firstOptimizationDate = firstOptimizationDate; this.lastOptimizationDate = lastOptimizationDate; this.eligibleTickers = eligibleTickers; ! } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers ) { this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, -1, PortfolioType.ShortAndLong, -1); } - - public GenomeRepresentation( Genome genome , - DateTime firstOptimizationDate , DateTime lastOptimizationDate , - int generationCounter, int eligibleTickers, int halfPeriodDays ) - { - this.genomeRepresentation( genome , firstOptimizationDate , - lastOptimizationDate , generationCounter, eligibleTickers, halfPeriodDays, - PortfolioType.ShortAndLong, -1); - } - - public GenomeRepresentation( Genome genome , - DateTime firstOptimizationDate , DateTime lastOptimizationDate , - int generationCounter, int eligibleTickers, - int halfPeriodDays, PortfolioType portfolioType ) - { - this.genomeRepresentation( genome , firstOptimizationDate , - lastOptimizationDate , generationCounter, eligibleTickers, - halfPeriodDays, portfolioType, -1); - } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, ! int halfPeriodDays, PortfolioType portfolioType, int createdGenerations ) ! { ! this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, ! halfPeriodDays, portfolioType, createdGenerations); ! } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, ! int halfPeriodDays, PortfolioType portfolioType, int createdGenerations, ! double oversoldThreshold, double overboughtThreshold) ! { ! this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, ! halfPeriodDays, portfolioType, createdGenerations); ! this.oversoldThreshold = oversoldThreshold; ! this.overboughtThreshold = overboughtThreshold; ! } #region GetObjectData --- 338,396 ---- this.signedTickers = this.getSignedTickers(signedTickersArray); this.weights = null; ! this.firstOptimizationDate = firstOptimizationDate; this.lastOptimizationDate = lastOptimizationDate; this.eligibleTickers = eligibleTickers; ! } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers ) { this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, -1, PortfolioType.ShortAndLong, -1); } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, int halfPeriodDays ) ! { ! this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, halfPeriodDays, ! PortfolioType.ShortAndLong, -1); ! } ! ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, ! int halfPeriodDays, PortfolioType portfolioType ) ! { ! this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, ! halfPeriodDays, portfolioType, -1); ! } ! ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, ! int halfPeriodDays, PortfolioType portfolioType, int createdGenerations ) ! { ! this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, ! halfPeriodDays, portfolioType, createdGenerations); ! } ! public GenomeRepresentation( Genome genome , ! DateTime firstOptimizationDate , DateTime lastOptimizationDate , ! int generationCounter, int eligibleTickers, ! int halfPeriodDays, PortfolioType portfolioType, int createdGenerations, ! double oversoldThreshold, double overboughtThreshold) ! { ! this.genomeRepresentation( genome , firstOptimizationDate , ! lastOptimizationDate , generationCounter, eligibleTickers, ! halfPeriodDays, portfolioType, createdGenerations); ! this.oversoldThreshold = oversoldThreshold; ! this.overboughtThreshold = overboughtThreshold; ! } #region GetObjectData *************** *** 399,411 **** /// <param name="context"></param> void ISerializable.GetObjectData( ! SerializationInfo info, StreamingContext context) { // get the set of serializable members for this class and base classes Type thisType = this.GetType(); ! MemberInfo[] mi = FormatterServices.GetSerializableMembers( thisType , context); // serialize the fields to the info object ! for (Int32 i = 0 ; i < mi.Length; i++) { info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this)); --- 401,413 ---- /// <param name="context"></param> void ISerializable.GetObjectData( ! SerializationInfo info, StreamingContext context) { // get the set of serializable members for this class and base classes Type thisType = this.GetType(); ! MemberInfo[] mi = FormatterServices.GetSerializableMembers( thisType , context); // serialize the fields to the info object ! for (Int32 i = 0 ; i < mi.Length; i++) { info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this)); Index: ExtremeCounterTrendStrategy.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/ExtremeCounterTrendStrategy.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ExtremeCounterTrendStrategy.cs 12 Mar 2008 22:09:04 -0000 1.5 --- ExtremeCounterTrendStrategy.cs 19 Aug 2008 17:11:28 -0000 1.6 *************** *** 3,7 **** ExtremeCounterTrendStrategy.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- ExtremeCounterTrendStrategy.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; *************** *** 44,154 **** private WeightedPositions weightedPositions; private int numDaysForReturnCalculation; ! private int numOfClosesElapsed = 0; ! private int numOfDaysWithOpenPosition = 0; ! private PortfolioType portfolioType; ! public Account Account { get { return this.account; } set { this.account = value; } } ! public ExtremeCounterTrendStrategy( Account account , ! WeightedPositions weightedPositions, ! int numDaysForReturnCalculation, ! PortfolioType portfolioType) { this.account = account; this.weightedPositions = weightedPositions; ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.portfolioType = portfolioType; } - - public void MarketOpenEventHandler( - Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) - { - } ! public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! } ! private double marketCloseEventHandler_openPositions_getLastHalfPeriodGain(IndexBasedEndOfDayTimer timer) ! { ! double returnValue = 999.0; ! try ! { ! DateTime initialDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForReturnCalculation + 1]["quDate"]; ! DateTime finalDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! this.weightedPositions.GetCloseToCloseReturn(initialDateForHalfPeriod,finalDateForHalfPeriod); ! } ! catch(MissingQuotesException ex) ! { ! ex = ex; ! } ! return returnValue; ! } ! private void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! double lastHalfPeriodGain = ! this.marketCloseEventHandler_openPositions_getLastHalfPeriodGain(timer); ! if(lastHalfPeriodGain != 999.0) ! //last half period gain has been properly computed ! { ! if(lastHalfPeriodGain < 0.0) ! AccountManager.OpenPositions(this.weightedPositions,this.account); ! else if (lastHalfPeriodGain > 0.0) ! //if gain of the last half period is positive ! { ! this.weightedPositions.Reverse(); ! //short the portfolio (short --> long; long --> short) ! try{ ! AccountManager.OpenPositions(this.weightedPositions,this.account); ! } ! catch(Exception ex) ! { ! ex = ex; ! } ! finally{ ! this.weightedPositions.Reverse(); ! } ! } ! } ! } ! ! private void marketCloseEventHandler_closePositions() ! { ! AccountManager.ClosePositions(this.account); ! this.numOfDaysWithOpenPosition = 0; ! } ! ! public void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! this.numOfClosesElapsed++; ! if(this.account.Transactions.Count == 0) ! this.account.AddCash(15000); ! ! if(this.account.Portfolio.Count > 0) ! this.numOfDaysWithOpenPosition++; ! ! if(this.numOfDaysWithOpenPosition == this.numDaysForReturnCalculation) ! this.marketCloseEventHandler_closePositions(); ! ! if(this.account.Portfolio.Count == 0 && ! this.numOfClosesElapsed >= this.numDaysForReturnCalculation) ! //portfolio is empty and previous closes can be now checked ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! } ! public void OneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! } } } --- 44,154 ---- private WeightedPositions weightedPositions; private int numDaysForReturnCalculation; ! private int numOfClosesElapsed = 0; ! private int numOfDaysWithOpenPosition = 0; ! private PortfolioType portfolioType; ! public Account Account { get { return this.account; } set { this.account = value; } } ! public ExtremeCounterTrendStrategy( Account account , ! WeightedPositions weightedPositions, ! int numDaysForReturnCalculation, ! PortfolioType portfolioType) { this.account = account; this.weightedPositions = weightedPositions; ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.portfolioType = portfolioType; } ! public void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! } ! ! public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! } ! private double marketCloseEventHandler_openPositions_getLastHalfPeriodGain(IndexBasedEndOfDayTimer timer) ! { ! double returnValue = 999.0; ! try ! { ! DateTime initialDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForReturnCalculation + 1]["quDate"]; ! DateTime finalDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! this.weightedPositions.GetCloseToCloseReturn(initialDateForHalfPeriod,finalDateForHalfPeriod); ! } ! catch(MissingQuotesException ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! return returnValue; ! } ! private void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! double lastHalfPeriodGain = ! this.marketCloseEventHandler_openPositions_getLastHalfPeriodGain(timer); ! if(lastHalfPeriodGain != 999.0) ! //last half period gain has been properly computed ! { ! if(lastHalfPeriodGain < 0.0) ! AccountManager.OpenPositions(this.weightedPositions,this.account); ! else if (lastHalfPeriodGain > 0.0) ! //if gain of the last half period is positive ! { ! this.weightedPositions.Reverse(); ! //short the portfolio (short --> long; long --> short) ! try{ ! AccountManager.OpenPositions(this.weightedPositions,this.account); ! } ! catch(Exception ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! finally{ ! this.weightedPositions.Reverse(); ! } ! } ! } ! } ! ! private void marketCloseEventHandler_closePositions() ! { ! AccountManager.ClosePositions(this.account); ! this.numOfDaysWithOpenPosition = 0; ! } ! ! public void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! this.numOfClosesElapsed++; ! if(this.account.Transactions.Count == 0) ! this.account.AddCash(15000); ! ! if(this.account.Portfolio.Count > 0) ! this.numOfDaysWithOpenPosition++; ! ! if(this.numOfDaysWithOpenPosition == this.numDaysForReturnCalculation) ! this.marketCloseEventHandler_closePositions(); ! ! if(this.account.Portfolio.Count == 0 && ! this.numOfClosesElapsed >= this.numDaysForReturnCalculation) ! //portfolio is empty and previous closes can be now checked ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! } ! public void OneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! } } } |