[Quantproject-developers] QuantProject/b4_Business/a2_Strategies Benchmark.cs, 1.4, 1.5
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2010-03-28 14:10:15
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv18449/a2_Strategies Modified Files: Benchmark.cs Log Message: The following public method has been added: DateTime GetThisOrNextDateTimeWhenTraded( DateTime startingDateTime , Time dailyTime , DateTime lastAcceptableDateTime ) Starting from startingDateTime, this method searches for the next dateTime when the benchmark is traded at time dailyTime. The returned value is greater than or equal to startingDateTime, that is, startingDateTime itself is a valid candidate. If no solution is found within maxNumberOfDaysToLookAheadFor days, then an exception is returned Index: Benchmark.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/Benchmark.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Benchmark.cs 29 Sep 2008 21:14:23 -0000 1.4 --- Benchmark.cs 28 Mar 2010 14:10:07 -0000 1.5 *************** *** 3,6 **** --- 3,7 ---- using QuantProject.ADT; using QuantProject.ADT.Histories; + using QuantProject.ADT.Timing; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies.ReturnsManagement.Time; *************** *** 17,21 **** { private string ticker; ! private HistoricalMarketValueProvider historicalQuoteProvider; public string Ticker --- 18,22 ---- { private string ticker; ! private HistoricalMarketValueProvider historicalMarketValueProvider; public string Ticker *************** *** 28,32 **** this.ticker = ticker; ! this.historicalQuoteProvider = new HistoricalAdjustedQuoteProvider(); } --- 29,41 ---- this.ticker = ticker; ! this.historicalMarketValueProvider = new HistoricalAdjustedQuoteProvider(); ! } ! ! public Benchmark( ! string ticker , HistoricalMarketValueProvider historicalMarketValueProvider ) ! { ! this.ticker = ticker; ! ! this.historicalMarketValueProvider = historicalMarketValueProvider; } *************** *** 35,43 **** DateTime timeStepBegin ) { ! if ( !this.historicalQuoteProvider.WasExchanged( this.ticker , ! timeStepBegin ) ) throw new Exception( "The benchmark '" + this.ticker + ! "' was not exchanged at the given timeStepBegin " + ! timeStepBegin.ToString() ); } private void isExchanged_checkParameter( --- 44,52 ---- DateTime timeStepBegin ) { ! if ( !this.historicalMarketValueProvider.WasExchanged( this.ticker , ! timeStepBegin ) ) throw new Exception( "The benchmark '" + this.ticker + ! "' was not exchanged at the given timeStepBegin " + ! timeStepBegin.ToString() ); } private void isExchanged_checkParameter( *************** *** 45,58 **** { if ( dateTime.Year > ! DateTime.Now.Year ) throw new Exception( "It looks like this is a loop! " + ! "You have probably asked for a GetTimeStep, but " + ! "there is no other trading day for this benchmark: '" + ! this.ticker + "'" ); } private bool isExchanged( DateTime dateTime ) { this.isExchanged_checkParameter( dateTime ); ! bool isTraded = this.historicalQuoteProvider.WasExchanged( this.ticker , dateTime ); return isTraded; --- 54,67 ---- { if ( dateTime.Year > ! DateTime.Now.Year ) throw new Exception( "It looks like this is a loop! " + ! "You have probably asked for a GetTimeStep, but " + ! "there is no other trading day for this benchmark: '" + ! this.ticker + "'" ); } private bool isExchanged( DateTime dateTime ) { this.isExchanged_checkParameter( dateTime ); ! bool isTraded = this.historicalMarketValueProvider.WasExchanged( this.ticker , dateTime ); return isTraded; *************** *** 89,93 **** /// <summary> /// Returns either the next market close or the next market open ! /// (when the benchmark is exchanged), whichever is the nearest. /// If dateTime is either a market close or a market open /// when the benchmark is exchanged, then dateTime itself --- 98,102 ---- /// <summary> /// Returns either the next market close or the next market open ! /// (when the benchmark is exchanged), whichever is the nearest. /// If dateTime is either a market close or a market open /// when the benchmark is exchanged, then dateTime itself *************** *** 122,126 **** if( lastDateTime <= firstDateTime ) throw new Exception("lastDateTime has to be greater than " + ! "firstEndOfDayDateTime !!"); History endOfDayHistory = new History(); DateTime dateTimeToAddToHistory = --- 131,135 ---- if( lastDateTime <= firstDateTime ) throw new Exception("lastDateTime has to be greater than " + ! "firstEndOfDayDateTime !!"); History endOfDayHistory = new History(); DateTime dateTimeToAddToHistory = *************** *** 140,143 **** --- 149,219 ---- } + #region GetThisOrNextDateTimeWhenTraded + private DateTime getDateTimeCandidate( + DateTime minDateTimeToTry , Time dailyTime ) + { + Date dateUsedToBuildDateTimeCandidate = new Date( minDateTimeToTry ); + Time timeForMinDateTimeToTry = new Time( minDateTimeToTry ); + if ( timeForMinDateTimeToTry > dailyTime ) + dateUsedToBuildDateTimeCandidate = + dateUsedToBuildDateTimeCandidate.AddDays( 1 ); + DateTime dateTimeCandidate = new DateTime( + dateUsedToBuildDateTimeCandidate.Year , + dateUsedToBuildDateTimeCandidate.Month , + dateUsedToBuildDateTimeCandidate.Day , + dailyTime.Hour , + dailyTime.Minute , + dailyTime.Second ); + return dateTimeCandidate; + } + // private bool isCandidateTooFarAhead( + // DateTime startingDateTime , DateTime dateTimeCandidate , + // int maxNumberOfDaysToLookAheadFor ) + // { + // bool isTooFarAhead; + // TimeSpan timeSpan = dateTimeCandidate.Subtract( startingDateTime ); + // isTooFarAhead = ( timeSpan.Days > maxNumberOfDaysToLookAheadFor ); + // return isTooFarAhead; + // } + /// <summary> + /// Starting from startingDateTime, this method searches for the next + /// dateTime when the benchmark is traded at time dailyTime. + /// The returned value is greater than or equal to startingDateTime, + /// that is, startingDateTime itself is a valid candidate. + /// If no solution is found within maxNumberOfDaysToLookAheadFor days, + /// then an exception is returned + /// </summary> + /// <param name="startingDateTime"></param> + /// <param name="dailyTime"></param> + /// <param name="maxNumberOfDaysToLookAheadFor"></param> + /// <returns></returns> + public virtual DateTime GetThisOrNextDateTimeWhenTraded( + DateTime startingDateTime , + Time dailyTime , + DateTime lastAcceptableDateTime ) + { + DateTime dateTimeCandidate = this.getDateTimeCandidate( + startingDateTime , dailyTime ); + DateTime thisOrNextDateTimeWhenTraded = DateTime.MinValue; + while ( dateTimeCandidate <= lastAcceptableDateTime && + // !this.isCandidateTooFarAhead( + // startingDateTime , dateTimeCandidate , maxNumberOfDaysToLookAheadFor ) && + ( thisOrNextDateTimeWhenTraded == DateTime.MinValue ) ) + { + // a valid dateTime when traded has not been found yet + if ( this.isExchanged( dateTimeCandidate ) ) + thisOrNextDateTimeWhenTraded = dateTimeCandidate; + else + dateTimeCandidate = this.getDateTimeCandidate( + dateTimeCandidate.AddSeconds( 1 ) , dailyTime ); + } + if ( thisOrNextDateTimeWhenTraded == DateTime.MinValue ) + throw new Exception( + "In the specified number of days, the benchmark is never " + + "traded at the specified dailyTime!" ); + return thisOrNextDateTimeWhenTraded; + } + #endregion GetThisOrNextDateTimeWhenTraded + } } |