[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger/W
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-09-29 21:21:24
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger/WFLagDebugPositions In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv18316/WFLagDebugger/WFLagDebugPositions Modified Files: WFLagDebugPositions.cs WFLagDebugPositionsEndOfDayTimerHandler.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: WFLagDebugPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger/WFLagDebugPositions/WFLagDebugPositions.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** WFLagDebugPositions.cs 28 Oct 2007 19:16:07 -0000 1.6 --- WFLagDebugPositions.cs 29 Sep 2008 21:20:54 -0000 1.7 *************** *** 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; *************** *** 91,105 **** this.endOfDayTimer = new IndexBasedEndOfDayTimer( ! new EndOfDayDateTime( this.preSampleFirstDateTime , ! EndOfDaySpecificTime.MarketOpen ), this.benchmark ); } private void run_initializeAccount() { this.account = new Account( "WFLagDebugPositions" , ! this.endOfDayTimer , ! new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , ! this.historicalQuoteProvider ) , ! new HistoricalEndOfDayOrderExecutor( this.endOfDayTimer , ! this.historicalQuoteProvider ) ); } private void run_initializeEndOfDayTimerHandler() --- 91,107 ---- this.endOfDayTimer = new IndexBasedEndOfDayTimer( ! HistoricalEndOfDayTimer.GetMarketOpen( this.preSampleFirstDateTime ) , ! // new EndOfDayDateTime( this.preSampleFirstDateTime , ! // EndOfDaySpecificTime.MarketOpen ), ! this.benchmark ); } private void run_initializeAccount() { this.account = new Account( "WFLagDebugPositions" , ! this.endOfDayTimer , ! new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , ! this.historicalQuoteProvider ) , ! new HistoricalEndOfDayOrderExecutor( this.endOfDayTimer , ! this.historicalQuoteProvider ) ); } private void run_initializeEndOfDayTimerHandler() *************** *** 107,118 **** this.endOfDayTimerHandler = new WFLagDebugPositionsEndOfDayTimerHandler( ! this.account , this.wFLagWeightedPositions ); } ! public void marketOpenEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if ( this.account.Transactions.Count == 0 ) this.account.AddCash( 30000 ); } #region oneHourAfterMarketCloseEventHandler #region getEquityLineForSignedTickers --- 109,121 ---- this.endOfDayTimerHandler = new WFLagDebugPositionsEndOfDayTimerHandler( ! this.account , this.wFLagWeightedPositions ); } ! public void marketOpenEventHandler( ! Object sender , DateTime dateTime ) { if ( this.account.Transactions.Count == 0 ) this.account.AddCash( 30000 ); } + #region oneHourAfterMarketCloseEventHandler #region getEquityLineForSignedTickers *************** *** 124,128 **** /// <returns></returns> private Hashtable getVirtualQuantities( ICollection positions , ! DateTime dateTime ) { Hashtable virtualQuantities = new Hashtable(); --- 127,131 ---- /// <returns></returns> private Hashtable getVirtualQuantities( ICollection positions , ! DateTime dateTime ) { Hashtable virtualQuantities = new Hashtable(); *************** *** 133,141 **** SignedTicker signedTicker = new SignedTicker( stringForSignedTicker ); string ticker = signedTicker.Ticker; ! EndOfDayDateTime endOfDayDateTime = ! new EndOfDayDateTime( dateTime , EndOfDaySpecificTime.MarketClose ); double tickerQuote = this.historicalQuoteProvider.GetMarketValue( ! ticker , endOfDayDateTime ); double virtualQuantity = valueForEachPosition / tickerQuote; if ( signedTicker.IsShort ) --- 136,145 ---- SignedTicker signedTicker = new SignedTicker( stringForSignedTicker ); string ticker = signedTicker.Ticker; ! DateTime dateTimeOnClose = ! HistoricalEndOfDayTimer.GetMarketClose( dateTime ); ! // new EndOfDayDateTime( dateTime , EndOfDaySpecificTime.MarketClose ); double tickerQuote = this.historicalQuoteProvider.GetMarketValue( ! ticker , dateTimeOnClose ); double virtualQuantity = valueForEachPosition / tickerQuote; if ( signedTicker.IsShort ) *************** *** 151,155 **** cash / drivingTickerVirtualQuantities.Count; foreach ( double virtualQuantity in ! drivingTickerVirtualQuantities.Values ) { if ( virtualQuantity > 0 ) --- 155,159 ---- cash / drivingTickerVirtualQuantities.Count; foreach ( double virtualQuantity in ! drivingTickerVirtualQuantities.Values ) { if ( virtualQuantity > 0 ) *************** *** 163,175 **** } private double getPortfolioValueForSignedTickers( DateTime dateTime , ! Hashtable tickerVirtualQuantities ) { double portfolioValueForDrivingPositions = 0; foreach( string ticker in tickerVirtualQuantities.Keys ) { ! EndOfDayDateTime endOfDayDateTime = new EndOfDayDateTime( ! dateTime , EndOfDaySpecificTime.MarketClose ); double tickerQuote = this.historicalQuoteProvider.GetMarketValue( ! ticker , endOfDayDateTime ); double virtualQuantity = (double)tickerVirtualQuantities[ ticker ]; portfolioValueForDrivingPositions += virtualQuantity * tickerQuote; --- 167,182 ---- } private double getPortfolioValueForSignedTickers( DateTime dateTime , ! Hashtable tickerVirtualQuantities ) { double portfolioValueForDrivingPositions = 0; foreach( string ticker in tickerVirtualQuantities.Keys ) { ! DateTime dateTimeOnClose = ! HistoricalEndOfDayTimer.GetMarketClose( ! dateTime ); ! // new EndOfDayDateTime( ! // dateTime , EndOfDaySpecificTime.MarketClose ); double tickerQuote = this.historicalQuoteProvider.GetMarketValue( ! ticker , dateTimeOnClose ); double virtualQuantity = (double)tickerVirtualQuantities[ ticker ]; portfolioValueForDrivingPositions += virtualQuantity * tickerQuote; *************** *** 186,196 **** Hashtable virtualQuantities = this.getVirtualQuantities( signedTickers , ! (DateTime)equityLineForPortfolioPositions.GetKey( 0 ) ); ! double cash = this.getCash( virtualQuantities ); foreach( DateTime dateTime in ! equityLineForPortfolioPositions.Keys ) equityLineForSignedTickers.Add( dateTime , ! cash + this.getPortfolioValueForSignedTickers( dateTime , ! virtualQuantities ) ); return equityLineForSignedTickers; } --- 193,203 ---- Hashtable virtualQuantities = this.getVirtualQuantities( signedTickers , ! (DateTime)equityLineForPortfolioPositions.GetKey( 0 ) ); ! double cash = this.getCash( virtualQuantities ); foreach( DateTime dateTime in ! equityLineForPortfolioPositions.Keys ) equityLineForSignedTickers.Add( dateTime , ! cash + this.getPortfolioValueForSignedTickers( dateTime , ! virtualQuantities ) ); return equityLineForSignedTickers; } *************** *** 210,230 **** EquityLine equityLineForWeightedPositions = weightedPositions.GetVirtualEquityLine( ! 30000 , report.AccountReport.EquityLine ); report.AddEquityLine( equityLineForWeightedPositions , ! color ); } public void oneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! if ( ( ( IEndOfDayTimer )sender ).GetCurrentTime().DateTime > ! this.postSampleLastDateTime ) { // the simulation has reached the ending date ! this.account.EndOfDayTimer.Stop(); Report report = new Report( this.account , this.historicalQuoteProvider ); ! report.Create( "WFLag debug positions" , 1 , ! new EndOfDayDateTime( this.postSampleLastDateTime , ! EndOfDaySpecificTime.OneHourAfterMarketClose ) , this.benchmark , false ); // EquityLine equityLineForDrivingPositions = --- 217,240 ---- EquityLine equityLineForWeightedPositions = weightedPositions.GetVirtualEquityLine( ! 30000 , report.AccountReport.EquityLine ); report.AddEquityLine( equityLineForWeightedPositions , ! color ); } public void oneHourAfterMarketCloseEventHandler( ! Object sender , DateTime dateTime ) { ! if ( ( ( Timer )sender ).GetCurrentDateTime() > ! this.postSampleLastDateTime ) { // the simulation has reached the ending date ! this.account.Timer.Stop(); Report report = new Report( this.account , this.historicalQuoteProvider ); ! report.Create( ! "WFLag debug positions" , 1 , ! HistoricalEndOfDayTimer.GetOneHourAfterMarketClose( ! this.postSampleLastDateTime ) , ! // new EndOfDayDateTime( this.postSampleLastDateTime , ! // EndOfDaySpecificTime.OneHourAfterMarketClose ) , this.benchmark , false ); // EquityLine equityLineForDrivingPositions = *************** *** 246,249 **** --- 256,271 ---- } #endregion + + private void newDateTimeEventHandler( + Object sender , DateTime dateTime ) + { + if ( HistoricalEndOfDayTimer.IsMarketOpen( dateTime ) ) + this.marketOpenEventHandler( sender , dateTime ); + // if ( HistoricalEndOfDayTimer.IsMarketClose( dateTime ) ) + // this.marketCloseEventHandler( sender , dateTime ); + if ( HistoricalEndOfDayTimer.IsOneHourAfterMarketClose( dateTime ) ) + this.oneHourAfterMarketCloseEventHandler( sender , dateTime ); + } + public void Run() { *************** *** 252,263 **** run_initializeAccount(); run_initializeEndOfDayTimerHandler(); ! this.endOfDayTimer.MarketOpen += ! new MarketOpenEventHandler( this.marketOpenEventHandler ); ! this.endOfDayTimer.FiveMinutesBeforeMarketClose += ! new FiveMinutesBeforeMarketCloseEventHandler( ! this.endOfDayTimerHandler.FiveMinutesBeforeMarketCloseEventHandler ); ! this.endOfDayTimer.OneHourAfterMarketClose += ! new OneHourAfterMarketCloseEventHandler( ! this.oneHourAfterMarketCloseEventHandler ); this.endOfDayTimer.Start(); } --- 274,289 ---- run_initializeAccount(); run_initializeEndOfDayTimerHandler(); ! this.endOfDayTimer.NewDateTime += ! new NewDateTimeEventHandler( this.endOfDayTimerHandler.NewDateTimeEventHandler ); ! this.endOfDayTimer.NewDateTime += ! new NewDateTimeEventHandler( this.newDateTimeEventHandler ); ! // this.endOfDayTimer.MarketOpen += ! // new MarketOpenEventHandler( this.marketOpenEventHandler ); ! // this.endOfDayTimer.FiveMinutesBeforeMarketClose += ! // new FiveMinutesBeforeMarketCloseEventHandler( ! // this.endOfDayTimerHandler.FiveMinutesBeforeMarketCloseEventHandler ); ! // this.endOfDayTimer.OneHourAfterMarketClose += ! // new OneHourAfterMarketCloseEventHandler( ! // this.oneHourAfterMarketCloseEventHandler ); this.endOfDayTimer.Start(); } Index: WFLagDebugPositionsEndOfDayTimerHandler.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger/WFLagDebugPositions/WFLagDebugPositionsEndOfDayTimerHandler.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** WFLagDebugPositionsEndOfDayTimerHandler.cs 28 Oct 2007 19:16:48 -0000 1.3 --- WFLagDebugPositionsEndOfDayTimerHandler.cs 29 Sep 2008 21:20:54 -0000 1.4 *************** *** 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; *************** *** 35,39 **** /// Strategy to debug the log for the current positions /// </summary> ! public class WFLagDebugPositionsEndOfDayTimerHandler { private Account account; --- 35,39 ---- /// Strategy to debug the log for the current positions /// </summary> ! public class WFLagDebugPositionsEndOfDayTimerHandler : EndOfDayTimerHandler { private Account account; *************** *** 47,57 **** this.wFLagWeightedPositions = wFLagWeightedPositions; } ! #region FiveMinutesBeforeMarketCloseEventHandler private double todayTotalGainForLinearCombination() { double todayTotalGain = 0; ! DateTime today = this.account.EndOfDayTimer.GetCurrentTime().DateTime; foreach ( WeightedPosition weightedPosition in ! this.wFLagWeightedPositions.DrivingWeightedPositions.Values ) todayTotalGain += weightedPosition.GetCloseToCloseDailyReturn( today ); --- 47,58 ---- this.wFLagWeightedPositions = wFLagWeightedPositions; } ! ! #region marketCloseEventHandler private double todayTotalGainForLinearCombination() { double todayTotalGain = 0; ! DateTime today = this.account.Timer.GetCurrentDateTime(); foreach ( WeightedPosition weightedPosition in ! this.wFLagWeightedPositions.DrivingWeightedPositions.Values ) todayTotalGain += weightedPosition.GetCloseToCloseDailyReturn( today ); *************** *** 77,84 **** WeightedPosition weightedPosition = this.wFLagWeightedPositions.PortfolioWeightedPositions.GetWeightedPosition( ! firstAccountPosition.Instrument.Key ); bool isReversed = ( ( weightedPosition.IsLong && firstAccountPosition.IsShort ) || ! ( weightedPosition.IsShort && firstAccountPosition.IsLong ) ); // bool isReversed = // this.wFLagChosenPositions.PortfolioWeightedPositions.ContainsKey( --- 78,85 ---- WeightedPosition weightedPosition = this.wFLagWeightedPositions.PortfolioWeightedPositions.GetWeightedPosition( ! firstAccountPosition.Instrument.Key ); bool isReversed = ( ( weightedPosition.IsLong && firstAccountPosition.IsShort ) || ! ( weightedPosition.IsShort && firstAccountPosition.IsLong ) ); // bool isReversed = // this.wFLagChosenPositions.PortfolioWeightedPositions.ContainsKey( *************** *** 86,99 **** return isReversed; } ! private bool fiveMinutesBeforeMarketCloseEventHandler_arePositionsToBeClosed() { bool returnValue = ( this.account.Portfolio.Count > 0 ) && ( ( this.isDrivingPositionsTodayValueHigherThanYesterday() && ! this.isCurrentlyReversed() ) || ! ( !this.isDrivingPositionsTodayValueHigherThanYesterday() && ! !this.isCurrentlyReversed() ) ); return returnValue; } ! private void fiveMinutesBeforeMarketCloseEventHandler_closePositions() { ArrayList tickers = new ArrayList(); --- 87,100 ---- return isReversed; } ! private bool marketCloseEventHandler_arePositionsToBeClosed() { bool returnValue = ( this.account.Portfolio.Count > 0 ) && ( ( this.isDrivingPositionsTodayValueHigherThanYesterday() && ! this.isCurrentlyReversed() ) || ! ( !this.isDrivingPositionsTodayValueHigherThanYesterday() && ! !this.isCurrentlyReversed() ) ); return returnValue; } ! private void marketCloseEventHandler_closePositions() { ArrayList tickers = new ArrayList(); *************** *** 103,107 **** this.account.ClosePosition( ticker ); } ! private bool fiveMinutesBeforeMarketCloseEventHandler_arePositionsToBeOpened() { bool returnValue = ( this.account.Portfolio.Count == 0 ); --- 104,108 ---- this.account.ClosePosition( ticker ); } ! private bool marketCloseEventHandler_arePositionsToBeOpened() { bool returnValue = ( this.account.Portfolio.Count == 0 ); *************** *** 127,168 **** this.account.AddOrder( order ); } ! private void fiveMinutesBeforeMarketCloseEventHandler_openThisPosition( WeightedPosition weightedPosition ) { this.openPosition( weightedPosition ); } ! private void fiveMinutesBeforeMarketCloseEventHandler_openOppositePosition( WeightedPosition weightedPosition ) { ! this.fiveMinutesBeforeMarketCloseEventHandler_openThisPosition( weightedPosition.GetOppositeWeightedPosition() ); } ! private void fiveMinutesBeforeMarketCloseEventHandler_openPosition( WeightedPosition weightedPosition ) { if ( this.isDrivingPositionsTodayValueHigherThanYesterday() ) ! fiveMinutesBeforeMarketCloseEventHandler_openThisPosition( weightedPosition ); else ! fiveMinutesBeforeMarketCloseEventHandler_openOppositePosition( weightedPosition ); } ! private void fiveMinutesBeforeMarketCloseEventHandler_openPositions() { // this.chosenTickers.SetTickers( this.bestPerformingTickers , this.account ); foreach ( WeightedPosition weightedPosition in ! this.wFLagWeightedPositions.PortfolioWeightedPositions.Values ) ! this.fiveMinutesBeforeMarketCloseEventHandler_openPosition( weightedPosition ); } ! public void FiveMinutesBeforeMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! if ( this.fiveMinutesBeforeMarketCloseEventHandler_arePositionsToBeClosed() ) ! this.fiveMinutesBeforeMarketCloseEventHandler_closePositions(); ! if ( this.fiveMinutesBeforeMarketCloseEventHandler_arePositionsToBeOpened() ) ! fiveMinutesBeforeMarketCloseEventHandler_openPositions(); } #endregion } } --- 128,181 ---- this.account.AddOrder( order ); } ! private void marketCloseEventHandler_openThisPosition( WeightedPosition weightedPosition ) { this.openPosition( weightedPosition ); } ! private void marketCloseEventHandler_openOppositePosition( WeightedPosition weightedPosition ) { ! this.marketCloseEventHandler_openThisPosition( weightedPosition.GetOppositeWeightedPosition() ); } ! private void marketCloseEventHandler_openPosition( WeightedPosition weightedPosition ) { if ( this.isDrivingPositionsTodayValueHigherThanYesterday() ) ! this.marketCloseEventHandler_openThisPosition( weightedPosition ); else ! this.marketCloseEventHandler_openOppositePosition( weightedPosition ); } ! private void marketCloseEventHandler_openPositions() { // this.chosenTickers.SetTickers( this.bestPerformingTickers , this.account ); foreach ( WeightedPosition weightedPosition in ! this.wFLagWeightedPositions.PortfolioWeightedPositions.Values ) ! this.marketCloseEventHandler_openPosition( weightedPosition ); } ! protected override void marketCloseEventHandler( ! Object sender , DateTime dateTime ) { ! if ( this.marketCloseEventHandler_arePositionsToBeClosed() ) ! this.marketCloseEventHandler_closePositions(); ! if ( this.marketCloseEventHandler_arePositionsToBeOpened() ) ! this.marketCloseEventHandler_openPositions(); } #endregion + + protected override void marketOpenEventHandler( + Object sender , DateTime dateTime ) + { + ; + } + + protected override void oneHourAfterMarketCloseEventHandler( + Object sender , DateTime dateTime ) + { + ; + } } } |