[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting EndOfDayTimerHandler.cs,
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2007-08-29 09:47:16
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv12296/b7_Scripts/TickerSelectionTesting Modified Files: EndOfDayTimerHandler.cs Log Message: Code has been re-organized in a more logical and Object Oriented way: - WeightedPositions class has been used. - Some static methods have been deleted from SignedTicker class. They have been moved to WeightedPositions. - Basic orders'management has been moved to the primitive base class EndOfDayTimerHandler and to AccountManager class Index: EndOfDayTimerHandler.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandler.cs,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** EndOfDayTimerHandler.cs 9 Apr 2007 18:15:40 -0000 1.24 --- EndOfDayTimerHandler.cs 29 Aug 2007 09:47:09 -0000 1.25 *************** *** 24,35 **** using System.Collections; - using QuantProject.ADT; using QuantProject.Business.Financial.Accounting; ! using QuantProject.Business.Financial.Instruments; ! using QuantProject.Business.Financial.Ordering; ! using QuantProject.Business.Timing; ! using QuantProject.Data.DataProviders; ! using QuantProject.Data.Selectors; ! using QuantProject.Data.DataTables; using QuantProject.ADT.Optimizing.Genetic; using QuantProject.ADT.Optimizing.BruteForce; --- 24,30 ---- using System.Collections; using QuantProject.Business.Financial.Accounting; ! using QuantProject.Business.Strategies; ! using QuantProject.Business.Timing; using QuantProject.ADT.Optimizing.Genetic; using QuantProject.ADT.Optimizing.BruteForce; *************** *** 38,42 **** namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios { - /// <summary> /// Base class for EndOfDayTimerHandlers for efficient portfolios --- 33,36 ---- *************** *** 46,64 **** { protected DataTable eligibleTickers; ! protected string[] chosenTickers; ! protected string[] reversedTickersForOpenedPositions; ! protected double[] chosenTickersPortfolioWeights; ! protected string[] lastOrderedTickers; ! protected string tickerGroupID; protected int numberOfEligibleTickers; protected int numberOfTickersToBeChosen; protected int numDaysForOptimizationPeriod; protected int generationNumberForGeneticOptimizer; protected int populationSizeForGeneticOptimizer; - protected Account account = null; - protected ArrayList orders; - protected string benchmark; //these two values have to be updated during --- 40,53 ---- { 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 *************** *** 67,85 **** protected double minPriceForMinimumCommission = 30; protected double maxPriceForMinimumCommission = 500; - protected double targetReturn; - protected PortfolioType portfolioType; ! protected GenomeCounter genomeCounter; public GenomeCounter GenomeCounter { get{return this.genomeCounter;} } ! ! public string[] LastOrderedTickers ! { ! get { return this.lastOrderedTickers; } ! } public int NumberOfEligibleTickers { --- 56,73 ---- 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 { *************** *** 101,105 **** get { return this.bestGenomes; } } - private void endOfDayTimerHandler_initializeBasic(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, --- 89,92 ---- *************** *** 116,128 **** this.populationSizeForGeneticOptimizer = populationSizeForGeneticOptimizer; this.benchmark = benchmark; - this.orders = new ArrayList(); - this.chosenTickers = new string[numberOfTickersToBeChosen]; - this.lastOrderedTickers = new string[numberOfTickersToBeChosen]; this.targetReturn = targetReturn; this.portfolioType = portfolioType; - this.setDefaultChosenTickersPortfolioWeights(); - } - public EndOfDayTimerHandler(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, --- 103,109 ---- *************** *** 155,363 **** portfolioType); } ! ! ! ! public EndOfDayTimerHandler(string[] chosenTickers, PortfolioType portfolioType, Account account, string benchmark) { - this.account = account; this.benchmark = benchmark; ! this.orders = new ArrayList(); ! this.chosenTickers = chosenTickers; ! this.numberOfTickersToBeChosen = chosenTickers.Length; ! this.lastOrderedTickers = new string[chosenTickers.Length]; this.portfolioType = portfolioType; - this.setDefaultChosenTickersPortfolioWeights(); } ! ! private void setDefaultChosenTickersPortfolioWeights() ! { ! this.chosenTickersPortfolioWeights = new double[this.chosenTickers.Length]; ! for(int i = 0;i<this.chosenTickers.Length;i++) ! this.chosenTickersPortfolioWeights[i]=1.0/this.chosenTickers.Length; ! } ! ! protected virtual void addOrderForTicker(string[] tickers, ! int tickerPosition ) ! { ! string tickerCode = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(tickers[tickerPosition]); ! double cashForSinglePosition = ! this.account.CashAmount * this.chosenTickersPortfolioWeights[tickerPosition]; ! long quantity = ! Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( tickerCode ) ) ); ! Order order; ! if(this.portfolioType == PortfolioType.OnlyShort || ! (this.portfolioType == PortfolioType.ShortAndLong && ! tickers[tickerPosition] != tickerCode)) ! order = new Order( OrderType.MarketSellShort, new Instrument( tickerCode ) , quantity ); ! else ! order = new Order( OrderType.MarketBuy, new Instrument( tickerCode ) , quantity ); ! this.orders.Add(order); ! } ! ! protected virtual void closePosition( ! string ticker ) ! { ! this.account.ClosePosition( ticker ); ! } ! ! protected virtual void closePositions() ! { ! if(this.lastOrderedTickers != null) ! { ! foreach( string ticker in this.lastOrderedTickers) ! { ! for(int i = 0; i<this.account.Portfolio.Keys.Count; i++) ! { ! if(this.account.Portfolio[ticker]!=null) ! closePosition( ticker ); ! } ! } ! } ! } ! ! protected virtual void addChosenTickersToOrderList(string[] tickers) ! { ! for( int i = 0; i<tickers.Length; i++) ! { ! if(tickers[i] != null) ! { ! this.addOrderForTicker( tickers, i ); ! this.lastOrderedTickers[i] = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(tickers[i]); ! } ! } ! } ! ! protected bool openPositions_allTickersToOpenQuotedAtCurrentDate(string[] tickers) ! { ! bool returnValue = true; ! DateTime currentDate = this.Account.EndOfDayTimer.GetCurrentTime().DateTime; ! foreach(string ticker in tickers) ! { ! if(ticker != null) ! { ! Quotes tickerQuotes = new Quotes(GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker), ! currentDate, currentDate); ! if(tickerQuotes.Rows.Count == 0) ! //no quote available for the current ticker ! returnValue = false; ! } ! } ! return returnValue; ! } ! ! protected virtual void openPositions(string[] tickers, double[] tickersWeights) ! { ! this.chosenTickersPortfolioWeights = tickersWeights; ! this.openPositions(tickers); ! } ! ! protected virtual void openPositions(string[] tickers) { //add cash first ! if(this.orders.Count == 0 && this.account.Transactions.Count == 0) ! this.account.AddCash(30000); ! if(this.openPositions_allTickersToOpenQuotedAtCurrentDate(tickers)) ! //all tickers have quotes at the current date, so orders can be filled ! { ! this.addChosenTickersToOrderList(tickers); ! //execute orders actually ! foreach(object item in this.orders) ! this.account.AddOrder((Order)item); ! } ! } ! ! 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, ! string[] signedTickers, ! DateTime firstOptimizationDate, ! DateTime secondOptimizationDate, ! int eligibleTickers) ! { ! if(this.bestGenomes == null) ! this.bestGenomes = new ArrayList(); ! ! this.bestGenomes.Add(new GenomeRepresentation(BFOptimizableParameters, ! signedTickers, ! 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 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)); } ! public virtual void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) --- 136,250 ---- 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 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); ! 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 virtual void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) *************** *** 375,441 **** ; } ! ! #region reversePositions ! private void reversePositions_addReversedTickersToOrderList_addOrderForTicker(int tickerPosition ) ! { ! string tickerCode = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(this.reversedTickersForOpenedPositions[tickerPosition]); ! double cashForSinglePosition = ! this.account.CashAmount * this.chosenTickersPortfolioWeights[tickerPosition]; ! long quantity = ! Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( tickerCode ) ) ); ! Order order; ! if(this.portfolioType == PortfolioType.OnlyShort || ! (this.portfolioType == PortfolioType.ShortAndLong && ! this.reversedTickersForOpenedPositions[tickerPosition] != tickerCode)) ! order = new Order( OrderType.MarketSellShort, new Instrument( tickerCode ) , quantity ); ! else ! order = new Order( OrderType.MarketBuy, new Instrument( tickerCode ) , quantity ); ! ! this.orders.Add(order); ! } ! ! private void reversePositions_addReversedTickersToOrderList() ! { ! for( int i = 0; i<this.reversedTickersForOpenedPositions.Length; i++) ! { ! if(this.reversedTickersForOpenedPositions[i] != null) ! { ! this.reversePositions_addReversedTickersToOrderList_addOrderForTicker( i ); ! this.lastOrderedTickers[i] = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(this.reversedTickersForOpenedPositions[i]); ! } ! } ! } ! ! ! private void reversePositions_setReversedTickers() ! { ! string sign = ""; ! string instrumentKey = ""; ! Position[] positions = new Position[this.chosenTickers.Length]; ! this.account.Portfolio.Positions.CopyTo(positions, 0); ! for(int i = 0; i<this.account.Portfolio.Count; i++) ! { ! instrumentKey = positions[i].Instrument.Key; ! if(positions[i].IsShort) ! sign = ""; ! else ! sign = "-"; ! this.reversedTickersForOpenedPositions[i] = sign + instrumentKey; ! } ! } ! ! protected void reversePositions() ! { ! this.reversedTickersForOpenedPositions = ! new string[this.chosenTickers.Length]; ! this.reversePositions_setReversedTickers(); ! this.closePositions(); ! this.orders.Clear(); ! this.openPositions(this.reversedTickersForOpenedPositions); ! } ! #endregion ! ! } // end of class } --- 262,265 ---- ; } ! } } |