[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/LinearRegression/Logging Anal
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2011-01-06 19:28:30
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearRegression/Logging In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv27008/b7_Scripts/WalkForwardTesting/LinearRegression/Logging Modified Files: AnalyzerForLinearRegressionTestingPositions.cs Log Message: now it has been actually written: analyzes a LinearRegressionTestingPositions that has been logged in sample Index: AnalyzerForLinearRegressionTestingPositions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearRegression/Logging/AnalyzerForLinearRegressionTestingPositions.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AnalyzerForLinearRegressionTestingPositions.cs 28 Mar 2010 16:12:46 -0000 1.1 --- AnalyzerForLinearRegressionTestingPositions.cs 6 Jan 2011 19:28:22 -0000 1.2 *************** *** 22,27 **** --- 22,35 ---- using System; + using System.Drawing; + using System.Windows.Forms; + using QuantProject.ADT.Histories; + using QuantProject.ADT.Statistics; + using QuantProject.Business.DataProviders; using QuantProject.Business.Scripting; + using QuantProject.Business.Strategies.ReturnsManagement.Time; + using QuantProject.Business.Strategies.ReturnsManagement.Time.IntervalsSelectors; + using QuantProject.Presentation; namespace QuantProject.Scripts.WalkForwardTesting.LinearRegression *************** *** 34,49 **** public class AnalyzerForLinearRegressionTestingPositions : IExecutable { ! LinearRegressionTestingPositions linearRegressionTestingPositions; ! DateTime accountTimerDateTimeWhenThisObjectWasLogged; public AnalyzerForLinearRegressionTestingPositions( LinearRegressionTestingPositions linearRegressionTestingPositions , ! // int numberOfInSampleDays , ! DateTime accountTimerDateTimeWhenThisObjectWasLogged ) { this.linearRegressionTestingPositions = linearRegressionTestingPositions; this.accountTimerDateTimeWhenThisObjectWasLogged = accountTimerDateTimeWhenThisObjectWasLogged; } public void Run() { --- 42,227 ---- public class AnalyzerForLinearRegressionTestingPositions : IExecutable { ! private LinearRegressionTestingPositions linearRegressionTestingPositions; ! private DateTime firstDateTimeInSample; ! private IIntervalsSelector intervalsSelector; ! private IReturnIntervalSelectorForSignaling returnIntervalSelectorForSignaling; ! private DateTime accountTimerDateTimeWhenThisObjectWasLogged; + /// <summary> + /// Generation when the TestingPositions object has been created + /// (if genetically optimized) + /// </summary> + public int Generation + { + get + { + return this.linearRegressionTestingPositions.Generation; + } + } + public double FitnessInSample + { + get { return this.linearRegressionTestingPositions.FitnessInSample; } + } + public AnalyzerForLinearRegressionTestingPositions( LinearRegressionTestingPositions linearRegressionTestingPositions , ! DateTime accountTimerDateTimeWhenThisObjectWasLogged ! ) { this.linearRegressionTestingPositions = linearRegressionTestingPositions; + // this.firstDateTimeInSample = firstDateTimeInSample; + // this.intervalsSelector = intervalsSelector; + // this.returnIntervalSelectorForSignaling = returnIntervalSelectorForSignaling; this.accountTimerDateTimeWhenThisObjectWasLogged = accountTimerDateTimeWhenThisObjectWasLogged; } + + public void SetOtherMembers( + DateTime firstDateTimeInSample , + IIntervalsSelector intervalsSelector , + IReturnIntervalSelectorForSignaling returnIntervalSelectorForSignaling ) + { + this.firstDateTimeInSample = firstDateTimeInSample; + this.intervalsSelector = intervalsSelector; + this.returnIntervalSelectorForSignaling = returnIntervalSelectorForSignaling; + } + + #region Run + + #region getReturnIntervals + private ReturnIntervals initializeReturnIntervalsWithTheFirstInterval( + DateTime firstDateTime ) + { + ReturnInterval firstReturnInterval = + this.intervalsSelector.GetFirstInterval( firstDateTime ); + ReturnIntervals returnIntervals = new ReturnIntervals( firstReturnInterval ); + return returnIntervals; + } + private DateTime getFirstDateTime() + { + DateTime firstDateTime = this.accountTimerDateTimeWhenThisObjectWasLogged.AddDays( - 180 ); + return firstDateTime; + } + private DateTime getLastDateTime() + { + DateTime lastDateTime = this.accountTimerDateTimeWhenThisObjectWasLogged; + return lastDateTime; + } + private ReturnInterval getNextInterval( ReturnIntervals returnIntervals ) + { + ReturnInterval returnIntervalForTrading = this.intervalsSelector.GetNextInterval( + returnIntervals ); + // ReturnInterval returnIntervalForSignaling = + // this.returnIntervalSelectorForSignaling.GetReturnIntervalUsedForSignaling( + // returnIntervalForTrading ); + // return returnIntervalForSignaling; + return returnIntervalForTrading; + } + private ReturnIntervals getReturnIntervals( + DateTime firstDateTime , DateTime lastDateTime ) + { + ReturnIntervals returnIntervals = + this.initializeReturnIntervalsWithTheFirstInterval( firstDateTime ); + while ( returnIntervals.LastInterval.End < lastDateTime ) + { + ReturnInterval nextInterval = this.getNextInterval( returnIntervals ); + returnIntervals.Add( nextInterval ); + } + return returnIntervals; + } + private ReturnIntervals getReturnIntervals() + { + // DateTime firstDateTime = this.getFirstDateTime(); + DateTime firstDateTime = this.firstDateTimeInSample; + DateTime lastDateTime = this.getLastDateTime(); + ReturnIntervals returnIntervals = this.getReturnIntervals( + firstDateTime , lastDateTime ); + return returnIntervals; + } + #endregion getReturnIntervals + + private History getVirtualValues( + ReturnIntervals returnIntervals , IVirtualReturnComputer virtualReturnComputer ) + { + History virtualValues = new History(); + int currentIntervalIndex = 0; + double currentValue = 30000; + while ( currentIntervalIndex < returnIntervals.Count ) + { + ReturnInterval currentInterval = returnIntervals[ currentIntervalIndex ]; + if ( !virtualValues.ContainsKey( currentInterval.Begin ) ) + virtualValues.Add( currentInterval.Begin , currentValue ); + try + { + double currentReturn = virtualReturnComputer.ComputeReturn( currentInterval ); + currentValue = currentValue + currentValue * currentReturn; + virtualValues.Add( currentInterval.End , currentValue ); + } + catch( TickerNotExchangedException ) + { + } + currentIntervalIndex++; + } + return virtualValues; + } + + #region getVirtualValues + private void getVirtualValues( + ReturnIntervals returnIntervals , + out History predictedVirtualValues , out History tradingPortfolioVirtualValues ) + { + IHistoricalMarketValueProvider historicalMarketValueProvider = + new HistoricalAdjustedQuoteProvider(); + + ReturnPredictor returnPredictor = new ReturnPredictor( + this.linearRegressionTestingPositions , this.returnIntervalSelectorForSignaling , + historicalMarketValueProvider ); + predictedVirtualValues = this.getVirtualValues( + returnIntervals , returnPredictor ); + + PortfolioReturnComputer tradingPortfolioReturnComputer = + new PortfolioReturnComputer( + this.linearRegressionTestingPositions.TradingPortfolio , + historicalMarketValueProvider ); + tradingPortfolioVirtualValues = this.getVirtualValues( + returnIntervals , tradingPortfolioReturnComputer ); + } + #endregion getVirtualValues + + #region showHistoriesPlots + private HistoriesViewer showHistoriesPlots( + History predictedVirtualValues , History tradingPortfolioVirtualValues ) + { + HistoriesViewer historiesViewer = + new HistoriesViewer( "Linear Regression Log Analyzer" ); + historiesViewer.StartPosition = FormStartPosition.Manual; + historiesViewer.Location = new Point( 200 , 200 ); + historiesViewer.Add( predictedVirtualValues , Color.Red ); + historiesViewer.Add( tradingPortfolioVirtualValues , Color.Green ); + historiesViewer.Show(); + return historiesViewer; + } + #endregion showHistoriesPlots + + #region showCorrelation + private double[] getArrayFromHistory( History history ) + { + double[] arrayValues = new double[ history.Count ]; + for( int i = 0 ; i < arrayValues.Length ; i++ ) + arrayValues[ i ] = (double)history.GetValueList()[ i ]; + return arrayValues; + } + + private void showCorrelation( + History predictedVirtualValues , History tradingPortfolioVirtualValues ) + { + double correlationValue = BasicFunctions.PearsonCorrelationCoefficient( + this.getArrayFromHistory( predictedVirtualValues ) , + this.getArrayFromHistory( tradingPortfolioVirtualValues ) ); + MessageBox.Show( "Correlation value: " + correlationValue ); + } + #endregion showCorrelation + + public void Run() { *************** *** 51,58 **** // coefficients and then show which are those who seem to // be more significant ! System.Windows.Forms.MessageBox.Show( ! "A good analyzer has not yet been thought about, for " + ! "a LinearRegressionTestingPositions" ); } } } --- 229,244 ---- // coefficients and then show which are those who seem to // be more significant ! ReturnIntervals returnIntervals = this.getReturnIntervals(); ! ! History predictedVirtualValues; ! History tradingPortfolioVirtualValues; ! this.getVirtualValues( ! returnIntervals , out predictedVirtualValues , out tradingPortfolioVirtualValues ); ! ! this.showHistoriesPlots( predictedVirtualValues , tradingPortfolioVirtualValues ); ! ! this.showCorrelation( predictedVirtualValues , tradingPortfolioVirtualValues ); } + #endregion Run } } |