[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/TrendFollowing/Immediate
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-08-19 17:12:02
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/TrendFollowing/ImmediateTrendFollower In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv2916/b7_Scripts/TechnicalAnalysisTesting/TrendFollowing/ImmediateTrendFollower Modified Files: EndOfDayTimerHandlerITF.cs Log Message: - the code has been cleaned up to avoid warnings - standard indentation has been applied Index: EndOfDayTimerHandlerITF.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/TrendFollowing/ImmediateTrendFollower/EndOfDayTimerHandlerITF.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** EndOfDayTimerHandlerITF.cs 14 Jan 2008 23:32:30 -0000 1.6 --- EndOfDayTimerHandlerITF.cs 19 Aug 2008 17:11:28 -0000 1.7 *************** *** 3,7 **** EndOfDayTimerHandlerITF.cs ! Copyright (C) 2003 Marco Milletti --- 3,7 ---- EndOfDayTimerHandlerITF.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,297 **** { /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the extreme ! /// counter trend strategy! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerITF : EndOfDayTimerHandler ! { ! private int numDaysForReturnCalculation; ! private double maxAcceptableCloseToCloseDrawdown; ! private int daysCounterWithPositions; ! private DateTime lastCloseDate; ! private IGenomeManager iGenomeManager; ! private int seedForRandomGenerator; ! ! public EndOfDayTimerHandlerITF(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, ! int numDaysForReturnCalculation, ! int numDaysBetweenEachOptimization, ! PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, 0.0, ! portfolioType) ! { ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; ! this.stopLossConditionReached = false; ! this.currentAccountValue = 0.0; ! this.previousAccountValue = 0.0; ! // this.numDaysBetweenEachOptimization = 2* numDaysForReturnCalculation; ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; ! } ! ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ; ! } ! #region MarketCloseEventHandler ! ! protected void marketCloseEventHandler_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; ! } ! } ! private double marketCloseEventHandler_openPositions_getLastHalfPeriodGain(IndexBasedEndOfDayTimer timer) ! { ! double returnValue = 999.0; ! try ! { ! DateTime initialDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForReturnCalculation + 1]["quDate"]; ! DateTime finalDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! returnValue = ! chosenWeightedPositions.GetCloseToCloseReturn(initialDateForHalfPeriod,finalDateForHalfPeriod); ! } ! catch(MissingQuotesException ex) ! { ! ex = ex; ! } ! return returnValue; ! } ! private void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! double lastHalfPeriodGain = ! this.marketCloseEventHandler_openPositions_getLastHalfPeriodGain(timer); ! if(lastHalfPeriodGain != 999.0) ! //last half period gain has been properly computed ! { ! if(lastHalfPeriodGain > 0.0) ! AccountManager.OpenPositions(chosenWeightedPositions, this.account); ! else//the last HalfPeriodGain has been negative ! { ! chosenWeightedPositions.Reverse(); ! //short the portfolio ! try{ ! AccountManager.OpenPositions(chosenWeightedPositions, this.account); ! } ! catch(Exception ex) ! { ! ex = ex; ! } ! finally{ ! chosenWeightedPositions.Reverse(); ! } ! } ! } ! } ! ! private void marketCloseEventHandler_closePositions() ! { ! if(this.daysCounterWithPositions == this.numDaysForReturnCalculation || ! this.stopLossConditionReached) ! { ! //Close if halfPeriod has elapsed or stop loss condition reached ! AccountManager.ClosePositions(this.account); ! this.daysCounterWithPositions = 0; ! } ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! //this.marketCloseEventHandler_updateStopLossCondition(); ! if(this.account.Portfolio.Count > 0) ! this.daysCounterWithPositions++; ! this.marketCloseEventHandler_closePositions(); ! if(chosenWeightedPositions != null) ! //tickers to buy have been chosen ! { ! if(this.account.Portfolio.Count == 0) ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! } ! ! } ! #endregion ! ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); ! DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); ! int numOfTickersInGroupAtCurrentDate = tickersFromGroup.Rows.Count; ! ! SelectorByAverageRawOpenPrice byPrice = ! new SelectorByAverageRawOpenPrice(tickersFromGroup,false,currentDate, ! currentDate.AddDays(-30), ! numOfTickersInGroupAtCurrentDate, ! 30,500, 0.0001,100); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromByPrice = ! new SelectorByQuotationAtEachMarketDay(byPrice.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! numOfTickersInGroupAtCurrentDate, this.benchmark); ! ! // SelectorByCloseToCloseVolatility lessVolatile = ! // new SelectorByCloseToCloseVolatility(quotedAtEachMarketDayFromByPrice.GetTableOfSelectedTickers(), ! // true,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! // this.numberOfEligibleTickers); ! ! SelectorByLiquidity mostLiquidSelector = ! new SelectorByLiquidity(quotedAtEachMarketDayFromByPrice.GetTableOfSelectedTickers(), ! true,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! ! return mostLiquidSelector.GetTableOfSelectedTickers(); ! //OLD for etf ! // SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, ! // currentDate); ! // ! // SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromTemporized = ! // new SelectorByQuotationAtEachMarketDay(temporizedGroup.GetTableOfSelectedTickers(), ! // false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! // 600, this.benchmark); ! // // filter to be used with plain stocks ! // DataTable tickersQuotedAtEachMarketDay = quotedAtEachMarketDayFromTemporized.GetTableOfSelectedTickers(); ! // SelectorByLiquidity mostLiquid = ! // new SelectorByLiquidity(tickersQuotedAtEachMarketDay, ! // false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! // tickersQuotedAtEachMarketDay.Rows.Count/2); ! // ! // DataTable mostLiquidTickers = mostLiquid.GetTableOfSelectedTickers(); ! // ! // SelectorByCloseToCloseVolatility lessVolatile = ! // new SelectorByCloseToCloseVolatility(mostLiquidTickers, ! // true,currentDate.AddDays(-30), currentDate, ! // Math.Min(this.numberOfEligibleTickers, mostLiquidTickers.Rows.Count/2)); ! ////// return mostLiquid.GetTableOfSelectedTickers(); ! // return lessVolatile.GetTableOfSelectedTickers(); ! // // ! //// return quotedAtEachMarketDayFromTemporized.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 meaningful only if the initial set of tickers is ! //larger than the number of tickers to be chosen ! ! { ! this.iGenomeManager = ! new GenomeManagerITF(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, this.numberOfTickersToBeChosen, ! this.numDaysForReturnCalculation, ! this.portfolioType, this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(this.iGenomeManager, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); ! ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,((GenomeManagerForEfficientPortfolio)this.iGenomeManager).FirstQuoteDate, ! ((GenomeManagerForEfficientPortfolio)this.iGenomeManager).LastQuoteDate, setOfTickersToBeOptimized.Rows.Count, ! this.numDaysForReturnCalculation); 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 ) ! { ! this.lastCloseDate = endOfDayTimingEventArgs.EndOfDayDateTime.DateTime; ! this.seedForRandomGenerator++; ! this.numDaysElapsedSinceLastOptimization++; ! if((this.numDaysElapsedSinceLastOptimization - 1 == ! this.numDaysBetweenEachOptimization)) //|| this.isTheFirstClose ) ! //num days without optimization has elapsed or ! //it is the first close (OLD IMPLEMENTATION) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Close event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! ! } #endregion ! } } --- 40,297 ---- { /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the extreme ! /// counter trend strategy! ! /// </summary> ! [Serializable] ! public class EndOfDayTimerHandlerITF : EndOfDayTimerHandler ! { ! private int numDaysForReturnCalculation; ! private double maxAcceptableCloseToCloseDrawdown; ! private int daysCounterWithPositions; ! private DateTime lastCloseDate; ! private IGenomeManager iGenomeManager; ! private int seedForRandomGenerator; ! ! public EndOfDayTimerHandlerITF(string tickerGroupID, int numberOfEligibleTickers, ! int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, ! Account account, ! int generationNumberForGeneticOptimizer, ! int populationSizeForGeneticOptimizer, ! string benchmark, ! int numDaysForReturnCalculation, ! int numDaysBetweenEachOptimization, ! PortfolioType portfolioType, double maxAcceptableCloseToCloseDrawdown): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! generationNumberForGeneticOptimizer, ! populationSizeForGeneticOptimizer, ! benchmark, 0.0, ! portfolioType) ! { ! this.numDaysForReturnCalculation = numDaysForReturnCalculation; ! this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; ! this.stopLossConditionReached = false; ! this.currentAccountValue = 0.0; ! this.previousAccountValue = 0.0; ! // this.numDaysBetweenEachOptimization = 2* numDaysForReturnCalculation; ! this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; ! this.seedForRandomGenerator = ConstantsProvider.SeedForRandomGenerator; ! } ! ! public override void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ; ! } ! #region MarketCloseEventHandler ! ! protected void marketCloseEventHandler_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; ! } ! } ! private double marketCloseEventHandler_openPositions_getLastHalfPeriodGain(IndexBasedEndOfDayTimer timer) ! { ! double returnValue = 999.0; ! try ! { ! DateTime initialDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition - this.numDaysForReturnCalculation + 1]["quDate"]; ! DateTime finalDateForHalfPeriod = ! (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition]["quDate"]; ! returnValue = ! chosenWeightedPositions.GetCloseToCloseReturn(initialDateForHalfPeriod,finalDateForHalfPeriod); ! } ! catch(MissingQuotesException ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! return returnValue; ! } ! private void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! double lastHalfPeriodGain = ! this.marketCloseEventHandler_openPositions_getLastHalfPeriodGain(timer); ! if(lastHalfPeriodGain != 999.0) ! //last half period gain has been properly computed ! { ! if(lastHalfPeriodGain > 0.0) ! AccountManager.OpenPositions(chosenWeightedPositions, this.account); ! else//the last HalfPeriodGain has been negative ! { ! chosenWeightedPositions.Reverse(); ! //short the portfolio ! try{ ! AccountManager.OpenPositions(chosenWeightedPositions, this.account); ! } ! catch(Exception ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! finally{ ! chosenWeightedPositions.Reverse(); ! } ! } ! } ! } ! ! private void marketCloseEventHandler_closePositions() ! { ! if(this.daysCounterWithPositions == this.numDaysForReturnCalculation || ! this.stopLossConditionReached) ! { ! //Close if halfPeriod has elapsed or stop loss condition reached ! AccountManager.ClosePositions(this.account); ! this.daysCounterWithPositions = 0; ! } ! } ! ! public override void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! //this.marketCloseEventHandler_updateStopLossCondition(); ! if(this.account.Portfolio.Count > 0) ! this.daysCounterWithPositions++; ! this.marketCloseEventHandler_closePositions(); ! if(chosenWeightedPositions != null) ! //tickers to buy have been chosen ! { ! if(this.account.Portfolio.Count == 0) ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! } ! ! } ! #endregion ! ! #region OneHourAfterMarketCloseEventHandler ! ! protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) ! { SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, currentDate); ! DataTable tickersFromGroup = temporizedGroup.GetTableOfSelectedTickers(); ! int numOfTickersInGroupAtCurrentDate = tickersFromGroup.Rows.Count; ! ! SelectorByAverageRawOpenPrice byPrice = ! new SelectorByAverageRawOpenPrice(tickersFromGroup,false,currentDate, ! currentDate.AddDays(-30), ! numOfTickersInGroupAtCurrentDate, ! 30,500, 0.0001,100); ! ! SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromByPrice = ! new SelectorByQuotationAtEachMarketDay(byPrice.GetTableOfSelectedTickers(), ! false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! numOfTickersInGroupAtCurrentDate, this.benchmark); ! ! // SelectorByCloseToCloseVolatility lessVolatile = ! // new SelectorByCloseToCloseVolatility(quotedAtEachMarketDayFromByPrice.GetTableOfSelectedTickers(), ! // true,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! // this.numberOfEligibleTickers); ! ! SelectorByLiquidity mostLiquidSelector = ! new SelectorByLiquidity(quotedAtEachMarketDayFromByPrice.GetTableOfSelectedTickers(), ! true,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! this.numberOfEligibleTickers); ! ! ! return mostLiquidSelector.GetTableOfSelectedTickers(); ! //OLD for etf ! // SelectorByGroup temporizedGroup = new SelectorByGroup(this.tickerGroupID, ! // currentDate); ! // ! // SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromTemporized = ! // new SelectorByQuotationAtEachMarketDay(temporizedGroup.GetTableOfSelectedTickers(), ! // false, currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! // 600, this.benchmark); ! // // filter to be used with plain stocks ! // DataTable tickersQuotedAtEachMarketDay = quotedAtEachMarketDayFromTemporized.GetTableOfSelectedTickers(); ! // SelectorByLiquidity mostLiquid = ! // new SelectorByLiquidity(tickersQuotedAtEachMarketDay, ! // false,currentDate.AddDays(-this.numDaysForOptimizationPeriod), currentDate, ! // tickersQuotedAtEachMarketDay.Rows.Count/2); ! // ! // DataTable mostLiquidTickers = mostLiquid.GetTableOfSelectedTickers(); ! // ! // SelectorByCloseToCloseVolatility lessVolatile = ! // new SelectorByCloseToCloseVolatility(mostLiquidTickers, ! // true,currentDate.AddDays(-30), currentDate, ! // Math.Min(this.numberOfEligibleTickers, mostLiquidTickers.Rows.Count/2)); ! ////// return mostLiquid.GetTableOfSelectedTickers(); ! // return lessVolatile.GetTableOfSelectedTickers(); ! // // ! //// return quotedAtEachMarketDayFromTemporized.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 meaningful only if the initial set of tickers is ! //larger than the number of tickers to be chosen ! ! { ! this.iGenomeManager = ! new GenomeManagerITF(setOfTickersToBeOptimized, ! currentDate.AddDays(-this.numDaysForOptimizationPeriod), ! currentDate, this.numberOfTickersToBeChosen, ! this.numDaysForReturnCalculation, ! this.portfolioType, this.benchmark); ! GeneticOptimizer GO = new GeneticOptimizer(this.iGenomeManager, ! this.populationSizeForGeneticOptimizer, ! this.generationNumberForGeneticOptimizer, ! this.seedForRandomGenerator); ! if(setGenomeCounter) ! this.genomeCounter = new GenomeCounter(GO); ! ! GO.Run(false); ! this.addGenomeToBestGenomes(GO.BestGenome,((GenomeManagerForEfficientPortfolio)this.iGenomeManager).FirstQuoteDate, ! ((GenomeManagerForEfficientPortfolio)this.iGenomeManager).LastQuoteDate, setOfTickersToBeOptimized.Rows.Count, ! this.numDaysForReturnCalculation); 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 ) ! { ! this.lastCloseDate = endOfDayTimingEventArgs.EndOfDayDateTime.DateTime; ! this.seedForRandomGenerator++; ! this.numDaysElapsedSinceLastOptimization++; ! if((this.numDaysElapsedSinceLastOptimization - 1 == ! this.numDaysBetweenEachOptimization)) //|| this.isTheFirstClose ) ! //num days without optimization has elapsed or ! //it is the first close (OLD IMPLEMENTATION) ! { ! this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); ! //sets tickers to be chosen next Market Close event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! ! } #endregion ! } } |