[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/FixedLengthTwoPhases FixedLen
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-08-16 19:27:02
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/FixedLengthTwoPhases In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv21126/b7_Scripts/WalkForwardTesting/FixedLengthTwoPhases Modified Files: FixedLengthTwoPhasesMain.cs Log Message: Not the class extends BasicScriptForBacktesting (all changes have been applied for this goal) Index: FixedLengthTwoPhasesMain.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/FixedLengthTwoPhases/FixedLengthTwoPhasesMain.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** FixedLengthTwoPhasesMain.cs 11 May 2008 19:00:20 -0000 1.8 --- FixedLengthTwoPhasesMain.cs 16 Aug 2008 19:26:57 -0000 1.9 *************** *** 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; *************** *** 38,41 **** --- 38,42 ---- using QuantProject.Business.Timing; using QuantProject.Presentation; + using QuantProject.Scripts.General; using QuantProject.Scripts.General.Logging; using QuantProject.Scripts.General.Reporting; *************** *** 49,138 **** /// be done /// </summary> ! public class FixedLengthTwoPhasesMain { public FixedLengthTwoPhasesMain() { } ! #region Run ! private MessageManager setMessageManager( ! IEligiblesSelector eligiblesSelector , ! IInSampleChooser inSampleChooser , ! IEndOfDayStrategy endOfDayStrategy , ! EndOfDayStrategyBackTester endOfDayStrategyBackTester ) ! { ! MessageManager messageManager = ! new MessageManager( "FixedLengthUpDown.Txt" ); ! messageManager.Monitor( eligiblesSelector ); ! messageManager.Monitor( inSampleChooser ); ! // messageManager.Monitor( endOfDayStrategy ); ! messageManager.Monitor( endOfDayStrategyBackTester ); ! return messageManager; ! } ! ! // TO DO check if you can add this to QuantProject.Presentation.Reporting.WindowsForm.Report ! // as a public method or as a new constructor ! // private void showReport( ! // DateTime lastDateTimeRequestedForTheScript , ! // EndOfDayStrategyBackTester endOfDayStrategyBackTester ) ! // { ! //// DateTime lastReportDateTime = ExtendedDateTime.Min( ! //// lastDateTimeRequestedForTheScript , ! //// endOfDayStrategyBackTester.EndOfDayTimer.GetCurrentTime().DateTime ); ! // DateTime lastReportDateTime = ! // endOfDayStrategyBackTester.ActualLastDateTime; ! // Report report = new Report( ! // endOfDayStrategyBackTester.AccountReport , ! // true ); ! // report.Create( endOfDayStrategyBackTester.DescriptionForLogFileName , 1 , ! // new EndOfDayDateTime( lastReportDateTime , ! // EndOfDaySpecificTime.OneHourAfterMarketClose ) , ! // endOfDayStrategyBackTester.Benchmark.Ticker ); ! // report.Show(); ! // } ! private void saveLog( BackTestLog backTestLog , ! string suggestedLogFileName ) { ! string defaultFolderPath = ! "C:\\qpReports\\"; ! // this.wFLagLog.TransactionHistory = this.account.Transactions; ! LogArchiver.Save( backTestLog , ! suggestedLogFileName , defaultFolderPath ); ! } ! public void Run1() ! { ! BackTestLog backTestLog = LogArchiver.Load( "C:\\qpReports\\" ); ! LogViewer logViewer = ! new LogViewer( backTestLog ); ! logViewer.Show(); } ! ! public void Run() { ! string backTestId = "WFFLTP"; ! double cashToStart = 30000; ! ! // int numberOfPortfolioPositions = 2; ! // int inSampleDays = 90; ! // string tickersGroupId = "SP500"; ! // uncomment the following three lines for faster scripts ! int numberOfPortfolioPositions = 2; ! int inSampleDays = 30; ! string tickersGroupId = "fastTest"; ! ! Benchmark benchmark = new Benchmark( "MSFT" ); ! int maxNumberOfEligiblesToBeChosen = 100; ! IDecoderForWeightedPositions decoderForWeightedPositions ! = new DecoderForBalancedWeightedPositions(); ! IHistoricalQuoteProvider historicalQuoteProvider = ! new HistoricalAdjustedQuoteProvider(); // definition for the Fitness Evaluator ! IEquityEvaluator equityEvaluator = new SharpeRatio(); IFitnessEvaluator fitnessEvaluator = ! new FixedLengthTwoPhasesFitnessEvaluator( equityEvaluator ); // parameters for the genetic optimizer --- 50,124 ---- /// be done /// </summary> ! public class FixedLengthTwoPhasesMain : BasicScriptForBacktesting { + private Benchmark benchmark; + private int numberOfPortfolioPositions; + private IHistoricalQuoteProvider historicalQuoteProviderForInSample; + private IHistoricalQuoteProvider + historicalQuoteProviderForTheBacktesterAccount; + public FixedLengthTwoPhasesMain() { + this.benchmark = new Benchmark( "CCE" ); + this.numberOfPortfolioPositions = 1; + this.historicalQuoteProviderForInSample = + new HistoricalRawQuoteProvider(); + this.historicalQuoteProviderForTheBacktesterAccount = + new HistoricalRawQuoteProvider(); } ! ! protected override IEligiblesSelector getEligiblesSelector() { ! string tickersGroupId = "SP500"; ! // uncomment the following line for a faster script ! tickersGroupId = "fastTest"; ! ! int maxNumberOfEligibleTickersToBeChosen = 100; ! int maxNumberOfMostLiquidTickersToBeChosen = ! maxNumberOfEligibleTickersToBeChosen + 50; ! int numOfDaysForAverageOpenRawPriceComputation = 10; ! int numOfDaysForVolatilityComputation = 10; ! double minPrice = 20; ! double maxPrice = 75; + IEligiblesSelector eligiblesSelector = + new ByPriceMostLiquidAlwaysQuoted( + tickersGroupId , + true , + maxNumberOfEligibleTickersToBeChosen , + numOfDaysForAverageOpenRawPriceComputation , + minPrice , + maxPrice ); + eligiblesSelector = + new ByPriceMostLiquidLessVolatileOTCAlwaysQuoted( + tickersGroupId , + true , + maxNumberOfEligibleTickersToBeChosen , + maxNumberOfMostLiquidTickersToBeChosen , + numOfDaysForAverageOpenRawPriceComputation , + numOfDaysForVolatilityComputation , + minPrice , + maxPrice ); + // uncomment the following line for a (logbased) log based in sample chooser + // eligiblesSelector = new DummyEligibleSelector(); ! return eligiblesSelector; } ! ! protected override IInSampleChooser getInSampleChooser() { ! int numberOfBestTestingPositionsToBeReturned = 20; ! // uncomment the following line for a faster script ! numberOfBestTestingPositionsToBeReturned = 2; ! IDecoderForTestingPositions decoderForWeightedPositions = ! new DecoderForFLTPTestingPositionsWithBalancedWeights(); // definition for the Fitness Evaluator ! IEquityEvaluator equityEvaluator = new SharpeRatio(); IFitnessEvaluator fitnessEvaluator = ! new FixedLengthTwoPhasesFitnessEvaluator( ! equityEvaluator ); // parameters for the genetic optimizer *************** *** 140,204 **** double mutationRate = 0.02; double elitismRate = 0.001; ! int populationSizeForGeneticOptimizer = 3000; ! int generationNumberForGeneticOptimizer = 5; int seedForRandomGenerator = QuantProject.ADT.ConstantsProvider.SeedForRandomGenerator; IInSampleChooser inSampleChooser = new FixedLengthTwoPhasesGeneticChooser( ! numberOfPortfolioPositions , inSampleDays , benchmark , ! decoderForWeightedPositions , fitnessEvaluator , ! historicalQuoteProvider , ! crossoverRate , mutationRate , elitismRate , ! populationSizeForGeneticOptimizer , generationNumberForGeneticOptimizer , ! seedForRandomGenerator ); IIntervalsSelector intervalsSelector = new FixedLengthTwoPhasesIntervalsSelector( 1 , 1 , benchmark ); ! // IEligiblesSelector eligiblesSelector = ! // new MostLiquidAndLessVolatile( ! // tickersGroupId , maxNumberOfEligiblesToBeChosen ); ! IEligiblesSelector eligiblesSelector = ! new ByPriceMostLiquidLessVolatileOTCAlwaysQuoted( ! tickersGroupId , ! true , ! maxNumberOfEligiblesToBeChosen , ! maxNumberOfEligiblesToBeChosen + 50 , ! 10 , 10 , 20 , 75 ); ! FixedLengthTwoPhasesStrategy fixedLengthTwoPhasesStrategy = new FixedLengthTwoPhasesStrategy( ! numberOfPortfolioPositions , ! 7 , 90 , benchmark , intervalsSelector , ! eligiblesSelector , inSampleChooser , historicalQuoteProvider ); ! DateTime firstDateTime = new DateTime( 2001 , 1 , 2 ); ! DateTime lastDateTime = new DateTime( 2001 , 1 , 6 ); ! double maxRunningHours = 7; EndOfDayStrategyBackTester endOfDayStrategyBackTester = new EndOfDayStrategyBackTester( ! backTestId , fixedLengthTwoPhasesStrategy , ! historicalQuoteProvider , ! new SimpleAccountProvider(), firstDateTime , ! lastDateTime , benchmark , cashToStart , maxRunningHours ); ! ! // TO DO check if you can do this assign in the EndOfDayStrategyBackTester ! // constructor ! fixedLengthTwoPhasesStrategy.Account = endOfDayStrategyBackTester.Account; ! ! MessageManager messageManager = this.setMessageManager( ! eligiblesSelector , inSampleChooser , ! fixedLengthTwoPhasesStrategy , endOfDayStrategyBackTester ); ! endOfDayStrategyBackTester.Run(); ! BackTesterReportViewer.ShowReport( lastDateTime , ! endOfDayStrategyBackTester ); ! this.saveLog( ! endOfDayStrategyBackTester.Log , ! endOfDayStrategyBackTester.Description ); } ! #endregion Run } } --- 126,242 ---- double mutationRate = 0.02; double elitismRate = 0.001; ! int populationSizeForGeneticOptimizer = 10000; ! int generationNumberForGeneticOptimizer = 1; int seedForRandomGenerator = QuantProject.ADT.ConstantsProvider.SeedForRandomGenerator; IInSampleChooser inSampleChooser = new FixedLengthTwoPhasesGeneticChooser( ! this.numberOfPortfolioPositions , ! numberOfBestTestingPositionsToBeReturned , ! benchmark , ! decoderForWeightedPositions , fitnessEvaluator , ! this.historicalQuoteProviderForInSample , ! crossoverRate , mutationRate , elitismRate , ! populationSizeForGeneticOptimizer , generationNumberForGeneticOptimizer , ! seedForRandomGenerator ); ! // inSampleChooser = ! // new PairsTradingBruteForceChooser( ! // numberOfBestTestingPositionsToBeReturned , ! // decoderForWeightedPositions , ! // fitnessEvaluator ); + // uncomment the following line for a (logbased) log based in sample chooser + // inSampleChooser = + // new PairsTradingChooserFromSavedBackTestLog( + // @"C:\qpReports\pairsTrading\2008_05_08_23_49_18_pairsTrdng_from_2005_01_01_to_2008_04_30_annlRtrn_90.70_maxDD_5.43\2008_05_08_23_49_18_pairsTrdng_from_2005_01_01_to_2008_04_30_annlRtrn_90.70_maxDD_5.43.qpL", + // numberOfBestTestingPositionsToBeReturned); + + return inSampleChooser; + } + + protected override IEndOfDayStrategyForBacktester getEndOfDayStrategy() + { + // int numberOfPortfolioPositions = 2; + int numDaysForInSampleOptimization = 180; + // uncomment the following line for a faster script + numDaysForInSampleOptimization = 5; + numDaysForInSampleOptimization = 45; + + int numDaysBetweenEachOtpimization = 3; + IIntervalsSelector intervalsSelector = new FixedLengthTwoPhasesIntervalsSelector( 1 , 1 , benchmark ); ! RankBasedOutOfSampleChooser outOfSampleChooser = ! new RankBasedOutOfSampleChooser( 0 ); FixedLengthTwoPhasesStrategy fixedLengthTwoPhasesStrategy = new FixedLengthTwoPhasesStrategy( ! this.numberOfPortfolioPositions , ! numDaysBetweenEachOtpimization , ! numDaysForInSampleOptimization , ! benchmark , intervalsSelector , ! eligiblesSelector , inSampleChooser , ! this.historicalQuoteProviderForInSample , ! outOfSampleChooser ); ! // IEndOfDayStrategyForBacktester endOfDayStrategyForBacktester = ! // new PairsTradingStrategy( ! // 7 , inSampleDays , intervalsSelector , ! // eligiblesSelector , inSampleChooser , ! // this.historicalQuoteProviderForInSample , ! // this.historicalQuoteProviderForChosingPositionsOutOfSample , ! // 0.006 , 0.99 , 0.006 , 0.99 ); ! // endOfDayStrategyForBacktester = ! // new LongOnlyPairsTradingStrategy( ! // 7 , inSampleDays , intervalsSelector , ! // eligiblesSelector , inSampleChooser , ! // this.historicalQuoteProviderForInSample , ! // this.historicalQuoteProviderForChosingPositionsOutOfSample , ! // 0.006 , 0.02 , 0.006 , 0.02 ); ! return fixedLengthTwoPhasesStrategy; ! } ! ! protected override EndOfDayStrategyBackTester ! getEndOfDayStrategyBackTester() ! { ! string backTestId = "FixedLengthTwoPhases"; ! IAccountProvider accountProvider = new SimpleAccountProvider(); ! double cashToStart = 30000; ! DateTime firstDateTime = new DateTime( 2006 , 1 , 2 ); ! DateTime lastDateTime = new DateTime( 2006 , 1 , 6 ); ! ! double maxRunningHours = 1; ! EndOfDayStrategyBackTester endOfDayStrategyBackTester = new EndOfDayStrategyBackTester( ! backTestId , this.endOfDayStrategy , ! this.historicalQuoteProviderForTheBacktesterAccount , ! accountProvider , ! firstDateTime , lastDateTime , ! this.benchmark , cashToStart , maxRunningHours ); ! return endOfDayStrategyBackTester; } ! ! protected override string getPathForTheMainFolderWhereScriptsResultsAreToBeSaved() ! { ! string pathForTheMainFolderWhereScriptsResultsAreToBeSaved = ! "C:\\qpReports\\fixedLengthTwoPhases\\"; ! return pathForTheMainFolderWhereScriptsResultsAreToBeSaved; ! } ! ! protected override string getCustomSmallTextForFolderName() ! { ! return "fltp"; ! } ! ! protected override string getFullPathFileNameForMain() ! { ! string fullPathFileNameForMain = ! @"C:\QuantProject\QuantProject\b7_Scripts\WalkForwardTesting\FixedLengthTwoPhases\FixedLengthTwoPhasesMain.cs"; ! return fullPathFileNameForMain; ! } } } |