[Quantproject-developers] QuantProject/b7_Scripts/ArbitrageTesting/PairTrading RunPairTrading.cs,
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-09-29 21:17:14
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/ArbitrageTesting/PairTrading In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv15453/PairTrading Modified Files: RunPairTrading.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: RunPairTrading.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/ArbitrageTesting/PairTrading/RunPairTrading.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RunPairTrading.cs 19 Aug 2008 17:13:09 -0000 1.2 --- RunPairTrading.cs 29 Sep 2008 21:16:40 -0000 1.3 *************** *** 3,7 **** RunPairTrading.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- RunPairTrading.cs ! Copyright (C) 2003 Marco Milletti *************** *** 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,44 **** using QuantProject.Business.Financial.Accounting.Commissions; using QuantProject.Data.DataProviders; ! using QuantProject.Data.Selectors; using QuantProject.Presentation.Reporting.WindowsForm; using QuantProject.Scripts.WalkForwardTesting.LinearCombination; --- 40,44 ---- using QuantProject.Business.Financial.Accounting.Commissions; using QuantProject.Data.DataProviders; ! using QuantProject.Data.Selectors; using QuantProject.Presentation.Reporting.WindowsForm; using QuantProject.Scripts.WalkForwardTesting.LinearCombination; *************** *** 52,255 **** /// </summary> [Serializable] ! public class RunPairTrading { ! protected string tickerGroupID; ! protected int numberOfEligibleTickers; ! protected int numDaysForOptimizationPeriod; ! protected int generationNumberForGeneticOptimizer; ! protected int populationSizeForGeneticOptimizer; ! protected EndOfDayDateTime startDateTime; ! protected EndOfDayDateTime endDateTime; ! protected IHistoricalQuoteProvider historicalQuoteProvider; ! protected Account account; ! protected IEndOfDayTimer endOfDayTimer; ! protected string benchmark; ! ! protected string scriptName; ! ! protected double maxNumOfStdDevForNormalGap; ! protected int minNumOfDaysForGapComputation; ! protected int maxNumOfDaysForGapComputation; ! protected int numDaysBetweenEachOptimization; ! protected DateTime startingTimeForScript; ! protected double maxRunningHours; ! //if MaxNumberOfHoursForScript has elapsed and the script ! //is still running, it will be stopped. ! ! public virtual string ScriptName ! { ! get{return this.scriptName;} ! set{this.scriptName = value;} ! } ! ! public DateTime TimerLastDate ! { ! get{return this.endOfDayTimer.GetCurrentTime().DateTime ;} ! } ! // public RunPairTrading(string benchmark, ! // DateTime startDate, DateTime endDate, ! // double maxLevelForNormalGap, ! // double maxRunningHours) // { ! // ! // this.startDateTime = new EndOfDayDateTime( ! // startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! // this.endDateTime = new EndOfDayDateTime( ! // endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! // this.maxLevelForNormalGap = maxLevelForNormalGap; ! // this.benchmark = benchmark; ! // this.ScriptName = "EfficientGeneric"; ! // this.startingTimeForScript = DateTime.Now; ! // this.maxRunningHours = maxRunningHours; ! // //this.numIntervalDays = 3; // } ! ! public RunPairTrading(string tickerGroupID, int numberOfEligibleTickers, ! int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, string benchmark, ! DateTime startDate, DateTime endDate, ! int minNumOfDaysForGapComputation, int maxNumOfDaysForGapComputation, ! double maxNumOfStdDevForNormalGap, int numDaysBetweenEachOptimization, ! double maxRunningHours) { ! //this.progressBarForm = new ProgressBarForm(); ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numDaysForOptimizationPeriod = numDaysForOptimizationPeriod; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.startDateTime = new EndOfDayDateTime( ! startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = new EndOfDayDateTime( ! endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.benchmark = benchmark; ! this.ScriptName = "PairTradingGeneric"; ! this.maxNumOfStdDevForNormalGap = maxNumOfStdDevForNormalGap; ! this.minNumOfDaysForGapComputation = minNumOfDaysForGapComputation; ! this.maxNumOfDaysForGapComputation = maxNumOfDaysForGapComputation; ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.startingTimeForScript = DateTime.Now; ! this.maxRunningHours = maxRunningHours; ! //this.numIntervalDays = 3; } ! ! ! #region Run ! ! protected virtual void run_initializeEndOfDayTimer() ! { ! //default endOfDayTimer ! this.endOfDayTimer = ! new IndexBasedEndOfDayTimer( this.startDateTime, this.benchmark ); ! ! } ! ! protected virtual void run_initializeAccount() ! { ! //default account with no commissions and no slippage calculation ! this.account = new Account( this.scriptName , this.endOfDayTimer , ! new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , ! this.historicalQuoteProvider ) , ! new HistoricalEndOfDayOrderExecutor( this.endOfDayTimer , ! this.historicalQuoteProvider )); ! ! } ! protected virtual void run_initializeEndOfDayTimerHandler() ! { ! //always needs specific implementation in inherited classes; ! } ! ! protected virtual void run_initializeHistoricalQuoteProvider() ! { ! this.historicalQuoteProvider = new HistoricalAdjustedQuoteProvider(); ! } ! ! protected void checkDateForReport_createDirIfNotPresent(string dirPath) ! { ! if(!Directory.Exists(dirPath)) ! Directory.CreateDirectory(dirPath); ! } ! ! protected virtual void checkDateForReport(Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) ! { ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime || ! DateTime.Now >= this.startingTimeForScript.AddHours(this.maxRunningHours)) ! //last date is reached by the timer or maxRunning hours ! //are elapsed from the time script started ! this.SaveScriptResults(); ! } ! ! public virtual void SaveScriptResults() ! { ! string fileName = "From"+this.numberOfEligibleTickers + ! "OptDays" + this.numDaysForOptimizationPeriod + "GenNum" + ! this.generationNumberForGeneticOptimizer + ! "PopSize" + this.populationSizeForGeneticOptimizer; ! string dirNameWhereToSaveReports = ! System.Configuration.ConfigurationManager.AppSettings["ReportsArchive"] + ! "\\" + this.ScriptName + "\\"; ! string dirNameWhereToSaveTransactions = ! System.Configuration.ConfigurationManager.AppSettings["TransactionsArchive"] + ! "\\" + this.ScriptName + "\\"; ! // string dirNameWhereToSaveBestGenomes = System.Configuration.ConfigurationSettings.AppSettings["GenomesArchive"] + ! // "\\" + this.ScriptName + "\\"; ! //default report with numIntervalDays = 1 ! AccountReport accountReport = this.account.CreateReport(fileName,1, ! this.endOfDayTimer.GetCurrentTime(), ! this.benchmark, ! new HistoricalAdjustedQuoteProvider()); ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveReports); ! ObjectArchiver.Archive(accountReport, ! dirNameWhereToSaveReports + ! fileName + ".qPr"); ! // ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveTransactions); ! ObjectArchiver.Archive(this.account.Transactions, ! dirNameWhereToSaveTransactions + ! fileName + ".qPt"); ! // ! // this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveBestGenomes); ! ! this.endOfDayTimer.Stop(); ! ! } ! ! protected virtual void run_initialize() ! { ! run_initializeHistoricalQuoteProvider(); ! run_initializeEndOfDayTimer(); ! run_initializeAccount(); ! run_initializeEndOfDayTimerHandler(); ! //run_initializeProgressHandlers(); ! } ! protected virtual void run_addEventHandlers() ! { ! this.endOfDayTimer.MarketClose += ! new MarketCloseEventHandler( ! this.checkDateForReport); ! ! //in inherited classes'override method: ! //add here TimerHandler's handlers to timer's events ! //example ! //this.endOfDayTimer.EVENT_NAME += ! // new EVENT_NAMEEventHandler( ! // this.endOfDayTimerHandler.EVENT_NAMEEventHandler); ! } ! ! ! public virtual void Run() ! { ! this.run_initialize(); ! this.run_addEventHandlers(); ! //this.progressBarForm.Show(); ! this.endOfDayTimer.Start(); ! } ! ! #endregion ! } } --- 52,263 ---- /// </summary> [Serializable] ! public class RunPairTrading { ! protected string tickerGroupID; ! protected int numberOfEligibleTickers; ! protected int numDaysForOptimizationPeriod; ! protected int generationNumberForGeneticOptimizer; ! protected int populationSizeForGeneticOptimizer; ! protected DateTime startDateTime; ! protected DateTime endDateTime; ! protected HistoricalMarketValueProvider historicalMarketValueProvider; ! protected Account account; ! protected QuantProject.Business.Timing.Timer timer; ! protected string benchmark; ! ! protected string scriptName; ! ! protected double maxNumOfStdDevForNormalGap; ! protected int minNumOfDaysForGapComputation; ! protected int maxNumOfDaysForGapComputation; ! protected int numDaysBetweenEachOptimization; ! protected DateTime startingTimeForScript; ! protected double maxRunningHours; ! //if MaxNumberOfHoursForScript has elapsed and the script ! //is still running, it will be stopped. ! ! public virtual string ScriptName ! { ! get{return this.scriptName;} ! set{this.scriptName = value;} ! } ! ! public DateTime TimerLastDate ! { ! get{return this.timer.GetCurrentDateTime() ;} ! } ! // public RunPairTrading(string benchmark, ! // DateTime startDate, DateTime endDate, ! // double maxLevelForNormalGap, ! // double maxRunningHours) // { ! // ! // this.startDateTime = new EndOfDayDateTime( ! // startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! // this.endDateTime = new EndOfDayDateTime( ! // endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! // this.maxLevelForNormalGap = maxLevelForNormalGap; ! // this.benchmark = benchmark; ! // this.ScriptName = "EfficientGeneric"; ! // this.startingTimeForScript = DateTime.Now; ! // this.maxRunningHours = maxRunningHours; ! // //this.numIntervalDays = 3; // } ! ! public RunPairTrading(string tickerGroupID, int numberOfEligibleTickers, ! int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, string benchmark, ! DateTime startDate, DateTime endDate, ! int minNumOfDaysForGapComputation, int maxNumOfDaysForGapComputation, ! double maxNumOfStdDevForNormalGap, int numDaysBetweenEachOptimization, ! double maxRunningHours) { ! //this.progressBarForm = new ProgressBarForm(); ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numDaysForOptimizationPeriod = numDaysForOptimizationPeriod; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.startDateTime = ! HistoricalEndOfDayTimer.GetFiveMinutesBeforeMarketClose( startDate ); ! // new EndOfDayDateTime( ! // startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = ! HistoricalEndOfDayTimer.GetOneHourAfterMarketClose( endDate ); ! // new EndOfDayDateTime( ! // endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.benchmark = benchmark; ! this.ScriptName = "PairTradingGeneric"; ! this.maxNumOfStdDevForNormalGap = maxNumOfStdDevForNormalGap; ! this.minNumOfDaysForGapComputation = minNumOfDaysForGapComputation; ! this.maxNumOfDaysForGapComputation = maxNumOfDaysForGapComputation; ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.startingTimeForScript = DateTime.Now; ! this.maxRunningHours = maxRunningHours; ! //this.numIntervalDays = 3; } ! ! ! #region Run ! ! protected virtual void run_initializeEndOfDayTimer() ! { ! //default endOfDayTimer ! this.timer = ! new IndexBasedEndOfDayTimer( this.startDateTime, this.benchmark ); ! ! } ! ! protected virtual void run_initializeAccount() ! { ! //default account with no commissions and no slippage calculation ! this.account = new Account( this.scriptName , this.timer , ! new HistoricalEndOfDayDataStreamer( this.timer , ! this.historicalMarketValueProvider ) , ! new HistoricalEndOfDayOrderExecutor( this.timer , ! this.historicalMarketValueProvider )); ! ! } ! protected virtual void run_initializeEndOfDayTimerHandler() ! { ! //always needs specific implementation in inherited classes; ! } ! ! protected virtual void run_initializeHistoricalQuoteProvider() ! { ! this.historicalMarketValueProvider = new HistoricalAdjustedQuoteProvider(); ! } ! ! protected void checkDateForReport_createDirIfNotPresent(string dirPath) ! { ! if(!Directory.Exists(dirPath)) ! Directory.CreateDirectory(dirPath); ! } ! ! protected virtual void checkDateForReport( ! Object sender , DateTime dateTime ) ! { ! if(dateTime>=this.endDateTime || ! DateTime.Now >= this.startingTimeForScript.AddHours(this.maxRunningHours)) ! //last date is reached by the timer or maxRunning hours ! //are elapsed from the time script started ! this.SaveScriptResults(); ! } ! ! public virtual void SaveScriptResults() ! { ! string fileName = "From"+this.numberOfEligibleTickers + ! "OptDays" + this.numDaysForOptimizationPeriod + "GenNum" + ! this.generationNumberForGeneticOptimizer + ! "PopSize" + this.populationSizeForGeneticOptimizer; ! string dirNameWhereToSaveReports = ! System.Configuration.ConfigurationManager.AppSettings["ReportsArchive"] + ! "\\" + this.ScriptName + "\\"; ! string dirNameWhereToSaveTransactions = ! System.Configuration.ConfigurationManager.AppSettings["TransactionsArchive"] + ! "\\" + this.ScriptName + "\\"; ! // string dirNameWhereToSaveBestGenomes = System.Configuration.ConfigurationSettings.AppSettings["GenomesArchive"] + ! // "\\" + this.ScriptName + "\\"; ! //default report with numIntervalDays = 1 ! AccountReport accountReport = this.account.CreateReport(fileName,1, ! this.timer.GetCurrentDateTime(), ! this.benchmark, ! new HistoricalAdjustedQuoteProvider()); ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveReports); ! ObjectArchiver.Archive(accountReport, ! dirNameWhereToSaveReports + ! fileName + ".qPr"); ! // ! this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveTransactions); ! ObjectArchiver.Archive(this.account.Transactions, ! dirNameWhereToSaveTransactions + ! fileName + ".qPt"); ! // ! // this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveBestGenomes); ! ! this.timer.Stop(); ! ! } ! ! protected virtual void run_initialize() ! { ! run_initializeHistoricalQuoteProvider(); ! run_initializeEndOfDayTimer(); ! run_initializeAccount(); ! run_initializeEndOfDayTimerHandler(); ! //run_initializeProgressHandlers(); ! } ! protected virtual void run_addEventHandlers() ! { ! this.timer.NewDateTime += ! new NewDateTimeEventHandler( this.checkDateForReport ); ! ! // this.timer.MarketClose += ! // new MarketCloseEventHandler( ! // this.checkDateForReport); ! ! //in inherited classes'override method: ! //add here TimerHandler's handlers to timer's events ! //example ! //this.endOfDayTimer.EVENT_NAME += ! // new EVENT_NAMEEventHandler( ! // this.endOfDayTimerHandler.EVENT_NAMEEventHandler); ! } ! ! ! public virtual void Run() ! { ! this.run_initialize(); ! this.run_addEventHandlers(); ! //this.progressBarForm.Show(); ! this.timer.Start(); ! } ! ! #endregion ! } } |