[Quantproject-developers] QuantProject/b4_Business/a2_Strategies WeightedPositions.cs, 1.12, 1.13
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2008-01-14 23:27:29
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv16409/b4_Business/a2_Strategies Modified Files: WeightedPositions.cs Log Message: Added methods: - HasTheSameSignedTickersAs; - HasTheOppositeSignedTickersAs Index: WeightedPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/WeightedPositions.cs,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** WeightedPositions.cs 4 Nov 2007 18:59:19 -0000 1.12 --- WeightedPositions.cs 14 Jan 2008 23:27:26 -0000 1.13 *************** *** 605,626 **** } ! #region getLastNightReturn ! private double getLastNightReturn( float[] weightedPositionsLastNightReturns ) ! { ! double returnValue = 0.0; ! for(int i = 0; i<weightedPositionsLastNightReturns.Length; i++) ! returnValue += weightedPositionsLastNightReturns[i] * 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> --- 605,625 ---- } ! // private double getLastNightReturn( float[] weightedPositionsLastNightReturns ) ! // { ! // double returnValue = 0.0; ! // for(int i = 0; i<weightedPositionsLastNightReturns.Length; i++) ! // returnValue += weightedPositionsLastNightReturns[i] * 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 ); ! // } ! /// <summary> *************** *** 629,641 **** /// <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( ! this[i].Ticker, lastMarketDay, today ); ! return getLastNightReturn( weightedPositionsLastNightReturns ); ! } private double getCloseToCloseReturn_setReturns_getReturn( --- 628,640 ---- /// <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( ! // this[i].Ticker, lastMarketDay, today ); ! // return getLastNightReturn( weightedPositionsLastNightReturns ); ! // } private double getCloseToCloseReturn_setReturns_getReturn( *************** *** 707,710 **** --- 706,749 ---- return toString; } + + public bool HasTheSameSignedTickersAs(WeightedPositions weightedPositions) + { + //Check for null and compare run-time types and compare length of the weightedPositions + if (weightedPositions.Count != this.Count) + return false; + int numOfEquals = 0; + //WeightedPositions can't contain the same ticker twice: + //so, if at the end of the nested cycle + //numOfEquals is equal to the number of + //positions, the two instances represent + //portfolios that contain the same signed tickers + for (int i = 0; i<this.Count; i++) + for (int j = 0; j<this.Count; j++) + if ( this[i].HasTheSameSignedTickerAs(weightedPositions[j]) ) + numOfEquals++; + + return numOfEquals == this.Count; + } + + public bool HasTheOppositeSignedTickersAs(WeightedPositions weightedPositions) + { + //Check for null and compare run-time types and compare length of the weightedPositions + if (weightedPositions.Count != this.Count) + return false; + int numOfEqualsWithOppositeSign = 0; + //WeightedPositions can't contain the same ticker twice: + //so, if at the end of the nested cycle + //numOfEqualsWithOppositeSign is equal to the number of + //positions, the two instances represent + //portfolios that contain the same signed tickers with + //opposite signs + for (int i = 0; i<this.Count; i++) + for (int j = 0; j<this.Count; j++) + if ( this[i].HasTheOppositeSignedTickerAs(weightedPositions[j]) ) + numOfEqualsWithOppositeSign++; + + return numOfEqualsWithOppositeSign == this.Count; + } + } } |