[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting/SimpleSelection EndOfDayT
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2007-08-29 09:44:17
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/SimpleSelection In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv10276/b7_Scripts/TickerSelectionTesting/SimpleSelection Modified Files: EndOfDayTimerHandlerSimpleSelection.cs EndOfDayTimerHandlerSimpleSelectionOpenToClose.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: EndOfDayTimerHandlerSimpleSelection.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/SimpleSelection/EndOfDayTimerHandlerSimpleSelection.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** EndOfDayTimerHandlerSimpleSelection.cs 21 Oct 2005 17:54:11 -0000 1.1 --- EndOfDayTimerHandlerSimpleSelection.cs 29 Aug 2007 09:43:34 -0000 1.2 *************** *** 41,205 **** /// </summary> [Serializable] ! public class EndOfDayTimerHandlerSimpleSelection { ! protected DataTable eligibleTickers; ! protected string[] chosenTickers; ! protected string[] lastOrderedTickers; ! ! protected string tickerGroupID; ! protected int numberOfEligibleTickers; ! protected int numberOfTickersToBeChosen; ! protected int numDaysForOptimizationPeriod; ! ! protected Account account; ! protected ArrayList orders; ! ! protected string benchmark; ! //these two values have to be updated during ! //backtest, for minimizing commission amount, ! //according to broker's commission scheme ! protected double minPriceForMinimumCommission = 0; ! protected double maxPriceForMinimumCommission = 500; ! ! protected double targetReturn; ! ! protected PortfolioType portfolioType; ! ! public string[] LastOrderedTickers ! { ! get { return this.lastOrderedTickers; } ! } ! public int NumberOfEligibleTickers ! { ! get { return this.numberOfEligibleTickers; } ! } ! ! public Account Account ! { ! get { return this.account; } ! } ! ! ! public EndOfDayTimerHandlerSimpleSelection(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, string benchmark, double targetReturn, ! PortfolioType portfolioType) ! { ! this.tickerGroupID = tickerGroupID; ! this.numberOfEligibleTickers = numberOfEligibleTickers; ! this.numberOfTickersToBeChosen = numberOfTickersToBeChosen; ! this.numDaysForOptimizationPeriod = numDaysForOptimizationPeriod; ! this.account = account; ! this.benchmark = benchmark; ! this.orders = new ArrayList(); ! this.chosenTickers = new string[numberOfTickersToBeChosen]; ! this.lastOrderedTickers = new string[numberOfTickersToBeChosen]; ! this.targetReturn = targetReturn; ! this.portfolioType = portfolioType; ! ! } ! ! ! protected virtual void addOrderForTicker(string ticker ) ! { ! string tickerCode = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); ! double cashForSinglePosition = this.account.CashAmount / this.numberOfTickersToBeChosen; ! long quantity = ! Convert.ToInt64( Math.Floor( cashForSinglePosition / this.account.DataStreamer.GetCurrentBid( tickerCode ) ) ); ! Order order; ! if(this.portfolioType == PortfolioType.OnlyShort || ! (this.portfolioType == PortfolioType.ShortAndLong && ! ticker != 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() ! { ! int idx = 0; ! foreach ( string ticker in this.chosenTickers ) ! { ! if(ticker != null) ! { ! this.addOrderForTicker( ticker ); ! this.lastOrderedTickers[idx] = ! GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); ! } ! idx++; ! } ! } ! ! protected bool openPositions_allChosenTickersQuotedAtCurrentDate() ! { ! bool returnValue = true; ! DateTime currentDate = this.Account.EndOfDayTimer.GetCurrentTime().DateTime; ! foreach(string ticker in this.chosenTickers) ! { ! 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() { ! //add cash first ! if(this.orders.Count == 0 && this.account.Transactions.Count == 0) ! this.account.AddCash(17000); ! 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) ! this.account.AddOrder((Order)item); ! } } ! ! public virtual void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } ! public virtual void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } ! public virtual void OneHourAfterMarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { --- 41,70 ---- /// </summary> [Serializable] ! public class EndOfDayTimerHandlerSimpleSelection : EndOfDayTimerHandler { ! public EndOfDayTimerHandlerSimpleSelection(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, string benchmark, double targetReturn, ! PortfolioType portfolioType): ! base(tickerGroupID, numberOfEligibleTickers, ! numberOfTickersToBeChosen, numDaysForOptimizationPeriod, account, ! 0,0, ! benchmark, targetReturn, ! portfolioType) { ! } ! ! public override void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } ! public override void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ; } ! public override void OneHourAfterMarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { Index: EndOfDayTimerHandlerSimpleSelectionOpenToClose.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/SimpleSelection/EndOfDayTimerHandlerSimpleSelectionOpenToClose.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** EndOfDayTimerHandlerSimpleSelectionOpenToClose.cs 21 Oct 2005 17:54:11 -0000 1.1 --- EndOfDayTimerHandlerSimpleSelectionOpenToClose.cs 29 Aug 2007 09:43:34 -0000 1.2 *************** *** 29,32 **** --- 29,33 ---- using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Timing; + using QuantProject.Business.Strategies; using QuantProject.Data.DataProviders; using QuantProject.Data.Selectors; *************** *** 43,49 **** public class EndOfDayTimerHandlerSimpleSelectionOpenToClose : EndOfDayTimerHandlerSimpleSelection { ! protected int numDaysBetweenEachOptimization; ! private int numDaysElapsedSinceLastOptimization; ! public EndOfDayTimerHandlerSimpleSelectionOpenToClose(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, --- 44,48 ---- public class EndOfDayTimerHandlerSimpleSelectionOpenToClose : EndOfDayTimerHandlerSimpleSelection { ! public EndOfDayTimerHandlerSimpleSelectionOpenToClose(string tickerGroupID, int numberOfEligibleTickers, int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, Account account, *************** *** 68,72 **** Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! this.closePositions(); } --- 67,71 ---- Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { ! AccountManager.ClosePositions(this.account); } *************** *** 115,119 **** DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count >= this.chosenTickers.Length) //the optimization process is possible only if the initial set of tickers is //as large as the number of tickers to be chosen --- 114,118 ---- DataTable setOfTickersToBeOptimized = this.getSetOfTickersToBeOptimized(currentDate); ! if(setOfTickersToBeOptimized.Rows.Count >= this.numberOfTickersToBeChosen) //the optimization process is possible only if the initial set of tickers is //as large as the number of tickers to be chosen *************** *** 125,129 **** currentDate, this.targetReturn, this.portfolioType); ! this.chosenTickers = OTCScreener.GetBestTickers(this.numberOfTickersToBeChosen); } //else it will be buyed again the previous optimized portfolio --- 124,128 ---- currentDate, this.targetReturn, this.portfolioType); ! this.chosenWeightedPositions = new WeightedPositions(OTCScreener.GetBestTickers(this.numberOfTickersToBeChosen)); } //else it will be buyed again the previous optimized portfolio *************** *** 148,153 **** Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { - this.orders.Clear(); - //this.oneHourAfterMarketCloseEventHandler_updatePrices(); if(this.numDaysElapsedSinceLastOptimization == this.numDaysBetweenEachOptimization - 1) --- 147,150 ---- |