[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting EndOfDayTimerHandlerCTO_W
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-08-19 17:12:04
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv2916/b7_Scripts/TickerSelectionTesting Modified Files: EndOfDayTimerHandlerCTO_WorstAtDay.cs EndOfDayTimerHandlerOTC_WorstAtNight.cs EndOfDayTimerHandlerOTCCTO.cs Log Message: - the code has been cleaned up to avoid warnings - standard indentation has been applied Index: EndOfDayTimerHandlerOTCCTO.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerOTCCTO.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** EndOfDayTimerHandlerOTCCTO.cs 14 Jan 2008 23:32:31 -0000 1.8 --- EndOfDayTimerHandlerOTCCTO.cs 19 Aug 2008 17:11:27 -0000 1.9 *************** *** 3,7 **** EndOfDayTimerHandlerOTCCTO.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- EndOfDayTimerHandlerOTCCTO.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,109 **** { ! /// <summary> ! /// Implements MarketOpenEventHandler, ! /// TwoMinutesBeforeMarketCloseEventHandler and OneHourAfterMarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to open portfolio! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerOTCCTO : EndOfDayTimerHandler ! { ! protected int seedForRandomGenerator; ! ! public EndOfDayTimerHandlerOTCCTO(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType, int numDaysBetweenEachOptimization): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.numDaysElapsedSinceLastOptimization = 0; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; ! } ! ! ! /// <summary> ! /// Handles a "Market Open" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! //temporarily the if condition ! //if(this.numDaysElapsedSinceLastOptimization == 0) ! AccountManager.ClosePositions(this.account); ! this.openPositions(); ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ! //temporarily ! //if(this.numDaysElapsedSinceLastOptimization == ! // this.numDaysBetweenEachOptimization) ! AccountManager.ClosePositions(this.account); ! try{ ! this.chosenWeightedPositions.Reverse(); ! this.openPositions(); ! } ! catch(Exception ex){ ! ex = ex; ! } ! this.chosenWeightedPositions.Reverse(); ! } ! ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { ! /* ! SelectorByAverageRawOpenPrice selectorByOpenPrice = new SelectorByAverageRawOpenPrice(this.tickerGroupID, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, --- 37,110 ---- { ! /// <summary> ! /// Implements MarketOpenEventHandler, ! /// TwoMinutesBeforeMarketCloseEventHandler and OneHourAfterMarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to open portfolio! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerOTCCTO : EndOfDayTimerHandler ! { ! protected int seedForRandomGenerator; ! ! public EndOfDayTimerHandlerOTCCTO(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType, int numDaysBetweenEachOptimization): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.numDaysElapsedSinceLastOptimization = 0; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; ! } ! ! ! /// <summary> ! /// Handles a "Market Open" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! //temporarily the if condition ! //if(this.numDaysElapsedSinceLastOptimization == 0) ! AccountManager.ClosePositions(this.account); ! this.openPositions(); ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ! //temporarily ! //if(this.numDaysElapsedSinceLastOptimization == ! // this.numDaysBetweenEachOptimization) ! AccountManager.ClosePositions(this.account); ! try{ ! this.chosenWeightedPositions.Reverse(); ! this.openPositions(); ! } ! catch(Exception ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! this.chosenWeightedPositions.Reverse(); ! } ! ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { ! /* ! SelectorByAverageRawOpenPrice selectorByOpenPrice = new SelectorByAverageRawOpenPrice(this.tickerGroupID, false, currentDate.AddDays(-this.numDaysForLiquidity), currentDate, *************** *** 111,203 **** this.maxPriceForMinimumCommission, 0, 2); DataTable tickersByPrice = selectorByOpenPrice.GetTableOfSelectedTickers(); ! */ ! ! SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); ! DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); ! SelectorByLiquidity mostLiquid = ! new SelectorByLiquidity(tickersFromGroup, ! false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = ! new SelectorByQuotationAtEachMarketDay(mostLiquid.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! return quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); ! ! } ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is possible only if the initial set of tickers is ! //as large as the number of tickers to be chosen ! ! { ! IGenomeManager genManEfficientOTCCTOPortfolio = ! new GenomeManagerForEfficientOTCCTOPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, ! this.numberOfTickersToBeChosen, ! this.targetReturn, ! this.portfolioType, ! this.benchmark); ! ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientOTCCTOPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); ! ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, 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 ! } ! protected void oneHourAfterMarketCloseEventHandler_updatePrices() ! { ! //min price for minimizing commission amount ! //according to IB Broker's commission scheme ! this.minPriceForMinimumCommission = this.account.CashAmount/(this.numberOfTickersToBeChosen*100); ! this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; ! //just to avoid warning message ! } ! ! /// <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 ) ! { ! this.seedForRandomGenerator++; ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); ! if(this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization - 1) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Open event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! else ! { ! this.numDaysElapsedSinceLastOptimization++; ! } ! ! } ! ! #endregion ! } } --- 112,204 ---- this.maxPriceForMinimumCommission, 0, 2); DataTable tickersByPrice = selectorByOpenPrice.GetTableOfSelectedTickers(); ! */ ! ! SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); ! DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); ! SelectorByLiquidity mostLiquid = ! new SelectorByLiquidity(tickersFromGroup, ! false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = ! new SelectorByQuotationAtEachMarketDay(mostLiquid.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! return quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(); ! ! } ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is possible only if the initial set of tickers is ! //as large as the number of tickers to be chosen ! ! { ! IGenomeManager genManEfficientOTCCTOPortfolio = ! new GenomeManagerForEfficientOTCCTOPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, ! this.numberOfTickersToBeChosen, ! this.targetReturn, ! this.portfolioType, ! this.benchmark); ! ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientOTCCTOPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); ! ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, 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 ! } ! protected void oneHourAfterMarketCloseEventHandler_updatePrices() ! { ! //min price for minimizing commission amount ! //according to IB Broker's commission scheme ! this.minPriceForMinimumCommission = this.account.CashAmount/(this.numberOfTickersToBeChosen*100); ! this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; ! //just to avoid warning message ! } ! /// <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 ) ! { ! this.seedForRandomGenerator++; ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); ! if(this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization - 1) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Open event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! else ! { ! this.numDaysElapsedSinceLastOptimization++; ! } ! ! } ! ! #endregion ! ! } } Index: EndOfDayTimerHandlerCTO_WorstAtDay.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerCTO_WorstAtDay.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** EndOfDayTimerHandlerCTO_WorstAtDay.cs 14 Jan 2008 23:32:31 -0000 1.3 --- EndOfDayTimerHandlerCTO_WorstAtDay.cs 19 Aug 2008 17:11:26 -0000 1.4 *************** *** 3,7 **** EndOfDayTimerHandlerCTO_WorstAtDay.cs ! Copyright (C) 2007 Marco Milletti --- 3,7 ---- EndOfDayTimerHandlerCTO_WorstAtDay.cs ! Copyright (C) 2007 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; *************** *** 38,88 **** { ! /// <summary> ! /// Implements MarketOpenEventHandler, ! /// MarketCloseEventHandler, ! /// OneHourAfterMarketCloseEventHandler ! /// These handlers contain the core strategy ! /// for the efficient close to open worst by day portfolio! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerCTO_WorstAtDay : EndOfDayTimerHandler ! { ! protected int seedForRandomGenerator; ! protected GeneticOptimizer currentGO; protected int numOfGenomesForOTCScanning; ! ! public EndOfDayTimerHandlerCTO_WorstAtDay(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType, int numDaysBetweenEachOptimization, ! int numOfGenomesForOTCScanning): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.numDaysElapsedSinceLastOptimization = 0; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; ! this.numOfGenomesForOTCScanning = numOfGenomesForOTCScanning; ! } ! ! /// <summary> ! /// Handles a "Market Open" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! AccountManager.ClosePositions(this.account); ! } ! private void marketCloseEventHandler_chooseTheWorstOTCGenome() { int populationSize = this.currentGO.PopulationSize; --- 38,88 ---- { ! /// <summary> ! /// Implements MarketOpenEventHandler, ! /// MarketCloseEventHandler, ! /// OneHourAfterMarketCloseEventHandler ! /// These handlers contain the core strategy ! /// for the efficient close to open worst by day portfolio! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerCTO_WorstAtDay : EndOfDayTimerHandler ! { ! protected int seedForRandomGenerator; ! protected GeneticOptimizer currentGO; protected int numOfGenomesForOTCScanning; ! ! public EndOfDayTimerHandlerCTO_WorstAtDay(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType, int numDaysBetweenEachOptimization, ! int numOfGenomesForOTCScanning): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.numDaysElapsedSinceLastOptimization = 0; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; ! this.numOfGenomesForOTCScanning = numOfGenomesForOTCScanning; ! } ! ! /// <summary> ! /// Handles a "Market Open" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! AccountManager.ClosePositions(this.account); ! } ! private void marketCloseEventHandler_chooseTheWorstOTCGenome() { int populationSize = this.currentGO.PopulationSize; *************** *** 92,243 **** 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++) ! { ! if( i == 0 || ! ( ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness < ! fitnessOfPreviousCombination ) ) ! //it is the best genome or the current genome is different from - and ! //so it has to be strictly less of - the previous scanned genome ! { ! fitnessOfPreviousCombination = ! ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness; ! try{ ! //retrieves loss at day for the CurrentCombination ! SignedTickers signedTickers = new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).Tickers); ! WeightedPositions weightedPositions = ! new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).TickersPortfolioWeights, ! signedTickers); ! lossOfCurrentCombination = ! - weightedPositions.GetOpenToCloseReturn( today ); ! //- because fitness in GenomeOTCCTO is just the reversal of what needed ! numOfGenomesScanned++; ! if(lossOfCurrentCombination < lossOfCurrentWorstCombination) ! { ! indexOfWorstCombination = populationSize - i - 1; ! lossOfCurrentWorstCombination = lossOfCurrentCombination; ! } ! } ! catch(Exception ex){ ! //if retrieving of loss of current combination fails ! ex = ex; ! } ! } ! } this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).Tickers)); } ! ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! if(this.currentGO != null) ! //so a list of genomes is available this.marketCloseEventHandler_chooseTheWorstOTCGenome(); ! this.openPositions(); ! } #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); SelectorByLiquidity mostLiquid = new SelectorByLiquidity(tickersFromGroup, ! false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay(mostLiquid.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! SelectorByAverageRawOpenPrice byPrice = new SelectorByAverageRawOpenPrice(quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(), ! false,currentDate.AddDays(-30), ! currentDate, ! this.numberOfEligibleTickers, ! 30); ! return byPrice.GetTableOfSelectedTickers(); ! } ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen * 2) ! //the optimization process is possible only if the initial set of tickers is ! //as large as the number of tickers to be chosen ! ! { ! IGenomeManager genManEfficientCTOPortfolio = ! new GenomeManagerForEfficientOTCCTOPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, ! this.numberOfTickersToBeChosen, ! this.targetReturn, ! this.portfolioType, ! this.benchmark); ! ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTOPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); GO.MutationRate = 0.25; GO.CrossoverRate = 0.95; ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, setOfTickersToBeOptimized.Rows.Count); this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)GO.BestGenome.Meaning).Tickers) ); this.currentGO = GO; ! } ! //else it will be buyed again the previous optimized portfolio ! //that's it the actual chosenTickers member ! } ! protected void oneHourAfterMarketCloseEventHandler_updatePrices() ! { ! //min price for minimizing commission amount ! //according to IB Broker's commission scheme ! this.minPriceForMinimumCommission = this.account.CashAmount/(this.numberOfTickersToBeChosen*100); ! this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; ! //just to avoid warning message ! } ! ! /// <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 ) ! { ! this.seedForRandomGenerator++; ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); ! if(this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization - 1) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Close event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! else ! { ! this.numDaysElapsedSinceLastOptimization++; ! } ! ! } ! #endregion ! } } --- 92,244 ---- 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++) ! { ! if( i == 0 || ! ( ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness < ! fitnessOfPreviousCombination ) ) ! //it is the best genome or the current genome is different from - and ! //so it has to be strictly less of - the previous scanned genome ! { ! fitnessOfPreviousCombination = ! ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness; ! try{ ! //retrieves loss at day for the CurrentCombination ! SignedTickers signedTickers = new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).Tickers); ! WeightedPositions weightedPositions = ! new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).TickersPortfolioWeights, ! signedTickers); ! lossOfCurrentCombination = ! - weightedPositions.GetOpenToCloseReturn( today ); ! //- because fitness in GenomeOTCCTO is just the reversal of what needed ! numOfGenomesScanned++; ! if(lossOfCurrentCombination < lossOfCurrentWorstCombination) ! { ! indexOfWorstCombination = populationSize - i - 1; ! lossOfCurrentWorstCombination = lossOfCurrentCombination; ! } ! } ! catch(Exception ex) ! { ! //if retrieving of loss of current combination fails ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! } ! } this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).Tickers)); } ! ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! if(this.currentGO != null) ! //so a list of genomes is available this.marketCloseEventHandler_chooseTheWorstOTCGenome(); ! this.openPositions(); ! } #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); SelectorByLiquidity mostLiquid = new SelectorByLiquidity(tickersFromGroup, ! false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay(mostLiquid.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! SelectorByAverageRawOpenPrice byPrice = new SelectorByAverageRawOpenPrice(quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(), ! false,currentDate.AddDays(-30), ! currentDate, ! this.numberOfEligibleTickers, ! 30); ! return byPrice.GetTableOfSelectedTickers(); ! } ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen * 2) ! //the optimization process is possible only if the initial set of tickers is ! //as large as the number of tickers to be chosen ! ! { ! IGenomeManager genManEfficientCTOPortfolio = ! new GenomeManagerForEfficientOTCCTOPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, ! this.numberOfTickersToBeChosen, ! this.targetReturn, ! this.portfolioType, ! this.benchmark); ! ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientCTOPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); GO.MutationRate = 0.25; GO.CrossoverRate = 0.95; ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, setOfTickersToBeOptimized.Rows.Count); this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)GO.BestGenome.Meaning).Tickers) ); this.currentGO = GO; ! } ! //else it will be buyed again the previous optimized portfolio ! //that's it the actual chosenTickers member ! } ! protected void oneHourAfterMarketCloseEventHandler_updatePrices() ! { ! //min price for minimizing commission amount ! //according to IB Broker's commission scheme ! this.minPriceForMinimumCommission = this.account.CashAmount/(this.numberOfTickersToBeChosen*100); ! this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; ! //just to avoid warning message ! } ! ! /// <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 ) ! { ! this.seedForRandomGenerator++; ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); ! if(this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization - 1) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Close event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! else ! { ! this.numDaysElapsedSinceLastOptimization++; ! } ! ! } ! #endregion ! } } Index: EndOfDayTimerHandlerOTC_WorstAtNight.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandlerOTC_WorstAtNight.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** EndOfDayTimerHandlerOTC_WorstAtNight.cs 14 Jan 2008 23:32:31 -0000 1.3 --- EndOfDayTimerHandlerOTC_WorstAtNight.cs 19 Aug 2008 17:11:26 -0000 1.4 *************** *** 3,7 **** EndOfDayTimerHandlerOTC_WorstAtNight.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- EndOfDayTimerHandlerOTC_WorstAtNight.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; *************** *** 40,77 **** { ! /// <summary> ! /// Implements MarketOpenEventHandler, ! /// TwoMinutesBeforeMarketCloseEventHandler and OneHourAfterMarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to open portfolio! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerOTC_WorstAtNight : EndOfDayTimerHandler ! { ! protected int seedForRandomGenerator; protected GeneticOptimizer currentGO; protected int numOfGenomesForCTOScanning; private HistoricalAdjustedQuoteProvider historicalAdjustedQuoteProvider; ! ! public EndOfDayTimerHandlerOTC_WorstAtNight(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType, int numDaysBetweenEachOptimization, ! int numOfGenomesForCTOScanning): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.numDaysElapsedSinceLastOptimization = 0; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; this.numOfGenomesForCTOScanning = numOfGenomesForCTOScanning; this.historicalAdjustedQuoteProvider = new HistoricalAdjustedQuoteProvider(); ! } ! private void marketOpenEventHandler_chooseTheWorstCTOGenome() { --- 40,77 ---- { ! /// <summary> ! /// Implements MarketOpenEventHandler, ! /// TwoMinutesBeforeMarketCloseEventHandler and OneHourAfterMarketCloseEventHandler ! /// These handlers contain the core strategy for the efficient close to open portfolio! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerOTC_WorstAtNight : EndOfDayTimerHandler ! { ! protected int seedForRandomGenerator; protected GeneticOptimizer currentGO; protected int numOfGenomesForCTOScanning; private HistoricalAdjustedQuoteProvider historicalAdjustedQuoteProvider; ! ! public EndOfDayTimerHandlerOTC_WorstAtNight(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, double targetReturn, ! PortfolioType portfolioType, int numDaysBetweenEachOptimization, ! int numOfGenomesForCTOScanning): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, targetReturn, ! portfolioType) ! { ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.numDaysElapsedSinceLastOptimization = 0; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; this.numOfGenomesForCTOScanning = numOfGenomesForCTOScanning; this.historicalAdjustedQuoteProvider = new HistoricalAdjustedQuoteProvider(); ! } ! private void marketOpenEventHandler_chooseTheWorstCTOGenome() { *************** *** 82,265 **** double fitnessOfPreviousCombination = 0.0; IndexBasedEndOfDayTimer currentTimer = (IndexBasedEndOfDayTimer)this.account.EndOfDayTimer; ! DateTime today = currentTimer.GetCurrentTime().DateTime; ! DateTime lastMarketDay = currentTimer.GetPreviousDateTime(); ! ReturnsManager returnsManager = new ReturnsManager( ! new CloseToOpenIntervals(new EndOfDayDateTime(lastMarketDay, EndOfDaySpecificTime.MarketClose), ! new EndOfDayDateTime(today, EndOfDaySpecificTime.MarketOpen), ! this.benchmark), ! this.historicalAdjustedQuoteProvider ); ! int numOfGenomesScanned = 0; ! for(int i = 0; ! numOfGenomesScanned < this.numOfGenomesForCTOScanning && ! i < populationSize - 1; ! i++) ! { ! if( i == 0 || ! ( ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness < ! fitnessOfPreviousCombination ) ) ! //it is the best genome or the current genome is different from - and ! //so it has to be strictly less of - the previous scanned genome ! { ! fitnessOfPreviousCombination = ! ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness; ! try{ ! //tries to retrieve loss at night for the CurrentCombination ! SignedTickers signedTickers = new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).Tickers); ! WeightedPositions weightedPositions = ! new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).TickersPortfolioWeights, ! signedTickers); ! lossOfCurrentCombination = ! weightedPositions.GetReturn( 0 , returnsManager ); ! numOfGenomesScanned++; ! if(lossOfCurrentCombination < lossOfCurrentWorstCombination) ! { ! indexOfWorstCombination = populationSize - i - 1; ! lossOfCurrentWorstCombination = lossOfCurrentCombination; ! } ! } ! catch(Exception ex){ ! //retrieve of loss of current combination fails ! ex = ex; ! } ! } ! } ! this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).Tickers)); } ! ! /// <summary> ! /// Handles a "Market Open" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! if(this.currentGO != null) ! //so a list of genomes is available ! this.marketOpenEventHandler_chooseTheWorstCTOGenome(); this.openPositions(); ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! AccountManager.ClosePositions(this.account); ! } ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); SelectorByLiquidity mostLiquid = new SelectorByLiquidity(tickersFromGroup, ! false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay(mostLiquid.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! SelectorByAverageRawOpenPrice byPrice = new SelectorByAverageRawOpenPrice(quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(), ! false,currentDate.AddDays(-30), ! currentDate, ! this.numberOfEligibleTickers, ! 35); DataTable tickersByPrice = byPrice.GetTableOfSelectedTickers(); ! ! SelectorByOpenCloseCorrelationToBenchmark tickersLessCorrelatedToBenchmark = new SelectorByOpenCloseCorrelationToBenchmark(tickersByPrice, ! "^GSPC",true, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! tickersByPrice.Rows.Count/2); ! return tickersLessCorrelatedToBenchmark.GetTableOfSelectedTickers(); ! } ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is possible only if the initial set of tickers is ! //as large as the number of tickers to be chosen ! ! { ! IGenomeManager genManEfficientOTCPortfolio = ! // new GenomeManagerForEfficientOTCPortfolio(setOfTickersToBeOptimized, ! // currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! // currentDate, ! // this.numberOfTickersToBeChosen, ! // this.targetReturn, ! // this.portfolioType); ! new GenomeManagerForEfficientOTCCTOPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, ! this.numberOfTickersToBeChosen, ! this.targetReturn, ! this.portfolioType,this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientOTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); GO.CrossoverRate = 0.0; GO.MutationRate = 0.50; ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, setOfTickersToBeOptimized.Rows.Count); ! this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)GO.BestGenome.Meaning).Tickers) ); this.currentGO = GO; ! } ! //else it will be buyed again the previous optimized portfolio ! //that's it the actual chosenTickers member ! } ! protected void oneHourAfterMarketCloseEventHandler_updatePrices() ! { ! //min price for minimizing commission amount ! //according to IB Broker's commission scheme ! this.minPriceForMinimumCommission = this.account.CashAmount/(this.numberOfTickersToBeChosen*100); ! this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; ! //just to avoid warning message ! } ! ! /// <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 ) ! { ! this.seedForRandomGenerator++; ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); ! if(this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization - 1) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Open event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! else ! { ! this.numDaysElapsedSinceLastOptimization++; ! } ! ! } ! ! #endregion ! } } --- 82,266 ---- double fitnessOfPreviousCombination = 0.0; IndexBasedEndOfDayTimer currentTimer = (IndexBasedEndOfDayTimer)this.account.EndOfDayTimer; ! DateTime today = currentTimer.GetCurrentTime().DateTime; ! DateTime lastMarketDay = currentTimer.GetPreviousDateTime(); ! ReturnsManager returnsManager = new ReturnsManager( ! new CloseToOpenIntervals(new EndOfDayDateTime(lastMarketDay, EndOfDaySpecificTime.MarketClose), ! new EndOfDayDateTime(today, EndOfDaySpecificTime.MarketOpen), ! this.benchmark), ! this.historicalAdjustedQuoteProvider ); ! int numOfGenomesScanned = 0; ! for(int i = 0; ! numOfGenomesScanned < this.numOfGenomesForCTOScanning && ! i < populationSize - 1; ! i++) ! { ! if( i == 0 || ! ( ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness < ! fitnessOfPreviousCombination ) ) ! //it is the best genome or the current genome is different from - and ! //so it has to be strictly less of - the previous scanned genome ! { ! fitnessOfPreviousCombination = ! ((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Fitness; ! try{ ! //tries to retrieve loss at night for the CurrentCombination ! SignedTickers signedTickers = new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).Tickers); ! WeightedPositions weightedPositions = ! new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[populationSize - i - 1]).Meaning).TickersPortfolioWeights, ! signedTickers); ! lossOfCurrentCombination = ! weightedPositions.GetReturn( 0 , returnsManager ); ! numOfGenomesScanned++; ! if(lossOfCurrentCombination < lossOfCurrentWorstCombination) ! { ! indexOfWorstCombination = populationSize - i - 1; ! lossOfCurrentWorstCombination = lossOfCurrentCombination; ! } ! } ! catch(Exception ex) ! { ! //retrieve of loss of current combination fails ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! } ! } ! this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)((Genome)this.currentGO.CurrentGeneration[indexOfWorstCombination]).Meaning).Tickers)); } ! ! /// <summary> ! /// Handles a "Market Open" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! if(this.currentGO != null) ! //so a list of genomes is available ! this.marketOpenEventHandler_chooseTheWorstCTOGenome(); this.openPositions(); ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! AccountManager.ClosePositions(this.account); ! } ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); SelectorByLiquidity mostLiquid = new SelectorByLiquidity(tickersFromGroup, ! false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromMostLiquid = new SelectorByQuotationAtEachMarketDay(mostLiquid.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers, this.benchmark); ! ! SelectorByAverageRawOpenPrice byPrice = new SelectorByAverageRawOpenPrice(quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(), ! false,currentDate.AddDays(-30), ! currentDate, ! this.numberOfEligibleTickers, ! 35); DataTable tickersByPrice = byPrice.GetTableOfSelectedTickers(); ! ! SelectorByOpenCloseCorrelationToBenchmark tickersLessCorrelatedToBenchmark = new SelectorByOpenCloseCorrelationToBenchmark(tickersByPrice, ! "^GSPC",true, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! tickersByPrice.Rows.Count/2); ! return tickersLessCorrelatedToBenchmark.GetTableOfSelectedTickers(); ! } ! ! protected virtual void setTickers(DateTime currentDate, ! bool setGenomeCounter) ! { ! ! DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count > this.numberOfTickersToBeChosen*2) ! //the optimization process is possible only if the initial set of tickers is ! //as large as the number of tickers to be chosen ! ! { ! IGenomeManager genManEfficientOTCPortfolio = ! // new GenomeManagerForEfficientOTCPortfolio(setOfTickersToBeOptimized, ! // currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! // currentDate, ! // this.numberOfTickersToBeChosen, ! // this.targetReturn, ! // this.portfolioType); ! new GenomeManagerForEfficientOTCCTOPortfolio(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, ! this.numberOfTickersToBeChosen, ! this.targetReturn, ! this.portfolioType,this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(genManEfficientOTCPortfolio, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); GO.CrossoverRate = 0.0; GO.MutationRate = 0.50; ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, setOfTickersToBeOptimized.Rows.Count); ! this.chosenWeightedPositions = new WeightedPositions( ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights, ! new SignedTickers( ((GenomeMeaning)GO.BestGenome.Meaning).Tickers) ); this.currentGO = GO; ! } ! //else it will be buyed again the previous optimized portfolio ! //that's it the actual chosenTickers member ! } ! protected void oneHourAfterMarketCloseEventHandler_updatePrices() ! { ! //min price for minimizing commission amount ! //according to IB Broker's commission scheme ! this.minPriceForMinimumCommission = this.account.CashAmount/(this.numberOfTickersToBeChosen*100); ! this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; ! //just to avoid warning message ! } ! /// <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 ) ! { ! this.seedForRandomGenerator++; ! //this.oneHourAfterMarketCloseEventHandler_updatePrices(); ! if(this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization - 1) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Open event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! else ! { ! this.numDaysElapsedSinceLastOptimization++; ! } ! ! } ! ! #endregion ! ! } } |