[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoose
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-12-10 19:38:55
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoosers In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv14317/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoosers Modified Files: OutOfSampleChooser.cs Log Message: the new revision choses positions only if market value are available to later close the positions. 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 Index: OutOfSampleChooser.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/PairsTrading/OutOfSampleChoosers/OutOfSampleChooser.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** OutOfSampleChooser.cs 18 Nov 2008 23:03:15 -0000 1.4 --- OutOfSampleChooser.cs 10 Dec 2008 19:38:43 -0000 1.5 *************** *** 75,79 **** private void checkParameters( DateTime firstDateTimeToTestInefficiency , ! DateTime lastDateTimeToTestInefficiency ) { if ( firstDateTimeToTestInefficiency >= lastDateTimeToTestInefficiency ) --- 75,80 ---- private void checkParameters( DateTime firstDateTimeToTestInefficiency , ! DateTime lastDateTimeToTestInefficiency , ! DateTime dateTimeToClosePositions ) { if ( firstDateTimeToTestInefficiency >= lastDateTimeToTestInefficiency ) *************** *** 81,84 **** --- 82,89 ---- "The first date time to test inefficiency must be strictly before " + "the last date time to test inefficiency." ); + if ( lastDateTimeToTestInefficiency >= dateTimeToClosePositions ) + throw new Exception( + "The last date time to test inefficiency must be strictly before " + + "the date time to close positions." ); } *************** *** 165,170 **** } #endregion getReturnsManagerForLastSecondPhaseInterval ! #region getInefficientCouples #region addPositionsIfInefficiencyForCurrentCoupleIsInTheRange --- 170,213 ---- } #endregion getReturnsManagerForLastSecondPhaseInterval ! #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 = + this.areAllNeededMarketValuesAvailableForTheCurrentCouple( + currentCouple , dateTimeToClosePositions , + historicalMarketValueProviderForChosingPositionsOutOfSample ); + return areAllAvailable; + } + #endregion areAllNeededMarketValuesAvailableForTheCurrentCouple #region addPositionsIfInefficiencyForCurrentCoupleIsInTheRange *************** *** 217,230 **** currentWeightedPositions ); if ( ( returnForTheLastSecondPhaseInterval >= ! this.minThresholdForGoingLong ) && ( returnForTheLastSecondPhaseInterval <= ! this.maxThresholdForGoingLong ) ) // it looks like there has been an inefficiency that // might be recovered, by going short weightedPositionsToBeOpened = currentWeightedPositions.Opposite; if ( ( -returnForTheLastSecondPhaseInterval >= ! this.minThresholdForGoingShort ) && ( -returnForTheLastSecondPhaseInterval <= ! this.maxThresholdForGoingShort ) ) // it looks like there has been an inefficiency that // might be recovered, by going long --- 260,273 ---- 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 // might be recovered, by going long *************** *** 261,265 **** protected ArrayList getInefficientCouples( TestingPositions[] bestTestingPositionsInSample , ! ReturnsManager returnsManagerForLastSecondPhaseInterval ) { ArrayList inefficientCouples = new ArrayList(); --- 304,311 ---- protected ArrayList getInefficientCouples( TestingPositions[] bestTestingPositionsInSample , ! ReturnsManager returnsManagerForLastSecondPhaseInterval , ! DateTime dateTimeToClosePositions , ! HistoricalMarketValueProvider ! historicalMarketValueProviderForChosingPositionsOutOfSample ) { ArrayList inefficientCouples = new ArrayList(); *************** *** 267,275 **** currentTestingPositionsIndex < bestTestingPositionsInSample.Length ; currentTestingPositionsIndex++ ) ! this.addPositionsIfInefficiencyForCurrentCoupleIsInTheRange( bestTestingPositionsInSample , - returnsManagerForLastSecondPhaseInterval , currentTestingPositionsIndex , ! inefficientCouples ); return inefficientCouples; } --- 313,328 ---- currentTestingPositionsIndex < bestTestingPositionsInSample.Length ; currentTestingPositionsIndex++ ) ! { ! if ( this.areAllNeededMarketValuesAvailableForTheCurrentCouple( bestTestingPositionsInSample , currentTestingPositionsIndex , ! dateTimeToClosePositions , ! historicalMarketValueProviderForChosingPositionsOutOfSample ) ) ! this.addPositionsIfInefficiencyForCurrentCoupleIsInTheRange( ! bestTestingPositionsInSample , ! returnsManagerForLastSecondPhaseInterval , ! currentTestingPositionsIndex , ! inefficientCouples ); ! } return inefficientCouples; } *************** *** 281,284 **** --- 334,338 ---- DateTime firstDateTimeToTestInefficiency , DateTime lastDateTimeToTestInefficiency , + DateTime dateTimeToClosePositions , // ReturnIntervals outOfSampleReturnIntervals , HistoricalMarketValueProvider *************** *** 292,297 **** historicalMarketValueProviderForChosingPositionsOutOfSample ); ArrayList inefficientCouples = ! this.getInefficientCouples( bestTestingPositionsInSample , ! returnsManagerForLastSecondPhaseInterval ); return inefficientCouples; } --- 346,354 ---- historicalMarketValueProviderForChosingPositionsOutOfSample ); ArrayList inefficientCouples = ! this.getInefficientCouples( ! bestTestingPositionsInSample , ! returnsManagerForLastSecondPhaseInterval , ! dateTimeToClosePositions , ! historicalMarketValueProviderForChosingPositionsOutOfSample ); return inefficientCouples; } *************** *** 327,330 **** --- 384,388 ---- DateTime firstDateTimeToTestInefficiency , DateTime lastDateTimeToTestInefficiency , + DateTime dateTimeToClosePositions , // ReturnIntervals outOfSampleReturnIntervals , HistoricalMarketValueProvider *************** *** 336,339 **** --- 394,398 ---- firstDateTimeToTestInefficiency , lastDateTimeToTestInefficiency , + dateTimeToClosePositions , // outOfSampleReturnIntervals , historicalMarketValueProviderForChosingPositionsOutOfSample ); *************** *** 346,349 **** --- 405,409 ---- DateTime firstDateTimeToTestInefficiency , DateTime lastDateTimeToTestInefficiency , + DateTime dateTimeToClosePositions , // ReturnIntervals outOfSampleReturnIntervals , HistoricalMarketValueProvider *************** *** 358,361 **** --- 418,422 ---- firstDateTimeToTestInefficiency , lastDateTimeToTestInefficiency , + dateTimeToClosePositions , // outOfSampleReturnIntervals , historicalMarketValueProviderForChosingPositionsOutOfSample ); *************** *** 369,372 **** --- 430,434 ---- DateTime firstDateTimeToTestInefficiency , DateTime lastDateTimeToTestInefficiency , + DateTime dateTimeToClosePositions , // ReturnIntervals outOfSampleReturnIntervals , HistoricalMarketValueProvider *************** *** 380,383 **** --- 442,446 ---- firstDateTimeToTestInefficiency , lastDateTimeToTestInefficiency , + dateTimeToClosePositions , // outOfSampleReturnIntervals , historicalMarketValueProviderForChosingPositionsOutOfSample ); *************** *** 394,401 **** /// Selects the WeghtedPositions to actually be opened /// </summary> ! /// <param name="bestTestingPositionsInSample">most correlated couples, ! /// in sample</param> ! /// <param name="minThreshold">min requested inefficiency</param> ! /// <param name="maxThreshold">max allowed inefficiency</param> /// <param name="inSampleReturnsManager"></param> /// <returns></returns> --- 457,465 ---- /// Selects the WeghtedPositions to actually be opened /// </summary> ! /// <param name="bestTestingPositionsInSample"></param> ! /// <param name="firstDateTimeToTestInefficiency"></param> ! /// <param name="lastDateTimeToTestInefficiency"></param> ! /// <param name="timeToClosePositins"></param> ! /// <param name="historicalMarketValueProviderForChosingPositionsOutOfSample"></param> /// <param name="inSampleReturnsManager"></param> /// <returns></returns> *************** *** 404,407 **** --- 468,472 ---- DateTime firstDateTimeToTestInefficiency , DateTime lastDateTimeToTestInefficiency , + DateTime dateTimeToClosePositions , // ReturnIntervals outOfSampleReturnIntervals , HistoricalMarketValueProvider *************** *** 410,414 **** { this.checkParameters( ! firstDateTimeToTestInefficiency , lastDateTimeToTestInefficiency ); WeightedPositions positionsToBeOpened = this.getPositionsToBeOpened_actually( --- 475,481 ---- { this.checkParameters( ! firstDateTimeToTestInefficiency , ! lastDateTimeToTestInefficiency , ! dateTimeToClosePositions ); WeightedPositions positionsToBeOpened = this.getPositionsToBeOpened_actually( *************** *** 416,419 **** --- 483,487 ---- firstDateTimeToTestInefficiency , lastDateTimeToTestInefficiency , + dateTimeToClosePositions , historicalMarketValueProviderForChosingPositionsOutOfSample , inSampleReturnsManager ); |