[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting EndOfDayTimerHandler.cs,1.9
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-04-11 18:52:41
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5916/b7_Scripts/TickerSelectionTesting Modified Files: EndOfDayTimerHandlerCTC.cs EndOfDayTimerHandlerCTO.cs GenomeManagerForEfficientCTCPortfolio.cs GenomeManagerForEfficientCTOPortfolio.cs GenomeManagerForEfficientPortfolio.cs RunEfficientCTCPortfolio.cs RunEfficientCTOPortfolio.cs Added Files: EndOfDayTimerHandler.cs PortfolioType.cs RunEfficientPortfolio.cs Log Message: Reorganized efficient portfolio scripts in a more object oriented way, with three base classes from which the other classes derive. In addition, parameters have been added to Run files, in order to make it easy to run multiple scripts in one session. --- NEW FILE: PortfolioType.cs --- /* QuantProject - Quantitative Finance Library PortfolioType.cs Copyright (C) 2004 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License 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; namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { /// <summary> /// Specifies what types of orders are to be added /// to portfolio /// </summary> public enum PortfolioType { OnlyLong, OnlyShort } } Index: EndOfDayTimerHandlerCTO.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTO.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** EndOfDayTimerHandlerCTO.cs 30 Mar 2005 16:07:28 -0000 1.6 --- EndOfDayTimerHandlerCTO.cs 11 Apr 2005 18:52:31 -0000 1.7 *************** *** 42,111 **** /// </summary> [Serializable] ! public class EndOfDayTimerHandlerCTO { - private DataTable eligibleTickers; - private string[] chosenTickers; - private string[] lastChosenTickers; - private string tickerGroupID; - private int numberOfEligibleTickers; - private int numberOfTickersToBeChosen; - private int numDaysForLiquidity; - private int generationNumberForGeneticOptimizer; - private int populationSizeForGeneticOptimizer; - - private Account account; - private ArrayList orders; - - private string benchmark; - //these two values have to be updated during - //backtest, for minimizing commission amount, - //according to broker's commission scheme - private double minPriceForMinimumCommission = 0; - private double maxPriceForMinimumCommission = 200; - - - public int NumberOfEligibleTickers - { - get { return this.numberOfEligibleTickers; } - } - - public Account Account - { - get { return this.account; } - } - public EndOfDayTimerHandlerCTO(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForLiquidity, Account account, int generationNumberForGeneticOptimizer, int populationSizeForGeneticOptimizer, ! string benchmark) { ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; ! this.numDaysForLiquidity = numDaysForLiquidity; ! this.account = account; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.benchmark = benchmark; ! this.orders = new ArrayList(); ! this.chosenTickers = new string[numberOfTickersToBeChosen]; ! this.lastChosenTickers = new string[numberOfTickersToBeChosen]; } #region MarketOpenEventHandler ! ! private void marketOpenEventHandler_orderChosenTickers_addToOrderList_forTicker( ! string ticker ) ! { ! ! double cashForSinglePosition = this.account.CashAmount / this.numberOfTickersToBeChosen; ! long quantity = ! Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( ticker ) ) ); ! Order order = new Order( OrderType.MarketBuy, new Instrument( ticker ) , quantity ); ! this.orders.Add(order); ! } ! private void marketOpenEventHandler_orderChosenTickers_addToOrderList() { --- 42,66 ---- /// </summary> [Serializable] ! public class EndOfDayTimerHandlerCTO : EndOfDayTimerHandler { public EndOfDayTimerHandlerCTO(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForLiquidity, Account account, int generationNumberForGeneticOptimizer, int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForLiquidity, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) { ! } #region MarketOpenEventHandler ! private void marketOpenEventHandler_orderChosenTickers_addToOrderList() { *************** *** 115,119 **** if(ticker != null) { ! marketOpenEventHandler_orderChosenTickers_addToOrderList_forTicker( ticker ); this.lastChosenTickers[idx] = ticker; } --- 70,74 ---- if(ticker != null) { ! this.AddOrderForTicker( ticker ); this.lastChosenTickers[idx] = ticker; } *************** *** 132,140 **** /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if(this.orders.Count == 0 && this.account.Transactions.Count == 0) ! this.account.AddCash(15000); this.marketOpenEventHandler_orderChosenTickers(); --- 87,95 ---- /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if(this.orders.Count == 0 && this.account.Transactions.Count == 0) ! this.account.AddCash(17000); this.marketOpenEventHandler_orderChosenTickers(); *************** *** 169,173 **** } ! public void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { --- 124,128 ---- } ! public override void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { *************** *** 182,199 **** private DataTable getSetOfTickersToBeOptimized(DateTime currentDate) { SelectorByAverageRawOpenPrice selectorByOpenPrice = new SelectorByAverageRawOpenPrice(this.tickerGroupID, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers, this.minPriceForMinimumCommission, ! this.maxPriceForMinimumCommission, 0, 1.5); DataTable tickersByPrice = selectorByOpenPrice.GetTableOfSelectedTickers(); ! SelectorByLiquidity mostLiquid = new SelectorByLiquidity(tickersByPrice, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers); this.eligibleTickers = mostLiquid.GetTableOfSelectedTickers(); ! SelectorByQuotationAtEachMarketDay quotedInEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay( this.eligibleTickers, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers, this.benchmark); ! return quotedInEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); } --- 137,157 ---- private DataTable getSetOfTickersToBeOptimized(DateTime currentDate) { + /* SelectorByAverageRawOpenPrice selectorByOpenPrice = new SelectorByAverageRawOpenPrice(this.tickerGroupID, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers, this.minPriceForMinimumCommission, ! this.maxPriceForMinimumCommission, 0, 2); DataTable tickersByPrice = selectorByOpenPrice.GetTableOfSelectedTickers(); ! */ ! ! SelectorByLiquidity mostLiquid = new SelectorByLiquidity(this.tickerGroupID, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers); this.eligibleTickers = mostLiquid.GetTableOfSelectedTickers(); ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay( this.eligibleTickers, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers, this.benchmark); ! return quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); } *************** *** 209,214 **** { IGenomeManager genManEfficientCTOPortfolio = ! new GenomeManagerForEfficientCTOPortfolio(setOfTickersToBeOptimized, currentDate.AddDays(-30), ! currentDate, this.numberOfTickersToBeChosen, 1, 0.005); GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTOPortfolio); //GO.KeepOnRunningUntilConvergenceIsReached = true; --- 167,176 ---- { IGenomeManager genManEfficientCTOPortfolio = ! new GenomeManagerForEfficientCTOPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForLiquidity), ! currentDate, ! this.numberOfTickersToBeChosen, ! this.targetReturn, ! this.portfolioType); GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTOPortfolio); //GO.KeepOnRunningUntilConvergenceIsReached = true; *************** *** 228,231 **** --- 190,195 ---- //according to IB Broker's commission scheme this.minPriceForMinimumCommission = this.account.CashAmount/(this.numberOfTickersToBeChosen*100); + this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; + //just to avoid warning message } *************** *** 235,242 **** /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public void OneHourAfterMarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! this.oneHourAfterMarketCloseEventHandler_updatePrices(); this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime); //sets tickers to be chosen next Market Open event --- 199,206 ---- /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public override void OneHourAfterMarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime); //sets tickers to be chosen next Market Open event --- NEW FILE: EndOfDayTimerHandler.cs --- /* QuantProject - Quantitative Finance Library EndOfDayTimerHandler.cs Copyright (C) 2003 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License 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; using System.Data; using System.Collections; using QuantProject.ADT; using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Financial.Instruments; using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Timing; using QuantProject.Data.DataProviders; using QuantProject.Data.Selectors; using QuantProject.ADT.Optimizing.Genetic; namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { /// <summary> /// Base class for EndOfDayTimerHandlers for efficient portfolios /// </summary> [Serializable] public class EndOfDayTimerHandler { protected DataTable eligibleTickers; protected string[] chosenTickers; protected string[] lastChosenTickers; protected string tickerGroupID; protected int numberOfEligibleTickers; protected int numberOfTickersToBeChosen; protected int numDaysForLiquidity; protected int generationNumberForGeneticOptimizer; protected int populationSizeForGeneticOptimizer; protected Account account; protected ArrayList orders; protected string benchmark; //these two values have to be updated during //backtest, for minimizing commission amount, //according to broker's commission scheme protected double minPriceForMinimumCommission = 0; protected double maxPriceForMinimumCommission = 500; protected double targetReturn; protected PortfolioType portfolioType; public int NumberOfEligibleTickers { get { return this.numberOfEligibleTickers; } } public Account Account { get { return this.account; } } public EndOfDayTimerHandler(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForLiquidity, Account account, int generationNumberForGeneticOptimizer, int populationSizeForGeneticOptimizer, string benchmark, double targetReturn, PortfolioType portfolioType) { this.tickerGroupID = tickerGroupID; this.numberOfEligibleTickers = numberOfEligibleTickers; this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; this.numDaysForLiquidity = numDaysForLiquidity; this.account = account; this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; this.benchmark = benchmark; this.orders = new ArrayList(); this.chosenTickers = new string[numberOfTickersToBeChosen]; this.lastChosenTickers = new string[numberOfTickersToBeChosen]; this.targetReturn = targetReturn; this.portfolioType = portfolioType; } public virtual void AddOrderForTicker(string ticker ) { double cashForSinglePosition = this.account.CashAmount / this.numberOfTickersToBeChosen; long quantity = Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( ticker ) ) ); Order order; if(this.portfolioType == PortfolioType.OnlyLong) order = new Order( OrderType.MarketBuy, new Instrument( ticker ) , quantity ); else// only short orders are permitted order = new Order( OrderType.MarketSellShort, new Instrument( ticker ) , quantity ); this.orders.Add(order); } public virtual void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } public virtual void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } public virtual void OneHourAfterMarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } } // end of class } Index: RunEfficientCTCPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/RunEfficientCTCPortfolio.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RunEfficientCTCPortfolio.cs 6 Feb 2005 20:14:01 -0000 1.4 --- RunEfficientCTCPortfolio.cs 11 Apr 2005 18:52:31 -0000 1.5 *************** *** 57,180 **** /// </summary> [Serializable] ! public class RunEfficientCTCPorfolio : Script { ! ! private ReportTable reportTable; ! private EndOfDayDateTime startDateTime; ! private EndOfDayDateTime endDateTime; ! ! // private int numIntervalDays;// number of days for the equity line graph ! ! private IHistoricalQuoteProvider historicalQuoteProvider = ! new HistoricalAdjustedQuoteProvider(); ! ! ! //private ProgressBarForm progressBarForm; ! ! private EndOfDayTimerHandlerCTC endOfDayTimerHandler; ! ! private Account account; ! ! private IEndOfDayTimer endOfDayTimer; ! public RunEfficientCTCPorfolio() { ! //this.progressBarForm = new ProgressBarForm(); ! this.reportTable = new ReportTable( "Summary_Reports" ); ! this.startDateTime = new EndOfDayDateTime( ! new DateTime( 2004 , 1 , 1 ) , EndOfDaySpecificTime.MarketOpen ); ! this.endDateTime = new EndOfDayDateTime( ! new DateTime( 2004 , 3 , 31 ) , EndOfDaySpecificTime.MarketClose ); ! //this.numIntervalDays = 3; //for report ! } #region Run ! ! ! ! private void run_initializeEndOfDayTimer() ! { ! this.endOfDayTimer = ! new IndexBasedEndOfDayTimer( this.startDateTime, "^MIBTEL" ); ! } ! private void run_initializeAccount() ! { ! this.account = new Account( "EfficientCloseToClosePortfolio" , this.endOfDayTimer , ! new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , ! this.historicalQuoteProvider ) , ! new HistoricalEndOfDayOrderExecutor( this.endOfDayTimer , ! this.historicalQuoteProvider ) ); ! ! } ! private void run_initializeEndOfDayTimerHandler() ! { ! this.endOfDayTimerHandler = new EndOfDayTimerHandlerCTC("STOCKMI",70,5,3,this.account,10); ! } ! /* ! private void inSampleNewProgressEventHandler( ! Object sender , NewProgressEventArgs eventArgs ) ! { ! this.progressBarForm.ProgressBarInSample.Value = eventArgs.CurrentProgress; ! this.progressBarForm.ProgressBarInSample.Refresh(); ! } ! private void run_initializeProgressHandlers() ! { ! this.endOfDayTimerHandler.InSampleNewProgress += ! new InSampleNewProgressEventHandler( this.inSampleNewProgressEventHandler ); ! } ! */ ! #region oneHourAfterMarketCloseEventHandler ! /* ! private void oneHourAfterMarketCloseEventHandler_handleProgessBarForm( ! IEndOfDayTimer endOfDayTimer ) ! { ! long elapsedDays = Convert.ToInt64( ((TimeSpan)( endOfDayTimer.GetCurrentTime().DateTime - ! this.startDateTime.DateTime )).TotalDays ); ! long totalDays = Convert.ToInt64( ((TimeSpan)( this.endDateTime.DateTime - ! this.startDateTime.DateTime )).TotalDays ); ! if ( Math.Floor( elapsedDays / totalDays * 100 ) > ! Math.Floor( ( elapsedDays - 1 ) / totalDays * 100 ) ) ! // a new out of sample time percentage point has been elapsed ! this.progressBarForm.ProgressBarOutOfSample.Value = ! Convert.ToInt16( Math.Floor( elapsedDays / totalDays * 100 ) ); ! } ! public void oneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! this.oneHourAfterMarketCloseEventHandler_handleProgessBarForm( ! ( IEndOfDayTimer )sender ); ! if ( ( ( IEndOfDayTimer )sender ).GetCurrentTime().DateTime > ! this.endDateTime.DateTime ) ! { ! // the simulation has reached the ending date ! this.account.EndOfDayTimer.Stop(); ! this.progressBarForm.Close(); ! } } - */ - #endregion ! private void checkDateForReport(Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! Report report; ! ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime ) ! { ! this.endOfDayTimer.Stop(); ! report = new Report( this.account , this.historicalQuoteProvider ); ! //report.Show("CTC_Portfolio" , this.numIntervalDays , this.endDateTime , "^MIBTEL" ); ! ObjectArchiver.Archive(this.account,"C:\\CtcPortfolio.qP"); ! ! } } public override void Run() { //old script //this.run_FindBestPortfolioForNextTrade(); ! run_initializeEndOfDayTimer(); run_initializeAccount(); run_initializeEndOfDayTimerHandler(); //run_initializeProgressHandlers(); this.endOfDayTimer.MarketOpen += --- 57,109 ---- /// </summary> [Serializable] ! public class RunEfficientCTCPorfolio : RunEfficientPorfolio { ! private int numDayOfPortfolioLife; ! public RunEfficientCTCPorfolio(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForLiquidity, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, string benchmark, ! DateTime startDate, DateTime endDate, ! int numDaysOfPortfolioLife, double targetReturn, ! PortfolioType portfolioType): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForLiquidity, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, benchmark, ! startDate, endDate, targetReturn, ! portfolioType) { ! this.ScriptName = "CloseToCloseScripts"; ! this.numDayOfPortfolioLife = numDaysOfPortfolioLife; } #region Run + ! protected override void run_initializeEndOfDayTimerHandler() { ! this.endOfDayTimerHandler = new EndOfDayTimerHandlerCTC(this.tickerGroupID, this.numberOfEligibleTickers, ! this.numberOfTickersToBeChosen, this.numDaysForLiquidity, ! this.account, ! this.generationNumberForGeneticOptimizer, ! this.populationSizeForGeneticOptimizer, this.benchmark, ! this.numDayOfPortfolioLife, this.targetReturn, ! this.portfolioType); } ! protected override void run_initializeHistoricalQuoteProvider() { ! this.historicalQuoteProvider = new HistoricalAdjustedQuoteProvider(); } + public override void Run() { //old script //this.run_FindBestPortfolioForNextTrade(); ! run_initializeHistoricalQuoteProvider(); run_initializeEndOfDayTimer(); run_initializeAccount(); run_initializeEndOfDayTimerHandler(); + //run_initializeProgressHandlers(); this.endOfDayTimer.MarketOpen += --- NEW FILE: RunEfficientPortfolio.cs --- /* QuantProject - Quantitative Finance Library RunEfficientPorfolio.cs Copyright (C) 2003 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License 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; using System.IO; using System.Collections; using System.Data; using QuantProject.ADT; using QuantProject.ADT.Optimizing.Genetic; using QuantProject.ADT.FileManaging; using QuantProject.Business.DataProviders; using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Financial.Accounting.Reporting; using QuantProject.Business.Financial.Instruments; using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Scripting; using QuantProject.Business.Strategies; using QuantProject.Business.Testing; using QuantProject.Business.Timing; using QuantProject.Business.Financial.Accounting.Commissions; using QuantProject.Data.DataProviders; using QuantProject.Data.Selectors; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; using QuantProject.Presentation.Reporting.WindowsForm; namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { /// <summary> /// Base class for running efficient portfolio tests using genetic optimizer /// </summary> [Serializable] public class RunEfficientPorfolio { protected string tickerGroupID; protected int numberOfEligibleTickers; protected int numberOfTickersToBeChosen; protected int numDaysForLiquidity; protected int generationNumberForGeneticOptimizer; protected int populationSizeForGeneticOptimizer; protected ReportTable reportTable; protected EndOfDayDateTime startDateTime; protected EndOfDayDateTime endDateTime; //protected int numIntervalDays;// number of days for the equity line graph protected IHistoricalQuoteProvider historicalQuoteProvider; //protected ProgressBarForm progressBarForm; protected EndOfDayTimerHandler endOfDayTimerHandler; protected Account account; protected IEndOfDayTimer endOfDayTimer; protected string benchmark; protected string scriptName; protected double targetReturn; protected PortfolioType portfolioType; public virtual string ScriptName { get{return this.scriptName;} set{this.scriptName = value;} } public RunEfficientPorfolio(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForLiquidity, int generationNumberForGeneticOptimizer, int populationSizeForGeneticOptimizer, string benchmark, DateTime startDate, DateTime endDate, double targetReturn, PortfolioType portfolioType) { //this.progressBarForm = new ProgressBarForm(); this.tickerGroupID = tickerGroupID; this.numberOfEligibleTickers = numberOfEligibleTickers; this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; this.numDaysForLiquidity = numDaysForLiquidity; this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; this.reportTable = new ReportTable( "Summary_Reports" ); this.startDateTime = new EndOfDayDateTime( startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); this.endDateTime = new EndOfDayDateTime( endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); this.benchmark = benchmark; this.ScriptName = "EfficientGeneric"; this.targetReturn = targetReturn; this.portfolioType = portfolioType; //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 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() { //always needs specific implementation in inherited classes; } private void checkDateForReport_createDirIfNotPresent(string dirPath) { if(!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath); } protected virtual void checkDateForReport(Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) { //Report report; if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime ) { this.endOfDayTimer.Stop(); string fileName = "From"+this.numberOfEligibleTickers + "LiqDays" + this.numDaysForLiquidity + "Portfolio" + this.numberOfTickersToBeChosen + "GenNum" + this.generationNumberForGeneticOptimizer + "PopSize" + this.populationSizeForGeneticOptimizer + "Target" + Convert.ToString(this.targetReturn) + Convert.ToString(this.portfolioType); string dirNameWhereToSaveReports = System.Configuration.ConfigurationSettings.AppSettings["ReportsArchive"] + "\\" + this.ScriptName + "\\"; string dirNameWhereToSaveAccounts = System.Configuration.ConfigurationSettings.AppSettings["AccountsArchive"] + "\\" + this.ScriptName + "\\"; //default report with numIntervalDays = 1 AccountReport accountReport = this.account.CreateReport(fileName,1,this.endDateTime,this.benchmark, new HistoricalAdjustedQuoteProvider()); this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveReports); ObjectArchiver.Archive(accountReport, dirNameWhereToSaveReports + fileName + ".rep"); this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveAccounts); ObjectArchiver.Archive(this.account, dirNameWhereToSaveAccounts + fileName + ".acc"); } } public virtual void Run() { run_initializeHistoricalQuoteProvider(); run_initializeEndOfDayTimer(); run_initializeAccount(); run_initializeEndOfDayTimerHandler(); //run_initializeProgressHandlers(); this.endOfDayTimer.MarketClose += new MarketCloseEventHandler( this.checkDateForReport); //add here TimerHandler's handlers to timer's events //example //this.endOfDayTimer.EVENT_NAME += // new EVENT_NAMEEventHandler( // this.endOfDayTimerHandler.EVENT_NAMEEventHandler); //this.progressBarForm.Show(); this.endOfDayTimer.Start(); } #endregion } } Index: EndOfDayTimerHandlerCTC.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTC.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** EndOfDayTimerHandlerCTC.cs 27 Feb 2005 19:56:08 -0000 1.2 --- EndOfDayTimerHandlerCTC.cs 11 Apr 2005 18:52:30 -0000 1.3 *************** *** 42,86 **** /// </summary> [Serializable] ! public class EndOfDayTimerHandlerCTC { - private DataTable eligibleTickers; - private string[] chosenTickers; - private string[] lastChosenTickers; - - private string tickerGroupID; - private int numberOfEligibleTickers; - private int numberOfTickersToBeChosen; private int numDaysOfPortfolioLife; private int daysCounter; - private int generationNumberForGeneticOptimizer; - - private Account account; - private ArrayList orders; - - public int NumberOfEligibleTickers - { - get { return this.numberOfEligibleTickers; } - } - - public Account Account - { - get { return this.account; } - } - public EndOfDayTimerHandlerCTC(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysOfPortfolioLife, ! Account account, ! int generationNumberForGeneticOptimizer) { - this.tickerGroupID = tickerGroupID; - this.numberOfEligibleTickers = numberOfEligibleTickers; - this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; this.numDaysOfPortfolioLife = numDaysOfPortfolioLife; this.daysCounter = 0; - this.account = account; - this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; - this.orders = new ArrayList(); - this.chosenTickers = new string[numberOfTickersToBeChosen]; - this.lastChosenTickers = new string[numberOfTickersToBeChosen]; } --- 42,67 ---- /// </summary> [Serializable] ! public class EndOfDayTimerHandlerCTC : EndOfDayTimerHandler { private int numDaysOfPortfolioLife; private int daysCounter; public EndOfDayTimerHandlerCTC(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForLiquidity, ! Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, ! int numDaysOfPortfolioLife, ! double targetReturn, ! PortfolioType portfolioType): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForLiquidity, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) { this.numDaysOfPortfolioLife = numDaysOfPortfolioLife; this.daysCounter = 0; } *************** *** 89,98 **** { SelectorByLiquidity mostLiquid = new SelectorByLiquidity(this.tickerGroupID,false, ! currentDate.AddDays(-90), currentDate, this.numberOfEligibleTickers); this.eligibleTickers = mostLiquid.GetTableOfSelectedTickers(); SelectorByQuotationAtEachMarketDay quotedInEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay(this.eligibleTickers, ! false, currentDate.AddDays(-90),currentDate, ! this.numberOfEligibleTickers, "^MIBTEL"); return quotedInEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); } --- 70,79 ---- { SelectorByLiquidity mostLiquid = new SelectorByLiquidity(this.tickerGroupID,false, ! currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfEligibleTickers); this.eligibleTickers = mostLiquid.GetTableOfSelectedTickers(); SelectorByQuotationAtEachMarketDay quotedInEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay(this.eligibleTickers, ! false, currentDate.AddDays(-this.numDaysForLiquidity),currentDate, ! this.numberOfEligibleTickers, this.benchmark); return quotedInEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); } *************** *** 107,115 **** { ! IGenomeManager genManEfficientCTCPortfolio = new GenomeManagerForEfficientCTCPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-90), currentDate, this.numberOfTickersToBeChosen, ! this.numDaysOfPortfolioLife, 0.01); GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTCPortfolio); //GO.KeepOnRunningUntilConvergenceIsReached = true; --- 88,102 ---- { ! ! //double targetReturnForEachPeriodOfPortfolioLife = ! // Math.Pow(1.60,(double)(1.0/(360.0/this.numDaysOfPortfolioLife))) - 1.0; ! //the target has to be such that annual system return is minimum 50% ! //(with no commissions and bid-ask spreads) ! IGenomeManager genManEfficientCTCPortfolio = new GenomeManagerForEfficientCTCPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForLiquidity), currentDate, this.numberOfTickersToBeChosen, ! this.numDaysOfPortfolioLife, this.targetReturn, ! this.portfolioType); GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTCPortfolio); //GO.KeepOnRunningUntilConvergenceIsReached = true; *************** *** 127,131 **** /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { --- 114,118 ---- /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { *************** *** 141,152 **** #region MarketCloseEventHandler ! private void marketCloseEventHandler_orderChosenTickers_addToOrderList_forTicker(string ticker) ! { ! double cashForSinglePosition = this.account.CashAmount / this.numberOfTickersToBeChosen; ! long quantity = ! Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( ticker ) ) ); ! Order order = new Order( OrderType.MarketBuy, new Instrument( ticker ) , quantity ); ! this.orders.Add(order); ! } private void marketCloseEventHandler_orderChosenTickers_addToOrderList() { --- 128,132 ---- #region MarketCloseEventHandler ! private void marketCloseEventHandler_orderChosenTickers_addToOrderList() { *************** *** 156,160 **** this.lastChosenTickers[idx] = ticker; if(ticker != null) ! marketCloseEventHandler_orderChosenTickers_addToOrderList_forTicker( ticker ); idx++; } --- 136,140 ---- this.lastChosenTickers[idx] = ticker; if(ticker != null) ! this.AddOrderForTicker( ticker ); idx++; } *************** *** 199,203 **** } ! public void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { --- 179,183 ---- } ! public override void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { Index: GenomeManagerForEfficientCTCPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientCTCPortfolio.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** GenomeManagerForEfficientCTCPortfolio.cs 30 Jan 2005 19:53:12 -0000 1.1 --- GenomeManagerForEfficientCTCPortfolio.cs 11 Apr 2005 18:52:31 -0000 1.2 *************** *** 39,42 **** --- 39,43 ---- public class GenomeManagerForEfficientCTCPortfolio : GenomeManagerForEfficientPortfolio { + private int numDaysOfPortfolioLife; public GenomeManagerForEfficientCTCPortfolio(DataTable setOfInitialTickers, *************** *** 45,49 **** int numberOfTickersInPortfolio, int numDaysOfPortfolioLife, ! double targetPerformance) : base(setOfInitialTickers, --- 46,51 ---- int numberOfTickersInPortfolio, int numDaysOfPortfolioLife, ! double targetPerformance, ! PortfolioType portfolioType) : base(setOfInitialTickers, *************** *** 51,60 **** lastQuoteDate, numberOfTickersInPortfolio, ! numDaysOfPortfolioLife, ! targetPerformance) { ! } --- 53,63 ---- lastQuoteDate, numberOfTickersInPortfolio, ! targetPerformance, ! portfolioType) { ! this.numDaysOfPortfolioLife = numDaysOfPortfolioLife; ! this.retrieveData(); } Index: RunEfficientCTOPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/RunEfficientCTOPortfolio.cs,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** RunEfficientCTOPortfolio.cs 23 Mar 2005 21:35:37 -0000 1.16 --- RunEfficientCTOPortfolio.cs 11 Apr 2005 18:52:32 -0000 1.17 *************** *** 54,145 **** /// </summary> [Serializable] ! public class RunEfficientCTOPorfolio : Script { ! //DateTime lastDate = DateTime.Now.Date; ! //DateTime firstDate = DateTime.Now.Date.AddDays(-60); ! //these two members are used by the old script ! DateTime lastDate = new DateTime(2004,11,25); ! DateTime firstDate = new DateTime(2004,9,25); ! // ! private string tickerGroupID; ! private int numberOfEligibleTickers; ! private int numberOfTickersToBeChosen; ! private int numDaysForLiquidity; ! private int generationNumberForGeneticOptimizer; ! private int populationSizeForGeneticOptimizer; ! ! private ReportTable reportTable; ! private EndOfDayDateTime startDateTime; ! private EndOfDayDateTime endDateTime; ! //private int numIntervalDays;// number of days for the equity line graph ! private IHistoricalQuoteProvider historicalQuoteProvider = ! new HistoricalRawQuoteProvider(); ! ! ! //private ProgressBarForm progressBarForm; ! ! private EndOfDayTimerHandlerCTO endOfDayTimerHandler; ! ! private Account account; ! ! private IEndOfDayTimer endOfDayTimer; ! ! private string benchmark; ! ! public RunEfficientCTOPorfolio(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForLiquidity, int generationNumberForGeneticOptimizer, int populationSizeForGeneticOptimizer, string benchmark, ! DateTime startDate, DateTime endDate) { ! //this.progressBarForm = new ProgressBarForm(); ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; ! this.numDaysForLiquidity = numDaysForLiquidity; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.reportTable = new ReportTable( "Summary_Reports" ); ! this.startDateTime = new EndOfDayDateTime( ! startDate, EndOfDaySpecificTime.FiveMinutesBeforeMarketClose ); ! this.endDateTime = new EndOfDayDateTime( ! endDate, EndOfDaySpecificTime.OneHourAfterMarketClose ); ! this.benchmark = benchmark; ! //this.numIntervalDays = 3; } - #region Run ! ! private void run_FindBestPortfolioForNextTrade() ! { ! //"STOCKMI" ! /* ! * SelectorByLiquidity mostLiquid = new TickerSelector(SelectionType.Liquidity, ! false, "STOCKMI", firstDate, lastDate, 70); ! DataTable tickers = mostLiquid.GetTableOfSelectedTickers(); ! IGenomeManager genManEfficientCTOPortfolio = ! new GenomeManagerForEfficientCTOPortfolio(tickers,firstDate, ! lastDate, 6,1, 0.005); ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTOPortfolio); ! //GO.KeepOnRunningUntilConvergenceIsReached = true; ! GO.GenerationNumber = 10; ! GO.MutationRate = 0.05; ! GO.Run(true); ! //it has to be changed the decode implementation for this IGenomeManager ! System.Console.WriteLine("\n\nThe best solution found is: " + (string)GO.BestGenome.Meaning + ! " with {0} generations", GO.GenerationCounter); ! */ ! ; ! } ! ! private void run_initializeEndOfDayTimer() ! { ! this.endOfDayTimer = ! new IndexBasedEndOfDayTimer( this.startDateTime, this.benchmark ); ! } ! private void run_initializeAccount() { ! this.account = new Account( "EfficientCloseToOpenPortfolio" , this.endOfDayTimer , new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , this.historicalQuoteProvider ) , --- 54,82 ---- /// </summary> [Serializable] ! public class RunEfficientCTOPorfolio : RunEfficientPorfolio { ! public RunEfficientCTOPorfolio(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForLiquidity, int generationNumberForGeneticOptimizer, int populationSizeForGeneticOptimizer, string benchmark, ! DateTime startDate, DateTime endDate, double targetReturn, ! PortfolioType portfolioType): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForLiquidity, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, benchmark, ! startDate, endDate, targetReturn, ! portfolioType) { ! this.ScriptName = "OpenCloseScripts"; } ! #region Run ! ! /* ! protected override void run_initializeAccount() { ! this.account = new Account( this.ScriptName , this.endOfDayTimer , new HistoricalEndOfDayDataStreamer( this.endOfDayTimer , this.historicalQuoteProvider ) , *************** *** 148,152 **** } ! private void run_initializeEndOfDayTimerHandler() { this.endOfDayTimerHandler = new EndOfDayTimerHandlerCTO(this.tickerGroupID, --- 85,91 ---- } ! */ ! ! protected override void run_initializeEndOfDayTimerHandler() { this.endOfDayTimerHandler = new EndOfDayTimerHandlerCTO(this.tickerGroupID, *************** *** 157,245 **** this.generationNumberForGeneticOptimizer, this.populationSizeForGeneticOptimizer, ! this.benchmark); } - /* - private void inSampleNewProgressEventHandler( - Object sender , NewProgressEventArgs eventArgs ) - { - this.progressBarForm.ProgressBarInSample.Value = eventArgs.CurrentProgress; - this.progressBarForm.ProgressBarInSample.Refresh(); - } - private void run_initializeProgressHandlers() - { - this.endOfDayTimerHandler.InSampleNewProgress += - new InSampleNewProgressEventHandler( this.inSampleNewProgressEventHandler ); - } - */ - #region oneHourAfterMarketCloseEventHandler - /* - private void oneHourAfterMarketCloseEventHandler_handleProgessBarForm( - IEndOfDayTimer endOfDayTimer ) - { - long elapsedDays = Convert.ToInt64( ((TimeSpan)( endOfDayTimer.GetCurrentTime().DateTime - - this.startDateTime.DateTime )).TotalDays ); - long totalDays = Convert.ToInt64( ((TimeSpan)( this.endDateTime.DateTime - - this.startDateTime.DateTime )).TotalDays ); - if ( Math.Floor( elapsedDays / totalDays * 100 ) > - Math.Floor( ( elapsedDays - 1 ) / totalDays * 100 ) ) - // a new out of sample time percentage point has been elapsed - this.progressBarForm.ProgressBarOutOfSample.Value = - Convert.ToInt16( Math.Floor( elapsedDays / totalDays * 100 ) ); - } - public void oneHourAfterMarketCloseEventHandler( - Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) - { - this.oneHourAfterMarketCloseEventHandler_handleProgessBarForm( - ( IEndOfDayTimer )sender ); - if ( ( ( IEndOfDayTimer )sender ).GetCurrentTime().DateTime > - this.endDateTime.DateTime ) - { - // the simulation has reached the ending date - this.account.EndOfDayTimer.Stop(); - this.progressBarForm.Close(); - } - } - */ - #endregion ! private void checkDateForReport(Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) { ! //Report report; ! ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime>=this.endDateTime.DateTime ) ! { ! this.endOfDayTimer.Stop(); ! //report = new Report( this.account , this.historicalQuoteProvider ); ! //report.Show("CTO_Portfolio" , this.numIntervalDays , this.endDateTime , this.benchmark ); ! string name = "From"+this.numberOfEligibleTickers + ! "LiqDays" + this.numDaysForLiquidity + "Portfolio" + ! this.numberOfTickersToBeChosen + "GenNum" + ! this.generationNumberForGeneticOptimizer + ! "PopSize" + this.populationSizeForGeneticOptimizer; ! AccountReport accountReport = this.account.CreateReport(name,1,this.endDateTime,this.benchmark, ! new HistoricalAdjustedQuoteProvider()); ! ObjectArchiver.Archive(accountReport, ! System.Configuration.ConfigurationSettings.AppSettings["ReportsArchive"] + ! "\\OpenCloseScripts\\" + ! name + ".rep"); - ObjectArchiver.Archive(this.account, - System.Configuration.ConfigurationSettings.AppSettings["AccountsArchive"] + - "\\OpenCloseScripts\\" + - name + ".acc"); - - } } public override void Run() { ! //old script ! //this.run_FindBestPortfolioForNextTrade(); ! run_initializeEndOfDayTimer(); run_initializeAccount(); run_initializeEndOfDayTimerHandler(); ! //run_initializeProgressHandlers(); this.endOfDayTimer.MarketOpen += new MarketOpenEventHandler( --- 96,120 ---- this.generationNumberForGeneticOptimizer, this.populationSizeForGeneticOptimizer, ! this.benchmark, ! this.targetReturn, ! this.portfolioType); } ! protected override void run_initializeHistoricalQuoteProvider() { ! this.historicalQuoteProvider = ! new HistoricalRawQuoteProvider(); } + public override void Run() { ! run_initializeHistoricalQuoteProvider(); run_initializeEndOfDayTimer(); run_initializeAccount(); run_initializeEndOfDayTimerHandler(); ! this.endOfDayTimer.MarketOpen += new MarketOpenEventHandler( *************** *** 257,265 **** new OneHourAfterMarketCloseEventHandler( this.endOfDayTimerHandler.OneHourAfterMarketCloseEventHandler ); - //this.endOfDayTimer.OneHourAfterMarketClose += - //new OneHourAfterMarketCloseEventHandler( - //this.oneHourAfterMarketCloseEventHandler ); - //this.progressBarForm.Show(); this.endOfDayTimer.Start(); --- 132,136 ---- Index: GenomeManagerForEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientPortfolio.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** GenomeManagerForEfficientPortfolio.cs 6 Feb 2005 20:14:01 -0000 1.3 --- GenomeManagerForEfficientPortfolio.cs 11 Apr 2005 18:52:31 -0000 1.4 *************** *** 45,52 **** protected DateTime firstQuoteDate; protected DateTime lastQuoteDate; - protected int numDaysOfPortfolioLife; protected double targetPerformance; protected double variance; protected double rateOfReturn; //IGenomeManager implementation for properties --- 45,52 ---- protected DateTime firstQuoteDate; protected DateTime lastQuoteDate; protected double targetPerformance; protected double variance; protected double rateOfReturn; + protected PortfolioType portfolioType; //IGenomeManager implementation for properties *************** *** 77,86 **** } ! public GenomeManagerForEfficientPortfolio( DataTable setOfInitialTickers, ! DateTime firstQuoteDate, ! DateTime lastQuoteDate, ! int numberOfTickersInPortfolio, ! int numDaysOfPortfolioLife, ! double targetPerformance) { --- 77,92 ---- } ! public PortfolioType PortfolioType ! { ! get{return this.portfolioType;} ! set{this.portfolioType = value;} ! } ! ! public GenomeManagerForEfficientPortfolio(DataTable setOfInitialTickers, ! DateTime firstQuoteDate, ! DateTime lastQuoteDate, ! int numberOfTickersInPortfolio, ! double targetPerformance, ! PortfolioType portfolioType) { *************** *** 93,102 **** this.targetPerformance = targetPerformance; this.genomeSize = numberOfTickersInPortfolio; - this.numDaysOfPortfolioLife = numDaysOfPortfolioLife; //each genes is the index for the setOfTickers table this.minValueForGenes = 0; this.maxValueForGenes = this.setOfTickers.Rows.Count - 1; ! ! this.retrieveData(); } --- 99,106 ---- this.targetPerformance = targetPerformance; this.genomeSize = numberOfTickersInPortfolio; //each genes is the index for the setOfTickers table this.minValueForGenes = 0; this.maxValueForGenes = this.setOfTickers.Rows.Count - 1; ! this.portfolioType = portfolioType; } *************** *** 110,115 **** NormalDistribution normal = new NormalDistribution(portfolioRateOfReturn, Math.Sqrt(portfolioVariance)); ! returnValue = normal.GetProbability(this.targetPerformance*0.75,this.targetPerformance*1.25); ! //returnValue = 1 - normal.GetProbability(this.targetPerformance); return returnValue; } --- 114,122 ---- NormalDistribution normal = new NormalDistribution(portfolioRateOfReturn, Math.Sqrt(portfolioVariance)); ! if(this.portfolioType == PortfolioType.OnlyLong) ! returnValue = normal.GetProbability(this.targetPerformance*0.75,this.targetPerformance*1.25); ! else//only short orders are permitted ! returnValue = normal.GetProbability(-this.targetPerformance*1.25,-this.targetPerformance*0.75); ! return returnValue; } *************** *** 218,221 **** --- 225,229 ---- } } + //this protected method can be overriden by inherited classes //which can specify the type of rates of return *************** *** 250,254 **** returnValue += BasicFunctions.SimpleAverage((float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]); } ! //the investement is assumed to be equally divided return (returnValue/this.GenomeSize); } --- 258,262 ---- returnValue += BasicFunctions.SimpleAverage((float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]); } ! //the investment is assumed to be equally divided return (returnValue/this.GenomeSize); } Index: GenomeManagerForEfficientCTOPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientCTOPortfolio.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Genome... [truncated message content] |