[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/WalkForwardT
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-09-29 21:20:42
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/WalkForwardTest In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv18211/WalkForwardTest Modified Files: RunWalkForwardLinearCombination.cs WalkForwardOpenToCloseDailyStrategy.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: WalkForwardOpenToCloseDailyStrategy.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/WalkForwardTest/WalkForwardOpenToCloseDailyStrategy.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** WalkForwardOpenToCloseDailyStrategy.cs 12 Mar 2008 22:09:59 -0000 1.8 --- WalkForwardOpenToCloseDailyStrategy.cs 29 Sep 2008 21:20:25 -0000 1.9 *************** *** 3,7 **** WalkForwardOpenToCloseDailyStrategy.cs ! Copyright (C) 2003 Glauco Siliprandi --- 3,7 ---- WalkForwardOpenToCloseDailyStrategy.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; *************** *** 40,46 **** /// </summary> [Serializable] ! public class WalkForwardOpenToCloseDailyStrategy : IEndOfDayStrategy { ! private Account account; private string tickerGroupID; private int numDaysForInSampleOptimization; --- 40,46 ---- /// </summary> [Serializable] ! public class WalkForwardOpenToCloseDailyStrategy : EndOfDayStrategy { ! // private Account account; private string tickerGroupID; private int numDaysForInSampleOptimization; *************** *** 58,66 **** private OptimizationOutput optimizationOutput; ! public Account Account ! { ! get { return this.account; } ! set { this.account = value; } ! } /// best genomes, one for each optimization process --- 58,66 ---- private OptimizationOutput optimizationOutput; ! // public Account Account ! // { ! // get { return this.account; } ! // set { this.account = value; } ! // } /// best genomes, one for each optimization process *************** *** 71,80 **** public WalkForwardOpenToCloseDailyStrategy( Account account , ! string tickerGroupID , int numDaysForInSampleOptimization , ! int numberOfEligibleTickers , ! int numberOfTickersToBeChosen , string benchmark , ! double targetReturn , PortfolioType portfolioType , ! int populationSizeForGeneticOptimizer , ! int generationNumberForGeneticOptimizer ) { this.account = account; --- 71,80 ---- public WalkForwardOpenToCloseDailyStrategy( Account account , ! string tickerGroupID , int numDaysForInSampleOptimization , ! int numberOfEligibleTickers , ! int numberOfTickersToBeChosen , string benchmark , ! double targetReturn , PortfolioType portfolioType , ! int populationSizeForGeneticOptimizer , ! int generationNumberForGeneticOptimizer ) { this.account = account; *************** *** 107,111 **** long quantity = marketOpenEventHandler_addOrder_getQuantity( ticker ); Order order = new Order( orderType , new Instrument( ticker ) , ! quantity ); this.account.AddOrder( order ); } --- 107,111 ---- long quantity = marketOpenEventHandler_addOrder_getQuantity( ticker ); Order order = new Order( orderType , new Instrument( ticker ) , ! quantity ); this.account.AddOrder( order ); } *************** *** 114,129 **** if ( this.signedTickersFromLastOptimization != null ) foreach ( string signedTicker in this.signedTickersFromLastOptimization ) ! marketOpenEventHandler_addOrder( signedTicker ); } ! public void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if ( ( this.account.CashAmount == 0 ) && ! ( this.account.Transactions.Count == 0 ) ) // cash has not been added yet this.account.AddCash( 30000 ); marketOpenEventHandler_addOrders(); } ! private void fiveMinutesBeforeMarketCloseEventHandler_closePositions() { ArrayList tickers = new ArrayList(); --- 114,129 ---- if ( this.signedTickersFromLastOptimization != null ) foreach ( string signedTicker in this.signedTickersFromLastOptimization ) ! marketOpenEventHandler_addOrder( signedTicker ); } ! protected override void marketOpenEventHandler( ! Object sender , DateTime dateTime ) { if ( ( this.account.CashAmount == 0 ) && ! ( this.account.Transactions.Count == 0 ) ) // cash has not been added yet this.account.AddCash( 30000 ); marketOpenEventHandler_addOrders(); } ! private void marketCloseEventHandler_closePositions() { ArrayList tickers = new ArrayList(); *************** *** 133,146 **** this.account.ClosePosition( ticker ); } ! public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! this.fiveMinutesBeforeMarketCloseEventHandler_closePositions(); } - public void MarketCloseEventHandler( Object sender , - EndOfDayTimingEventArgs endOfDayTimingEventArgs) - { - } protected DataTable getSetOfTickersToBeOptimized( DateTime optimizationFirstDate , DateTime optimizationLastDate ) --- 133,142 ---- this.account.ClosePosition( ticker ); } ! protected override void marketCloseEventHandler( Object sender , ! DateTime dateTime) { ! this.marketCloseEventHandler_closePositions(); } protected DataTable getSetOfTickersToBeOptimized( DateTime optimizationFirstDate , DateTime optimizationLastDate ) *************** *** 150,167 **** optimizationFirstDate , optimizationLastDate , this.numberOfEligibleTickers ); ! DataTable eligibleTickers = mostLiquid.GetTableOfSelectedTickers(); // SelectorByGroup selectorByGroup = // new SelectorByGroup( "SP500" , optimizationLastDate ); // DataTable eligibleTickers = selectorByGroup.GetTableOfSelectedTickers(); ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromEligible = new SelectorByQuotationAtEachMarketDay( eligibleTickers, ! false , optimizationFirstDate , ! optimizationLastDate , ! this.numberOfEligibleTickers, this.benchmark); return quotedAtEachMarketDayFromEligible.GetTableOfSelectedTickers(); } private void newGenerationEventHandler( object sender , ! NewGenerationEventArgs newGenerationEventArgs ) { Console.WriteLine( --- 146,163 ---- optimizationFirstDate , optimizationLastDate , this.numberOfEligibleTickers ); ! DataTable eligibleTickers = mostLiquid.GetTableOfSelectedTickers(); // SelectorByGroup selectorByGroup = // new SelectorByGroup( "SP500" , optimizationLastDate ); // DataTable eligibleTickers = selectorByGroup.GetTableOfSelectedTickers(); ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromEligible = new SelectorByQuotationAtEachMarketDay( eligibleTickers, ! false , optimizationFirstDate , ! optimizationLastDate , ! this.numberOfEligibleTickers, this.benchmark); return quotedAtEachMarketDayFromEligible.GetTableOfSelectedTickers(); } private void newGenerationEventHandler( object sender , ! NewGenerationEventArgs newGenerationEventArgs ) { Console.WriteLine( *************** *** 171,183 **** } private void addGenomeToBestGenomes( Genome genome, ! DateTime firstOptimizationDate , ! DateTime lastOptimizationDate ) { if( this.optimizationOutput == null) this.optimizationOutput = new OptimizationOutput(); ! this.optimizationOutput.Add( new GenomeRepresentation( genome , ! firstOptimizationDate , ! lastOptimizationDate ) ); } private void oneHourAfterMarketCloseEventHandler_set_signedTickersFromLastOptimization( --- 167,179 ---- } private void addGenomeToBestGenomes( Genome genome, ! DateTime firstOptimizationDate , ! DateTime lastOptimizationDate ) { if( this.optimizationOutput == null) this.optimizationOutput = new OptimizationOutput(); ! this.optimizationOutput.Add( new GenomeRepresentation( genome , ! firstOptimizationDate , ! lastOptimizationDate ) ); } private void oneHourAfterMarketCloseEventHandler_set_signedTickersFromLastOptimization( *************** *** 192,211 **** DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized( optimizationFirstDate , ! optimizationLastDate ); Console.WriteLine( "Number of tickers to be optimized: " + ! setOfTickersToBeOptimized.Rows.Count.ToString() ); ! GenomeManagerForEfficientOTCPortfolio genManEfficientOTCPortfolio = new GenomeManagerForEfficientOTCPortfolio( ! setOfTickersToBeOptimized , ! currentDate.AddDays( -this.numDaysForInSampleOptimization + 1 ) , ! currentDate , ! this.numberOfTickersToBeChosen , ! this.targetReturn , ! this.portfolioType, ! this.benchmark); ! this.geneticOptimizer = new GeneticOptimizer(genManEfficientOTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer); this.geneticOptimizer.NewGeneration += new NewGenerationEventHandler( this.newGenerationEventHandler ); --- 188,207 ---- DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized( optimizationFirstDate , ! optimizationLastDate ); Console.WriteLine( "Number of tickers to be optimized: " + ! setOfTickersToBeOptimized.Rows.Count.ToString() ); ! GenomeManagerForEfficientOTCPortfolio genManEfficientOTCPortfolio = new GenomeManagerForEfficientOTCPortfolio( ! setOfTickersToBeOptimized , ! currentDate.AddDays( -this.numDaysForInSampleOptimization + 1 ) , ! currentDate , ! this.numberOfTickersToBeChosen , ! this.targetReturn , ! this.portfolioType, ! this.benchmark); ! this.geneticOptimizer = new GeneticOptimizer(genManEfficientOTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer); this.geneticOptimizer.NewGeneration += new NewGenerationEventHandler( this.newGenerationEventHandler ); *************** *** 220,239 **** ((GenomeMeaning)this.geneticOptimizer.BestGenome.Meaning).Tickers; this.addGenomeToBestGenomes( geneticOptimizer.BestGenome , ! optimizationFirstDate , optimizationLastDate ); } ! public void OneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! if ( endOfDayTimingEventArgs.EndOfDayDateTime.DateTime.DayOfWeek == ! DayOfWeek.Friday ) // current day is Friday { Console.WriteLine( ! endOfDayTimingEventArgs.EndOfDayDateTime.DateTime.ToString() + " - " + DateTime.Now.ToString() ); oneHourAfterMarketCloseEventHandler_set_signedTickersFromLastOptimization( ! endOfDayTimingEventArgs.EndOfDayDateTime.DateTime ); } } } } --- 216,246 ---- ((GenomeMeaning)this.geneticOptimizer.BestGenome.Meaning).Tickers; this.addGenomeToBestGenomes( geneticOptimizer.BestGenome , ! optimizationFirstDate , optimizationLastDate ); } ! protected override void oneHourAfterMarketCloseEventHandler( Object sender , ! DateTime dateTime) { ! if ( dateTime.DayOfWeek == ! DayOfWeek.Friday ) // current day is Friday { Console.WriteLine( ! dateTime.ToString() + " - " + DateTime.Now.ToString() ); oneHourAfterMarketCloseEventHandler_set_signedTickersFromLastOptimization( ! dateTime ); } } + + // public virtual 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 ); + // } } } Index: RunWalkForwardLinearCombination.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/WalkForwardTest/RunWalkForwardLinearCombination.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RunWalkForwardLinearCombination.cs 27 Dec 2005 15:43:14 -0000 1.4 --- RunWalkForwardLinearCombination.cs 29 Sep 2008 21:20:24 -0000 1.5 *************** *** 3,7 **** RunWalkForwardLinearCombination.cs ! Copyright (C) 2003 Glauco Siliprandi --- 3,7 ---- RunWalkForwardLinearCombination.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; *************** *** 58,62 **** private DateTime deadlineForScript; ! private IHistoricalQuoteProvider historicalQuoteProvider; private HistoricalEndOfDayTimer historicalEndOfDayTimer; private Account account; --- 58,62 ---- private DateTime deadlineForScript; ! private HistoricalMarketValueProvider historicalMarketValueProvider; private HistoricalEndOfDayTimer historicalEndOfDayTimer; private Account account; *************** *** 64,76 **** public RunWalkForwardLinearCombination(string tickerGroupID, ! int numDaysForInSampleOptimization , ! int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForLiquidity, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, string benchmark, ! DateTime firstDate, DateTime lastDate, double targetReturn, ! PortfolioType portfolioType , ! bool openToCloseDaily , ! double maxRunningHours ) { this.tickerGroupID = tickerGroupID; --- 64,76 ---- public RunWalkForwardLinearCombination(string tickerGroupID, ! int numDaysForInSampleOptimization , ! int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForLiquidity, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, string benchmark, ! DateTime firstDate, DateTime lastDate, double targetReturn, ! PortfolioType portfolioType , ! bool openToCloseDaily , ! double maxRunningHours ) { this.tickerGroupID = tickerGroupID; *************** *** 93,102 **** private void oneHourAfterMarketCloseEventHandler( Object sender , ! EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! if ( ( this.account.EndOfDayTimer.GetCurrentTime().DateTime >= ! this.lastDate ) || ! ( DateTime.Now > this.deadlineForScript ) ) ! this.account.EndOfDayTimer.Stop(); } --- 93,102 ---- private void oneHourAfterMarketCloseEventHandler( Object sender , ! DateTime dateTime ) { ! if ( ( this.account.Timer.GetCurrentDateTime() >= ! this.lastDate ) || ! ( DateTime.Now > this.deadlineForScript ) ) ! this.account.Timer.Stop(); } *************** *** 104,110 **** { if ( this.openToCloseDaily ) ! this.historicalQuoteProvider = new HistoricalRawQuoteProvider(); else ! this.historicalQuoteProvider = new HistoricalAdjustedQuoteProvider(); } private void run_setStrategy() --- 104,110 ---- { if ( this.openToCloseDaily ) ! this.historicalMarketValueProvider = new HistoricalRawQuoteProvider(); else ! this.historicalMarketValueProvider = new HistoricalAdjustedQuoteProvider(); } private void run_setStrategy() *************** *** 113,123 **** this.endOfDayStrategy = new WalkForwardOpenToCloseDailyStrategy( ! this.account , this.tickerGroupID , ! this.numDaysForInSampleOptimization , ! this.numberOfEligibleTickers , ! this.numberOfTickersToBeChosen , ! this.benchmark , this.targetReturn , this.portfolioType , ! this.populationSizeForGeneticOptimizer , ! this.generationNumberForGeneticOptimizer ); // else // this.endOfDayStrategy = new OpenToCloseWeeklyStrategy( --- 113,123 ---- this.endOfDayStrategy = new WalkForwardOpenToCloseDailyStrategy( ! this.account , this.tickerGroupID , ! this.numDaysForInSampleOptimization , ! this.numberOfEligibleTickers , ! this.numberOfTickersToBeChosen , ! this.benchmark , this.targetReturn , this.portfolioType , ! this.populationSizeForGeneticOptimizer , ! this.generationNumberForGeneticOptimizer ); // else // this.endOfDayStrategy = new OpenToCloseWeeklyStrategy( *************** *** 128,159 **** this.historicalEndOfDayTimer = new IndexBasedEndOfDayTimer( ! new EndOfDayDateTime( this.firstDate , ! EndOfDaySpecificTime.MarketOpen ) , "MSFT" ); run_setHistoricalQuoteProvider(); this.account = new Account( "LinearCombination" , historicalEndOfDayTimer , ! new HistoricalEndOfDayDataStreamer( historicalEndOfDayTimer , ! this.historicalQuoteProvider ) , ! new HistoricalEndOfDayOrderExecutor( historicalEndOfDayTimer , ! this.historicalQuoteProvider ) ); run_setStrategy(); // OneRank oneRank = new OneRank( account , // this.endDateTime ); ! this.historicalEndOfDayTimer.MarketOpen += ! new MarketOpenEventHandler( ! this.endOfDayStrategy.MarketOpenEventHandler ); ! this.historicalEndOfDayTimer.FiveMinutesBeforeMarketClose += ! new FiveMinutesBeforeMarketCloseEventHandler( ! this.endOfDayStrategy.FiveMinutesBeforeMarketCloseEventHandler ); ! this.historicalEndOfDayTimer.OneHourAfterMarketClose += ! new OneHourAfterMarketCloseEventHandler( ! this.endOfDayStrategy.OneHourAfterMarketCloseEventHandler ); ! this.historicalEndOfDayTimer.OneHourAfterMarketClose += ! new OneHourAfterMarketCloseEventHandler( ! this.oneHourAfterMarketCloseEventHandler ); ! this.account.EndOfDayTimer.Start(); ! Report report = new Report( this.account , this.historicalQuoteProvider ); ! report.Create( "Linear Combination" , 1 , ! new EndOfDayDateTime( this.lastDate , EndOfDaySpecificTime.MarketClose ) , "^SPX" ); // ObjectArchiver.Archive( report.AccountReport , --- 128,165 ---- this.historicalEndOfDayTimer = new IndexBasedEndOfDayTimer( ! HistoricalEndOfDayTimer.GetMarketOpen( this.firstDate ) , ! // new EndOfDayDateTime( this.firstDate , ! // EndOfDaySpecificTime.MarketOpen ) , ! "MSFT" ); run_setHistoricalQuoteProvider(); this.account = new Account( "LinearCombination" , historicalEndOfDayTimer , ! new HistoricalEndOfDayDataStreamer( historicalEndOfDayTimer , ! this.historicalMarketValueProvider ) , ! new HistoricalEndOfDayOrderExecutor( historicalEndOfDayTimer , ! this.historicalMarketValueProvider ) ); run_setStrategy(); // OneRank oneRank = new OneRank( account , // this.endDateTime ); ! this.historicalEndOfDayTimer.NewDateTime += ! new NewDateTimeEventHandler( this.endOfDayStrategy.NewDateTimeEventHandler ); ! // this.historicalEndOfDayTimer.MarketOpen += ! // new MarketOpenEventHandler( ! // this.endOfDayStrategy.MarketOpenEventHandler ); ! // this.historicalEndOfDayTimer.FiveMinutesBeforeMarketClose += ! // new FiveMinutesBeforeMarketCloseEventHandler( ! // this.endOfDayStrategy.FiveMinutesBeforeMarketCloseEventHandler ); ! // this.historicalEndOfDayTimer.OneHourAfterMarketClose += ! // new OneHourAfterMarketCloseEventHandler( ! // this.endOfDayStrategy.OneHourAfterMarketCloseEventHandler ); ! // this.historicalEndOfDayTimer.OneHourAfterMarketClose += ! // new OneHourAfterMarketCloseEventHandler( ! // this.oneHourAfterMarketCloseEventHandler ); ! this.account.Timer.Start(); ! Report report = new Report( this.account , this.historicalMarketValueProvider ); ! report.Create( ! "Linear Combination" , 1 , ! HistoricalEndOfDayTimer.GetMarketClose( this.lastDate ) , ! // new EndOfDayDateTime( this.lastDate , EndOfDaySpecificTime.MarketClose ) , "^SPX" ); // ObjectArchiver.Archive( report.AccountReport , *************** *** 173,177 **** new VisualObjectArchiver(); visualObjectArchiver.Save( this.endOfDayStrategy.OptimizationOutput , ! "bgn" , title ); } } --- 179,183 ---- new VisualObjectArchiver(); visualObjectArchiver.Save( this.endOfDayStrategy.OptimizationOutput , ! "bgn" , title ); } } |