[Quantproject-developers] QuantProject/b1_ADT/Histories History.cs, 1.15, 1.16
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-11-20 20:36:36
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Histories In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv30205/b1_ADT/Histories Modified Files: History.cs Log Message: A new property has been added: public List< DateTime > DateTimes Index: History.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Histories/History.cs,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** History.cs 9 Nov 2008 19:23:02 -0000 1.15 --- History.cs 20 Nov 2008 20:36:30 -0000 1.16 *************** *** 3,7 **** History.cs ! Copyright (C) 2003 Glauco Siliprandi --- 3,7 ---- History.cs ! Copyright (C) 2003 Glauco Siliprandi *************** *** 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 QuantProject.ADT; --- 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 QuantProject.ADT; *************** *** 106,110 **** foreach ( DateTime dateTime in selectingHistory.Keys ) if ( this.ContainsKey( dateTime ) ) ! returnValue.Add( dateTime , this.GetValue( dateTime ) ); return returnValue; } --- 107,111 ---- foreach ( DateTime dateTime in selectingHistory.Keys ) if ( this.ContainsKey( dateTime ) ) ! returnValue.Add( dateTime , this.GetValue( dateTime ) ); return returnValue; } *************** *** 122,126 **** foreach ( DateTime dateTime in comparingHistory.Keys ) if ( ! this.ContainsKey( ExtendedDateTime.GetDate( dateTime ) ) ) ! returnValue = false; return returnValue; } --- 123,127 ---- foreach ( DateTime dateTime in comparingHistory.Keys ) if ( ! this.ContainsKey( ExtendedDateTime.GetDate( dateTime ) ) ) ! returnValue = false; return returnValue; } *************** *** 135,139 **** foreach ( DateTime dateTime in comparingHistoryOfDateTimes.Keys ) if ( ! this.ContainsKey( dateTime ) ) ! returnValue = false; return returnValue; } --- 136,140 ---- foreach ( DateTime dateTime in comparingHistoryOfDateTimes.Keys ) if ( ! this.ContainsKey( dateTime ) ) ! returnValue = false; return returnValue; } *************** *** 151,166 **** foreach ( DateTime dateTime in comparingHistoryOfDateTimes.Keys ) if ( this.ContainsKey( dateTime ) ) ! numberOfContainedDateTimes++; ! return numberOfContainedDateTimes >= ! (percentageOfDateTimes * numberOfComparingDateTimes)/100; } public void Interpolate( ICollection dateTimeCollection , ! IInterpolationMethod interpolationMethod ) { foreach ( DateTime dateTime in dateTimeCollection ) if ( !this.ContainsKey( dateTime ) ) ! this.Add( dateTime , interpolationMethod.GetValue( this , dateTime ) ); } --- 152,167 ---- foreach ( DateTime dateTime in comparingHistoryOfDateTimes.Keys ) if ( this.ContainsKey( dateTime ) ) ! numberOfContainedDateTimes++; ! return numberOfContainedDateTimes >= ! (percentageOfDateTimes * numberOfComparingDateTimes)/100; } public void Interpolate( ICollection dateTimeCollection , ! IInterpolationMethod interpolationMethod ) { foreach ( DateTime dateTime in dateTimeCollection ) if ( !this.ContainsKey( dateTime ) ) ! this.Add( dateTime , interpolationMethod.GetValue( this , dateTime ) ); } *************** *** 229,233 **** History returnValue = new History(); foreach ( DateTime key in this.Keys ) ! { double currentValue = this.multiplyBy_getCurrentValue( key ); returnValue.Add( key , currentValue * factor ); --- 230,234 ---- History returnValue = new History(); foreach ( DateTime key in this.Keys ) ! { double currentValue = this.multiplyBy_getCurrentValue( key ); returnValue.Add( key , currentValue * factor ); *************** *** 259,264 **** //millo ! #region "GetFunctionHistory" ! /// <summary> /// Gets a History object based on a statistical available function --- 260,265 ---- //millo ! #region "GetFunctionHistory" ! /// <summary> /// Gets a History object based on a statistical available function *************** *** 280,295 **** /// /// <param name="endDateTime"> /// It sets the end date for the time interval containing the returned History ! /// </param> /// public History GetFunctionHistory(Function functionToBeCalculated, int onEachPeriodOf, ! DateTime startDateTime , DateTime endDateTime ) { History functionHistory = new History(); ! int currentHistoryIndex = this.IndexOfKeyOrPrevious(startDateTime); double[] data = new double[onEachPeriodOf]; //the array contains the set of data whose length is specified by the user double periodIndex = 0; //in the while statement, if it isn't equal to Floor(currentHistoryIndex/onEachPeriodOf) ! //the current index belongs to the period with periodIndex increased by one int cursorThroughDataArray = 0; while ( --- 281,296 ---- /// /// <param name="endDateTime"> /// It sets the end date for the time interval containing the returned History ! /// </param> /// public History GetFunctionHistory(Function functionToBeCalculated, int onEachPeriodOf, ! DateTime startDateTime , DateTime endDateTime ) { History functionHistory = new History(); ! int currentHistoryIndex = this.IndexOfKeyOrPrevious(startDateTime); double[] data = new double[onEachPeriodOf]; //the array contains the set of data whose length is specified by the user double periodIndex = 0; //in the while statement, if it isn't equal to Floor(currentHistoryIndex/onEachPeriodOf) ! //the current index belongs to the period with periodIndex increased by one int cursorThroughDataArray = 0; while ( *************** *** 297,317 **** ( ((IComparable)this.GetKey( currentHistoryIndex )).CompareTo( endDateTime ) <= 0 ) ) { ! if (Math.Floor(Convert.ToDouble(currentHistoryIndex/onEachPeriodOf)) == periodIndex && ! cursorThroughDataArray < onEachPeriodOf) //currentHistoryIndex belongs to the current period ! { data[cursorThroughDataArray] = Convert.ToDouble(this.GetByIndex(currentHistoryIndex)); cursorThroughDataArray++; functionHistory.Add(this.GetKey( currentHistoryIndex ), null); currentHistoryIndex++; ! } else //currentHistoryIndex doesn't belong to the current period //so a new item can be added to the object History to be returned ! { cursorThroughDataArray = 0; DateTime dateTime = (DateTime)this.GetKey( currentHistoryIndex - onEachPeriodOf); --- 298,318 ---- ( ((IComparable)this.GetKey( currentHistoryIndex )).CompareTo( endDateTime ) <= 0 ) ) { ! if (Math.Floor(Convert.ToDouble(currentHistoryIndex/onEachPeriodOf)) == periodIndex && ! cursorThroughDataArray < onEachPeriodOf) //currentHistoryIndex belongs to the current period ! { data[cursorThroughDataArray] = Convert.ToDouble(this.GetByIndex(currentHistoryIndex)); cursorThroughDataArray++; functionHistory.Add(this.GetKey( currentHistoryIndex ), null); currentHistoryIndex++; ! } else //currentHistoryIndex doesn't belong to the current period //so a new item can be added to the object History to be returned ! { cursorThroughDataArray = 0; DateTime dateTime = (DateTime)this.GetKey( currentHistoryIndex - onEachPeriodOf); *************** *** 320,329 **** case Function.SimpleAverage: functionHistory.SetByIndex(currentHistoryIndex - onEachPeriodOf, ! BasicFunctions.SimpleAverage(data)); //functionHistory.Add( dateTime , BasicFunctions.SimpleAverage(data) ); break; case Function.StandardDeviation : functionHistory.SetByIndex(currentHistoryIndex - onEachPeriodOf, ! BasicFunctions.StdDev(data)); //functionHistory.Add( dateTime , BasicFunctions.StdDev(data) ); break; --- 321,330 ---- case Function.SimpleAverage: functionHistory.SetByIndex(currentHistoryIndex - onEachPeriodOf, ! BasicFunctions.SimpleAverage(data)); //functionHistory.Add( dateTime , BasicFunctions.SimpleAverage(data) ); break; case Function.StandardDeviation : functionHistory.SetByIndex(currentHistoryIndex - onEachPeriodOf, ! BasicFunctions.StdDev(data)); //functionHistory.Add( dateTime , BasicFunctions.StdDev(data) ); break; *************** *** 338,498 **** } ! #endregion ! ! /// <summary> ! /// It returns true if the current History item value is not null ! /// and is less than the immediate previous History item whose value is not null ! /// </summary> ! /// <param name="dateTime">The date key for current History item</param> ! public bool IsDecreased(DateTime dateTime) ! { ! bool isDecreased = false; ! int index = this.IndexOfKey(dateTime); ! int previousIndex = index - 1; ! if ( index <= 0) ! isDecreased = false; ! else ! { ! if(this.GetByIndex(index) != null) ! { while (this.GetByIndex(previousIndex) == null) ! { previousIndex --; } ! isDecreased = Convert.ToDouble( this.GetByIndex(index)) < ! Convert.ToDouble( this.GetByIndex(previousIndex) ); ! } ! } ! return isDecreased; ! } ! #region "GetSimpleMovingAverage( int , DateTime , int )" ! private double currentContributionToCurrentSum( ! int index, ! DateTime dateTime , ! int numPeriods ! ) ! { ! double currentContribution; ! currentContribution = Convert.ToDouble( this.GetByIndex( index ) ); ! if ( index >= numPeriods ) ! currentContribution -= ! Convert.ToDouble( this.GetByIndex( index - numPeriods ) ); ! return currentContribution; ! } ! ! public History GetSimpleMovingAverage( int numPeriods , DateTime startDateTime , DateTime endDateTime ) ! { ! History simpleMovingAverage = new History(); ! int index = this.IndexOfKeyOrPrevious( startDateTime ); ! double currentSum = 0; ! while ( ! ( index < this.Count ) && ! ( ((IComparable)this.GetKey( index )).CompareTo( endDateTime ) <= 0 ) ) ! { ! DateTime dateTime = (DateTime)this.GetKey( index ); ! currentSum = currentSum + ! currentContributionToCurrentSum( index , dateTime , numPeriods ); ! if ( index < ( numPeriods - 1 ) ) ! // current period is not after numPeriods ! simpleMovingAverage.Add( this.GetKey( index ) , null ); ! else ! { ! simpleMovingAverage.Add( dateTime , currentSum/numPeriods ); ! } ! index++; ! } ! return simpleMovingAverage; ! } ! #endregion ! public History GetSimpleMovingAverage( int numPeriods ) ! { ! // History simpleMovingAverage = new History(); ! // double currentSum = 0; ! // foreach (DictionaryEntry dictionaryEntry in this) ! // { ! // currentSum = currentSum + ! // currentContributionToCurrentSum( dictionaryEntry , numPeriods ); ! // if ( this.IndexOfKey( dictionaryEntry.Key ) >= numPeriods ) ! // { ! // simpleMovingAverage.Add( dictionaryEntry.Key , currentSum/numPeriods ); ! // } ! // else ! // // current period is not after numPeriods ! // simpleMovingAverage.Add( dictionaryEntry.Key , null ); ! // } ! // return simpleMovingAverage; ! return GetSimpleMovingAverage( numPeriods , ! (DateTime) this.GetKey( 0 ) , (DateTime) this.GetKey( this.Count - 1 ) ); ! } ! #region "Cross" ! private bool wasLessThan( History history , DateTime dateTime ) ! { ! int backStep = 1; ! while ((backStep < this.IndexOfKey( dateTime )) && ! (this.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (history.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (Convert.ToDouble( this.GetByIndex(this.IndexOfKey( dateTime )-backStep) )== ! Convert.ToDouble(history.GetByIndex(history.IndexOfKey( dateTime )-backStep) ! ))) ! backStep ++; ! return ! (this.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (history.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (Convert.ToDouble( this.GetByIndex(this.IndexOfKey( dateTime )-backStep) )< ! Convert.ToDouble(history.GetByIndex(history.IndexOfKey( dateTime )-backStep) ! )); ! } ! public bool Cross( History history , DateTime dateTime ) ! { ! bool cross; ! if ( ( this.IndexOfKey( dateTime ) <= 0 ) || ( history.IndexOfKey( dateTime ) <= 0 ) ) ! cross = false; ! else ! { ! cross = ( ( Convert.ToDouble( this[ dateTime ] ) ) > ! ( Convert.ToDouble( history[ dateTime ] ) ) ) && ! ( wasLessThan( history , dateTime ) ); ! } ! return cross; ! } ! #endregion ! #region "ToString" ! private string singleToString( DateTime dateTime , Object historyValue ) ! { ! return "\nDate: " + dateTime + ! " Value: " + historyValue.ToString(); ! } ! private string dictionaryEntryToString( DictionaryEntry dictionaryEntry ) ! { ! string returnString = ""; ! if ( dictionaryEntry.Value.GetType() != Type.GetType( "System.Collections.ArrayList" ) ) ! // a single value is stored for this DateTime ! returnString = singleToString( (DateTime)dictionaryEntry.Key , dictionaryEntry.Value ); ! else ! // possibly multivalues are stored for this DateTime ! foreach (Object historyValue in ((ArrayList)dictionaryEntry.Value) ) ! returnString += singleToString( (DateTime)dictionaryEntry.Key , historyValue ); ! return returnString; ! } ! public override string ToString() ! { ! string toString = ""; ! foreach ( DictionaryEntry dictionaryEntry in this ) ! toString += dictionaryEntryToString( dictionaryEntry ); ! return toString; ! } ! #endregion ! public void ReportToConsole() ! { ! Console.Write( this.ToString() ); ! } ! } } --- 339,512 ---- } ! #endregion ! ! /// <summary> ! /// It returns true if the current History item value is not null ! /// and is less than the immediate previous History item whose value is not null ! /// </summary> ! /// <param name="dateTime">The date key for current History item</param> ! public bool IsDecreased(DateTime dateTime) ! { ! bool isDecreased = false; ! int index = this.IndexOfKey(dateTime); ! int previousIndex = index - 1; ! if ( index <= 0) ! isDecreased = false; ! else ! { ! if(this.GetByIndex(index) != null) ! { while (this.GetByIndex(previousIndex) == null) ! { previousIndex --; } ! isDecreased = Convert.ToDouble( this.GetByIndex(index)) < ! Convert.ToDouble( this.GetByIndex(previousIndex) ); ! } ! } ! return isDecreased; ! } ! #region "GetSimpleMovingAverage( int , DateTime , int )" ! private double currentContributionToCurrentSum( ! int index, ! DateTime dateTime , ! int numPeriods ! ) ! { ! double currentContribution; ! currentContribution = Convert.ToDouble( this.GetByIndex( index ) ); ! if ( index >= numPeriods ) ! currentContribution -= ! Convert.ToDouble( this.GetByIndex( index - numPeriods ) ); ! return currentContribution; ! } ! ! public History GetSimpleMovingAverage( int numPeriods , DateTime startDateTime , DateTime endDateTime ) ! { ! History simpleMovingAverage = new History(); ! int index = this.IndexOfKeyOrPrevious( startDateTime ); ! double currentSum = 0; ! while ( ! ( index < this.Count ) && ! ( ((IComparable)this.GetKey( index )).CompareTo( endDateTime ) <= 0 ) ) ! { ! DateTime dateTime = (DateTime)this.GetKey( index ); ! currentSum = currentSum + ! currentContributionToCurrentSum( index , dateTime , numPeriods ); ! if ( index < ( numPeriods - 1 ) ) ! // current period is not after numPeriods ! simpleMovingAverage.Add( this.GetKey( index ) , null ); ! else ! { ! simpleMovingAverage.Add( dateTime , currentSum/numPeriods ); ! } ! index++; ! } ! return simpleMovingAverage; ! } ! #endregion ! public History GetSimpleMovingAverage( int numPeriods ) ! { ! // History simpleMovingAverage = new History(); ! // double currentSum = 0; ! // foreach (DictionaryEntry dictionaryEntry in this) ! // { ! // currentSum = currentSum + ! // currentContributionToCurrentSum( dictionaryEntry , numPeriods ); ! // if ( this.IndexOfKey( dictionaryEntry.Key ) >= numPeriods ) ! // { ! // simpleMovingAverage.Add( dictionaryEntry.Key , currentSum/numPeriods ); ! // } ! // else ! // // current period is not after numPeriods ! // simpleMovingAverage.Add( dictionaryEntry.Key , null ); ! // } ! // return simpleMovingAverage; ! return GetSimpleMovingAverage( numPeriods , ! (DateTime) this.GetKey( 0 ) , (DateTime) this.GetKey( this.Count - 1 ) ); ! } ! #region "Cross" ! private bool wasLessThan( History history , DateTime dateTime ) ! { ! int backStep = 1; ! while ((backStep < this.IndexOfKey( dateTime )) && ! (this.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (history.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (Convert.ToDouble( this.GetByIndex(this.IndexOfKey( dateTime )-backStep) )== ! Convert.ToDouble(history.GetByIndex(history.IndexOfKey( dateTime )-backStep) ! ))) ! backStep ++; ! return ! (this.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (history.GetByIndex(history.IndexOfKey( dateTime )-backStep) != null) && ! (Convert.ToDouble( this.GetByIndex(this.IndexOfKey( dateTime )-backStep) )< ! Convert.ToDouble(history.GetByIndex(history.IndexOfKey( dateTime )-backStep) ! )); ! } ! public bool Cross( History history , DateTime dateTime ) ! { ! bool cross; ! if ( ( this.IndexOfKey( dateTime ) <= 0 ) || ( history.IndexOfKey( dateTime ) <= 0 ) ) ! cross = false; ! else ! { ! cross = ( ( Convert.ToDouble( this[ dateTime ] ) ) > ! ( Convert.ToDouble( history[ dateTime ] ) ) ) && ! ( wasLessThan( history , dateTime ) ); ! } ! return cross; ! } ! #endregion ! public List< DateTime > DateTimes ! { ! get ! { ! List<DateTime> dateTimes = ! new List<DateTime>(); ! foreach ( DateTime dateTime in this.Keys ) ! dateTimes.Add( dateTime ); ! return dateTimes; ! } ! } ! ! #region "ToString" ! private string singleToString( DateTime dateTime , Object historyValue ) ! { ! return "\nDate: " + dateTime + ! " Value: " + historyValue.ToString(); ! } ! private string dictionaryEntryToString( DictionaryEntry dictionaryEntry ) ! { ! string returnString = ""; ! if ( dictionaryEntry.Value.GetType() != Type.GetType( "System.Collections.ArrayList" ) ) ! // a single value is stored for this DateTime ! returnString = singleToString( (DateTime)dictionaryEntry.Key , dictionaryEntry.Value ); ! else ! // possibly multivalues are stored for this DateTime ! foreach (Object historyValue in ((ArrayList)dictionaryEntry.Value) ) ! returnString += singleToString( (DateTime)dictionaryEntry.Key , historyValue ); ! return returnString; ! } ! public override string ToString() ! { ! string toString = ""; ! foreach ( DictionaryEntry dictionaryEntry in this ) ! toString += dictionaryEntryToString( dictionaryEntry ); ! return toString; ! } ! #endregion ! ! public void ReportToConsole() ! { ! Console.Write( this.ToString() ); ! } ! } } |