[Quantproject-developers] QuantProject/b4_Business/a2_Strategies/EndOfDayStrategies BasicEndOfDayS
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-09-29 21:14:49
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/EndOfDayStrategies In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv13959/EndOfDayStrategies Modified Files: BasicEndOfDayStrategyForBacktester.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: BasicEndOfDayStrategyForBacktester.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/EndOfDayStrategies/BasicEndOfDayStrategyForBacktester.cs,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** BasicEndOfDayStrategyForBacktester.cs 16 Aug 2008 19:28:02 -0000 1.7 --- BasicEndOfDayStrategyForBacktester.cs 29 Sep 2008 21:14:23 -0000 1.8 *************** *** 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; *************** *** 43,48 **** /// that should be inherited by specific strategies /// </summary> public abstract class BasicEndOfDayStrategyForBacktester : ! IEndOfDayStrategyForBacktester { public event NewLogItemEventHandler NewLogItem; --- 43,49 ---- /// that should be inherited by specific strategies /// </summary> + [Serializable] public abstract class BasicEndOfDayStrategyForBacktester : ! IStrategyForBacktester { public event NewLogItemEventHandler NewLogItem; *************** *** 55,59 **** protected IEligiblesSelector eligiblesSelector; protected IInSampleChooser inSampleChooser; ! protected IHistoricalQuoteProvider historicalQuoteProviderForInSample; protected DateTime lastOptimizationDateTime; --- 56,60 ---- protected IEligiblesSelector eligiblesSelector; protected IInSampleChooser inSampleChooser; ! protected HistoricalMarketValueProvider historicalMarketValueProviderForInSample; protected DateTime lastOptimizationDateTime; *************** *** 71,79 **** } ! public bool StopBacktestIfMaxRunningHoursHasBeenReached { get { ! return this.optimalWeightedPositionsAreToBeUpdated(); } } --- 72,80 ---- } ! public virtual bool StopBacktestIfMaxRunningHoursHasBeenReached { get { ! return this.areOptimalWeightedPositionsToBeUpdated(); } } *************** *** 113,118 **** IEligiblesSelector eligiblesSelector , IInSampleChooser inSampleChooser , ! IHistoricalQuoteProvider historicalQuoteProviderForInSample ! ) { this.numDaysBeetweenEachOtpimization = numDaysBeetweenEachOtpimization; --- 114,119 ---- IEligiblesSelector eligiblesSelector , IInSampleChooser inSampleChooser , ! HistoricalMarketValueProvider historicalMarketValueProviderForInSample ! ) { this.numDaysBeetweenEachOtpimization = numDaysBeetweenEachOtpimization; *************** *** 122,127 **** this.eligiblesSelector = eligiblesSelector; this.inSampleChooser = inSampleChooser; ! this.historicalQuoteProviderForInSample = ! historicalQuoteProviderForInSample; this.returnIntervals = --- 123,128 ---- this.eligiblesSelector = eligiblesSelector; this.inSampleChooser = inSampleChooser; ! this.historicalMarketValueProviderForInSample = ! historicalMarketValueProviderForInSample; this.returnIntervals = *************** *** 129,133 **** } ! #region MarketOpenEventHandler protected abstract bool marketOpenEventHandler_arePositionsToBeClosed(); --- 130,134 ---- } ! #region marketOpenEventHandler protected abstract bool marketOpenEventHandler_arePositionsToBeClosed(); *************** *** 136,141 **** WeightedPositions marketOpenEventHandler_getPositionsToBeOpened(); ! public void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { this.updateReturnIntervals(); --- 137,141 ---- WeightedPositions marketOpenEventHandler_getPositionsToBeOpened(); ! private void marketOpenEventHandler() { this.updateReturnIntervals(); *************** *** 153,164 **** } } ! #endregion MarketOpenEventHandler ! public void FiveMinutesBeforeMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! } ! #region MarketCloseEventHandler protected abstract bool marketCloseEventHandler_arePositionsToBeClosed(); --- 153,164 ---- } } ! #endregion marketOpenEventHandler ! // public void FiveMinutesBeforeMarketCloseEventHandler( ! // Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! // { ! // } ! #region marketCloseEventHandler protected abstract bool marketCloseEventHandler_arePositionsToBeClosed(); *************** *** 167,172 **** WeightedPositions marketCloseEventHandler_getPositionsToBeOpened(); ! public void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { this.updateReturnIntervals(); --- 167,171 ---- WeightedPositions marketCloseEventHandler_getPositionsToBeOpened(); ! private void marketCloseEventHandler() { this.updateReturnIntervals(); *************** *** 184,240 **** } } ! #endregion MarketCloseEventHandler ! #region OneHourAfterMarketCloseEventHandler ! private bool optimalWeightedPositionsAreToBeUpdated() { - // TimeSpan timeSpanSinceLastOptimization = - // this.now().DateTime - this.lastOptimizationDateTime; - // bool areToBeUpdated = ( timeSpanSinceLastOptimization.Days >= - // this.numDaysBeetweenEachOtpimization ); - // return areToBeUpdated; DateTime dateTimeForNextOptimization = this.lastOptimizationDateTime.AddDays( ! this.numDaysBeetweenEachOtpimization ); bool areToBeUpdated = ( ( ( this.account.Portfolio.Count == 0 ) ! && ( ( this.lastOptimizationDateTime == DateTime.MinValue ) ) ) || ! ( this.now().DateTime >= dateTimeForNextOptimization ) ); return areToBeUpdated; } #region getInSampleReturnIntervals ! private EndOfDayDateTime getInSampleReturnIntervals_getFirstDate() { ! DateTime firstDateTime = this.now().DateTime.AddDays( -this.numDaysForInSampleOptimization ); ! EndOfDayDateTime firstDate = new EndOfDayDateTime( ! firstDateTime , EndOfDaySpecificTime.MarketOpen ); ! return firstDate; } private ReturnIntervals getInSampleReturnIntervals() { ! EndOfDayDateTime firstDate = ! this.getInSampleReturnIntervals_getFirstDate(); ! EndOfDayDateTime lastDate = ! new EndOfDayDateTime( this.now().DateTime , ! EndOfDaySpecificTime.MarketClose ); ReturnIntervals inSampleReturnIntervals = new ReturnIntervals( this.intervalsSelectorForInSample ); ! inSampleReturnIntervals.AppendFirstInterval( firstDate ); ! if ( inSampleReturnIntervals.LastEndOfDayDateTime.IsLessThan( ! lastDate ) ) inSampleReturnIntervals.AppendIntervalsButDontGoBeyondLastDate( ! lastDate ); return inSampleReturnIntervals; } #endregion getInSampleReturnIntervals private void checkQualityFor_bestTestingPositionsInSample() { for( int i = 0 ; i < this.bestTestingPositionsInSample.Length ; i++ ) if ( this.bestTestingPositionsInSample[ i ] == null ) ! throw new Exception( ! "The IInSampleChooser should have returned an array " + ! "of non null bestTestingPositionsInSample!" ); } private void notifyMessage( EligibleTickers eligibleTickers ) --- 183,254 ---- } } ! #endregion marketCloseEventHandler ! #region oneHourAfterMarketCloseEventHandler ! ! #region areOptimalWeightedPositionsToBeUpdated ! private bool areOptimalWeightedPositionsToBeUpdated_actually() { DateTime dateTimeForNextOptimization = this.lastOptimizationDateTime.AddDays( ! this.numDaysBeetweenEachOtpimization ); ! DateTime currentSimulatedDateTime = this.now(); bool areToBeUpdated = ( ( ( this.account.Portfolio.Count == 0 ) ! && ( ( this.lastOptimizationDateTime == DateTime.MinValue ) ) ) || ! ( currentSimulatedDateTime >= dateTimeForNextOptimization ) ); return areToBeUpdated; } + private bool areOptimalWeightedPositionsToBeUpdated() + { + bool areToBeUpdated; + // if ( !this.account.Timer.IsActive ) + // // the backtester has stopped the timer because the + // // backtest has gone beyond max running hours + // areToBeUpdated = false; + // else + areToBeUpdated = + this.areOptimalWeightedPositionsToBeUpdated_actually(); + return areToBeUpdated; + } + #endregion areOptimalWeightedPositionsToBeUpdated + #region getInSampleReturnIntervals ! private DateTime getInSampleReturnIntervals_getFirstDateTime() { ! DateTime someDaysBefore = this.now().AddDays( -this.numDaysForInSampleOptimization ); ! DateTime firstDateTime = ! HistoricalEndOfDayTimer.GetMarketOpen( someDaysBefore ); ! // EndOfDayDateTime firstDate = new EndOfDayDateTime( ! // someDaysBefore , EndOfDaySpecificTime.MarketOpen ); ! return firstDateTime; } private ReturnIntervals getInSampleReturnIntervals() { ! DateTime firstDateTime = ! this.getInSampleReturnIntervals_getFirstDateTime(); ! DateTime lastDateTime = ! HistoricalEndOfDayTimer.GetMarketClose( this.now() ); ! // new EndOfDayDateTime( this.now().DateTime , ! // EndOfDaySpecificTime.MarketClose ); ReturnIntervals inSampleReturnIntervals = new ReturnIntervals( this.intervalsSelectorForInSample ); ! inSampleReturnIntervals.AppendFirstInterval( firstDateTime ); ! if ( inSampleReturnIntervals.LastDateTime < lastDateTime ) inSampleReturnIntervals.AppendIntervalsButDontGoBeyondLastDate( ! lastDateTime ); return inSampleReturnIntervals; } #endregion getInSampleReturnIntervals + private void checkQualityFor_bestTestingPositionsInSample() { for( int i = 0 ; i < this.bestTestingPositionsInSample.Length ; i++ ) if ( this.bestTestingPositionsInSample[ i ] == null ) ! throw new Exception( ! "The IInSampleChooser should have returned an array " + ! "of non null bestTestingPositionsInSample!" ); } private void notifyMessage( EligibleTickers eligibleTickers ) *************** *** 249,262 **** this.NewMessage( this , newMessageEventArgs ); } - #region logOptimizationInfo - // private void outputMessage( string message ) - // { - // string dateStamp = - // ExtendedDateTime.GetShortDescriptionForFileName( DateTime.Now ); - // MessageManager.DisplayMessage( message , - // "NotificationMessagesForCurrentStrategy_" + - // dateStamp + ".Txt" ); - // } protected abstract LogItem getLogItem( EligibleTickers eligibleTickers ); --- 263,268 ---- this.NewMessage( this , newMessageEventArgs ); } + #region logOptimizationInfo protected abstract LogItem getLogItem( EligibleTickers eligibleTickers ); *************** *** 274,277 **** --- 280,284 ---- } #endregion logOptimizationInfo + private void updateOptimalTestingPositions_actually() { *************** *** 280,290 **** EligibleTickers eligibleTickers = this.eligiblesSelector.GetEligibleTickers( ! inSampleReturnIntervals.BordersHistory ); this.inSampleReturnsManager = new ReturnsManager( inSampleReturnIntervals , ! this.historicalQuoteProviderForInSample ); this.bestTestingPositionsInSample = (TestingPositions[])this.inSampleChooser.AnalyzeInSample( ! eligibleTickers , this.inSampleReturnsManager ); this.checkQualityFor_bestTestingPositionsInSample(); --- 287,297 ---- EligibleTickers eligibleTickers = this.eligiblesSelector.GetEligibleTickers( ! inSampleReturnIntervals.BordersHistory ); this.inSampleReturnsManager = new ReturnsManager( inSampleReturnIntervals , ! this.historicalMarketValueProviderForInSample ); this.bestTestingPositionsInSample = (TestingPositions[])this.inSampleChooser.AnalyzeInSample( ! eligibleTickers , this.inSampleReturnsManager ); this.checkQualityFor_bestTestingPositionsInSample(); *************** *** 295,310 **** { this.updateOptimalTestingPositions_actually(); ! this.lastOptimizationDateTime = this.now().DateTime; // FixedLengthTwoPhasesLogItem logItem = // new FixedLengthTwoPhasesLogItem( this.now() ); // logItem.BestWeightedPositionsInSample = this.bestWeightedPositionsInSample; } ! public void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! if ( this.optimalWeightedPositionsAreToBeUpdated() ) this.updateOptimalTestingPositions(); } ! #endregion OneHourAfterMarketCloseEventHandler // protected bool isInsampleOptimizationNeeded() --- 302,328 ---- { this.updateOptimalTestingPositions_actually(); ! this.lastOptimizationDateTime = this.now(); // FixedLengthTwoPhasesLogItem logItem = // new FixedLengthTwoPhasesLogItem( this.now() ); // logItem.BestWeightedPositionsInSample = this.bestWeightedPositionsInSample; } ! private void oneHourAfterMarketCloseEventHandler() { ! if ( this.areOptimalWeightedPositionsToBeUpdated() ) this.updateOptimalTestingPositions(); } ! #endregion oneHourAfterMarketCloseEventHandler ! ! #region NewDateTimeEventHandler ! public void NewDateTimeEventHandler( Object sender , DateTime dateTime ) ! { ! if ( HistoricalEndOfDayTimer.IsMarketOpen( dateTime ) ) ! this.marketOpenEventHandler(); ! if ( HistoricalEndOfDayTimer.IsMarketClose( dateTime ) ) ! this.marketCloseEventHandler(); ! if ( HistoricalEndOfDayTimer.IsOneHourAfterMarketClose( dateTime ) ) ! this.oneHourAfterMarketCloseEventHandler(); ! } ! #endregion NewDateTimeEventHandler // protected bool isInsampleOptimizationNeeded() *************** *** 320,340 **** // } ! protected EndOfDayDateTime now() { ! return this.account.EndOfDayTimer.GetCurrentTime(); } private void updateReturnIntervals() { ! EndOfDayDateTime currentEndOfDayDateTime = this.now(); if ( this.returnIntervals.Count == 0 ) // no interval has been added yet this.returnIntervals.AppendFirstInterval( ! currentEndOfDayDateTime ); else // at least one interval has already been added ! if ( this.returnIntervals.LastEndOfDayDateTime.IsLessThanOrEqualTo( ! currentEndOfDayDateTime ) ) this.returnIntervals.AppendIntervalsToGoJustBeyond( ! currentEndOfDayDateTime ); } protected ReturnInterval lastIntervalAppended() --- 338,359 ---- // } ! protected DateTime now() { ! DateTime simulatedDateTime = ! this.account.Timer.GetCurrentDateTime(); ! return simulatedDateTime; } private void updateReturnIntervals() { ! DateTime currentDateTime = this.now(); if ( this.returnIntervals.Count == 0 ) // no interval has been added yet this.returnIntervals.AppendFirstInterval( ! currentDateTime ); else // at least one interval has already been added ! if ( this.returnIntervals.LastDateTime <= currentDateTime ) this.returnIntervals.AppendIntervalsToGoJustBeyond( ! currentDateTime ); } protected ReturnInterval lastIntervalAppended() |