[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoose
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2009-02-28 18:35:17
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoosers In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv21462/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoosers Modified Files: OutOfSampleChooser.cs Log Message: The code has been changed so that the derived classes can change the strategy by overriding some key methods Index: OutOfSampleChooser.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoosers/OutOfSampleChooser.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** OutOfSampleChooser.cs 18 Feb 2009 20:05:32 -0000 1.6 --- OutOfSampleChooser.cs 28 Feb 2009 18:35:10 -0000 1.7 *************** *** 25,28 **** --- 25,29 ---- using QuantProject.ADT.Collections; + using QuantProject.ADT.Timing; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies; *************** *** 41,44 **** --- 42,46 ---- public abstract class OutOfSampleChooser { + private Time firstTimeToTestInefficiency; private double minThresholdForGoingLong; private double maxThresholdForGoingLong; *************** *** 46,51 **** --- 48,57 ---- private double maxThresholdForGoingShort; + public double MinThresholdForGoingLong { + get { return this.minThresholdForGoingLong; } + } public OutOfSampleChooser( + Time firstTimeToTestInefficiency , double minThresholdForGoingLong , double maxThresholdForGoingLong , *************** *** 53,56 **** --- 59,63 ---- double maxThresholdForGoingShort ) { + this.firstTimeToTestInefficiency = firstTimeToTestInefficiency; this.minThresholdForGoingLong = minThresholdForGoingLong; this.maxThresholdForGoingLong = maxThresholdForGoingLong; *************** *** 64,68 **** /// <param name="inefficientCouples">a collection of couples that /// are strongly correlated in sample, but were not so ! /// correlated in the latest second phase interval</param> /// <param name="inSampleReturnsManager"></param> /// <returns></returns> --- 71,75 ---- /// <param name="inefficientCouples">a collection of couples that /// are strongly correlated in sample, but were not so ! /// correlated in the interval to test for inefficiency</param> /// <param name="inSampleReturnsManager"></param> /// <returns></returns> *************** *** 114,117 **** --- 121,139 ---- #region getReturnIntervalsForLastSecondPhaseInterval + + protected virtual DateTime getFirstDateTimeToTestInefficiency( DateTime currentDateTime ) + { + // DateTime now = this.now(); + // DateTime firstDateTimeToTestInefficiency = new DateTime( + // now.Year , now.Month , now.Day , + // this.firstTimeToTestInefficiency.Hour , + // this.firstTimeToTestInefficiency.Minute , + // this.firstTimeToTestInefficiency.Second ); + DateTime firstDateTimeToTestInefficiency = + Time.GetDateTimeFromMerge( + currentDateTime , this.firstTimeToTestInefficiency ); + return firstDateTimeToTestInefficiency; + } + private ReturnInterval getReturnIntervalToTestInefficiency( *************** *** 121,132 **** ) { - // qui!!! DateTime firstDateTimeToTestInefficiency = ! currentDateTime.AddMinutes( 30 ); ! ReturnInterval returnIntervalForLastSecondPhaseInterval = new ReturnInterval( firstDateTimeToTestInefficiency , currentDateTime ); ! return returnIntervalForLastSecondPhaseInterval; } private ReturnIntervals --- 143,154 ---- ) { DateTime firstDateTimeToTestInefficiency = ! this.getFirstDateTimeToTestInefficiency( currentDateTime ); ! // currentDateTime.AddMinutes( 30 ); ! ReturnInterval returnIntervalToTestInefficiency = new ReturnInterval( firstDateTimeToTestInefficiency , currentDateTime ); ! return returnIntervalToTestInefficiency; } private ReturnIntervals *************** *** 137,141 **** ) { ! ReturnInterval returnIntervalForLastSecondPhaseInterval = this.getReturnIntervalToTestInefficiency( currentDateTime --- 159,163 ---- ) { ! ReturnInterval returnIntervalToTestInefficiency = this.getReturnIntervalToTestInefficiency( currentDateTime *************** *** 143,149 **** // lastDateTimeToTestInefficiency ); ! ReturnIntervals returnIntervalsForLastSecondPhaseInterval = ! new ReturnIntervals( returnIntervalForLastSecondPhaseInterval ); ! return returnIntervalsForLastSecondPhaseInterval; } #endregion getReturnIntervalsForLastSecondPhaseInterval --- 165,171 ---- // lastDateTimeToTestInefficiency ); ! ReturnIntervals returnIntervalsToTestInefficiency = ! new ReturnIntervals( returnIntervalToTestInefficiency ); ! return returnIntervalsToTestInefficiency; } #endregion getReturnIntervalsForLastSecondPhaseInterval *************** *** 171,212 **** #region getInefficientCouples ! #region areAllNeededMarketValuesAvailableForTheCurrentCouple ! private bool areAllNeededMarketValuesAvailableForTheCurrentCouple( ! TestingPositions currentCouple , ! DateTime dateTimeToClosePositions , ! HistoricalMarketValueProvider ! historicalMarketValueProviderForChosingPositionsOutOfSample ) ! { ! bool areAllAvailable = true; ! foreach ( WeightedPosition weightedPosition in ! currentCouple.WeightedPositions.Values ) ! areAllAvailable = ! ( ! areAllAvailable && ! // attention! we are looking in the future here, but we do it ! // just to avoid picking a ticker for which we don't have ! // the market value when we will close the positions ! historicalMarketValueProviderForChosingPositionsOutOfSample.WasExchanged( ! weightedPosition.Ticker , dateTimeToClosePositions ) ! ); ! return areAllAvailable; ! } ! private bool areAllNeededMarketValuesAvailableForTheCurrentCouple( ! TestingPositions[] bestTestingPositionsInSample , ! int currentTestingPositionsIndex , // DateTime dateTimeToClosePositions , ! HistoricalMarketValueProvider ! historicalMarketValueProviderForChosingPositionsOutOfSample ) ! { ! TestingPositions currentCouple = ! bestTestingPositionsInSample[ currentTestingPositionsIndex ]; ! bool areAllAvailable = true; ! // qui!!! ! // this.areAllNeededMarketValuesAvailableForTheCurrentCouple( ! // currentCouple , dateTimeToClosePositions , ! // historicalMarketValueProviderForChosingPositionsOutOfSample ); ! return areAllAvailable; ! } ! #endregion areAllNeededMarketValuesAvailableForTheCurrentCouple #region addPositionsIfInefficiencyForCurrentCoupleIsInTheRange --- 193,232 ---- #region getInefficientCouples ! // #region areAllNeededMarketValuesAvailableForTheCurrentCouple ! // private bool areAllNeededMarketValuesAvailableForTheCurrentCouple( ! // TestingPositions currentCouple , // DateTime dateTimeToClosePositions , ! // HistoricalMarketValueProvider ! // historicalMarketValueProviderForChosingPositionsOutOfSample ) ! // { ! // bool areAllAvailable = true; ! // foreach ( WeightedPosition weightedPosition in ! // currentCouple.WeightedPositions.Values ) ! // areAllAvailable = ! // ( ! // areAllAvailable && ! // // attention! we are looking in the future here, but we do it ! // // just to avoid picking a ticker for which we don't have ! // // the market value when we will close the positions ! // historicalMarketValueProviderForChosingPositionsOutOfSample.WasExchanged( ! // weightedPosition.Ticker , dateTimeToClosePositions ) ! // ); ! // return areAllAvailable; ! // } ! // private bool areAllNeededMarketValuesAvailableForTheCurrentCouple( ! // TestingPositions[] bestTestingPositionsInSample , ! // int currentTestingPositionsIndex , ! //// DateTime dateTimeToClosePositions , ! // HistoricalMarketValueProvider ! // historicalMarketValueProviderForChosingPositionsOutOfSample ) ! // { ! // TestingPositions currentCouple = ! // bestTestingPositionsInSample[ currentTestingPositionsIndex ]; ! // this.areAllNeededMarketValuesAvailableForTheCurrentCouple( ! // currentCouple , dateTimeToClosePositions , ! // historicalMarketValueProviderForChosingPositionsOutOfSample ); ! // return areAllAvailable; ! // } ! // #endregion areAllNeededMarketValuesAvailableForTheCurrentCouple #region addPositionsIfInefficiencyForCurrentCoupleIsInTheRange *************** *** 231,244 **** #region getPositionsIfInefficiencyIsInTheRange ! private double getReturnForTheLastSecondPhaseInterval( ! ReturnsManager returnsManagerForLastSecondPhaseInterval , WeightedPositions weightedPositions ) { // returnsManager should contain a single ReturnInterval, and // this ReturnInterval should be the last second phase interval ! double returnForTheLastSecondPhaseInterval = weightedPositions.GetReturn( 0 , ! returnsManagerForLastSecondPhaseInterval ); ! return returnForTheLastSecondPhaseInterval; } --- 251,264 ---- #region getPositionsIfInefficiencyIsInTheRange ! private double getReturnToTestInefficiency( ! ReturnsManager returnsManagerToTestInefficiency , WeightedPositions weightedPositions ) { // returnsManager should contain a single ReturnInterval, and // this ReturnInterval should be the last second phase interval ! double returnToTestInefficiency = weightedPositions.GetReturn( 0 , ! returnsManagerToTestInefficiency ); ! return returnToTestInefficiency; } *************** *** 248,253 **** // satisfy the thresholds) this method returns null protected virtual WeightedPositions ! getWeightedPositionsFromCandidate( ! ReturnsManager returnsManagerForLastSecondPhaseInterval , WeightedPositions currentWeightedPositions ) { --- 268,274 ---- // satisfy the thresholds) this method returns null protected virtual WeightedPositions ! getWeightedPositionsIfThereIsInefficiency( ! DateTime currentDateTime , ! ReturnsManager returnsManagerToTestInefficiency , WeightedPositions currentWeightedPositions ) { *************** *** 255,272 **** try { ! double returnForTheLastSecondPhaseInterval = ! this.getReturnForTheLastSecondPhaseInterval( ! returnsManagerForLastSecondPhaseInterval , currentWeightedPositions ); ! if ( ( returnForTheLastSecondPhaseInterval >= this.minThresholdForGoingShort ) && ! ( returnForTheLastSecondPhaseInterval <= this.maxThresholdForGoingShort ) ) // it looks like there has been an inefficiency that // might be recovered, by going short weightedPositionsToBeOpened = currentWeightedPositions.Opposite; ! if ( ( -returnForTheLastSecondPhaseInterval >= this.minThresholdForGoingLong ) && ! ( -returnForTheLastSecondPhaseInterval <= this.maxThresholdForGoingLong ) ) // it looks like there has been an inefficiency that --- 276,293 ---- try { ! double returnForTheIntervalToTestInefficiency = ! this.getReturnToTestInefficiency( ! returnsManagerToTestInefficiency , currentWeightedPositions ); ! if ( ( returnForTheIntervalToTestInefficiency >= this.minThresholdForGoingShort ) && ! ( returnForTheIntervalToTestInefficiency <= this.maxThresholdForGoingShort ) ) // it looks like there has been an inefficiency that // might be recovered, by going short weightedPositionsToBeOpened = currentWeightedPositions.Opposite; ! if ( ( -returnForTheIntervalToTestInefficiency >= this.minThresholdForGoingLong ) && ! ( -returnForTheIntervalToTestInefficiency <= this.maxThresholdForGoingLong ) ) // it looks like there has been an inefficiency that *************** *** 285,289 **** addPositionsIfInefficiencyForCurrentCoupleIsInTheRange( TestingPositions[] bestTestingPositionsInSample , ! ReturnsManager returnsManagerForLastSecondPhaseInterval , int currentTestingPositionsIndex , ArrayList inefficientCouples ) --- 306,311 ---- addPositionsIfInefficiencyForCurrentCoupleIsInTheRange( TestingPositions[] bestTestingPositionsInSample , ! DateTime currentDateTime , ! ReturnsManager returnsManagerToTestInefficiency , int currentTestingPositionsIndex , ArrayList inefficientCouples ) *************** *** 294,299 **** this.getCandidateForPortfolio( currentWeightedPositions ); WeightedPositions weightedPositionsThatMightBeOpended = ! this.getWeightedPositionsFromCandidate( ! returnsManagerForLastSecondPhaseInterval , candidateForPortfolio ); if ( weightedPositionsThatMightBeOpended != null ) // the current couple has not an inefficiency that's in the range --- 316,321 ---- this.getCandidateForPortfolio( currentWeightedPositions ); WeightedPositions weightedPositionsThatMightBeOpended = ! this.getWeightedPositionsIfThereIsInefficiency( ! currentDateTime , returnsManagerToTestInefficiency , candidateForPortfolio ); if ( weightedPositionsThatMightBeOpended != null ) // the current couple has not an inefficiency that's in the range *************** *** 304,308 **** protected ArrayList getInefficientCouples( TestingPositions[] bestTestingPositionsInSample , ! ReturnsManager returnsManagerForLastSecondPhaseInterval , // DateTime dateTimeToClosePositions , HistoricalMarketValueProvider --- 326,331 ---- protected ArrayList getInefficientCouples( TestingPositions[] bestTestingPositionsInSample , ! DateTime currentDateTime , ! ReturnsManager returnsManagerToTestInefficiency , // DateTime dateTimeToClosePositions , HistoricalMarketValueProvider *************** *** 314,327 **** currentTestingPositionsIndex++ ) { ! if ( this.areAllNeededMarketValuesAvailableForTheCurrentCouple( bestTestingPositionsInSample , currentTestingPositionsIndex , ! // dateTimeToClosePositions , ! historicalMarketValueProviderForChosingPositionsOutOfSample ) ) ! this.addPositionsIfInefficiencyForCurrentCoupleIsInTheRange( ! bestTestingPositionsInSample , ! returnsManagerForLastSecondPhaseInterval , ! currentTestingPositionsIndex , ! inefficientCouples ); } return inefficientCouples; --- 337,351 ---- currentTestingPositionsIndex++ ) { ! // if ( this.areAllNeededMarketValuesAvailableForTheCurrentCouple( ! // bestTestingPositionsInSample , ! // currentTestingPositionsIndex , ! //// dateTimeToClosePositions , ! // historicalMarketValueProviderForChosingPositionsOutOfSample ) ) ! this.addPositionsIfInefficiencyForCurrentCoupleIsInTheRange( bestTestingPositionsInSample , + currentDateTime , + returnsManagerToTestInefficiency , currentTestingPositionsIndex , ! inefficientCouples ); } return inefficientCouples; *************** *** 340,344 **** historicalMarketValueProviderForChosingPositionsOutOfSample ) { ! ReturnsManager returnsManagerForLastSecondPhaseInterval = this.getReturnsManagerToTestInefficiency( currentDateTime , --- 364,368 ---- historicalMarketValueProviderForChosingPositionsOutOfSample ) { ! ReturnsManager returnsManagerToTestInefficiency = this.getReturnsManagerToTestInefficiency( currentDateTime , *************** *** 349,353 **** this.getInefficientCouples( bestTestingPositionsInSample , ! returnsManagerForLastSecondPhaseInterval , // dateTimeToClosePositions , historicalMarketValueProviderForChosingPositionsOutOfSample ); --- 373,378 ---- this.getInefficientCouples( bestTestingPositionsInSample , ! currentDateTime , ! returnsManagerToTestInefficiency , // dateTimeToClosePositions , historicalMarketValueProviderForChosingPositionsOutOfSample ); |