[Quantproject-developers] QuantProject/b4_Business/a2_Strategies WeightedPositions.cs, 1.15, 1.16
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2010-03-28 13:38:41
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv9087/a2_Strategies Modified Files: WeightedPositions.cs Log Message: This class, previously inherited a SortedList. Now it inherits List<WeightedPosition>. Now there is a "deterministic" mapping between genomes and weighted positions. Furthermore, now this class implements IReturnsCalculator (this helps with unit testing and mocking) Index: WeightedPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/WeightedPositions.cs,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** WeightedPositions.cs 29 Sep 2008 21:17:57 -0000 1.15 --- WeightedPositions.cs 28 Mar 2010 13:38:31 -0000 1.16 *************** *** 3,7 **** WeightedPositions.cs ! Copyright (C) 2003 Glauco Siliprandi --- 3,7 ---- WeightedPositions.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 QuantProject.ADT.Statistics; --- 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 QuantProject.ADT.Statistics; *************** *** 40,44 **** /// checked to sum up to 1. /// </summary> ! public class WeightedPositions : SortedList { /// <summary> --- 41,45 ---- /// checked to sum up to 1. /// </summary> ! public class WeightedPositions : List<WeightedPosition> , IReturnsCalculator { /// <summary> *************** *** 61,70 **** get { if(this.numberOfLongPositions == int.MaxValue) ! //that is private field has not been assigned yet { this.numberOfLongPositions = 0; ! foreach(WeightedPosition weightedPosition in this.Values) if(weightedPosition.IsLong) ! this.numberOfLongPositions++; } return this.numberOfLongPositions; --- 62,71 ---- get { if(this.numberOfLongPositions == int.MaxValue) ! //that is private field has not been assigned yet { this.numberOfLongPositions = 0; ! foreach(WeightedPosition weightedPosition in this) if(weightedPosition.IsLong) ! this.numberOfLongPositions++; } return this.numberOfLongPositions; *************** *** 75,84 **** get { if(this.numberOfShortPositions == int.MaxValue) ! //that is private field has not been assigned yet { this.numberOfShortPositions = 0; ! foreach(WeightedPosition weightedPosition in this.Values) if(weightedPosition.IsShort) ! this.numberOfShortPositions++; } return this.numberOfShortPositions; --- 76,85 ---- get { if(this.numberOfShortPositions == int.MaxValue) ! //that is private field has not been assigned yet { this.numberOfShortPositions = 0; ! foreach(WeightedPosition weightedPosition in this) if(weightedPosition.IsShort) ! this.numberOfShortPositions++; } return this.numberOfShortPositions; *************** *** 107,111 **** string[] tickers = {"DUMMY"}; WeightedPositions testInstance = new WeightedPositions( weights , tickers ); ! return testInstance; } } --- 108,112 ---- string[] tickers = {"DUMMY"}; WeightedPositions testInstance = new WeightedPositions( weights , tickers ); ! return testInstance; } } *************** *** 142,146 **** private void weightedPositions_default( double[] normalizedWeightValues , ! string[] tickers ) { this.checkParameters( normalizedWeightValues , tickers ); --- 143,147 ---- private void weightedPositions_default( double[] normalizedWeightValues , ! string[] tickers ) { this.checkParameters( normalizedWeightValues , tickers ); *************** *** 149,178 **** string ticker = tickers[ i ]; double weight = normalizedWeightValues[ i ]; ! if ( !this.ContainsKey( ticker ) ) ! this.Add( ticker , new WeightedPosition( weight , ticker ) ); } } public WeightedPositions( double[] normalizedWeightValues , ! string[] tickers ) { this.weightedPositions_default( normalizedWeightValues, ! tickers ); } public WeightedPositions( double[] normalizedUnsignedWeightValues, ! SignedTickers signedTickers ) { string[] unsignedTickers = new string [ signedTickers.Count ]; ! double[] normalizedSignedWeightValues = new double[ normalizedUnsignedWeightValues.Length ]; for(int i = 0; i < signedTickers.Count; i++) { unsignedTickers[i] = signedTickers[i].Ticker; ! normalizedSignedWeightValues[i] = ! signedTickers[i].Multiplier * normalizedUnsignedWeightValues[i]; } this.weightedPositions_default(normalizedSignedWeightValues, ! unsignedTickers); } --- 150,180 ---- string ticker = tickers[ i ]; double weight = normalizedWeightValues[ i ]; ! this.Add( new WeightedPosition( weight , ticker ) ); ! // if ( !this.ContainsKey( ticker ) ) ! // this.Add( ticker , new WeightedPosition( weight , ticker ) ); } } public WeightedPositions( double[] normalizedWeightValues , ! string[] tickers ) { this.weightedPositions_default( normalizedWeightValues, ! tickers ); } public WeightedPositions( double[] normalizedUnsignedWeightValues, ! SignedTickers signedTickers ) { string[] unsignedTickers = new string [ signedTickers.Count ]; ! double[] normalizedSignedWeightValues = new double[ normalizedUnsignedWeightValues.Length ]; for(int i = 0; i < signedTickers.Count; i++) { unsignedTickers[i] = signedTickers[i].Ticker; ! normalizedSignedWeightValues[i] = ! signedTickers[i].Multiplier * normalizedUnsignedWeightValues[i]; } this.weightedPositions_default(normalizedSignedWeightValues, ! unsignedTickers); } *************** *** 184,197 **** { string[] unsignedTickers = new string [ signedTickers.Count ]; ! double[] allEqualSignedWeights = new double[ signedTickers.Count ]; for(int i = 0; i < signedTickers.Count; i++) { unsignedTickers[i] = signedTickers[i].Ticker; ! allEqualSignedWeights[i] = signedTickers[i].Multiplier / (double)signedTickers.Count; } this.weightedPositions_default( allEqualSignedWeights, ! unsignedTickers ); } --- 186,199 ---- { string[] unsignedTickers = new string [ signedTickers.Count ]; ! double[] allEqualSignedWeights = new double[ signedTickers.Count ]; for(int i = 0; i < signedTickers.Count; i++) { unsignedTickers[i] = signedTickers[i].Ticker; ! allEqualSignedWeights[i] = signedTickers[i].Multiplier / (double)signedTickers.Count; } this.weightedPositions_default( allEqualSignedWeights, ! unsignedTickers ); } *************** *** 202,246 **** foreach ( string ticker in tickers ) if ( !sortedTicker.ContainsKey( ticker ) ) ! sortedTicker.Add( ticker , ticker ); ! else ! throw new Exception( "The WeightedPositions constructur " + ! "has received a tickers parameter with the ticker '" + ! ticker + "' that is contained twice! This is not allowed." ); } private void checkParameters( double[] normalizedWeightValues , ! string[] tickers ) { if ( normalizedWeightValues.Length != tickers.Length ) throw new Exception( "The number of normalized weights is " + ! "different from the number of tickers. They should be the same " + ! "number!" ); double totalWeight = ADT.Statistics.BasicFunctions.SumOfAbs( normalizedWeightValues ); if ( ( totalWeight < 0.999 ) || ( totalWeight > 1.001 ) ) throw new Exception( "The total of (absolute) weights " + ! "should sum up to 1, " + ! "but it sums up to " + totalWeight.ToString() ); this.checkParameters_checkDoubleTickers( tickers ); } #endregion ! public WeightedPosition GetWeightedPosition( string ticker ) { ! return (WeightedPosition)this[ ticker ]; } ! public WeightedPosition GetWeightedPosition( int i ) { ! return (WeightedPosition)this.GetByIndex( i ); } ! public WeightedPosition this[ int index ] { ! get ! { ! return (WeightedPosition)this.GetByIndex( index ); ! } ! set ! { ! this.SetByIndex( index, value ); ! } } #region GetEquityLine --- 204,281 ---- foreach ( string ticker in tickers ) if ( !sortedTicker.ContainsKey( ticker ) ) ! sortedTicker.Add( ticker , ticker ); ! else ! throw new Exception( "The WeightedPositions constructur " + ! "has received a tickers parameter with the ticker '" + ! ticker + "' that is contained twice! This is not allowed." ); } private void checkParameters( double[] normalizedWeightValues , ! string[] tickers ) { if ( normalizedWeightValues.Length != tickers.Length ) throw new Exception( "The number of normalized weights is " + ! "different from the number of tickers. They should be the same " + ! "number!" ); double totalWeight = ADT.Statistics.BasicFunctions.SumOfAbs( normalizedWeightValues ); if ( ( totalWeight < 0.999 ) || ( totalWeight > 1.001 ) ) throw new Exception( "The total of (absolute) weights " + ! "should sum up to 1, " + ! "but it sums up to " + totalWeight.ToString() ); this.checkParameters_checkDoubleTickers( tickers ); } #endregion ! ! /// <summary> ! /// if a position with the given ticker has been found, then its index ! /// is returned; if it is not found, then -1 is returned ! /// </summary> ! /// <param name="ticker"></param> ! /// <returns></returns> ! public int GetIndexOfTicker( string ticker ) { ! int indexOfTicker = -1; ! int indexCandidate = 0; ! while ( indexCandidate < this.Count && ! this[ indexCandidate ].Ticker != ticker ) ! indexCandidate++; ! if ( this[ indexCandidate ].Ticker == ticker ) ! indexOfTicker = indexCandidate; ! return indexOfTicker; } ! ! public bool ContainsTicker( string ticker ) { ! int indexOfTicker = this.GetIndexOfTicker( ticker ); ! bool containsTicker = ( indexOfTicker == -1 ); ! return containsTicker; } ! ! public WeightedPosition GetWeightedPosition( string ticker ) { ! WeightedPosition weightedPosition = null; ! int indexOfTicker = this.GetIndexOfTicker( ticker ); ! if ( indexOfTicker >= 0 ) ! // a position with the given ticker has been found ! weightedPosition = this[ indexOfTicker ]; ! return weightedPosition; } + + public WeightedPosition GetWeightedPosition( int i ) + { + return this[ i ]; + } + + // public WeightedPosition this[ int index ] + // { + // get + // { + // return (WeightedPosition)this.GetByIndex( index ); + // } + // set + // { + // this.SetByIndex( index, value ); + // } + // } #region GetEquityLine *************** *** 258,262 **** // EndOfDayDateTime endOfDayDateTime = // new EndOfDayDateTime( dateTime , EndOfDaySpecificTime.MarketClose ); ! foreach( WeightedPosition weightedPosition in this.Values ) { string ticker = weightedPosition.Ticker; --- 293,297 ---- // EndOfDayDateTime endOfDayDateTime = // new EndOfDayDateTime( dateTime , EndOfDaySpecificTime.MarketClose ); ! foreach( WeightedPosition weightedPosition in this ) { string ticker = weightedPosition.Ticker; *************** *** 276,280 **** { double cash = beginningCash; ! foreach ( WeightedPosition weightedPosition in this.Values ) { double thisVirtualQuantity = --- 311,315 ---- { double cash = beginningCash; ! foreach ( WeightedPosition weightedPosition in this ) { double thisVirtualQuantity = *************** *** 292,296 **** } private double getVirtualPortfolioValue( DateTime dateTime , ! Hashtable tickerVirtualQuantities ) { HistoricalAdjustedQuoteProvider historicalAdjustedQuoteProvider = --- 327,331 ---- } private double getVirtualPortfolioValue( DateTime dateTime , ! Hashtable tickerVirtualQuantities ) { HistoricalAdjustedQuoteProvider historicalAdjustedQuoteProvider = *************** *** 324,334 **** Hashtable virtualQuantities = this.getVirtualQuantities( beginningCash , firstDate ); ! double cash = this.getCash( beginningCash , virtualQuantities ); for( int i = 0 ; i < equityDates.Count ; i++ ) { DateTime dateTime = (DateTime)equityDates.GetKey( i ); equityLine.Add( dateTime , ! cash + this.getVirtualPortfolioValue( dateTime , ! virtualQuantities ) ); } return equityLine; --- 359,369 ---- Hashtable virtualQuantities = this.getVirtualQuantities( beginningCash , firstDate ); ! double cash = this.getCash( beginningCash , virtualQuantities ); for( int i = 0 ; i < equityDates.Count ; i++ ) { DateTime dateTime = (DateTime)equityDates.GetKey( i ); equityLine.Add( dateTime , ! cash + this.getVirtualPortfolioValue( dateTime , ! virtualQuantities ) ); } return equityLine; *************** *** 343,347 **** double dailyReturn = 0.0; foreach ( WeightedPosition weightedPosition in ! this.Values ) dailyReturn += weightedPosition.GetCloseToCloseDailyReturn( dateTime ); --- 378,382 ---- double dailyReturn = 0.0; foreach ( WeightedPosition weightedPosition in ! this ) dailyReturn += weightedPosition.GetCloseToCloseDailyReturn( dateTime ); *************** *** 358,362 **** closeToClosePortfolioReturns[ i ] = this.getCloseToClosePortfolioReturn( ! datesForReturnComputation , i ); return closeToClosePortfolioReturns; } --- 393,397 ---- closeToClosePortfolioReturns[ i ] = this.getCloseToClosePortfolioReturn( ! datesForReturnComputation , i ); return closeToClosePortfolioReturns; } *************** *** 403,407 **** } private static double[] getNormalizedWeights( double[] nonNormalizedWeights , ! double normalizingFactor ) { double[] normalizedWeights = new double[ nonNormalizedWeights.Length ]; --- 438,442 ---- } private static double[] getNormalizedWeights( double[] nonNormalizedWeights , ! double normalizingFactor ) { double[] normalizedWeights = new double[ nonNormalizedWeights.Length ]; *************** *** 437,442 **** // return tickerReturns; // } ! private static float getTickerReturnsStandardDeviations( int tickerIndex , ! SignedTickers signedTickers , ReturnsManager returnsManager ) { string ticker = signedTickers[ tickerIndex ].Ticker; --- 472,477 ---- // return tickerReturns; // } ! private static float getTickerReturnsStandardDeviations( ! int tickerIndex , SignedTickers signedTickers , IReturnsManager returnsManager ) { string ticker = signedTickers[ tickerIndex ].Ticker; *************** *** 446,450 **** } private static float[] getTickersReturnsStandardDeviations( ! SignedTickers signedTickers , ReturnsManager returnsManager ) { float[] tickersReturnsStandardDeviations = --- 481,485 ---- } private static float[] getTickersReturnsStandardDeviations( ! SignedTickers signedTickers , IReturnsManager returnsManager ) { float[] tickersReturnsStandardDeviations = *************** *** 453,457 **** tickersReturnsStandardDeviations[ i ] = getTickerReturnsStandardDeviations( i , ! signedTickers , returnsManager ); return tickersReturnsStandardDeviations; } --- 488,492 ---- tickersReturnsStandardDeviations[ i ] = getTickerReturnsStandardDeviations( i , ! signedTickers , returnsManager ); return tickersReturnsStandardDeviations; } *************** *** 469,473 **** nonNormalizedWeightsButBalancedForVolatility[ i ] = getNonNormalizedWeightsButBalancedForVolatility( ! standardDeviations , maxStandardDeviation , i ); return nonNormalizedWeightsButBalancedForVolatility; } --- 504,508 ---- nonNormalizedWeightsButBalancedForVolatility[ i ] = getNonNormalizedWeightsButBalancedForVolatility( ! standardDeviations , maxStandardDeviation , i ); return nonNormalizedWeightsButBalancedForVolatility; } *************** *** 481,489 **** } private static double[] getUnsignedNormalizedBalancedWeights( ! SignedTickers signedTickers , ReturnsManager returnManager ) { float[] standardDeviations = getTickersReturnsStandardDeviations( signedTickers , ! returnManager ); double[] nonNormalizedButBalancedWeights = getNonNormalizedWeightsButBalancedForVolatility( standardDeviations ); --- 516,524 ---- } private static double[] getUnsignedNormalizedBalancedWeights( ! SignedTickers signedTickers , IReturnsManager returnManager ) { float[] standardDeviations = getTickersReturnsStandardDeviations( signedTickers , ! returnManager ); double[] nonNormalizedButBalancedWeights = getNonNormalizedWeightsButBalancedForVolatility( standardDeviations ); *************** *** 511,515 **** double[] signedNormalizedBalancedWeights = getSignedNormalizedBalancedWeights( multipliers , ! unsignedNormalizedBalancedWeights ); return signedNormalizedBalancedWeights; } --- 546,550 ---- double[] signedNormalizedBalancedWeights = getSignedNormalizedBalancedWeights( multipliers , ! unsignedNormalizedBalancedWeights ); return signedNormalizedBalancedWeights; } *************** *** 522,533 **** /// <returns></returns> public static double[] GetBalancedWeights( ! SignedTickers signedTickers , ReturnsManager returnManager ) { double[] unsignedNormalizedBalancedWeights = getUnsignedNormalizedBalancedWeights( ! signedTickers , returnManager ); double[] balancedWeights = getSignedNormalizedBalancedWeights( ! signedTickers , unsignedNormalizedBalancedWeights ); return balancedWeights; } --- 557,568 ---- /// <returns></returns> public static double[] GetBalancedWeights( ! SignedTickers signedTickers , IReturnsManager returnManager ) { double[] unsignedNormalizedBalancedWeights = getUnsignedNormalizedBalancedWeights( ! signedTickers , returnManager ); double[] balancedWeights = getSignedNormalizedBalancedWeights( ! signedTickers , unsignedNormalizedBalancedWeights ); return balancedWeights; } *************** *** 535,539 **** #region GetReturn private void getReturnCheckParameters( int i , ! ReturnsManager returnsManager ) { if ( ( i < 0 ) || ( i > returnsManager.ReturnIntervals.Count - 1 ) ) --- 570,574 ---- #region GetReturn private void getReturnCheckParameters( int i , ! IReturnsManager returnsManager ) { if ( ( i < 0 ) || ( i > returnsManager.ReturnIntervals.Count - 1 ) ) *************** *** 541,562 **** } private float getTickerReturn( string ticker , int i , ! ReturnsManager returnsManager ) { return returnsManager.GetReturn( ticker , i ); } private float getReturnActually( WeightedPosition weightedPosition , ! int i , ReturnsManager returnsManager ) { float tickerReturn = this.getTickerReturn( weightedPosition.Ticker , ! i , returnsManager ); return tickerReturn * Convert.ToSingle( weightedPosition.Weight ); } ! private float getReturnActually( int i , ReturnsManager returnsManager ) { float linearCombinationReturn = 0; ! foreach ( WeightedPosition weightedPosition in this.Values ) linearCombinationReturn += this.getReturnActually( weightedPosition , ! i , returnsManager ); return linearCombinationReturn; } --- 576,597 ---- } private float getTickerReturn( string ticker , int i , ! IReturnsManager returnsManager ) { return returnsManager.GetReturn( ticker , i ); } private float getReturnActually( WeightedPosition weightedPosition , ! int i , IReturnsManager returnsManager ) { float tickerReturn = this.getTickerReturn( weightedPosition.Ticker , ! i , returnsManager ); return tickerReturn * Convert.ToSingle( weightedPosition.Weight ); } ! private float getReturnActually( int i , IReturnsManager returnsManager ) { float linearCombinationReturn = 0; ! foreach ( WeightedPosition weightedPosition in this ) linearCombinationReturn += this.getReturnActually( weightedPosition , ! i , returnsManager ); return linearCombinationReturn; } *************** *** 568,572 **** /// ticker returns</param> /// <returns></returns> ! public float GetReturn( int i , ReturnsManager returnsManager ) { this.getReturnCheckParameters( i , returnsManager ); --- 603,607 ---- /// ticker returns</param> /// <returns></returns> ! public float GetReturn( int i , IReturnsManager returnsManager ) { this.getReturnCheckParameters( i , returnsManager ); *************** *** 575,578 **** --- 610,614 ---- } #endregion GetReturn + /// <summary> /// Computes an array of floats representing the returns *************** *** 581,585 **** /// <param name="returnsManager"></param> /// <returns></returns> ! public float[] GetReturns( ReturnsManager returnsManager ) { float[] returns = new float[ --- 617,621 ---- /// <param name="returnsManager"></param> /// <returns></returns> ! public float[] GetReturns( IReturnsManager returnsManager ) { float[] returns = new float[ *************** *** 599,607 **** for ( int intervalIndex = 0 ; ! intervalIndex < returnsManager.NumberOfReturns ; intervalIndex++ ) { returns[ intervalIndex ] = 0; for ( int positionIndex = 0 ; positionIndex < this.Count ; ! positionIndex++ ) returns[ intervalIndex ] += tickersReturns[ positionIndex ][ intervalIndex ] * --- 635,643 ---- for ( int intervalIndex = 0 ; ! intervalIndex < returnsManager.NumberOfReturns ; intervalIndex++ ) { returns[ intervalIndex ] = 0; for ( int positionIndex = 0 ; positionIndex < this.Count ; ! positionIndex++ ) returns[ intervalIndex ] += tickersReturns[ positionIndex ][ intervalIndex ] * *************** *** 610,632 **** return returns; } /// <summary> ! /// Gets the Open To Close return for the current instance of WeightedPositions ! /// </summary> ! /// <param name="marketDate">Market date for which return has to be computed</param> ! public double GetOpenToCloseReturn(DateTime marketDate) ! { ! Quotes[] tickersQuotes = new Quotes[this.Count]; ! for(int i = 0; i<this.Count; i++) tickersQuotes[i] = new Quotes( this[i].Ticker,marketDate,marketDate ); ! double openToCloseReturn = 0.0; ! for(int i = 0; i < this.Count ; i++) ! openToCloseReturn += ! ( (float)tickersQuotes[i].Rows[0]["quClose"] / ! (float)tickersQuotes[i].Rows[0]["quOpen"] - 1.0f ) * ! (float)this[i].Weight; ! return openToCloseReturn; ! } ! // private double getLastNightReturn( float[] weightedPositionsLastNightReturns ) // { // double returnValue = 0.0; --- 646,711 ---- return returns; } + /// <summary> ! /// Computes an array of floats representing the returns ! /// of all weighted position ! /// </summary> ! /// <param name="returnsManager">intervals on which returns are to ! /// be computed</param> ! /// <param name="firstInterval">0-base index of the first interval to ! /// be considered</param> ! /// <param name="lastInterval">0-base index of the last interval to ! /// be considered</param> ! /// <returns></returns> ! public float[] GetReturns( ! IReturnsManager returnsManager , int firstInterval , int lastInterval ) ! { ! float[] returns = new float[ ! returnsManager.ReturnIntervals.Count ]; ! ! ! // weights[] is set to avoid several double to float conversions ! float[] weights = new float[ this.Count ]; ! float[][] tickersReturns = new float[ this.Count ][]; ! for ( int positionIndex = 0 ; positionIndex < this.Count ; positionIndex++ ) ! { ! weights[ positionIndex ] = ! Convert.ToSingle( ((WeightedPosition)(this[ positionIndex ])).Weight ); ! tickersReturns[ positionIndex ] = returnsManager.GetReturns( ! ((WeightedPosition)(this[ positionIndex ])).Ticker ); ! } ! ! for ( int intervalIndex = firstInterval ; ! intervalIndex <= lastInterval ; intervalIndex++ ) ! { ! returns[ intervalIndex ] = 0; ! for ( int positionIndex = 0 ; positionIndex < this.Count ; ! positionIndex++ ) ! returns[ intervalIndex ] += ! tickersReturns[ positionIndex ][ intervalIndex ] * ! weights[ positionIndex ]; ! } ! return returns; ! } ! ! /// <summary> ! /// Gets the Open To Close return for the current instance of WeightedPositions ! /// </summary> ! /// <param name="marketDate">Market date for which return has to be computed</param> ! public double GetOpenToCloseReturn(DateTime marketDate) ! { ! Quotes[] tickersQuotes = new Quotes[this.Count]; ! for(int i = 0; i<this.Count; i++) tickersQuotes[i] = new Quotes( this[i].Ticker,marketDate,marketDate ); ! double openToCloseReturn = 0.0; ! for(int i = 0; i < this.Count ; i++) ! openToCloseReturn += ! ( (float)tickersQuotes[i].Rows[0]["quClose"] / ! (float)tickersQuotes[i].Rows[0]["quOpen"] - 1.0f ) * ! (float)this[i].Weight; ! return openToCloseReturn; ! } ! // private double getLastNightReturn( float[] weightedPositionsLastNightReturns ) // { // double returnValue = 0.0; *************** *** 645,715 **** // } ! ! /// <summary> /// Gets the last night return for the current instance /// </summary> /// <param name="lastMarketDay">The last market date before today</param> ! /// <param name="today">today</param> // public double GetLastNightReturn( DateTime lastMarketDay , DateTime today ) // { // float[] weightedPositionsLastNightReturns = new float[this.Count]; // for(int i = 0; i<this.Count; i++) ! // weightedPositionsLastNightReturns[i] = // this.getLastNightReturn_getLastNightReturnForTicker( // this[i].Ticker, lastMarketDay, today ); // return getLastNightReturn( weightedPositionsLastNightReturns ); // } ! private double getCloseToCloseReturn_setReturns_getReturn( int returnDayIndex, Quotes[] tickersQuotes ) ! { ! double returnValue = 0.0; ! for(int indexForTicker = 0; indexForTicker<this.Count; indexForTicker++) ! returnValue += ! ((float)tickersQuotes[indexForTicker].Rows[returnDayIndex][Quotes.AdjustedCloseToCloseRatio] - 1.0f)* ! (float)this[indexForTicker].Weight; ! return returnValue; ! } ! private void getCloseToCloseReturn_setReturns( double[] returnsToSet, ! Quotes[] tickersQuotes ) ! { ! for(int i = 0; i < returnsToSet.Length; i++) ! { ! returnsToSet[i] = ! getCloseToCloseReturn_setReturns_getReturn(i,tickersQuotes); ! } ! } ! /// <summary> ! /// Gets portfolio's return for a given period, for the current instance ! /// of weighted positions ! /// </summary> ! /// <param name="startDate">Start date for the period for which return has to be computed</param> ! /// <param name="endDate">End date for the period for which return has to be computed</param> ! public double GetCloseToCloseReturn(DateTime startDate,DateTime endDate ) ! { ! const double initialEquity = 1.0; ! double equityValue = initialEquity; ! Quotes[] tickersQuotes = new Quotes[this.Count]; ! int numberOfQuotesOfPreviousTicker = 0; ! for(int i = 0; i < this.Count; i++) ! { ! tickersQuotes[i] = new Quotes( this[i].Ticker,startDate, endDate ); ! if( i == 0 ) ! numberOfQuotesOfPreviousTicker = tickersQuotes[i].Rows.Count; ! else if ( (i > 0 && ( tickersQuotes[i].Rows.Count > numberOfQuotesOfPreviousTicker)) || ! tickersQuotes[i].Rows.Count == 0) ! // not all the tickers have the same available n. of quotes ! // for the given period or a ticker has no quotes ! throw new MissingQuotesException(this.SignedTickers.Tickers, ! startDate, endDate); ! } ! double[] returns = new double[tickersQuotes[0].Rows.Count]; ! getCloseToCloseReturn_setReturns(returns,tickersQuotes); ! for(int i = 0; i < returns.Length; i++) ! equityValue = ! equityValue + equityValue * returns[i]; ! return (equityValue - initialEquity)/initialEquity; ! } /// <summary> --- 724,794 ---- // } ! ! /// <summary> /// Gets the last night return for the current instance /// </summary> /// <param name="lastMarketDay">The last market date before today</param> ! /// <param name="today">today</param> // public double GetLastNightReturn( DateTime lastMarketDay , DateTime today ) // { // float[] weightedPositionsLastNightReturns = new float[this.Count]; // for(int i = 0; i<this.Count; i++) ! // weightedPositionsLastNightReturns[i] = // this.getLastNightReturn_getLastNightReturnForTicker( // this[i].Ticker, lastMarketDay, today ); // return getLastNightReturn( weightedPositionsLastNightReturns ); // } ! private double getCloseToCloseReturn_setReturns_getReturn( int returnDayIndex, Quotes[] tickersQuotes ) ! { ! double returnValue = 0.0; ! for(int indexForTicker = 0; indexForTicker<this.Count; indexForTicker++) ! returnValue += ! ((float)tickersQuotes[indexForTicker].Rows[returnDayIndex][Quotes.AdjustedCloseToCloseRatio] - 1.0f)* ! (float)this[indexForTicker].Weight; ! return returnValue; ! } ! private void getCloseToCloseReturn_setReturns( double[] returnsToSet, ! Quotes[] tickersQuotes ) ! { ! for(int i = 0; i < returnsToSet.Length; i++) ! { ! returnsToSet[i] = ! getCloseToCloseReturn_setReturns_getReturn(i,tickersQuotes); ! } ! } ! /// <summary> ! /// Gets portfolio's return for a given period, for the current instance ! /// of weighted positions ! /// </summary> ! /// <param name="startDate">Start date for the period for which return has to be computed</param> ! /// <param name="endDate">End date for the period for which return has to be computed</param> ! public double GetCloseToCloseReturn(DateTime startDate,DateTime endDate ) ! { ! const double initialEquity = 1.0; ! double equityValue = initialEquity; ! Quotes[] tickersQuotes = new Quotes[this.Count]; ! int numberOfQuotesOfPreviousTicker = 0; ! for(int i = 0; i < this.Count; i++) ! { ! tickersQuotes[i] = new Quotes( this[i].Ticker,startDate, endDate ); ! if( i == 0 ) ! numberOfQuotesOfPreviousTicker = tickersQuotes[i].Rows.Count; ! else if ( (i > 0 && ( tickersQuotes[i].Rows.Count > numberOfQuotesOfPreviousTicker)) || ! tickersQuotes[i].Rows.Count == 0) ! // not all the tickers have the same available n. of quotes ! // for the given period or a ticker has no quotes ! throw new MissingQuotesException(this.SignedTickers.Tickers, ! startDate, endDate); ! } ! double[] returns = new double[tickersQuotes[0].Rows.Count]; ! getCloseToCloseReturn_setReturns(returns,tickersQuotes); ! for(int i = 0; i < returns.Length; i++) ! equityValue = ! equityValue + equityValue * returns[i]; ! return (equityValue - initialEquity)/initialEquity; ! } /// <summary> *************** *** 717,723 **** /// long positions become then short positions and viceversa /// </summary> ! public void Reverse() { ! foreach(WeightedPosition weightedPosition in this.Values) weightedPosition.Weight = - weightedPosition.Weight; } --- 796,802 ---- /// long positions become then short positions and viceversa /// </summary> ! public void ReverseSign() { ! foreach( WeightedPosition weightedPosition in this ) weightedPosition.Weight = - weightedPosition.Weight; } *************** *** 730,734 **** } ! public bool HasTheSameSignedTickersAs(WeightedPositions weightedPositions) { //Check for null and compare run-time types and compare length of the weightedPositions --- 809,813 ---- } ! public bool HasTheSameSignedTickersAs(WeightedPositions weightedPositions) { //Check for null and compare run-time types and compare length of the weightedPositions *************** *** 744,753 **** for (int j = 0; j<this.Count; j++) if ( this[i].HasTheSameSignedTickerAs(weightedPositions[j]) ) ! numOfEquals++; ! return numOfEquals == this.Count; } ! public bool HasTheOppositeSignedTickersAs(WeightedPositions weightedPositions) { //Check for null and compare run-time types and compare length of the weightedPositions --- 823,832 ---- for (int j = 0; j<this.Count; j++) if ( this[i].HasTheSameSignedTickerAs(weightedPositions[j]) ) ! numOfEquals++; ! return numOfEquals == this.Count; } ! public bool HasTheOppositeSignedTickersAs(WeightedPositions weightedPositions) { //Check for null and compare run-time types and compare length of the weightedPositions *************** *** 764,769 **** for (int j = 0; j<this.Count; j++) if ( this[i].HasTheOppositeSignedTickerAs(weightedPositions[j]) ) ! numOfEqualsWithOppositeSign++; ! return numOfEqualsWithOppositeSign == this.Count; } --- 843,848 ---- for (int j = 0; j<this.Count; j++) if ( this[i].HasTheOppositeSignedTickerAs(weightedPositions[j]) ) ! numOfEqualsWithOppositeSign++; ! return numOfEqualsWithOppositeSign == this.Count; } |