[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag WeightedPositi
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2007-07-13 10:10:56
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv26268/b7_Scripts/WalkForwardTesting/WalkForwardLag Modified Files: WeightedPositions.cs Log Message: public float[] GetReturns( ReturnsManager returnsManager ) has been added public float GetReturn( int i , ReturnsManager returnsManager ) has been added Index: WeightedPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WeightedPositions.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** WeightedPositions.cs 24 Jun 2007 21:31:05 -0000 1.4 --- WeightedPositions.cs 13 Jul 2007 10:10:45 -0000 1.5 *************** *** 28,31 **** --- 28,32 ---- using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Strategies; + using QuantProject.Business.Strategies.ReturnsManagement; using QuantProject.Business.Timing; using QuantProject.Data.DataTables; *************** *** 204,229 **** #endregion #region GetCloseToClosePortfolioReturns - // private static double getCloseToCloseReturn( string ticker , - // SortedList datesForReturnComputation , int i ) - // { - // DateTime previousDate = (DateTime)datesForReturnComputation.GetByIndex( i ); - // DateTime currentDate = - // (DateTime)datesForReturnComputation.GetByIndex( i + 1 ); - // HistoricalAdjustedQuoteProvider historicalQuoteProvider = - // new HistoricalAdjustedQuoteProvider(); - // double previousQuote = historicalQuoteProvider.GetMarketValue( ticker , - // new EndOfDayDateTime( previousDate , EndOfDaySpecificTime.MarketClose ) ); - // double currentQuote = historicalQuoteProvider.GetMarketValue( ticker , - // new EndOfDayDateTime( currentDate , EndOfDaySpecificTime.MarketClose ) ); - // double closeToCloseReturn = currentQuote / previousQuote - 1.0; - // return closeToCloseReturn; - // } - // private static double getMultiplier( string signedTicker ) - // { - // double multiplier = 1.0; - // if ( IsShort( signedTicker ) ) - // multiplier = -multiplier; - // return multiplier; - // } private double getCloseToClosePortfolioReturn( SortedList datesForReturnComputation , int i ) --- 205,208 ---- *************** *** 323,355 **** string ticker = SignedTicker.GetTicker( signedTickers[ i ] ); tickerReturns[ i ] = Quotes.GetArrayOfCloseToCloseRatios( ! ticker , ref firstDate , lastDate , 1 ); } return tickerReturns; } ! private static double getStandardDeviation( float[] returnsForTicker ) ! { ! return BasicFunctions.GetStdDev( returnsForTicker ); ! } ! private static double[] getStandardDeviations( float[][] returnsForTickers ) { ! double[] standardDeviations = new double[ returnsForTickers.Length ]; ! for ( int i = 0 ; i < standardDeviations.Length ; i++ ) ! standardDeviations[ i ] = getStandardDeviation( returnsForTickers[ i ] ); ! return standardDeviations; } ! private static double[] getStandardDeviations( string[] signedTickers , ! DateTime firstDate , DateTime lastDate ) { ! float[][] returnsForTickers = getTickerCloseToCloseReturnsForSignedTickers( ! signedTickers , firstDate , lastDate ); ! return getStandardDeviations( returnsForTickers ); } private static double getNonNormalizedWeightsButBalancedForVolatility( ! double[] standardDeviations , double maxStandardDeviation , int i ) { return maxStandardDeviation / standardDeviations[ i ]; } private static double[] getNonNormalizedWeightsButBalancedForVolatility( ! double[] standardDeviations , double maxStandardDeviation ) { double[] nonNormalizedWeightsButBalancedForVolatility = --- 302,333 ---- string ticker = SignedTicker.GetTicker( signedTickers[ i ] ); tickerReturns[ i ] = Quotes.GetArrayOfCloseToCloseRatios( ! ticker , firstDate , lastDate ); } return tickerReturns; } ! private static float getTickerReturnsStandardDeviations( int tickerIndex , ! string[] signedTickers , ReturnsManager returnsManager ) { ! string ticker = SignedTicker.GetTicker( signedTickers[ tickerIndex ] ); ! return returnsManager.GetReturnsStandardDeviation( ticker ); } ! private static float[] getTickersReturnsStandardDeviations( ! string[] signedTickers , ReturnsManager returnsManager ) { ! float[] tickersReturnsStandardDeviations = ! new float[ signedTickers.Length ]; ! for ( int i = 0 ; i < signedTickers.Length ; i++ ) ! tickersReturnsStandardDeviations[ i ] = ! getTickerReturnsStandardDeviations( i , ! signedTickers , returnsManager ); ! return tickersReturnsStandardDeviations; } private static double getNonNormalizedWeightsButBalancedForVolatility( ! float[] standardDeviations , float maxStandardDeviation , int i ) { return maxStandardDeviation / standardDeviations[ i ]; } private static double[] getNonNormalizedWeightsButBalancedForVolatility( ! float[] standardDeviations , float maxStandardDeviation ) { double[] nonNormalizedWeightsButBalancedForVolatility = *************** *** 362,368 **** } private static double[] getNonNormalizedWeightsButBalancedForVolatility( ! double[] standardDeviations ) { ! double maxStandardDeviation = BasicFunctions.GetMax( standardDeviations ); return getNonNormalizedWeightsButBalancedForVolatility( standardDeviations , maxStandardDeviation ); --- 340,347 ---- } private static double[] getNonNormalizedWeightsButBalancedForVolatility( ! float[] standardDeviations ) { ! float maxStandardDeviation = ! (float)BasicFunctions.GetMax( standardDeviations ); return getNonNormalizedWeightsButBalancedForVolatility( standardDeviations , maxStandardDeviation ); *************** *** 376,382 **** /// <returns></returns> public static double[] GetBalancedWeights( ! string[] signedTickers , DateTime firstDate , DateTime lastDate ) { ! double[] standardDeviations = getStandardDeviations( signedTickers , firstDate , lastDate ); double[] nonNormalizedButBalancedWeights = getNonNormalizedWeightsButBalancedForVolatility( standardDeviations ); --- 355,363 ---- /// <returns></returns> public static double[] GetBalancedWeights( ! string[] signedTickers , ReturnsManager returnManager ) { ! float[] standardDeviations = ! getTickersReturnsStandardDeviations( signedTickers , ! returnManager ); double[] nonNormalizedButBalancedWeights = getNonNormalizedWeightsButBalancedForVolatility( standardDeviations ); *************** *** 386,389 **** --- 367,447 ---- } #endregion //GetBalancedWeights + #region GetReturn + private void getReturnCheckParameters( int i , + ReturnsManager returnsManager ) + { + if ( ( i < 0 ) || ( i > returnsManager.TimeLine.Count - 2 ) ) + throw new Exception( "i is larger than the number of returns" ); + } + 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 ); + + + // int numberOfWeightedPositions = weightedPositions.Count; + // string[] tickers = this.getTickers( weightedPositions ); + // float[] multipliers = this.getMultipliers( weightedPositions ); + // // arrays of close to close returns, one for each signed ticker + // float[][] tickersReturns = + // this.wFLagCandidates.GetTickersReturns( tickers ); + // Aggiungi!!! weightedPositions.GetReturns( this.closeToCloseReturnsManager ) + // double[] linearCombinationReturns = + // new double[ tickersReturns[ 0 ].Length ]; + // for( int i = 0; i < linearCombinationReturns.Length ; i++ ) + // // computes linearCombinationReturns[ i ] + // { + // linearCombinationReturns[ i ] = 0; + // for ( int j=0 ; j < weightedPositions.Count ; j++ ) + // { + // double weightedPositionReturn = + // tickersReturns[ j ][ i ] * multipliers[ j ]; + // linearCombinationReturns[ i ] += weightedPositionReturn; + // } + // } + // return linearCombinationReturns; + } + private float getReturnActually( int i , ReturnsManager returnsManager ) + { + float linearCombinationReturn = 0; + foreach ( WeightedPosition weightedPosition in this.Values ) + linearCombinationReturn += this.getReturnActually( weightedPosition , + i , returnsManager ); + return linearCombinationReturn; + } + /// <summary> + /// returns the i_th return of the sum of the weighted positions + /// </summary> + /// <param name="i"></param> + /// <param name="returnsManager">used to efficiently store + /// ticker returns</param> + /// <returns></returns> + public float GetReturn( int i , ReturnsManager returnsManager ) + { + this.getReturnCheckParameters( i , returnsManager ); + return getReturnActually( i , returnsManager ); + } + #endregion GetReturn + /// <summary> + /// Computes an array of floats representing the returns + /// of all weighted position + /// </summary> + /// <param name="returnsManager"></param> + /// <returns></returns> + public float[] GetReturns( ReturnsManager returnsManager ) + { + float[] returns = new float[ + returnsManager.TimeLine.Count - 1 ]; + for ( int i = 0 ; i < returnsManager.TimeLine.Count ; i++ ) + returns[ i ] = this.GetReturn( i , returnsManager ); + return returns; + } } } |