[Quantproject-developers] QuantProject/b4_Business/a2_Strategies EndOfDayStrategyBackTester.cs, 1.9
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-09-29 21:17:09
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv15371 Modified Files: EndOfDayStrategyBackTester.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: EndOfDayStrategyBackTester.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/EndOfDayStrategyBackTester.cs,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** EndOfDayStrategyBackTester.cs 19 Mar 2008 23:21:33 -0000 1.9 --- EndOfDayStrategyBackTester.cs 29 Sep 2008 21:16:18 -0000 1.10 *************** *** 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; *************** *** 29,33 **** using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Financial.Accounting.Reporting; ! using QuantProject.Business.Financial.Accounting.AccountProviding; using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Strategies.Logging; --- 29,33 ---- using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Financial.Accounting.Reporting; ! using QuantProject.Business.Financial.Accounting.AccountProviding; using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Strategies.Logging; *************** *** 39,49 **** /// Performs a backtest for an end of day strategy /// </summary> public class EndOfDayStrategyBackTester : IMessageSender , ILogDescriptor { public event NewMessageEventHandler NewMessage; private string backTestID; ! private IEndOfDayStrategyForBacktester endOfDayStrategy; ! private IHistoricalQuoteProvider historicalQuoteProvider; private DateTime firstDateTime; private DateTime lastDateTime; --- 39,57 ---- /// Performs a backtest for an end of day strategy /// </summary> + [Serializable] public class EndOfDayStrategyBackTester : IMessageSender , ILogDescriptor { public event NewMessageEventHandler NewMessage; + + // the following event is used to avoid the strategy receiving + // NewDateTime events directly from the timer: this way the + // strategy does not receive any new message once the backtester + // has been completed + private event NewDateTimeEventHandler newDateTime; + private string backTestID; ! private IStrategyForBacktester strategyForBacktester; ! private HistoricalMarketValueProvider historicalMarketValueProvider; private DateTime firstDateTime; private DateTime lastDateTime; *************** *** 53,57 **** private IAccountProvider accountProvider; private DateTime startingTimeForScript; ! private IEndOfDayTimer endOfDayTimer; private DateTime actualLastDateTime; private Account account; --- 61,65 ---- private IAccountProvider accountProvider; private DateTime startingTimeForScript; ! private Timer timer; private DateTime actualLastDateTime; private Account account; *************** *** 60,66 **** private DateTime realDateTimeWhenTheBackTestIsStopped; ! public IHistoricalQuoteProvider HistoricalQuoteProvider { ! get { return this.historicalQuoteProvider; } } public Benchmark Benchmark --- 68,74 ---- private DateTime realDateTimeWhenTheBackTestIsStopped; ! public HistoricalMarketValueProvider HistoricalMarketValueProvider { ! get { return this.historicalMarketValueProvider; } } public Benchmark Benchmark *************** *** 117,125 **** /// The timer used by the backtester, to simulate the time ticking /// </summary> ! public IEndOfDayTimer EndOfDayTimer { get { ! return this.endOfDayTimer; } } --- 125,133 ---- /// The timer used by the backtester, to simulate the time ticking /// </summary> ! public Timer Timer { get { ! return this.timer; } } *************** *** 130,134 **** string description = ExtendedDateTime.GetCompleteShortDescriptionForFileName( ! this.realDateTimeWhenTheBackTestIsStopped ) + "_" + "from_" + ExtendedDateTime.GetShortDescriptionForFileName( this.firstDateTime ) + --- 138,142 ---- string description = ExtendedDateTime.GetCompleteShortDescriptionForFileName( ! this.realDateTimeWhenTheBackTestIsStopped ) + "_" + "from_" + ExtendedDateTime.GetShortDescriptionForFileName( this.firstDateTime ) + *************** *** 137,142 **** "_annlRtrn_" + this.AccountReport.Summary.AnnualSystemPercentageReturn.FormattedValue + "_maxDD_" + this.AccountReport.Summary.MaxEquityDrawDown.FormattedValue + ! "_" + this.historicalQuoteProvider.Description + ! "_" + this.endOfDayStrategy.Description; return description.Substring( 0 , Math.Min( description.Length , 200 ) ); } --- 145,150 ---- "_annlRtrn_" + this.AccountReport.Summary.AnnualSystemPercentageReturn.FormattedValue + "_maxDD_" + this.AccountReport.Summary.MaxEquityDrawDown.FormattedValue + ! "_" + this.historicalMarketValueProvider.Description + ! "_" + this.strategyForBacktester.Description; return description.Substring( 0 , Math.Min( description.Length , 200 ) ); } *************** *** 148,166 **** // the timer has not been stopped yet throw new Exception( "This property cannot be invoked " + ! "while the backtest is still running!" ); } public EndOfDayStrategyBackTester( string backTestID , ! IEndOfDayStrategyForBacktester endOfDayStrategy , ! IHistoricalQuoteProvider historicalQuoteProvider , ! IAccountProvider accountProvider, ! DateTime firstDateTime , DateTime lastDateTime , ! Benchmark benchmark , ! double cashToStart , ! double maxRunningHours ) { ! this.endOfDayStrategyBackTester_checkParameters( ! endOfDayStrategy , ! historicalQuoteProvider , accountProvider, firstDateTime , lastDateTime , --- 156,174 ---- // the timer has not been stopped yet throw new Exception( "This property cannot be invoked " + ! "while the backtest is still running!" ); } public EndOfDayStrategyBackTester( string backTestID , ! IStrategyForBacktester strategyForBacktester , ! HistoricalMarketValueProvider historicalMarketValueProvider , ! IAccountProvider accountProvider, ! DateTime firstDateTime , DateTime lastDateTime , ! Benchmark benchmark , ! double cashToStart , ! double maxRunningHours ) { ! this.endOfDayStrategyBackTester_checkParameters( ! strategyForBacktester , ! historicalMarketValueProvider , accountProvider, firstDateTime , lastDateTime , *************** *** 169,174 **** maxRunningHours ); this.backTestID = backTestID; ! this.endOfDayStrategy = endOfDayStrategy; ! this.historicalQuoteProvider = historicalQuoteProvider; this.accountProvider = accountProvider; this.firstDateTime = firstDateTime; --- 177,182 ---- maxRunningHours ); this.backTestID = backTestID; ! this.strategyForBacktester = strategyForBacktester; ! this.historicalMarketValueProvider = historicalMarketValueProvider; this.accountProvider = accountProvider; this.firstDateTime = firstDateTime; *************** *** 178,193 **** this.maxRunningHours = maxRunningHours; ! this.initialize_endOfDayTimer(); ! this.account = this.accountProvider.GetAccount(this.endOfDayTimer, ! this.historicalQuoteProvider); ! this.endOfDayStrategy.Account = this.account; this.backTestLog = new BackTestLog( backTestID , firstDateTime , ! lastDateTime , benchmark ); this.actualLastDateTime = DateTime.MinValue; this.realDateTimeWhenTheBackTestIsStopped = DateTime.MinValue; } private void endOfDayStrategyBackTester_checkParameters( ! IEndOfDayStrategyForBacktester endOfDayStrategy , ! IHistoricalQuoteProvider historicalQuoteProvider , IAccountProvider accountProvider, DateTime firstDateTime , DateTime lastDateTime , --- 186,201 ---- this.maxRunningHours = maxRunningHours; ! this.initialize_endOfDayTimer(); ! this.account = this.accountProvider.GetAccount( ! this.timer, this.historicalMarketValueProvider); ! this.strategyForBacktester.Account = this.account; this.backTestLog = new BackTestLog( backTestID , firstDateTime , ! lastDateTime , benchmark ); this.actualLastDateTime = DateTime.MinValue; this.realDateTimeWhenTheBackTestIsStopped = DateTime.MinValue; } private void endOfDayStrategyBackTester_checkParameters( ! IStrategyForBacktester strategyForBacktester , ! HistoricalMarketValueProvider historicalMarketValueProvider , IAccountProvider accountProvider, DateTime firstDateTime , DateTime lastDateTime , *************** *** 196,202 **** double maxRunningHours ) { ! if ( endOfDayStrategy == null ) throw new Exception( "endOfDayStrategy cannot be null!" ); ! if ( historicalQuoteProvider == null ) throw new Exception( "historicalQuoteProvider cannot be null!" ); if ( accountProvider == null ) --- 204,210 ---- double maxRunningHours ) { ! if ( strategyForBacktester == null ) throw new Exception( "endOfDayStrategy cannot be null!" ); ! if ( historicalMarketValueProvider == null ) throw new Exception( "historicalQuoteProvider cannot be null!" ); if ( accountProvider == null ) *************** *** 212,221 **** private void initialize_endOfDayTimer() { ! EndOfDayDateTime endOfDayDateTime = ! new EndOfDayDateTime( this.firstDateTime , ! EndOfDaySpecificTime.MarketOpen ); ! this.endOfDayTimer = ! new IndexBasedEndOfDayTimer( endOfDayDateTime , ! this.benchmark.Ticker ); } --- 220,229 ---- private void initialize_endOfDayTimer() { ! DateTime startingDateTime = HistoricalEndOfDayTimer.GetMarketOpen( ! this.firstDateTime ); ! // new EndOfDayDateTime( this.firstDateTime , ! // EndOfDaySpecificTime.MarketOpen ); ! this.timer = new IndexBasedEndOfDayTimer( ! startingDateTime , this.benchmark.Ticker ); } *************** *** 223,227 **** #region run_addEventHandlers private void handlerToAddCashToStart( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if ( this.account.Transactions.Count == 0 ) --- 231,235 ---- #region run_addEventHandlers private void handlerToAddCashToStart( ! Object sender , DateTime dateTime ) { if ( this.account.Transactions.Count == 0 ) *************** *** 231,272 **** private void run_addEventHandlers_addHandlersToAddCashToStart() { ! this.endOfDayTimer.MarketOpen += ! new MarketOpenEventHandler( ! this.handlerToAddCashToStart ); ! this.endOfDayTimer.FiveMinutesBeforeMarketClose += ! new FiveMinutesBeforeMarketCloseEventHandler( ! this.handlerToAddCashToStart ); ! this.endOfDayTimer.MarketClose += ! new MarketCloseEventHandler( ! this.handlerToAddCashToStart ); ! this.endOfDayTimer.OneHourAfterMarketClose += ! new OneHourAfterMarketCloseEventHandler( ! this.handlerToAddCashToStart ); } private void run_addEventHandlers_addStrategyHandlers() { ! this.endOfDayTimer.MarketOpen += ! new MarketOpenEventHandler( ! this.endOfDayStrategy.MarketOpenEventHandler ); ! this.endOfDayTimer.FiveMinutesBeforeMarketClose += ! new FiveMinutesBeforeMarketCloseEventHandler( ! this.endOfDayStrategy.FiveMinutesBeforeMarketCloseEventHandler ); ! this.endOfDayTimer.OneHourAfterMarketClose += ! new OneHourAfterMarketCloseEventHandler( ! this.endOfDayStrategy.OneHourAfterMarketCloseEventHandler ); ! this.endOfDayTimer.MarketClose += ! new MarketCloseEventHandler( ! this.endOfDayStrategy.MarketCloseEventHandler ); } private void run_addEventHandlers() { this.run_addEventHandlers_addHandlersToAddCashToStart(); this.run_addEventHandlers_addStrategyHandlers(); ! this.endOfDayTimer.MarketClose += ! new MarketCloseEventHandler( ! this.marketCloseEventHandler ); ! this.endOfDayStrategy.NewLogItem += new NewLogItemEventHandler( ! this.newLogItemEventHandler ); } #endregion run_addEventHandlers --- 239,292 ---- private void run_addEventHandlers_addHandlersToAddCashToStart() { ! this.newDateTime += ! new NewDateTimeEventHandler( ! this.handlerToAddCashToStart ); ! // this.timer.NewDateTime += ! // new NewDateTimeEventHandler( ! // this.handlerToAddCashToStart ); ! ! // this.endOfDayTimer.MarketOpen += ! // new MarketOpenEventHandler( ! // this.handlerToAddCashToStart ); ! // this.endOfDayTimer.FiveMinutesBeforeMarketClose += ! // new FiveMinutesBeforeMarketCloseEventHandler( ! // this.handlerToAddCashToStart ); ! // this.endOfDayTimer.MarketClose += ! // new MarketCloseEventHandler( ! // this.handlerToAddCashToStart ); ! // this.endOfDayTimer.OneHourAfterMarketClose += ! // new OneHourAfterMarketCloseEventHandler( ! // this.handlerToAddCashToStart ); } private void run_addEventHandlers_addStrategyHandlers() { ! this.newDateTime += ! new NewDateTimeEventHandler( ! this.strategyForBacktester.NewDateTimeEventHandler ); ! // this.endOfDayTimer.MarketOpen += ! // new MarketOpenEventHandler( ! // this.endOfDayStrategy.MarketOpenEventHandler ); ! // this.endOfDayTimer.FiveMinutesBeforeMarketClose += ! // new FiveMinutesBeforeMarketCloseEventHandler( ! // this.endOfDayStrategy.FiveMinutesBeforeMarketCloseEventHandler ); ! // this.endOfDayTimer.OneHourAfterMarketClose += ! // new OneHourAfterMarketCloseEventHandler( ! // this.endOfDayStrategy.OneHourAfterMarketCloseEventHandler ); ! // this.endOfDayTimer.MarketClose += ! // new MarketCloseEventHandler( ! // this.endOfDayStrategy.MarketCloseEventHandler ); } private void run_addEventHandlers() { this.run_addEventHandlers_addHandlersToAddCashToStart(); + this.timer.NewDateTime += + new NewDateTimeEventHandler( this.newDateTimeEventHandler ); this.run_addEventHandlers_addStrategyHandlers(); ! // this.endOfDayTimer.MarketClose += ! // new MarketCloseEventHandler( ! // this.marketCloseEventHandler ); ! this.strategyForBacktester.NewLogItem += new NewLogItemEventHandler( ! this.newLogItemEventHandler ); } #endregion run_addEventHandlers *************** *** 284,311 **** run_addEventHandlers(); // this.progressBarForm.Show(); ! this.endOfDayTimer.Start(); } #endregion Run ! #region marketCloseEventHandler private bool isTimeToStop( DateTime currentTime ) { DateTime maxEndingDateTimeForScript = this.startingTimeForScript.AddHours( this.maxRunningHours ); ! bool scriptTimeElapsed = ( DateTime.Now >= maxEndingDateTimeForScript ); bool stopBacktestIfMaxRunningHoursHasBeenReached = ! this.endOfDayStrategy.StopBacktestIfMaxRunningHoursHasBeenReached; return ( ( currentTime > this.lastDateTime ) || ! ( scriptTimeElapsed && ! stopBacktestIfMaxRunningHoursHasBeenReached ) ); } ! private void marketCloseEventHandler_notifyProgress( ! IEndOfDayTimer endOfDayTimer ) { string progressMessage = "Current out of sample date:" + ! endOfDayTimer.GetCurrentTime().DateTime.ToString() + " - " + "First date:" + this.firstDateTime.ToString() + " - " + ! "Last date:" + this.lastDateTime.ToString(); NewMessageEventArgs newMessageEventArgs = new NewMessageEventArgs( progressMessage ); --- 304,373 ---- run_addEventHandlers(); // this.progressBarForm.Show(); ! this.timer.Start(); } #endregion Run ! #region newDateTimeEventHandler ! private bool isTimeToStop( DateTime currentTime ) { DateTime maxEndingDateTimeForScript = this.startingTimeForScript.AddHours( this.maxRunningHours ); ! DateTime realTime = DateTime.Now; ! bool scriptTimeElapsed = ( realTime >= maxEndingDateTimeForScript ); bool stopBacktestIfMaxRunningHoursHasBeenReached = ! this.strategyForBacktester.StopBacktestIfMaxRunningHoursHasBeenReached; return ( ( currentTime > this.lastDateTime ) || ! ( scriptTimeElapsed && ! stopBacktestIfMaxRunningHoursHasBeenReached ) ); } ! // private void removeTimerEventHandlers() ! // { ! // foreach( NewDateTimeEventHandler newDateTimeEventHandler in ! // this.timer.NewDateTime.GetInvocationList()) ! // { ! // this.timer.NewDateTime -= newDateTimeEventHandler; ! // } ! // } ! ! #region stopTheScript ! private void stopTheScript( DateTime currentDateTime ) ! { ! this.actualLastDateTime = ! ExtendedDateTime.Min( this.lastDateTime , currentDateTime ); ! this.timer.Stop(); ! // this.removeTimerEventHandlers(); ! // this.timer.NewDateTime -= ! // new NewDateTimeEventHandler( ! // this.strategyForBacktester.NewDateTimeEventHandler ); ! this.realDateTimeWhenTheBackTestIsStopped = DateTime.Now; ! this.accountReport = this.account.CreateReport( ! "" , ! 1 , currentDateTime , this.benchmark.Ticker , ! this.historicalMarketValueProvider ); ! this.accountReport.Name = this.Description; ! } ! // private void stopTheScriptIfTheCase( Object sender ) ! // { ! // DateTime currentDateTime = ! // ( ( Timer )sender ).GetCurrentDateTime(); ! // if ( this.isTimeToStop( currentDateTime ) ) ! // { ! // // either the simulation has reached the ending date or ! // // too much time elapsed since the simulation started ! // this.stopTheScript( currentDateTime ); ! // } ! // } ! #endregion stopTheScript ! ! private void notifyProgress( ! Timer timer ) { string progressMessage = "Current out of sample date:" + ! this.timer.GetCurrentDateTime().ToString() + " - " + "First date:" + this.firstDateTime.ToString() + " - " + ! "Last date:" + this.lastDateTime.ToString() + " - " + ! "Real time:" + DateTime.Now; NewMessageEventArgs newMessageEventArgs = new NewMessageEventArgs( progressMessage ); *************** *** 313,357 **** this.NewMessage( this , newMessageEventArgs ); } ! private void marketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! EndOfDayDateTime currentEndOfDayDateTime = ! ( ( IEndOfDayTimer )sender ).GetCurrentTime(); ! DateTime currentDateTime = currentEndOfDayDateTime.DateTime; ! if ( this.isTimeToStop( currentDateTime ) ) ! { ! // either the simulation has reached the ending date or ! // too much time elapsed since the simulation started ! this.actualLastDateTime = ! ExtendedDateTime.Min( this.lastDateTime , currentDateTime ); ! this.endOfDayTimer.Stop(); ! this.realDateTimeWhenTheBackTestIsStopped = DateTime.Now; ! this.accountReport = this.account.CreateReport( ! "" , ! 1 , currentEndOfDayDateTime , this.benchmark.Ticker , ! this.historicalQuoteProvider ); ! this.accountReport.Name = this.Description; ! ! // this.progressBarForm.Close(); ! // ObjectArchiver.Archive( this.account , ! // @"C:\Documents and Settings\Glauco\Desktop\reports\final.qP" ); ! // this.saveLog( currentTime ); ! // this.showReport( sender ); ! // WFMultiOneRankReportDebugger wFMultiOneRankReportDebugger = ! // new WFMultiOneRankReportDebugger( this.numberOfPortfolioPositions , ! // this.numberDaysForInSampleOptimization , this.benchmark ); ! // report.TransactionGrid.MouseUp += ! // new MouseEventHandler( ! // wFMultiOneRankReportDebugger.MouseClickEventHandler ); ! } ! else ! // the simulation has not reached the ending date, yet ! this.marketCloseEventHandler_notifyProgress( ! ( IEndOfDayTimer )sender ); } ! #endregion marketCloseEventHandler private void newLogItemEventHandler( object sender , ! NewLogItemEventArgs eventArgs ) { // WFLagChosenPositions wFLagChosenPositions = --- 375,395 ---- this.NewMessage( this , newMessageEventArgs ); } ! private void newDateTimeEventHandler( ! Object sender , DateTime dateTime ) { ! // EndOfDayDateTime currentEndOfDayDateTime = ! // ( ( IEndOfDayTimer )sender ).GetCurrentTime(); ! // DateTime currentDateTime = currentEndOfDayDateTime.DateTime; ! if ( this.isTimeToStop( dateTime ) ) ! this.stopTheScript( dateTime ); ! else ! this.newDateTime( sender , dateTime ); ! if ( HistoricalEndOfDayTimer.IsMarketClose( dateTime ) ) ! this.notifyProgress( ( Timer )sender ); } ! #endregion newDateTimeEventHandler private void newLogItemEventHandler( object sender , ! NewLogItemEventArgs eventArgs ) { // WFLagChosenPositions wFLagChosenPositions = |