[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting EndOfDayTimerHandler.cs,
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2006-09-17 21:44:07
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv1049/b7_Scripts/TickerSelectionTesting Modified Files: EndOfDayTimerHandler.cs Log Message: Added a protected method "reversePosition", for reversing existing positions. Other minor changes, too. Index: EndOfDayTimerHandler.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/EndOfDayTimerHandler.cs,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** EndOfDayTimerHandler.cs 7 Aug 2006 21:09:15 -0000 1.22 --- EndOfDayTimerHandler.cs 17 Sep 2006 21:44:04 -0000 1.23 *************** *** 33,36 **** --- 33,37 ---- using QuantProject.Data.DataTables; using QuantProject.ADT.Optimizing.Genetic; + using QuantProject.ADT.Optimizing.BruteForce; using QuantProject.Scripts.WalkForwardTesting.LinearCombination; *************** *** 46,49 **** --- 47,51 ---- protected DataTable eligibleTickers; protected string[] chosenTickers; + protected string[] reversedTickersForOpenedPositions; protected double[] chosenTickersPortfolioWeights; protected string[] lastOrderedTickers; *************** *** 179,186 **** } ! protected virtual void addOrderForTicker(int tickerPosition ) { string tickerCode = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(this.chosenTickers[tickerPosition]); double cashForSinglePosition = this.account.CashAmount * this.chosenTickersPortfolioWeights[tickerPosition]; --- 181,189 ---- } ! protected virtual void addOrderForTicker(string[] tickers, ! int tickerPosition ) { string tickerCode = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(tickers[tickerPosition]); double cashForSinglePosition = this.account.CashAmount * this.chosenTickersPortfolioWeights[tickerPosition]; *************** *** 190,194 **** if(this.portfolioType == PortfolioType.OnlyShort || (this.portfolioType == PortfolioType.ShortAndLong && ! this.chosenTickers[tickerPosition] != tickerCode)) order = new Order( OrderType.MarketSellShort, new Instrument( tickerCode ) , quantity ); else --- 193,197 ---- if(this.portfolioType == PortfolioType.OnlyShort || (this.portfolioType == PortfolioType.ShortAndLong && ! tickers[tickerPosition] != tickerCode)) order = new Order( OrderType.MarketSellShort, new Instrument( tickerCode ) , quantity ); else *************** *** 222,243 **** } ! protected virtual void addChosenTickersToOrderList() { ! for( int i = 0; i<this.chosenTickers.Length; i++) { ! if(this.chosenTickers[i] != null) { ! this.addOrderForTicker( i ); this.lastOrderedTickers[i] = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(this.chosenTickers[i]); } } } ! protected bool openPositions_allChosenTickersQuotedAtCurrentDate() { bool returnValue = true; DateTime currentDate = this.Account.EndOfDayTimer.GetCurrentTime().DateTime; ! foreach(string ticker in this.chosenTickers) { if(ticker != null) --- 225,246 ---- } ! 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) *************** *** 253,265 **** } ! protected virtual void openPositions() { //add cash first if(this.orders.Count == 0 && this.account.Transactions.Count == 0) this.account.AddCash(30000); ! if(this.openPositions_allChosenTickersQuotedAtCurrentDate()) //all tickers have quotes at the current date, so orders can be filled { ! this.addChosenTickersToOrderList(); //execute orders actually foreach(object item in this.orders) --- 256,268 ---- } ! 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) *************** *** 283,286 **** --- 286,305 ---- } + 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, *************** *** 316,319 **** --- 335,356 ---- } + 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 ) *************** *** 331,334 **** --- 368,434 ---- ; } + + #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 } |