[Quantproject-developers] QuantProject/b4_Business/a2_Strategies WeightedPositions.cs, 1.3, 1.4
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2007-08-21 22:06:07
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv17652/b4_Business/a2_Strategies Modified Files: WeightedPositions.cs Log Message: Added to the class: - new property "Tickers" (SignedTickers type); - new constructor with only one parameter (SignedTicker signedTicker); - instance methods GetLastNightReturn adn GetCloseToCloseReturn (they were static methods in SignedTicker class). Index: WeightedPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/WeightedPositions.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** WeightedPositions.cs 15 Aug 2007 19:24:26 -0000 1.3 --- WeightedPositions.cs 21 Aug 2007 22:05:34 -0000 1.4 *************** *** 30,33 **** --- 30,34 ---- using QuantProject.Business.Strategies.ReturnsManagement; using QuantProject.Business.Timing; + using QuantProject.Data; using QuantProject.Data.DataTables; *************** *** 79,83 **** } } ! private void weightedPositions_default( double[] normalizedWeightValues , --- 80,97 ---- } } ! ! public SignedTickers Tickers { ! get { ! string[] arrayOfTickersWithSign = new string[this.Count]; ! for(int i = 0; i < this.Count; i++) ! { ! if(this[i].IsShort) ! arrayOfTickersWithSign[i] = "-" + this[i].Ticker; ! else ! arrayOfTickersWithSign[i] = this[i].Ticker; ! } ! return new SignedTickers(arrayOfTickersWithSign); ! } ! } private void weightedPositions_default( double[] normalizedWeightValues , *************** *** 117,120 **** --- 131,153 ---- } + /// <summary> + /// It creates a new instance with all equal weights for + /// the given SignedTickers object + /// </summary> + public WeightedPositions( SignedTickers signedTickers ) + { + 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 ); + } + #region checkParameters private void checkParameters_checkDoubleTickers( string[] tickers ) *************** *** 561,564 **** --- 594,679 ---- return openToCloseReturn; } + + #region getLastNightReturn + private double getLastNightReturn( float[] weightedPositionsLastNightReturns ) + { + double returnValue = 0.0; + for(int i = 0; i<weightedPositionsLastNightReturns.Length; i++) + returnValue += weightedPositionsLastNightReturns[i]*((WeightedPosition)this[i]).Weight; + return returnValue; + } + private float getLastNightReturn_getLastNightReturnForTicker(string ticker, + DateTime lastMarketDay, DateTime today) + { + Quotes tickerQuotes = new Quotes(ticker, lastMarketDay, today); + return ( ( (float)tickerQuotes.Rows[1]["quOpen"] * + (float)tickerQuotes.Rows[1]["quAdjustedClose"] / + (float)tickerQuotes.Rows[1]["quClose"] ) / + (float)tickerQuotes.Rows[0]["quAdjustedClose"] - 1 ); + } + #endregion + + /// <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( + ((WeightedPosition)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]; + for(int i = 0; i < this.Count; i++) + { + tickersQuotes[i] = new Quotes( this[i].Ticker,startDate, endDate ); + if(tickersQuotes[i].Rows.Count == 0) + //no quotes are available at the given period + throw new MissingQuotesException(this[i].Ticker, + 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> /// Reverse the sign of each weight for each position in the current instance: |