[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag WeightedPositi
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2007-06-24 21:31:17
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv27069/b7_Scripts/WalkForwardTesting/WalkForwardLag Modified Files: WeightedPositions.cs Log Message: The public static method GetBalancedWeights has been added: it returns weights balanced with respect to the close to close volatility, in the given period (the most volatile ticker is given the lighter weight) Index: WeightedPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WeightedPositions.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** WeightedPositions.cs 8 Oct 2006 16:13:54 -0000 1.3 --- WeightedPositions.cs 24 Jun 2007 21:31:05 -0000 1.4 *************** *** 24,31 **** --- 24,33 ---- using System.Collections; + using QuantProject.ADT.Statistics; using QuantProject.Business.DataProviders; using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Strategies; using QuantProject.Business.Timing; + using QuantProject.Data.DataTables; namespace QuantProject.Scripts.WalkForwardTesting.WalkForwardLag *************** *** 312,315 **** --- 314,389 ---- } #endregion + #region GetBalancedWeights + private static float[][] getTickerCloseToCloseReturnsForSignedTickers( + string[] signedTickers , DateTime firstDate , DateTime lastDate ) + { + float[][] tickerReturns = new float[ signedTickers.Length ][]; + for ( int i = 0 ; i < signedTickers.Length ; i++ ) + { + 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 = + new double[ standardDeviations.Length ]; + for ( int i = 0 ; i < standardDeviations.Length ; i++ ) + nonNormalizedWeightsButBalancedForVolatility[ i ] = + getNonNormalizedWeightsButBalancedForVolatility( + standardDeviations , maxStandardDeviation , i ); + return nonNormalizedWeightsButBalancedForVolatility; + } + private static double[] getNonNormalizedWeightsButBalancedForVolatility( + double[] standardDeviations ) + { + double maxStandardDeviation = BasicFunctions.GetMax( standardDeviations ); + return getNonNormalizedWeightsButBalancedForVolatility( + standardDeviations , maxStandardDeviation ); + } + /// <summary> + /// Returns weights balanced with respect to the close to close volatility, + /// in the given period + /// (the most volatile ticker is given the lighter weight) + /// </summary> + /// <param name="signedTickers"></param> + /// <returns></returns> + public static double[] GetBalancedWeights( + string[] signedTickers , DateTime firstDate , DateTime lastDate ) + { + double[] standardDeviations = getStandardDeviations( signedTickers , firstDate , lastDate ); + double[] nonNormalizedButBalancedWeights = + getNonNormalizedWeightsButBalancedForVolatility( standardDeviations ); + double[] normalizedBalancedWeights = + GetNormalizedWeights( nonNormalizedButBalancedWeights ); + return normalizedBalancedWeights; + } + #endregion //GetBalancedWeights } } |