[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting EndOfDayTimerHandler.cs,
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-09-29 21:19:46
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv16144 Modified Files: EndOfDayTimerHandler.cs EndOfDayTimerHandlerCTC.cs EndOfDayTimerHandlerCTCWeekly.cs EndOfDayTimerHandlerCTO.cs EndOfDayTimerHandlerCTO_WorstAtDay.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: EndOfDayTimerHandlerCTO_WorstAtDay.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTO_WorstAtDay.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** EndOfDayTimerHandlerCTO_WorstAtDay.cs 19 Aug 2008 17:11:26 -0000 1.4 --- EndOfDayTimerHandlerCTO_WorstAtDay.cs 29 Sep 2008 21:17:34 -0000 1.5 *************** *** 78,83 **** /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { AccountManager.ClosePositions(this.account); --- 78,83 ---- /// <param name="sender"></param> /// <param name="eventArgs"></param> ! protected override void marketOpenEventHandler( ! Object sender , DateTime dateTime ) { AccountManager.ClosePositions(this.account); *************** *** 91,96 **** double lossOfCurrentWorstCombination = 0.0; double fitnessOfPreviousCombination = 0.0; ! IndexBasedEndOfDayTimer currentTimer = (IndexBasedEndOfDayTimer)this.account.EndOfDayTimer; ! DateTime today = currentTimer.GetCurrentTime().DateTime; int numOfGenomesScanned = 0; for(int i = 0; numOfGenomesScanned < this.numOfGenomesForOTCScanning; i++) --- 91,97 ---- double lossOfCurrentWorstCombination = 0.0; double fitnessOfPreviousCombination = 0.0; ! IndexBasedEndOfDayTimer currentTimer = ! (IndexBasedEndOfDayTimer)this.account.Timer; ! DateTime today = currentTimer.GetCurrentDateTime(); int numOfGenomesScanned = 0; for(int i = 0; numOfGenomesScanned < this.numOfGenomesForOTCScanning; i++) *************** *** 134,139 **** ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { if(this.currentGO != null) --- 135,140 ---- ! protected override void marketCloseEventHandler( ! Object sender , DateTime dateTime ) { if(this.currentGO != null) *************** *** 144,148 **** } ! #region OneHourAfterMarketCloseEventHandler protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) --- 145,149 ---- } ! #region oneHourAfterMarketCloseEventHandler protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) *************** *** 222,227 **** /// <param name="sender"></param> /// <param name="eventArgs"></param> ! public override void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { this.seedForRandomGenerator++; --- 223,228 ---- /// <param name="sender"></param> /// <param name="eventArgs"></param> ! protected override void oneHourAfterMarketCloseEventHandler( ! Object sender , DateTime dateTime ) { this.seedForRandomGenerator++; *************** *** 230,234 **** this.numDaysBetweenEachOptimization - 1) { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); //sets tickers to be chosen next Market Close event this.numDaysElapsedSinceLastOptimization = 0; --- 231,235 ---- this.numDaysBetweenEachOptimization - 1) { ! this.setTickers( dateTime , false); //sets tickers to be chosen next Market Close event this.numDaysElapsedSinceLastOptimization = 0; Index: EndOfDayTimerHandler.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandler.cs,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** EndOfDayTimerHandler.cs 14 Aug 2008 23:29:40 -0000 1.28 --- EndOfDayTimerHandler.cs 29 Sep 2008 21:17:34 -0000 1.29 *************** *** 3,7 **** EndOfDayTimerHandler.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- EndOfDayTimerHandler.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; using System.Data; --- 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; using System.Data; *************** *** 33,79 **** namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { ! /// <summary> ! /// Base class for EndOfDayTimerHandlers for efficient portfolios ! /// </summary> ! [Serializable] ! public abstract class EndOfDayTimerHandler ! { ! protected DataTable eligibleTickers; protected WeightedPositions chosenWeightedPositions = null; ! protected string tickerGroupID; ! protected int numberOfEligibleTickers; ! protected int numberOfTickersToBeChosen; ! protected int numDaysForOptimizationPeriod; ! protected int numDaysBetweenEachOptimization = 0; ! protected int numDaysElapsedSinceLastOptimization = 0; ! protected int generationNumberForGeneticOptimizer; ! protected int populationSizeForGeneticOptimizer; ! protected Account account = null; ! 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 = 30; ! protected double maxPriceForMinimumCommission = 500; ! protected double targetReturn; ! protected PortfolioType portfolioType; ! ! protected bool stopLossConditionReached = false; ! protected bool takeProfitConditionReached = false; ! protected double currentAccountValue = 0.0; ! protected double previousAccountValue = 0.0; ! protected GenomeCounter genomeCounter; ! public GenomeCounter GenomeCounter ! { ! get{return this.genomeCounter;} ! } ! ! public int NumberOfEligibleTickers ! { ! get { return this.numberOfEligibleTickers; } ! } ! public Account Account { get { return this.account; } --- 33,79 ---- namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { ! /// <summary> ! /// Base class for EndOfDayTimerHandlers for efficient portfolios ! /// </summary> ! [Serializable] ! public abstract class EndOfDayTimerHandler ! { ! protected DataTable eligibleTickers; protected WeightedPositions chosenWeightedPositions = null; ! protected string tickerGroupID; ! protected int numberOfEligibleTickers; ! protected int numberOfTickersToBeChosen; ! protected int numDaysForOptimizationPeriod; ! protected int numDaysBetweenEachOptimization = 0; ! protected int numDaysElapsedSinceLastOptimization = 0; ! protected int generationNumberForGeneticOptimizer; ! protected int populationSizeForGeneticOptimizer; ! protected Account account = null; ! 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 = 30; ! protected double maxPriceForMinimumCommission = 500; ! protected double targetReturn; ! protected PortfolioType portfolioType; ! ! protected bool stopLossConditionReached = false; ! protected bool takeProfitConditionReached = false; ! protected double currentAccountValue = 0.0; ! protected double previousAccountValue = 0.0; ! protected GenomeCounter genomeCounter; ! public GenomeCounter GenomeCounter ! { ! get{return this.genomeCounter;} ! } ! public int NumberOfEligibleTickers ! { ! get { return this.numberOfEligibleTickers; } ! } ! ! public Account Account { get { return this.account; } *************** *** 81,284 **** } ! /// <summary> ! /// bestGenomes[ i ] contains an array list with the best genomes ! /// for generation i ! /// </summary> ! protected ArrayList bestGenomes; ! public ArrayList BestGenomes ! { ! get { return this.bestGenomes; } ! } ! private void endOfDayTimerHandler_initializeBasic(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, ! int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType) ! { ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; ! this.numDaysForOptimizationPeriod = numDaysForOptimizationPeriod; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.benchmark = benchmark; ! this.targetReturn = targetReturn; ! this.portfolioType = portfolioType; ! } ! public EndOfDayTimerHandler(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, ! int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType) ! { ! this.endOfDayTimerHandler_initializeBasic(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, ! numDaysForOptimizationPeriod, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType); ! this.account = account; ! ! } public EndOfDayTimerHandler(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, ! int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType) ! { ! this.endOfDayTimerHandler_initializeBasic(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, ! numDaysForOptimizationPeriod, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType); ! } ! public EndOfDayTimerHandler(WeightedPositions chosenWeightedPositions, ! PortfolioType portfolioType, ! Account account, ! string benchmark) ! { ! this.account = account; ! this.benchmark = benchmark; ! this.chosenWeightedPositions = chosenWeightedPositions; ! this.numberOfTickersToBeChosen = this.chosenWeightedPositions.Count; ! this.portfolioType = portfolioType; ! } #region addGenomeToBestGenomes protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers) ); } protected void addGenomeToBestGenomes(BruteForceOptimizableParameters BFOptimizableParameters, ! SignedTickers signedTickers, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(BFOptimizableParameters, ! signedTickers.ArrayForSignedTickers, ! firstOptimizationDate, ! secondOptimizationDate, ! eligibleTickers) ); } protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers, ! halfPeriodDays)); } protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays, ! PortfolioType portfolioType) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers, ! halfPeriodDays, portfolioType)); } ! protected void addGenomesToBestGenomes( Genome[] genomes, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays, ! PortfolioType portfolioType, ! int createdGenerations) ! { foreach(Genome genome in genomes) if(genome != null) ! this.addGenomeToBestGenomes(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! eligibleTickers, halfPeriodDays, ! portfolioType, ! createdGenerations); ! } protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays, ! PortfolioType portfolioType, ! int createdGenerations) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers, ! halfPeriodDays, portfolioType, createdGenerations)); } #endregion protected virtual void openPositions() ! { ! //add cash first ! if(this.account.Transactions.Count == 0) ! this.account.AddCash(15000); ! if(this.chosenWeightedPositions != null) ! AccountManager.OpenPositions(this.chosenWeightedPositions, this.account); ! } ! protected void reversePositions() { if ( this.portfolioType == PortfolioType.OnlyLong || ! this.portfolioType == PortfolioType.OnlyShort ) throw new Exception ("It's not possible to reverse positions " + ! "when portfolio has to contain only " + ! "long or short positions!"); AccountManager.ReversePositions(this.account); ! } ! public abstract void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ; ! public abstract void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! ; ! public abstract void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! ; ! } } --- 81,296 ---- } ! /// <summary> ! /// bestGenomes[ i ] contains an array list with the best genomes ! /// for generation i ! /// </summary> ! protected ArrayList bestGenomes; ! public ArrayList BestGenomes ! { ! get { return this.bestGenomes; } ! } ! private void endOfDayTimerHandler_initializeBasic(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, ! int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType) ! { ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; ! this.numDaysForOptimizationPeriod = numDaysForOptimizationPeriod; ! this.generationNumberForGeneticOptimizer = generationNumberForGeneticOptimizer; ! this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; ! this.benchmark = benchmark; ! this.targetReturn = targetReturn; ! this.portfolioType = portfolioType; ! } public EndOfDayTimerHandler(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, ! int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType) ! { ! this.endOfDayTimerHandler_initializeBasic(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, ! numDaysForOptimizationPeriod, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType); ! this.account = account; ! ! } ! public EndOfDayTimerHandler(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, ! int numDaysForOptimizationPeriod, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType) ! { ! this.endOfDayTimerHandler_initializeBasic(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, ! numDaysForOptimizationPeriod, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType); ! } ! public EndOfDayTimerHandler(WeightedPositions chosenWeightedPositions, ! PortfolioType portfolioType, ! Account account, ! string benchmark) ! { ! this.account = account; ! this.benchmark = benchmark; ! this.chosenWeightedPositions = chosenWeightedPositions; ! this.numberOfTickersToBeChosen = this.chosenWeightedPositions.Count; ! this.portfolioType = portfolioType; ! } #region addGenomeToBestGenomes protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers) ); } protected void addGenomeToBestGenomes(BruteForceOptimizableParameters BFOptimizableParameters, ! SignedTickers signedTickers, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(BFOptimizableParameters, ! signedTickers.ArrayForSignedTickers, ! firstOptimizationDate, ! secondOptimizationDate, ! eligibleTickers) ); } protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers, ! halfPeriodDays)); } protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays, ! PortfolioType portfolioType) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers, ! halfPeriodDays, portfolioType)); } ! protected void addGenomesToBestGenomes( Genome[] genomes, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays, ! PortfolioType portfolioType, ! int createdGenerations) ! { foreach(Genome genome in genomes) if(genome != null) ! this.addGenomeToBestGenomes(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! eligibleTickers, halfPeriodDays, ! portfolioType, ! createdGenerations); ! } protected void addGenomeToBestGenomes(Genome genome, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers, int halfPeriodDays, ! PortfolioType portfolioType, ! int createdGenerations) { if(this.bestGenomes == null) this.bestGenomes = new ArrayList(); ! this.bestGenomes.Add(new GenomeRepresentation(genome, ! firstOptimizationDate, ! secondOptimizationDate, ! genome.Generation, ! eligibleTickers, ! halfPeriodDays, portfolioType, createdGenerations)); } #endregion protected virtual void openPositions() ! { ! //add cash first ! if(this.account.Transactions.Count == 0) ! this.account.AddCash(15000); ! if(this.chosenWeightedPositions != null) ! AccountManager.OpenPositions(this.chosenWeightedPositions, this.account); ! } ! protected void reversePositions() { if ( this.portfolioType == PortfolioType.OnlyLong || ! this.portfolioType == PortfolioType.OnlyShort ) throw new Exception ("It's not possible to reverse positions " + ! "when portfolio has to contain only " + ! "long or short positions!"); AccountManager.ReversePositions(this.account); ! } ! #region NewDateTimeEventHandler ! protected abstract void marketOpenEventHandler( ! Object sender , DateTime dateTime ) ; ! protected abstract void marketCloseEventHandler( ! Object sender , DateTime dateTime ) ! ; ! protected abstract void oneHourAfterMarketCloseEventHandler( ! Object sender , DateTime 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 ); ! } ! #endregion NewDateTimeEventHandler ! } } Index: EndOfDayTimerHandlerCTCWeekly.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTCWeekly.cs,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** EndOfDayTimerHandlerCTCWeekly.cs 14 Jan 2008 23:32:31 -0000 1.9 --- EndOfDayTimerHandlerCTCWeekly.cs 29 Sep 2008 21:17:34 -0000 1.10 *************** *** 3,7 **** EndOfDayTimerHandlerCTCWeekly.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- EndOfDayTimerHandlerCTCWeekly.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; using System.Data; --- 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; using System.Data; *************** *** 37,180 **** { ! /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to close ! /// weekly portfolio (with a given days of life)! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerCTCWeekly : EndOfDayTimerHandler ! { ! protected int numDaysForReturnCalculation; ! protected double maxAcceptableCloseToCloseDrawdown; ! ! public EndOfDayTimerHandlerCTCWeekly(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, ! int numDaysForReturnCalculation, ! double targetReturn, ! PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; ! } ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ; ! } ! ! #region MarketCloseEventHandler ! ! ! ! protected void updateStopLossCondition() ! { ! this.previousAccountValue = this.currentAccountValue; ! this.currentAccountValue = this.account.GetMarketValue(); ! if((this.currentAccountValue - this.previousAccountValue) ! /this.previousAccountValue < -this.maxAcceptableCloseToCloseDrawdown) ! { ! this.stopLossConditionReached = true; ! } ! else ! { ! this.stopLossConditionReached = false; ! } ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! this.updateStopLossCondition(); ! ! if(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime.DayOfWeek == ! DayOfWeek.Monday) ! this.openPositions(); ! ! if(this.stopLossConditionReached || ! endOfDayTimingEventArgs.EndOfDayDateTime.DateTime.DayOfWeek == ! DayOfWeek.Friday) ! AccountManager.ClosePositions(this.account); ! ! } ! ! #endregion ! ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { ! SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, ! currentDate); ! ! this.eligibleTickers = temporizedGroup.GetTableOfSelectedTickers(); ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromEligible = ! new SelectorByQuotationAtEachMarketDay(this.eligibleTickers, ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod),currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! return quotedAtEachMarketDayFromEligible.GetTableOfSelectedTickers(); ! } ! ! ! protected virtual void setTickers(DateTime currentDate) ! { ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is meaningful only if the initial set of tickers is ! //larger than the number of tickers to be chosen ! ! { ! ! //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.numDaysForOptimizationPeriod), ! currentDate, this.numberOfTickersToBeChosen, ! this.numDaysForReturnCalculation, ! this.targetReturn, ! this.portfolioType, this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer); ! //GO.KeepOnRunningUntilConvergenceIsReached = true; ! GO.Run(false); this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)GO.BestGenome.Meaning).Tickers) ); ! } ! //else it will be buyed again the previous optimized portfolio ! //that's it the actual chosenTickers member ! } ! /// <summary> ! /// Handles a "One hour after market close" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ! if(this.account.Portfolio.Count == 0 && ! endOfDayTimingEventArgs.EndOfDayDateTime.DateTime.DayOfWeek == ! DayOfWeek.Friday) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime); ! //it sets tickers to be chosen at next Monday ! } ! } ! #endregion ! } } --- 37,177 ---- { ! /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to close ! /// weekly portfolio (with a given days of life)! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerCTCWeekly : EndOfDayTimerHandler ! { ! protected int numDaysForReturnCalculation; ! protected double maxAcceptableCloseToCloseDrawdown; ! ! public EndOfDayTimerHandlerCTCWeekly(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, ! int numDaysForReturnCalculation, ! double targetReturn, ! PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; ! } ! protected override void marketOpenEventHandler( ! Object sender , DateTime dateTime ) ! { ! ; ! } ! ! #region marketCloseEventHandler ! protected void updateStopLossCondition() ! { ! this.previousAccountValue = this.currentAccountValue; ! this.currentAccountValue = this.account.GetMarketValue(); ! if((this.currentAccountValue - this.previousAccountValue) ! /this.previousAccountValue < -this.maxAcceptableCloseToCloseDrawdown) ! { ! this.stopLossConditionReached = true; ! } ! else ! { ! this.stopLossConditionReached = false; ! } ! } ! ! protected override void marketCloseEventHandler( ! Object sender , DateTime dateTime ) ! { ! this.updateStopLossCondition(); ! ! if(dateTime.DayOfWeek == ! DayOfWeek.Monday) ! this.openPositions(); ! ! if(this.stopLossConditionReached || ! dateTime.DayOfWeek == ! DayOfWeek.Friday) ! AccountManager.ClosePositions(this.account); ! ! } ! ! #endregion marketCloseEventHandler ! ! #region oneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { ! SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, ! currentDate); ! ! this.eligibleTickers = temporizedGroup.GetTableOfSelectedTickers(); ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromEligible = ! new SelectorByQuotationAtEachMarketDay(this.eligibleTickers, ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod),currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! return quotedAtEachMarketDayFromEligible.GetTableOfSelectedTickers(); ! } ! ! ! protected virtual void setTickers(DateTime currentDate) ! { ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is meaningful only if the initial set of tickers is ! //larger than the number of tickers to be chosen ! ! { ! ! //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.numDaysForOptimizationPeriod), ! currentDate, this.numberOfTickersToBeChosen, ! this.numDaysForReturnCalculation, ! this.targetReturn, ! this.portfolioType, this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer); ! //GO.KeepOnRunningUntilConvergenceIsReached = true; ! GO.Run(false); this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)GO.BestGenome.Meaning).Tickers) ); ! } ! //else it will be buyed again the previous optimized portfolio ! //that's it the actual chosenTickers member ! } ! /// <summary> ! /// Handles a "One hour after market close" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! protected override void oneHourAfterMarketCloseEventHandler( ! Object sender , DateTime dateTime ) ! { ! ! if(this.account.Portfolio.Count == 0 && ! dateTime.DayOfWeek == ! DayOfWeek.Friday) ! { ! this.setTickers(dateTime); ! //it sets tickers to be chosen at next Monday ! } ! } ! #endregion oneHourAfterMarketCloseEventHandler ! } } Index: EndOfDayTimerHandlerCTC.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTC.cs,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** EndOfDayTimerHandlerCTC.cs 14 Jan 2008 23:32:30 -0000 1.25 --- EndOfDayTimerHandlerCTC.cs 29 Sep 2008 21:17:34 -0000 1.26 *************** *** 3,7 **** EndOfDayTimerHandlerCTC.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- EndOfDayTimerHandlerCTC.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; using System.Data; --- 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; using System.Data; *************** *** 37,259 **** { ! /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to close ! /// portfolio (with a given days of life)! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerCTC : EndOfDayTimerHandler ! { ! protected int numDaysOfPortfolioLife; ! protected int numDaysForReturnCalculation; ! protected int numDaysWithNoPositions; ! protected int daysCounterWithPositions; ! protected int daysCounterWithNoPositions; ! protected double maxAcceptableCloseToCloseDrawdown; ! ! public EndOfDayTimerHandlerCTC(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, ! int numDaysOfPortfolioLife, ! int numDaysForReturnCalculation, ! int numDaysWithNoPositions, ! double targetReturn, ! PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown, ! int numDaysBetweenEachOptimization): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysOfPortfolioLife = numDaysOfPortfolioLife; ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.numDaysWithNoPositions = numDaysWithNoPositions; ! this.daysCounterWithPositions = 0; ! this.daysCounterWithNoPositions = 0; ! this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; ! this.stopLossConditionReached = false; ! this.currentAccountValue = 0.0; ! this.previousAccountValue = 0.0; ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! } ! ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ; ! } ! #region MarketCloseEventHandler ! ! ! ! protected void updateStopLossCondition() ! { ! this.previousAccountValue = this.currentAccountValue; ! this.currentAccountValue = this.account.GetMarketValue(); ! if((this.currentAccountValue - this.previousAccountValue) ! /this.previousAccountValue < -this.maxAcceptableCloseToCloseDrawdown) ! { ! this.stopLossConditionReached = true; ! } ! else ! { ! this.stopLossConditionReached = false; ! } ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! //bool positionsJustClosed = false; ! this.updateStopLossCondition(); ! if(this.account.Portfolio.Count > 0) ! //portfolio is not empty ! { ! this.daysCounterWithPositions++; ! if(this.daysCounterWithPositions == this.numDaysOfPortfolioLife || ! this.stopLossConditionReached) ! //num days of portfolio life or ! //max acceptable close to close drawdown reached ! { ! AccountManager.ClosePositions(this.account); ! this.daysCounterWithPositions = 0; ! //positionsJustClosed = true; ! } ! } ! else//portfolio is empty ! { this.daysCounterWithNoPositions++; if(this.daysCounterWithNoPositions == this.numDaysWithNoPositions || ! this.Account.Transactions.Count <= 1) { AccountManager.OpenPositions(this.chosenWeightedPositions,this.account); ! this.daysCounterWithNoPositions = 0; } } ! } ! //old ! // if(this.account.Portfolio.Count == 0 && ! // !positionsJustClosed) ! // //portfolio is empty but it has not been closed ! // //at the current close ! // { ! // this.openPositions(); ! // this.daysCounter = 0; ! // } ! #endregion ! ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { ! SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, ! currentDate); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketFromTemporized = ! new SelectorByQuotationAtEachMarketDay(temporizedGroup.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod),currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! this.eligibleTickers = quotedAtEachMarketFromTemporized.GetTableOfSelectedTickers(); ! ! // SelectorByCloseToCloseCorrelationToBenchmark lessCorrelatedFromEligible = ! // new SelectorByCloseToCloseCorrelationToBenchmark(this.eligibleTickers, ! // this.benchmark,true, ! // currentDate.AddDays(-this.numDaysForOptimizationPeriod ), ! // currentDate, ! // this.numberOfEligibleTickers/2, ! // this.numDaysForReturnCalculation); ! // SelectorByAbsolutePerformance mostQuietFromLessCorrelated = ! // new SelectorByAbsolutePerformance(lessCorrelatedFromEligible.GetTableOfSelectedTickers(), ! // true,currentDate.AddDays(-this.numDaysForOptimizationPeriod ), ! // currentDate, ! // this.numberOfEligibleTickers/4, ! // 0.01f, 0.10f); ! // ! // return mostQuietFromLessCorrelated.GetTableOfSelectedTickers(); ! return this.eligibleTickers; ! } ! ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is meaningful only if the initial set of tickers is ! //larger than the number of tickers to be chosen ! ! { ! ! //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.numDaysForOptimizationPeriod), ! currentDate, this.numberOfTickersToBeChosen, ! this.numDaysForReturnCalculation, ! this.targetReturn, ! this.portfolioType, this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! ConstantsProvider.SeedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); ! ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,((GenomeManagerForEfficientCTCPortfolio)genManEfficientCTCPortfolio).FirstQuoteDate, ! ((GenomeManagerForEfficientCTCPortfolio)genManEfficientCTCPortfolio).LastQuoteDate, ! setOfTickersToBeOptimized.Rows.Count); this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)GO.BestGenome.Meaning).Tickers) ); } ! //else it will be buyed again the previous optimized portfolio ! //that's it the actual chosenTickers member ! } ! ! /// <summary> ! /// Handles a "One hour after market close" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ConstantsProvider.SeedForRandomGenerator++; ! //old ! // if(this.account.Portfolio.Count == 0 ) ! // { ! // this.orders.Clear(); ! // this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! // //it sets tickers to be chosen at next close ! // } ! ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); ! this.numDaysElapsedSinceLastOptimization++; ! if(this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization) ! //num days without optimization has elapsed or ! //just money has been added to the account ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Open event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! ! } ! #endregion ! } } --- 37,259 ---- { ! /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to close ! /// portfolio (with a given days of life)! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerCTC : EndOfDayTimerHandler ! { ! protected int numDaysOfPortfolioLife; ! protected int numDaysForReturnCalculation; ! protected int numDaysWithNoPositions; ! protected int daysCounterWithPositions; ! protected int daysCounterWithNoPositions; ! protected double maxAcceptableCloseToCloseDrawdown; ! ! public EndOfDayTimerHandlerCTC(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, ! int numDaysOfPortfolioLife, ! int numDaysForReturnCalculation, ! int numDaysWithNoPositions, ! double targetReturn, ! PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown, ! int numDaysBetweenEachOptimization): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysOfPortfolioLife = numDaysOfPortfolioLife; ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.numDaysWithNoPositions = numDaysWithNoPositions; ! this.daysCounterWithPositions = 0; ! this.daysCounterWithNoPositions = 0; ! this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; ! this.stopLossConditionReached = false; ! this.currentAccountValue = 0.0; ! this.previousAccountValue = 0.0; ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! } ! ! protected override void marketOpenEventHandler( ! Object sender , DateTime dateTime ) ! { ! ; ! } ! #region marketCloseEventHandler ! ! ! ! protected void updateStopLossCondition() ! { ! this.previousAccountValue = this.currentAccountValue; ! this.currentAccountValue = this.account.GetMarketValue(); ! if((this.currentAccountValue - this.previousAccountValue) ! /this.previousAccountValue < -this.maxAcceptableCloseToCloseDrawdown) ! { ! this.stopLossConditionReached = true; ! } ! else ! { ! this.stopLossConditionReached = false; ! } ! } ! ! protected override void marketCloseEventHandler( ! Object sender , DateTime dateTime ) ! { ! //bool positionsJustClosed = false; ! this.updateStopLossCondition(); ! if(this.account.Portfolio.Count > 0) ! //portfolio is not empty ! { ! this.daysCounterWithPositions++; ! if(this.daysCounterWithPositions == this.numDaysOfPortfolioLife || ! this.stopLossConditionReached) ! //num days of portfolio life or ! //max acceptable close to close drawdown reached ! { ! AccountManager.ClosePositions(this.account); ! this.daysCounterWithPositions = 0; ! //positionsJustClosed = true; ! } ! } ! else//portfolio is empty ! { this.daysCounterWithNoPositions++; if(this.daysCounterWithNoPositions == this.numDaysWithNoPositions || ! this.Account.Transactions.Count <= 1) { AccountManager.OpenPositions(this.chosenWeightedPositions,this.account); ! this.daysCounterWithNoPositions = 0; } } ! } ! //old ! // if(this.account.Portfolio.Count == 0 && ! // !positionsJustClosed) ! // //portfolio is empty but it has not been closed ! // //at the current close ! // { ! // this.openPositions(); ! // this.daysCounter = 0; ! // } ! #endregion marketCloseEventHandler ! ! #region oneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { ! SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, ! currentDate); ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketFromTemporized = ! new SelectorByQuotationAtEachMarketDay(temporizedGroup.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod),currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! this.eligibleTickers = quotedAtEachMarketFromTemporized.GetTableOfSelectedTickers(); ! ! // SelectorByCloseToCloseCorrelationToBenchmark lessCorrelatedFromEligible = ! // new SelectorByCloseToCloseCorrelationToBenchmark(this.eligibleTickers, ! // this.benchmark,true, ! // currentDate.AddDays(-this.numDaysForOptimizationPeriod ), ! // currentDate, ! // this.numberOfEligibleTickers/2, ! // this.numDaysForReturnCalculation); ! // SelectorByAbsolutePerformance mostQuietFromLessCorrelated = ! // new SelectorByAbsolutePerformance(lessCorrelatedFromEligible.GetTableOfSelectedTickers(), ! // true,currentDate.AddDays(-this.numDaysForOptimizationPeriod ), ! // currentDate, ! // this.numberOfEligibleTickers/4, ! // 0.01f, 0.10f); ! // ! // return mostQuietFromLessCorrelated.GetTableOfSelectedTickers(); ! ! return this.eligibleTickers; ! } ! ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is meaningful only if the initial set of tickers is ! //larger than the number of tickers to be chosen ! ! { ! ! //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.numDaysForOptimizationPeriod), ! currentDate, this.numberOfTickersToBeChosen, ! this.numDaysForReturnCalculation, ! this.targetReturn, ! this.portfolioType, this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! ConstantsProvider.SeedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); ! ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,((GenomeManagerForEfficientCTCPortfolio)genManEfficientCTCPortfolio).FirstQuoteDate, ! ((GenomeManagerForEfficientCTCPortfolio)genManEfficientCTCPortfolio).LastQuoteDate, ! setOfTickersToBeOptimized.Rows.Count); this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! ... [truncated message content] |