[Quantproject-developers] QuantProject/b3_Data/DataTables Quotes.cs,1.3,1.4 TickerDataTable.cs,1.2,1
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2004-07-25 12:09:03
|
Update of /cvsroot/quantproject/QuantProject/b3_Data/DataTables In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4857/b3_Data/DataTables Modified Files: Quotes.cs TickerDataTable.cs Log Message: New methods to: - ExtendedDataTable class; - Quotes and Tickers_tickerGroups in DataAccess layer; - Quotes and TickerDataTable in Data layer Index: Quotes.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b3_Data/DataTables/Quotes.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Quotes.cs 27 May 2004 16:45:38 -0000 1.3 --- Quotes.cs 25 Jul 2004 12:08:53 -0000 1.4 *************** *** 25,28 **** --- 25,52 ---- public static string AdjustedCloseToCloseRatio = "quAdjustedCloseToCloseRatio"; + /// <summary> + /// returns most liquid tickers within the given set of tickers + /// </summary> + + public static DataTable GetTickersByLiquidity( bool orderByASC, + DataTable setOfTickers, + DateTime firstQuoteDate, + DateTime lastQuoteDate, + long maxNumOfReturnedTickers) + { + if(!setOfTickers.Columns.Contains("AverageTradedValue")) + setOfTickers.Columns.Add("AverageTradedValue", System.Type.GetType("System.Double")); + foreach(DataRow row in setOfTickers.Rows) + { + row["AverageTradedValue"] = + QuantProject.DataAccess.Tables.Quotes.GetAverageTradedValue((string)row[0], + firstQuoteDate, + lastQuoteDate); + } + DataTable getMostLiquidTicker = ExtendedDataTable.CopyAndSort(setOfTickers,"AverageTradedValue", orderByASC); + ExtendedDataTable.DeleteRows(getMostLiquidTicker, maxNumOfReturnedTickers); + return getMostLiquidTicker; + } + private History history; *************** *** 36,43 **** --- 60,93 ---- } + /// <summary> + /// Gets the date of the first quote contained into the Quotes object + /// </summary> + /// <returns></returns> + public DateTime StartDate + { + get{ return ((DateTime)this.Rows[ 0 ][ Quotes.Date ]); } + } + /// <summary> + /// Gets the date of the last quote contained into the Quotes object + /// </summary> + /// <returns></returns> + public DateTime EndDate + { + get{ return ((DateTime)this.Rows[ this.Rows.Count - 1 ][ Quotes.Date ]); } + } + + private void setPrimaryKeys() + { + DataColumn[] columnPrimaryKeys = new DataColumn[1]; + columnPrimaryKeys[0] = this.Columns[Quotes.Date]; + this.PrimaryKey = columnPrimaryKeys; + } + + private void fillDataTable( string ticker , DateTime startDate , DateTime endDate ) { QuantProject.DataAccess.Tables.Quotes.SetDataTable( ticker , startDate , endDate , this ); + this.setPrimaryKeys(); } public Quotes( string ticker , DateTime startDate , DateTime endDate ) *************** *** 158,165 **** setHistory(); return (DateTime) history.GetKey( Math.Max( 0 , ! history.IndexOfKeyOrPrevious( quoteDate ) - followingDays ) ); } // public DateTime GetPrecedingDate( DateTime quoteDate , int precedingDays ) --- 208,299 ---- setHistory(); return (DateTime) history.GetKey( Math.Max( 0 , ! history.IndexOfKeyOrPrevious( quoteDate ) + followingDays ) ); } + /// <summary> + /// Returns true if a quote is available at the given date + /// </summary> + /// <param name="date">date</param> + /// <returns></returns> + public bool HasDate( DateTime date ) + { + /*alternative code, but primary keys need to be set first + bool hasDate; + hasDate = this.Rows.Contains(date.Date); + return hasDate;*/ + setHistory(); + return this.history.ContainsKey(date.Date); + } + /// <summary> + /// If the ticker has a quote at the given date, then it returns the given date, + /// else it returns the immediate following date at which a quote is available + /// </summary> + /// <param name="date">date</param> + /// <returns></returns> + public DateTime GetQuoteDateOrFollowing(DateTime date ) + { + if(this.HasDate(date)) + { + return date; + } + else + { + return GetQuoteDateOrFollowing(date.AddDays(1)); + } + } + /// <summary> + /// If the ticker has a quote at the given date, then it returns the given date, + /// else it returns the immediate preceding date at which a quote is available + /// </summary> + /// <param name="date">date</param> + /// <returns></returns> + public DateTime GetQuoteDateOrPreceding( DateTime date ) + { + if(this.HasDate(date)) + { + return date; + } + else + { + return GetQuoteDateOrPreceding(date.AddDays(-1)); + } + } + + /// <summary> + /// If the ticker has a quote at the given date, then it returns the given date, + /// else it returns the first valid following date at which a quote is available + /// (or the first valid preceding date, in case date is >= the last available quote) + /// </summary> + /// <param name="date">date</param> + /// <returns></returns> + public DateTime GetFirstValidQuoteDate(DateTime date) + { + DateTime startDate = this.StartDate; + DateTime endDate = this.EndDate; + if(date<startDate || (date>=startDate && date<=endDate)) + { + return this.GetQuoteDateOrFollowing(date); + } + else + { + return this.GetQuoteDateOrPreceding(date); + } + } + + /// <summary> + /// Gets the adjusted close at the given date + /// </summary> + /// <returns></returns> + public float GetAdjustedClose(DateTime date ) + { + object[] keys = new object[1]; + keys[0] = date.Date; + DataRow foundRow = this.Rows.Find(keys); + if(foundRow==null) + throw new Exception("No quote for such a date!"); + return (float)foundRow[Quotes.AdjustedClose]; + } + // public DateTime GetPrecedingDate( DateTime quoteDate , int precedingDays ) *************** *** 173,174 **** --- 307,309 ---- } } + Index: TickerDataTable.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b3_Data/DataTables/TickerDataTable.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TickerDataTable.cs 27 Jun 2004 19:20:31 -0000 1.2 --- TickerDataTable.cs 25 Jul 2004 12:08:53 -0000 1.3 *************** *** 59,63 **** } ! public static DataTable GetBestPerformingTickers(string groupID, DateTime firstQuoteDate, DateTime lastQuoteDate, --- 59,63 ---- } ! public static DataTable GetTickersByPerformance(bool orderByASC, string groupID, DateTime firstQuoteDate, DateTime lastQuoteDate, *************** *** 65,95 **** { DataTable groupOfTicker = Tickers_tickerGroups.GetTickers(groupID); ! //TO DO change to a structure compatible with TickerDataTable ! groupOfTicker.Columns.Add("SimpleReturn", System.Type.GetType("System.Double")); ! try { ! double firstQuote, lastQuote; ! foreach(DataRow row in groupOfTicker.Rows) { ! firstQuote = QuantProject.DataAccess.Tables.Quotes.GetAdjustedClose((string)row[0], ! firstQuoteDate); ! lastQuote = QuantProject.DataAccess.Tables.Quotes.GetAdjustedClose((string)row[0], ! lastQuoteDate); row["SimpleReturn"] = (lastQuote - firstQuote) / firstQuote; } - } ! catch(Exception ex) ! { ! System.Windows.Forms.MessageBox.Show(ex.ToString()); ! } ! ExtendedDataTable.Sort(groupOfTicker, "SimpleReturn"); ExtendedDataTable.DeleteRows(groupOfTicker, maxNumOfReturnedTickers); return groupOfTicker; } ! } } --- 65,132 ---- { DataTable groupOfTicker = Tickers_tickerGroups.GetTickers(groupID); ! //also possible, but slower: ! //return TickerDataTable.GetBestPerformingTickers(orderByASC, groupOfTicker, firstQuoteDate, ! // lastQuoteDate, maxNumOfReturnedTickers); ! ! TickerDataTable.addColumnsForPerformanceAnalysis(groupOfTicker); ! DateTime firstAvailableQuoteDate, lastAvailableQuoteDate; ! double firstQuote, lastQuote; ! QuantProject.Data.DataTables.GroupQuotes tickerQuotes = ! new QuantProject.Data.DataTables.GroupQuotes( ! groupID, new DateTime(1980,1,1), DateTime.Now); ! foreach(DataRow row in groupOfTicker.Rows) { ! if(tickerQuotes.GetNumberOfQuotes((string)row[0])>0) { ! firstAvailableQuoteDate = tickerQuotes.GetFirstValidQuoteDate((string)row[0], firstQuoteDate); ! lastAvailableQuoteDate = tickerQuotes.GetFirstValidQuoteDate((string)row[0], lastQuoteDate); ! firstQuote = tickerQuotes.GetAdjustedClose((string)row[0],firstAvailableQuoteDate); ! lastQuote = tickerQuotes.GetAdjustedClose((string)row[0],lastAvailableQuoteDate); row["SimpleReturn"] = (lastQuote - firstQuote) / firstQuote; + row["PeriodForSimpleReturn"] = "From " + firstAvailableQuoteDate.ToShortDateString() + " to " + lastAvailableQuoteDate.ToShortDateString(); } } ! ExtendedDataTable.Sort(groupOfTicker, "SimpleReturn", orderByASC); ExtendedDataTable.DeleteRows(groupOfTicker, maxNumOfReturnedTickers); return groupOfTicker; } + + public static DataTable GetTickersByPerformance(bool orderByASC, DataTable setOfTickers, + DateTime firstQuoteDate, + DateTime lastQuoteDate, + long maxNumOfReturnedTickers) + { + TickerDataTable.addColumnsForPerformanceAnalysis(setOfTickers); + DateTime firstAvailableQuoteDate, lastAvailableQuoteDate; + double firstQuote, lastQuote; + foreach(DataRow row in setOfTickers.Rows) + { + if(QuantProject.DataAccess.Tables.Quotes.GetNumberOfQuotes((string)row[0]) > 0) + { + QuantProject.Data.DataTables.Quotes quotesOfCurrentTicker = + new QuantProject.Data.DataTables.Quotes((string)row[0]); + firstAvailableQuoteDate = quotesOfCurrentTicker.GetFirstValidQuoteDate(firstQuoteDate); + lastAvailableQuoteDate = quotesOfCurrentTicker.GetFirstValidQuoteDate(lastQuoteDate); + firstQuote = quotesOfCurrentTicker.GetAdjustedClose(firstAvailableQuoteDate); + lastQuote = quotesOfCurrentTicker.GetAdjustedClose(lastAvailableQuoteDate); + row["SimpleReturn"] = (lastQuote - firstQuote) / firstQuote; + row["PeriodForSimpleReturn"] = "From " + firstAvailableQuoteDate.ToShortDateString() + " to " + lastAvailableQuoteDate.ToShortDateString(); + } + } + ExtendedDataTable.Sort(setOfTickers, "SimpleReturn", orderByASC); + ExtendedDataTable.DeleteRows(setOfTickers, maxNumOfReturnedTickers); + return setOfTickers; + } + private static void addColumnsForPerformanceAnalysis(DataTable tableToAnalyze) + { + if(!tableToAnalyze.Columns.Contains("SimpleReturn")) + tableToAnalyze.Columns.Add("SimpleReturn", System.Type.GetType("System.Double")); + if(!tableToAnalyze.Columns.Contains("PeriodForSimpleReturn")) + tableToAnalyze.Columns.Add("PeriodForSimpleReturn", System.Type.GetType("System.String")); + } ! } } |