[Quantproject-developers] QuantProject/b4_Business/a2_Strategies SignedTicker.cs, 1.8, 1.9
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2007-08-07 16:48:19
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv21337/b4_Business/a2_Strategies Modified Files: SignedTicker.cs Log Message: Dramatically changed: moved from a static string based approach to a OO oriented instance based scenario. Several static methods have been left for legacy support, but classes using those methods should be patched to use the OO based approach. Index: SignedTicker.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/SignedTicker.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** SignedTicker.cs 24 Jun 2007 21:29:00 -0000 1.8 --- SignedTicker.cs 7 Aug 2007 16:48:12 -0000 1.9 *************** *** 52,55 **** --- 52,100 ---- get { return this.positionType; } } + public double Multiplier + { + get + { + double multiplier = 1; + if ( this.IsShort ) + multiplier = -1; + return multiplier; + } + } + public bool IsShort + { + get + { + bool isShort = ( this.positionType == PositionType.Short ); + return isShort; + } + } + public bool IsLong + { + get + { + return !this.IsShort; + } + } + public SignedTicker OppositeSignedTicker + { + get + { + SignedTicker oppositeSignedTicker = + new SignedTicker( this.Ticker , + this.getOppositePositionType( this.PositionType ) ); + return oppositeSignedTicker; + } + } + private PositionType getOppositePositionType( PositionType positionType ) + { + PositionType oppositePositionType; + if ( this.IsLong ) + oppositePositionType = PositionType.Short; + else + // this.IsShort + oppositePositionType = PositionType.Long; + return oppositePositionType; + } public SignedTicker( string ticker , PositionType positionType ) { *************** *** 57,60 **** --- 102,158 ---- this.positionType = positionType; } + public SignedTicker( string signedTicker ) + { + this.signedTicker_checkParams( signedTicker ); + this.ticker = this.getTicker( signedTicker ); + this.positionType = this.getPositionType( signedTicker ); + } + private bool isValidSignedTicker( string signedTicker ) + { + bool isActuallyValidSignedTicker = false; + string firstCharacter = signedTicker.Substring( 0 , 1 ); + if ( ( firstCharacter.CompareTo( "A" ) != - 1 ) && + ( ( firstCharacter.CompareTo( "Z" ) != 1 ) ) ) + // the first signedTickers' character is an upcase letter + isActuallyValidSignedTicker = true; + if ( ( firstCharacter == "^" ) ) + isActuallyValidSignedTicker = true; + if ( ( firstCharacter == "-" ) ) + isActuallyValidSignedTicker = true; + return isActuallyValidSignedTicker; + } + private void signedTicker_checkParams( string signedTicker ) + { + if ( !this.isValidSignedTicker( signedTicker ) ) + throw new Exception( "signedTickers is not a valid string to " + + "identify and construct a new SignedTicker object!" ); + } + private bool isStringForLongSignedTicker( string signedTicker ) + { + return !(signedTicker.IndexOf( "-" ) == 0 ); + } + private string getTicker( string signedTicker ) + { + string ticker; + if ( this.isStringForLongSignedTicker( signedTicker ) ) + ticker = signedTicker; + else + // signedTicker is a string for a short SignedTicker and + // signedTicker begins with a '-' sign + ticker = signedTicker.Substring( 1 ); + return ticker; + } + private PositionType getPositionType( string signedTicker ) + { + PositionType positionType; + if ( this.isStringForLongSignedTicker( signedTicker ) ) + positionType = PositionType.Long; + else + // signedTicker is a string for a short SignedTicker and + // signedTicker begins with a '-' sign + positionType = PositionType.Short; + return positionType; + } + public static string GetSignedTicker( Position position ) { *************** *** 71,102 **** return ticker; } ! public static bool IsShort( string signedTicker ) ! { ! return ( signedTicker.StartsWith( "-" ) ); ! } ! public static bool IsLong( string signedTicker ) ! { ! return ( !SignedTicker.IsShort( signedTicker ) ); ! } ! public static string GetOppositeSignedTicker( string signedTicker ) ! { ! string oppositeSignedTicker = ""; ! if ( SignedTicker.IsShort( signedTicker ) ) ! oppositeSignedTicker = SignedTicker.GetTicker( signedTicker ); ! if ( SignedTicker.IsLong( signedTicker ) ) ! oppositeSignedTicker = "-" + SignedTicker.GetTicker( signedTicker ); ! return oppositeSignedTicker; ! } ! public static OrderType GetMarketOrderType( string signedTicker ) { ! OrderType orderType = OrderType.MarketBuy; ! if ( SignedTicker.IsShort( signedTicker ) ) ! orderType = OrderType.MarketSellShort; ! return orderType; } ! public static double GetCloseToCloseDailyReturn( ! string signedTicker , DateTime today ) { ! string ticker = SignedTicker.GetTicker( signedTicker ); HistoricalAdjustedQuoteProvider historicalAdjustedQuoteProvider = new HistoricalAdjustedQuoteProvider(); --- 169,193 ---- return ticker; } ! // public static bool IsShort( string signedTicker ) ! // { ! // return ( signedTicker.StartsWith( "-" ) ); ! // } ! // public static bool IsLong( string signedTicker ) ! // { ! // return ( !SignedTicker.IsShort( signedTicker ) ); ! // } ! public OrderType MarketOrderType { ! get ! { ! OrderType orderType = OrderType.MarketBuy; ! if ( this.IsShort ) ! orderType = OrderType.MarketSellShort; ! return orderType; ! } } ! public double GetCloseToCloseDailyReturn( DateTime today ) { ! string ticker = this.Ticker; HistoricalAdjustedQuoteProvider historicalAdjustedQuoteProvider = new HistoricalAdjustedQuoteProvider(); *************** *** 113,117 **** ( todayMarketValueAtClose / yesterdayMarketValueAtClose ) - 1; double dailyReturn; ! if ( SignedTicker.IsShort( signedTicker ) ) // signedTicker represents a short position dailyReturn = - dalyReturnForLongPosition; --- 204,208 ---- ( todayMarketValueAtClose / yesterdayMarketValueAtClose ) - 1; double dailyReturn; ! if ( this.IsShort ) // signedTicker represents a short position dailyReturn = - dalyReturnForLongPosition; *************** *** 155,160 **** #region getCloseToClosePortfolioReturn_setReturns ! private static double getCloseToClosePortfolioReturn_setReturns_getReturn(int returnDay, ! string[] signedTickers,double[] tickersWeights,Quotes[] tickersQuotes ) { double returnValue = 0.0; --- 246,252 ---- #region getCloseToClosePortfolioReturn_setReturns ! private static double getCloseToClosePortfolioReturn_setReturns_getReturn( ! int returnDay, string[] signedTickers , ! double[] tickersWeights, Quotes[] tickersQuotes ) { double returnValue = 0.0; *************** *** 162,166 **** for(int indexForTicker = 0; indexForTicker<signedTickers.Length; indexForTicker++) { ! if(SignedTicker.IsLong(signedTickers[indexForTicker])) signOfTicker = 1.0f; else --- 254,260 ---- for(int indexForTicker = 0; indexForTicker<signedTickers.Length; indexForTicker++) { ! SignedTicker signedTicker = ! new SignedTicker( signedTickers[indexForTicker] ); ! if( signedTicker.IsLong ) signOfTicker = 1.0f; else *************** *** 250,276 **** /// <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 , ! SortedList commonMarketDays ) ! { ! const double initialEquity = 1.0; ! double equityValue = initialEquity; ! Quotes[] tickersQuotes = new Quotes[ signedTickers.Length ]; ! for( int i = 0 ; i < signedTickers.Length ; i++ ) ! { ! tickersQuotes[ i ] = ! new Quotes( SignedTicker.GetTicker( (string)signedTickers[ i ] ) , ! commonMarketDays ); ! } ! double[] returns = new double[ tickersQuotes[0].Rows.Count ]; ! getCloseToClosePortfolioReturn_setReturns( returns , signedTickers , ! tickersWeights , tickersQuotes); ! for( int i = 0 ; i < returns.Length ; i++ ) ! equityValue = ! equityValue + equityValue * returns[i]; ! ! return (equityValue - initialEquity)/initialEquity; ! } ! /// <summary> /// Gets portfolio's return, for the given tickers, considering only --- 344,370 ---- /// <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 , ! // SortedList commonMarketDays ) ! // { ! // const double initialEquity = 1.0; ! // double equityValue = initialEquity; ! // Quotes[] tickersQuotes = new Quotes[ signedTickers.Length ]; ! // for( int i = 0 ; i < signedTickers.Length ; i++ ) ! // { ! // tickersQuotes[ i ] = ! // new Quotes( SignedTicker.GetTicker( (string)signedTickers[ i ] ) , ! // commonMarketDays ); ! // } ! // double[] returns = new double[ tickersQuotes[0].Rows.Count ]; ! // getCloseToClosePortfolioReturn_setReturns( returns , signedTickers , ! // tickersWeights , tickersQuotes); ! // for( int i = 0 ; i < returns.Length ; i++ ) ! // equityValue = ! // equityValue + equityValue * returns[i]; ! // ! // return (equityValue - initialEquity)/initialEquity; ! // } ! // /// <summary> /// Gets portfolio's return, for the given tickers, considering only *************** *** 280,295 **** /// <param name="commonMarketDays"></param> /// <returns></returns> ! public static double GetCloseToClosePortfolioReturn( ! string[] signedTickers, ! SortedList commonMarketDays ) ! { ! double[] tickersWeights = new double[signedTickers.Length]; ! for(int i = 0; i<signedTickers.Length; i++) ! tickersWeights[i] = 1.0/signedTickers.Length; ! ! return GetCloseToClosePortfolioReturn( ! signedTickers , tickersWeights , commonMarketDays ); ! } ! private static double getLastNightPortfolioReturn(float[] tickersLastNightReturns, double[] tickersWeights) --- 374,389 ---- /// <param name="commonMarketDays"></param> /// <returns></returns> ! // public static double GetCloseToClosePortfolioReturn( ! // string[] signedTickers, ! // SortedList commonMarketDays ) ! // { ! // double[] tickersWeights = new double[signedTickers.Length]; ! // for(int i = 0; i<signedTickers.Length; i++) ! // tickersWeights[i] = 1.0/signedTickers.Length; ! // ! // return GetCloseToClosePortfolioReturn( ! // signedTickers , tickersWeights , commonMarketDays ); ! // } ! // private static double getLastNightPortfolioReturn(float[] tickersLastNightReturns, double[] tickersWeights) *************** *** 373,406 **** return closeToCloseReturn; } ! private static double getMultiplier( string signedTicker ) ! { ! double multiplier = 1.0; ! if ( IsShort( signedTicker ) ) ! multiplier = -multiplier; ! return multiplier; ! } ! private static double getCloseToClosePortfolioReturn( ! string[] signedTickers , SortedList datesForReturnComputation , int i ) ! { ! double dailyReturn = 0.0; ! foreach ( String signedTicker in signedTickers ) ! dailyReturn += getMultiplier( signedTicker ) * ! getCloseToCloseReturn( GetTicker( signedTicker ) , ! datesForReturnComputation , i ) / ! signedTickers.Length; // every position is considered with same weight ! return dailyReturn; ! } ! private static double[] getCloseToClosePortfolioReturns( ! string[] signedTickers , SortedList datesForReturnComputation ) ! { ! // the return for first DateTime cannot be computed so the returned ! // array will have one element less the datesForReturnComputation ! double[] closeToClosePortfolioReturns = ! new double[ datesForReturnComputation.Count - 1 ]; ! for ( int i=0 ; i < closeToClosePortfolioReturns.Length ; i++ ) ! closeToClosePortfolioReturns[ i ] = getCloseToClosePortfolioReturn( ! signedTickers , datesForReturnComputation , i ); ! return closeToClosePortfolioReturns; ! } private static double[] getCloseToClosePortfolioReturns( ICollection signedTickers, SortedList datesForReturnComputation ) --- 467,500 ---- return closeToCloseReturn; } ! // private static double getMultiplier( string signedTicker ) ! // { ! // double multiplier; ! // if ( IsShort( signedTicker ) ) ! // multiplier = -multiplier; ! // return multiplier; ! // } ! // private static double getCloseToClosePortfolioReturn( ! // string[] signedTickers , SortedList datesForReturnComputation , int i ) ! // { ! // double dailyReturn = 0.0; ! // foreach ( String signedTicker in signedTickers ) ! // dailyReturn += getMultiplier( signedTicker ) * ! // getCloseToCloseReturn( GetTicker( signedTicker ) , ! // datesForReturnComputation , i ) / ! // signedTickers.Length; // every position is considered with same weight ! // return dailyReturn; ! // } ! // private static double[] getCloseToClosePortfolioReturns( ! // string[] signedTickers , SortedList datesForReturnComputation ) ! // { ! // // the return for first DateTime cannot be computed so the returned ! // // array will have one element less the datesForReturnComputation ! // double[] closeToClosePortfolioReturns = ! // new double[ datesForReturnComputation.Count - 1 ]; ! // for ( int i=0 ; i < closeToClosePortfolioReturns.Length ; i++ ) ! // closeToClosePortfolioReturns[ i ] = getCloseToClosePortfolioReturn( ! // signedTickers , datesForReturnComputation , i ); ! // return closeToClosePortfolioReturns; ! // } private static double[] getCloseToClosePortfolioReturns( ICollection signedTickers, SortedList datesForReturnComputation ) *************** *** 452,455 **** --- 546,555 ---- } } + public static string GetOppositeSignedTicker( string stringForSignedTicker ) + { + SignedTicker signedTicker = new SignedTicker( stringForSignedTicker ); + SignedTicker oppositeSignedTicker = signedTicker.OppositeSignedTicker; + return oppositeSignedTicker.Ticker; + } } } |