[Quantproject-developers] QuantProject/b4_Business/a2_Strategies SignedTicker.cs,1.1,1.2
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2006-05-14 18:40:27
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv6069/b4_Business/a2_Strategies Modified Files: SignedTicker.cs Log Message: Added GetCloseToClosePortfolioReturn and ChangeSignOfEachTicker static methods Index: SignedTicker.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/SignedTicker.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SignedTicker.cs 8 Apr 2006 18:18:47 -0000 1.1 --- SignedTicker.cs 14 May 2006 18:40:24 -0000 1.2 *************** *** 27,30 **** --- 27,33 ---- using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Timing; + using QuantProject.Data; + using QuantProject.Data.DataTables; + namespace QuantProject.Business.Strategies *************** *** 117,120 **** --- 120,221 ---- return dailyReturn; } + + #region getCloseToClosePortfolioReturn_setReturns + + private static double getCloseToClosePortfolioReturn_setReturns_getReturn(int returnDay, + string[] signedTickers,double[] tickersWeights,Quotes[] tickersQuotes ) + { + double returnValue = 0.0; + float signOfTicker = 1.0f; + for(int indexForTicker = 0; indexForTicker<signedTickers.Length; indexForTicker++) + { + if(SignedTicker.IsLong(signedTickers[indexForTicker])) + signOfTicker = 1.0f; + else + signOfTicker = -1.0f; + + returnValue += + signOfTicker * + ((float)tickersQuotes[indexForTicker].Rows[returnDay][Quotes.AdjustedCloseToCloseRatio] - 1.0f)* + (float)tickersWeights[indexForTicker]; + } + return returnValue; + } + + private static void getCloseToClosePortfolioReturn_setReturns(double[] returnsToSet, + string[] signedTickers, + double[] tickersWeights, + Quotes[] tickersQuotes ) + { + for(int i = 0; i < returnsToSet.Length; i++) + { + returnsToSet[i] = + getCloseToClosePortfolioReturn_setReturns_getReturn( + i,signedTickers,tickersWeights,tickersQuotes); + } + } + + #endregion + + /// <summary> + /// Gets portfolio's return for a given period, for given tickers + /// </summary> + /// <param name="signedTickers">Array of signed tickers that compose the portfolio</param> + /// <param name="tickersWeights">Array of weights for tickers - the same order has to be provided!</param> + /// <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 static double GetCloseToClosePortfolioReturn(string[] signedTickers, + double[] tickersWeights, + DateTime startDate, + DateTime endDate ) + { + double returnValue = 0.0; + Quotes[] tickersQuotes = new Quotes[signedTickers.Length]; + for(int i = 0; i<signedTickers.Length; i++) + { + tickersQuotes[i] = new Quotes(SignedTicker.GetTicker(signedTickers[i]), + startDate, endDate); + if(tickersQuotes[i].Rows.Count == 0) + //no quotes are available at the given period + throw new MissingQuotesException(SignedTicker.GetTicker(signedTickers[i]), + startDate, endDate); + } + double[] returns = new double[tickersQuotes[0].Rows.Count]; + getCloseToClosePortfolioReturn_setReturns(returns,signedTickers, + tickersWeights, tickersQuotes); + for(int i = 0; i < returns.Length; i++) + returnValue = (1.0+returnValue) * returns[i]; + + return returnValue; + } + + /// <summary> + /// Gets portfolio's return for a given period, for given tickers + /// </summary> + /// <param name="signedTickers">Array of signed tickers that compose the portfolio (each ticker has the same weight)</param> + /// <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 static double GetCloseToClosePortfolioReturn(string[] signedTickers, + DateTime startDate, + DateTime endDate ) + { + double[] tickersWeights = new double[signedTickers.Length]; + for(int i = 0; i<signedTickers.Length; i++) + tickersWeights[i] = 1.0/signedTickers.Length; + + return GetCloseToClosePortfolioReturn( + signedTickers,tickersWeights, startDate, endDate); + + } + + /// <summary> + /// Changes sign of each ticker contained in the given + /// array of signed tickers + /// </summary> + public static void ChangeSignOfEachTicker( string[] signedTickers ) + { + for(int i = 0; i<signedTickers.Length; i++) + signedTickers[i] = GetOppositeSignedTicker(signedTickers[i]); + } } } |