[Quantproject-developers] QuantProject/b3_Data/DataTables Bars.cs, 1.1, 1.2
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-11-12 20:40:48
|
Update of /cvsroot/quantproject/QuantProject/b3_Data/DataTables In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv13728/b3_Data/DataTables Modified Files: Bars.cs Log Message: The following new method has been added: public static History GetMarketDateTimes( string ticker , DateTime firstDateTime , DateTime lastDateTime , List< Time > dailyTimes ) Index: Bars.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b3_Data/DataTables/Bars.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Bars.cs 9 Nov 2008 19:17:55 -0000 1.1 --- Bars.cs 12 Nov 2008 20:40:43 -0000 1.2 *************** *** 3,7 **** Bars.cs ! Copyright (C) 2008 Marco Milletti --- 3,7 ---- Bars.cs ! Copyright (C) 2008 Marco Milletti *************** *** 19,26 **** 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.Collections; using System.Data; using System.Runtime.Serialization; --- 19,27 ---- 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.Collections; + using System.Collections.Generic; using System.Data; using System.Runtime.Serialization; *************** *** 31,34 **** --- 32,36 ---- using QuantProject.ADT.Statistics; using QuantProject.ADT.Histories; + using QuantProject.ADT.Timing; using QuantProject.DataAccess; using QuantProject.DataAccess.Tables; *************** *** 43,47 **** { public static string TickerFieldName = "baTicker"; // Ticker cannot be simply used because ! public static string Exchange = "baExchange"; public static string DateTimeForOpen = "baDateTimeForOpen"; public static string IntervalFrameInSeconds = "baInterval"; --- 45,49 ---- { public static string TickerFieldName = "baTicker"; // Ticker cannot be simply used because ! public static string Exchange = "baExchange"; public static string DateTimeForOpen = "baDateTimeForOpen"; public static string IntervalFrameInSeconds = "baInterval"; *************** *** 61,65 **** public Bars( ICollection tickerCollection , DateTime dateTime ) { ! QuantProject.DataAccess.Tables.Bars.SetDataTable( tickerCollection , dateTime , this ); } --- 63,67 ---- public Bars( ICollection tickerCollection , DateTime dateTime ) { ! QuantProject.DataAccess.Tables.Bars.SetDataTable( tickerCollection , dateTime , this ); } *************** *** 91,95 **** if ( !marketDateTimes.ContainsKey( (DateTime)dataRow[ Bars.DateTimeForOpen ] ) ) ! dataRowsToBeRemoved.Add( dataRow ); return dataRowsToBeRemoved; } --- 93,97 ---- if ( !marketDateTimes.ContainsKey( (DateTime)dataRow[ Bars.DateTimeForOpen ] ) ) ! dataRowsToBeRemoved.Add( dataRow ); return dataRowsToBeRemoved; } *************** *** 103,107 **** ArrayList dataRowsToBeRemoved = this.removeNonContainedDateTimes_getDataRowsToBeRemoved( ! marketDateTimes ); this.removeDataRows( dataRowsToBeRemoved ); } --- 105,109 ---- ArrayList dataRowsToBeRemoved = this.removeNonContainedDateTimes_getDataRowsToBeRemoved( ! marketDateTimes ); this.removeDataRows( dataRowsToBeRemoved ); } *************** *** 110,127 **** public Bars( string ticker ) { ! this.fillDataTable( ticker , QuantProject.DataAccess.Tables.Bars.GetFirstBarDateTime( ticker ) , QuantProject.DataAccess.Tables.Bars.GetLastBarDateTime( ticker ) ); } ! public Bars(SerializationInfo info, StreamingContext context) ! : base(info, context) ! { ! } private void fillDataTable( string ticker , DateTime startDateTime , DateTime endDateTime ) { ! QuantProject.DataAccess.Tables.Bars.SetDataTable( ticker , startDateTime , endDateTime , this ); ! this.setPrimaryKeys(); } private void setPrimaryKeys() --- 112,129 ---- public Bars( string ticker ) { ! this.fillDataTable( ticker , QuantProject.DataAccess.Tables.Bars.GetFirstBarDateTime( ticker ) , QuantProject.DataAccess.Tables.Bars.GetLastBarDateTime( ticker ) ); } ! public Bars(SerializationInfo info, StreamingContext context) ! : base(info, context) ! { ! } private void fillDataTable( string ticker , DateTime startDateTime , DateTime endDateTime ) { ! QuantProject.DataAccess.Tables.Bars.SetDataTable( ticker , startDateTime , endDateTime , this ); ! this.setPrimaryKeys(); } private void setPrimaryKeys() *************** *** 132,136 **** } ! /// <summary> /// returns date times when the ticker was exchanged, within a given /// date time interval --- 134,138 ---- } ! /// <summary> /// returns date times when the ticker was exchanged, within a given /// date time interval *************** *** 141,182 **** /// <returns></returns> public static History GetMarketDateTimes( string ticker , ! DateTime firstDateTime , DateTime lastDateTime ) { Bars bars = new Bars( ticker , firstDateTime , lastDateTime ); History marketDateTimes = new History(); ! int i = 0; foreach ( DataRow dataRow in bars.Rows ) ! { ! marketDateTimes.Add( (DateTime)dataRow[ Bars.DateTimeForOpen ] , (DateTime)dataRow[ Bars.DateTimeForOpen ] ); ! i++; ! } return marketDateTimes; } ! #region GetCommonMarketDateTimes private static Hashtable getMarketDateTimes( ICollection tickers , DateTime firstDateTime , ! DateTime lastDateTime ) { Hashtable marketDateTimes = new Hashtable(); foreach ( string ticker in tickers ) if ( !marketDateTimes.ContainsKey( ticker ) ) ! { ! SortedList marketDateTimesForSingleTicker = ! GetMarketDateTimes( ticker , firstDateTime , lastDateTime ); ! marketDateTimes.Add( ticker , marketDateTimesForSingleTicker ); ! } return marketDateTimes; } private static bool isCommonDateTime( ICollection tickers , DateTime dateTime , ! Hashtable marketDateTimes ) { bool itIsCommon = true; foreach ( string ticker in tickers ) itIsCommon = itIsCommon && ! ((SortedList)marketDateTimes[ ticker ]).ContainsKey( dateTime ); return itIsCommon; } private static void getCommonMarketDateTimes_ifTheCaseAdd( ICollection tickers , DateTime dateTime , ! Hashtable marketDateTimes , AdvancedSortedList commonMarketDateTimes ) { if ( isCommonDateTime( tickers , dateTime , marketDateTimes ) ) --- 143,270 ---- /// <returns></returns> public static History GetMarketDateTimes( string ticker , ! DateTime firstDateTime , DateTime lastDateTime ) { Bars bars = new Bars( ticker , firstDateTime , lastDateTime ); History marketDateTimes = new History(); ! // int i = 0; foreach ( DataRow dataRow in bars.Rows ) ! // { ! marketDateTimes.Add( ! (DateTime)dataRow[ Bars.DateTimeForOpen ] , ! (DateTime)dataRow[ Bars.DateTimeForOpen ] ); ! // i++; ! // } return marketDateTimes; } ! ! #region GetMarketDateTimes ! ! // #region checkIfAreTimes ! // private static void checkIfIsTime( DateTime timeCandidate ) ! // { ! // if ( !ExtendedDateTime.IsTime( timeCandidate ) ) ! // // the timeCandidate does not represent a time ! // throw new Exception( ! // "The given DateTime was expected to be a time, but " + ! // "it is not. Use QuantProject.ADT.ExtendedDateTime.GetTime() " + ! // "to build such times."; ! // } ! // private static void checkIfAreTimes( List< DateTime > dailyTimes ) ! // { ! // foreach ( DateTime dateTime in dailyTimes ) ! // this.checkIfIsTime( DateTime dateTime ); ! // } ! // #endregion checkIfAreTimes ! ! #region getMarketDateTimes ! ! #region removeMissingTimes ! ! #region removeMissingTime ! private static bool isTimeInDailyTimes( ! DateTime dateTime , List< Time > dailyTimes ) ! { ! Time time = new Time( dateTime ); ! bool isInDailyTimes = ! dailyTimes.Contains( time ); ! return isInDailyTimes; ! } ! private static void removeMissingTime( ! DateTime candidateToBeRemoved , ! List< Time > dailyTimes , History marketDateTimes ) ! { ! if ( !Bars.isTimeInDailyTimes( candidateToBeRemoved , dailyTimes ) ) ! // the candidate's time is not in the given list of daily times ! marketDateTimes.Remove( candidateToBeRemoved ); ! } ! #endregion removeMissingTime ! ! private static void removeMissingTimes( ! List< Time > dailyTimes , History marketDateTimes ) ! { ! foreach ( DateTime dateTime in marketDateTimes ) ! Bars.removeMissingTime( ! dateTime , dailyTimes , marketDateTimes ); ! } ! private static History getMarketDateTimes( ! string ticker , DateTime firstDateTime , DateTime lastDateTime , ! List< Time > dailyTimes ) ! { ! History marketDateTimes = ! Bars.GetMarketDateTimes( ! ticker , firstDateTime , lastDateTime ); ! Bars.removeMissingTimes( dailyTimes , marketDateTimes ); ! return marketDateTimes; ! } ! #endregion removeMissingTimes ! ! #endregion getMarketDateTimes ! ! /// <summary> ! /// returns date times when the ticker was exchanged, within a given ! /// date time interval, but only when the time is in dailyTimes ! /// </summary> ! /// <param name="ticker"></param> ! /// <param name="firstDateTime"></param> ! /// <param name="lastDateTime"></param> ! /// <param name="dailyTimes"></param> ! /// <returns></returns> ! public static History GetMarketDateTimes( ! string ticker , DateTime firstDateTime , DateTime lastDateTime , ! List< Time > dailyTimes ) ! { ! // Bars.checkIfAreTimes( dailyTimes ); ! History marketDateTimes = ! Bars.getMarketDateTimes( ! ticker , firstDateTime , lastDateTime , dailyTimes ); ! return marketDateTimes; ! } ! #endregion GetMarketDateTimes ! #region GetCommonMarketDateTimes private static Hashtable getMarketDateTimes( ICollection tickers , DateTime firstDateTime , ! DateTime lastDateTime ) { Hashtable marketDateTimes = new Hashtable(); foreach ( string ticker in tickers ) if ( !marketDateTimes.ContainsKey( ticker ) ) ! { ! History marketDateTimesForSingleTicker = ! GetMarketDateTimes( ticker , firstDateTime , lastDateTime ); ! marketDateTimes.Add( ticker , marketDateTimesForSingleTicker ); ! } return marketDateTimes; } private static bool isCommonDateTime( ICollection tickers , DateTime dateTime , ! Hashtable marketDateTimes ) { bool itIsCommon = true; foreach ( string ticker in tickers ) itIsCommon = itIsCommon && ! ((List< DateTime >)marketDateTimes[ ticker ]).Contains( dateTime ); return itIsCommon; } private static void getCommonMarketDateTimes_ifTheCaseAdd( ICollection tickers , DateTime dateTime , ! Hashtable marketDateTimes , AdvancedSortedList commonMarketDateTimes ) { if ( isCommonDateTime( tickers , dateTime , marketDateTimes ) ) *************** *** 184,188 **** } private static SortedList getCommonMarketDateTimes( ICollection tickers , ! DateTime firstDateTime , DateTime lastDateTime , Hashtable marketDateTimes ) { AdvancedSortedList commonMarketDateTimes = new AdvancedSortedList(); --- 272,276 ---- } private static SortedList getCommonMarketDateTimes( ICollection tickers , ! DateTime firstDateTime , DateTime lastDateTime , Hashtable marketDateTimes ) { AdvancedSortedList commonMarketDateTimes = new AdvancedSortedList(); *************** *** 190,195 **** while ( currentDateTime <= lastDateTime ) { ! getCommonMarketDateTimes_ifTheCaseAdd( tickers , ! currentDateTime , marketDateTimes , commonMarketDateTimes ); currentDateTime = currentDateTime.AddMinutes( 1 ); //CHECK this statement: it could be cause great delay ... --- 278,283 ---- while ( currentDateTime <= lastDateTime ) { ! getCommonMarketDateTimes_ifTheCaseAdd( tickers , ! currentDateTime , marketDateTimes , commonMarketDateTimes ); currentDateTime = currentDateTime.AddMinutes( 1 ); //CHECK this statement: it could be cause great delay ... *************** *** 199,206 **** public static SortedList GetCommonMarketDateTimes( ICollection tickers , ! DateTime firstDateTime , DateTime lastDateTime ) { Hashtable marketDateTimes = getMarketDateTimes( tickers , firstDateTime , lastDateTime ); ! return getCommonMarketDateTimes( tickers , firstDateTime , lastDateTime , marketDateTimes ); } --- 287,294 ---- public static SortedList GetCommonMarketDateTimes( ICollection tickers , ! DateTime firstDateTime , DateTime lastDateTime ) { Hashtable marketDateTimes = getMarketDateTimes( tickers , firstDateTime , lastDateTime ); ! return getCommonMarketDateTimes( tickers , firstDateTime , lastDateTime , marketDateTimes ); } *************** *** 216,236 **** } ! /// <summary> ! /// Gets the dateTime of the first bar contained into the Bars object ! /// </summary> ! /// <returns></returns> ! public DateTime StartDateTime ! { ! get{ return ((DateTime)this.Rows[ 0 ][ Bars.DateTimeForOpen ]); } ! } ! /// <summary> ! /// Gets the dateTime of the last bar contained into the Bars object ! /// </summary> ! /// <returns></returns> ! public DateTime EndDateTime ! { ! get{ return ((DateTime)this.Rows[ this.Rows.Count - 1 ][ Bars.DateTimeForOpen ]); } ! } ! #region GetHashValue /* --- 304,324 ---- } ! /// <summary> ! /// Gets the dateTime of the first bar contained into the Bars object ! /// </summary> ! /// <returns></returns> ! public DateTime StartDateTime ! { ! get{ return ((DateTime)this.Rows[ 0 ][ Bars.DateTimeForOpen ]); } ! } ! /// <summary> ! /// Gets the dateTime of the last bar contained into the Bars object ! /// </summary> ! /// <returns></returns> ! public DateTime EndDateTime ! { ! get{ return ((DateTime)this.Rows[ this.Rows.Count - 1 ][ Bars.DateTimeForOpen ]); } ! } ! #region GetHashValue /* *************** *** 302,306 **** return HashProvider.GetHashValue( getHashValue_getQuoteString( quotes ) ); } ! */ #endregion --- 390,394 ---- return HashProvider.GetHashValue( getHashValue_getQuoteString( quotes ) ); } ! */ #endregion *************** *** 325,330 **** setHistory(); return (DateTime) history.GetKey( Math.Max( 0 , ! history.IndexOfKeyOrPrevious( dateTime ) - ! precedingMinutes ) ); } --- 413,418 ---- setHistory(); return (DateTime) history.GetKey( Math.Max( 0 , ! history.IndexOfKeyOrPrevious( dateTime ) - ! precedingMinutes ) ); } *************** *** 346,350 **** { followingDateTime = (DateTime) history.GetKey( Math.Max( 0 , ! indexOfKeyOrPrevious + followingMinutes ) ); } catch ( ArgumentOutOfRangeException exception ) --- 434,438 ---- { followingDateTime = (DateTime) history.GetKey( Math.Max( 0 , ! indexOfKeyOrPrevious + followingMinutes ) ); } catch ( ArgumentOutOfRangeException exception ) *************** *** 352,430 **** string message = exception.Message; throw new Exception( "Quotes.GetFollowingDateTime() error: there is not " + ! "a dateTime for dateTime=" + dateTime.ToString() + ! " and followingMinutes=" + followingMinutes ); } return followingDateTime; } ! ! /// <summary> ! /// Returns true if a bar is available at the given dateTime ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public bool HasDateTime( DateTime dateTime ) ! { ! /*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(dateTime); ! } ! /// <summary> ! /// If the ticker has a bar at the given dateTime, then it returns the given dateTime, ! /// else it returns the immediate following dateTime at which a bar is available ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public DateTime GetBarDateTimeOrFollowing( DateTime dateTime ) ! { ! if( this.HasDateTime( dateTime ) ) ! { ! return dateTime; ! } ! else ! { ! return GetBarDateTimeOrFollowing( dateTime.AddMinutes( 1 ) ); ! } ! } ! /// <summary> ! /// If the ticker has a bar at the given dateTime, then it returns the given dateTime, ! /// else it returns the immediate preceding dateTime at which a bar is available ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public DateTime GetBarDateTimeOrPreceding( DateTime dateTime ) ! { ! if( this.HasDateTime( dateTime ) ) ! { ! return dateTime; ! } ! else ! { ! return GetBarDateTimeOrPreceding( dateTime.AddMinutes( - 1 ) ); ! } ! } ! /// <summary> ! /// If the ticker has a bar at the given dateTime, then it returns the given dateTime, ! /// else it returns the first valid following dateTime at which a bar is available ! /// (or the first valid preceding dateTime, in case dateTime is >= the dateTime of the last available bar) ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public DateTime GetFirstValidBarDateTime(DateTime dateTime) ! { ! DateTime startDateTime = this.StartDateTime; ! DateTime endDateTime = this.EndDateTime; ! if( dateTime < startDateTime || (dateTime>=startDateTime && dateTime<=endDateTime)) ! { ! return this.GetBarDateTimeOrFollowing(dateTime); ! } ! else ! { ! return this.GetBarDateTimeOrPreceding(dateTime); ! } ! } /// <summary> /// returns the first bar dateTime for the ticker --- 440,518 ---- string message = exception.Message; throw new Exception( "Quotes.GetFollowingDateTime() error: there is not " + ! "a dateTime for dateTime=" + dateTime.ToString() + ! " and followingMinutes=" + followingMinutes ); } return followingDateTime; } ! ! /// <summary> ! /// Returns true if a bar is available at the given dateTime ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public bool HasDateTime( DateTime dateTime ) ! { ! /*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(dateTime); ! } ! /// <summary> ! /// If the ticker has a bar at the given dateTime, then it returns the given dateTime, ! /// else it returns the immediate following dateTime at which a bar is available ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public DateTime GetBarDateTimeOrFollowing( DateTime dateTime ) ! { ! if( this.HasDateTime( dateTime ) ) ! { ! return dateTime; ! } ! else ! { ! return GetBarDateTimeOrFollowing( dateTime.AddMinutes( 1 ) ); ! } ! } ! /// <summary> ! /// If the ticker has a bar at the given dateTime, then it returns the given dateTime, ! /// else it returns the immediate preceding dateTime at which a bar is available ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public DateTime GetBarDateTimeOrPreceding( DateTime dateTime ) ! { ! if( this.HasDateTime( dateTime ) ) ! { ! return dateTime; ! } ! else ! { ! return GetBarDateTimeOrPreceding( dateTime.AddMinutes( - 1 ) ); ! } ! } ! /// <summary> ! /// If the ticker has a bar at the given dateTime, then it returns the given dateTime, ! /// else it returns the first valid following dateTime at which a bar is available ! /// (or the first valid preceding dateTime, in case dateTime is >= the dateTime of the last available bar) ! /// </summary> ! /// <param name="dateTime">dateTime</param> ! /// <returns></returns> ! public DateTime GetFirstValidBarDateTime(DateTime dateTime) ! { ! DateTime startDateTime = this.StartDateTime; ! DateTime endDateTime = this.EndDateTime; ! if( dateTime < startDateTime || (dateTime>=startDateTime && dateTime<=endDateTime)) ! { ! return this.GetBarDateTimeOrFollowing(dateTime); ! } ! else ! { ! return this.GetBarDateTimeOrPreceding(dateTime); ! } ! } /// <summary> /// returns the first bar dateTime for the ticker *************** *** 438,482 **** ! /// <summary> ! /// Gets the close at the given dateTime ! /// </summary> ! /// <returns></returns> ! public float GetClose(DateTime dateTime ) ! { ! object[] keys = new object[1]; ! keys[0] = dateTime.Date; ! DataRow foundRow = this.Rows.Find(keys); ! if(foundRow==null) ! throw new Exception("No bar for such a dateTime!"); ! return (float)foundRow[ Bars.Close ]; ! } ! /// <summary> ! /// Gets the first valid raw (not adjusted) close at the given date ! /// </summary> ! /// <returns></returns> ! public float GetFirstValidClose(DateTime dateTime ) ! { ! object[] keys = new object[1]; ! keys[0] = this.GetFirstValidBarDateTime(dateTime); ! DataRow foundRow = this.Rows.Find(keys); ! if(foundRow==null) ! throw new Exception("No bar for such a dateTime!"); ! return (float)foundRow[Bars.Close]; ! } ! ! /// <summary> ! /// Gets the first valid open at the given dateTime ! /// </summary> ! /// <returns></returns> ! public float GetFirstValidOpen(DateTime dateTime ) ! { ! object[] keys = new object[1]; ! keys[0] = this.GetFirstValidBarDateTime(dateTime); ! DataRow foundRow = this.Rows.Find(keys); ! if(foundRow==null) ! throw new Exception("No bar for such a dateTime!"); ! return (float)foundRow[Bars.Open]; ! } } } --- 526,570 ---- ! /// <summary> ! /// Gets the close at the given dateTime ! /// </summary> ! /// <returns></returns> ! public float GetClose(DateTime dateTime ) ! { ! object[] keys = new object[1]; ! keys[0] = dateTime.Date; ! DataRow foundRow = this.Rows.Find(keys); ! if(foundRow==null) ! throw new Exception("No bar for such a dateTime!"); ! return (float)foundRow[ Bars.Close ]; ! } ! /// <summary> ! /// Gets the first valid raw (not adjusted) close at the given date ! /// </summary> ! /// <returns></returns> ! public float GetFirstValidClose(DateTime dateTime ) ! { ! object[] keys = new object[1]; ! keys[0] = this.GetFirstValidBarDateTime(dateTime); ! DataRow foundRow = this.Rows.Find(keys); ! if(foundRow==null) ! throw new Exception("No bar for such a dateTime!"); ! return (float)foundRow[Bars.Close]; ! } ! ! /// <summary> ! /// Gets the first valid open at the given dateTime ! /// </summary> ! /// <returns></returns> ! public float GetFirstValidOpen(DateTime dateTime ) ! { ! object[] keys = new object[1]; ! keys[0] = this.GetFirstValidBarDateTime(dateTime); ! DataRow foundRow = this.Rows.Find(keys); ! if(foundRow==null) ! throw new Exception("No bar for such a dateTime!"); ! return (float)foundRow[Bars.Open]; ! } } } |