[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2006-07-30 13:50:29
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv13432/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger Modified Files: WFLagChosenPositions.cs Log Message: - WeightedPositions are used instead of SignedTickers - now the ISerializable interface is implemented, so that old logs can be examined yet - it may be worth looking at the deserializing code (not that simple...), as an example for future custom deserialization (if and when needed) Index: WFLagChosenPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagDebugger/WFLagChosenPositions.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** WFLagChosenPositions.cs 8 Apr 2006 18:12:40 -0000 1.1 --- WFLagChosenPositions.cs 30 Jul 2006 13:50:26 -0000 1.2 *************** *** 22,27 **** --- 22,31 ---- using System; + using System.Collections; + using System.Reflection; + using System.Runtime.Serialization; using QuantProject.ADT.Collections; + using QuantProject.Business.Strategies; using QuantProject.Scripts.WalkForwardTesting.WalkForwardLag; *************** *** 32,53 **** /// </summary> [Serializable] ! public class WFLagChosenPositions { private QPHashtable drivingPositions; private QPHashtable portfolioPositions; private DateTime lastOptimizationDate; ! public QPHashtable DrivingPositions { get { ! return this.drivingPositions; } } ! public QPHashtable PortfolioPositions { get { ! return this.portfolioPositions; } } --- 36,67 ---- /// </summary> [Serializable] ! public class WFLagChosenPositions : ISerializable { + // these private members are used for old versions' deserialization, only private QPHashtable drivingPositions; private QPHashtable portfolioPositions; + + private WeightedPositions drivingWeightedPositions; + private WeightedPositions portfolioWeightedPositions; private DateTime lastOptimizationDate; ! public WeightedPositions DrivingWeightedPositions { get { ! if ( this.drivingWeightedPositions == null ) ! // an old version has been deserialized ! this.setWeightedPositionsFromQPHashtables(); ! return this.drivingWeightedPositions; } } ! public WeightedPositions PortfolioWeightedPositions { get { ! if ( this.portfolioWeightedPositions == null ) ! // an old version has been deserialized ! this.setWeightedPositionsFromQPHashtables(); ! return this.portfolioWeightedPositions; } } *************** *** 63,80 **** DateTime lastOptimizationDate ) { ! this.drivingPositions = ! this.copy( wFLagChosenTickers.DrivingPositions ); ! this.portfolioPositions = ! this.copy( wFLagChosenTickers.PortfolioPositions ); this.lastOptimizationDate = lastOptimizationDate; } ! private QPHashtable copy( QPHashtable qPHashTable ) { ! QPHashtable newCopy = new QPHashtable(); ! foreach ( string key in qPHashTable.Keys ) ! newCopy.Add( key , null ); ! return newCopy; } } } --- 77,232 ---- DateTime lastOptimizationDate ) { ! // this.drivingPositions = ! // this.copy( wFLagChosenTickers.DrivingWeightedPositions ); ! // this.portfolioPositions = ! // this.copy( wFLagChosenTickers.PortfolioWeightedPositions ); ! this.drivingWeightedPositions = wFLagChosenTickers.DrivingWeightedPositions; ! this.portfolioWeightedPositions = wFLagChosenTickers.PortfolioWeightedPositions; this.lastOptimizationDate = lastOptimizationDate; } ! #region deserialization related constructor and methods ! protected WFLagChosenPositions( SerializationInfo info , StreamingContext context ) { ! this.deserializeBaseClassMembers( info , context ); ! this.deserializeThisClassMembers( info , context ); ! } ! private void deserializeBaseClassMembers( SerializationInfo info , StreamingContext context ) ! { ! // get the set of serializable members for this class and its base classes ! Type thisType = this.GetType(); ! MemberInfo[] memberInfos = FormatterServices.GetSerializableMembers( ! thisType , context); ! ! // deserialize the fields from the info object, only if of the base clas ! for (Int32 i = 0 ; i < memberInfos.Length; i++) ! { ! // Don't deserialize fields for this class ! if (memberInfos[i].DeclaringType != thisType) ! { ! FieldInfo fieldInfo = (FieldInfo) memberInfos[i]; ! ! // set the field to the deserialized value ! fieldInfo.SetValue( this , ! info.GetValue( fieldInfo.Name, fieldInfo.FieldType ) ); ! } ! } ! } ! private void deserializeDrivingWeightedPositions( ! SerializationInfo info , StreamingContext context ) ! { ! try ! { ! // this.drivingWeightedPositions = new WeightedPositions() ! // System.Type type = System.Type.GetType( ! // "QuantProject.Scripts.WalkForwardTesting.WalkForwardLag.WeightedPositions" ); ! this.drivingWeightedPositions = (WeightedPositions)info.GetValue( ! "drivingWeightedPositions" , WeightedPositions.Type ); ! } ! catch( Exception ex1 ) ! { ! // the serialized WFLagChosenPositions is of old type ! // drivingPositions and portfolioPositions are QPHashtable ! try ! { ! string errorMessage1 = ex1.Message; ! this.drivingPositions = new QPHashtable(); ! this.drivingPositions = (QPHashtable)info.GetValue( ! "drivingPositions" , this.drivingPositions.GetType() ); ! // drivingWeightedPositions = this.getWeightedPositions( drivingPositions ); ! } ! catch( Exception ex2 ) ! { ! string errorMessage = ex2.Message; ! errorMessage = errorMessage; ! } ! } ! } ! private void deserializePortfolioWeightedPositions( ! SerializationInfo info , StreamingContext context ) ! { ! try ! { ! // this.portfolioWeightedPositions = new WeightedPositions(); ! // System.Type type = System.Type.GetType( ! // "QuantProject.Scripts.WalkForwardTesting.WalkForwardLag.WeightedPositions" ); ! this.portfolioWeightedPositions = (WeightedPositions)info.GetValue( ! "portfolioWeightedPositions" , WeightedPositions.Type ); ! } ! catch ! { ! // the serialized WFLagChosenPositions is of old type ! // drivingPositions and portfolioPositions are QPHashtable ! this.portfolioPositions = new QPHashtable(); ! this.portfolioPositions = (QPHashtable)info.GetValue( ! "portfolioPositions" , this.portfolioPositions.GetType() ); ! // portfolioWeightedPositions = this.getWeightedPositions( portfolioPositions ); ! } ! } ! private void deserializeThisClassMembers( SerializationInfo info , StreamingContext context ) ! { ! this.lastOptimizationDate = (DateTime)info.GetValue( "lastOptimizationDate" , ! this.lastOptimizationDate.GetType() ); ! this.deserializeDrivingWeightedPositions( info , context ); ! this.deserializePortfolioWeightedPositions( info , context ); ! } ! void ISerializable.GetObjectData( ! SerializationInfo info, StreamingContext context) ! { ! // get the set of serializable members for this class and base classes ! Type thisType = this.GetType(); ! MemberInfo[] mi = ! FormatterServices.GetSerializableMembers( thisType , context); ! ! // serialize the fields to the info object ! for (Int32 i = 0 ; i < mi.Length; i++) ! { ! string memberName = mi[i].Name; ! if ( ( memberName != "drivingPositions" ) && ( memberName != "portfolioPositions" ) ) ! // current member is not used for old versions' deserialization, only ! info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this)); ! } } + // private Hashtable copy( Hashtable hashTable ) + // { + // Hashtable newCopy = new Hashtable(); + // foreach ( string key in hashTable.Keys ) + // newCopy.Add( key , null ); + // return newCopy; + // } + #region setWeightedPositionsFromQPHashtables + private double getWeightedPositions_getWeight( + double absoluteWeightForEachPosition , string signedTicker ) + { + double weight = absoluteWeightForEachPosition; + if ( SignedTicker.IsShort( signedTicker ) ) + weight = - absoluteWeightForEachPosition; + return weight; + } + private WeightedPositions getWeightedPositions( QPHashtable signedTickers ) + { + double absoluteWeightForEachPosition = + 1 / Convert.ToDouble( signedTickers.Count ); + double[] weights = new double[ signedTickers.Count ]; + string[] tickers = new string[ signedTickers.Count ]; + int i = 0; + foreach ( string signedTicker in signedTickers.Keys ) + { + weights[ i ] = this.getWeightedPositions_getWeight( + absoluteWeightForEachPosition , signedTicker ); + tickers[ i ] = SignedTicker.GetTicker( signedTicker ); + i++; + } + WeightedPositions weightedPositions = + new WeightedPositions( weights , tickers ); + return weightedPositions; + } + private void setWeightedPositionsFromQPHashtables() + { + this.drivingWeightedPositions = this.getWeightedPositions( this.drivingPositions ); + this.portfolioWeightedPositions = this.getWeightedPositions( this.portfolioPositions ); + } + #endregion + #endregion } } |