[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting/OTC/OTC_Intraday OTCIntra
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2009-08-31 20:40:41
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/OTC/OTC_Intraday In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv20871/OTC_Intraday Added Files: OTCIntradayLogItem.cs OTCIntradayMain.cs OTCIntradayStrategy.cs Log Message: Added script files for the implementation of the OTC strategy --- NEW FILE: OTCIntradayStrategy.cs --- /* QuantProject - Quantitative Finance Library OTCIntradayStrategy.cs Copyright (C) 2008 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Data; using System.Collections; using System.Collections.Generic; using System.IO; using QuantProject.ADT; using QuantProject.ADT.Statistics; using QuantProject.ADT.Histories; using QuantProject.ADT.Messaging; using QuantProject.ADT.Timing; using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Financial.Instruments; using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Timing; using QuantProject.Business.Strategies; using QuantProject.Business.Strategies.InSample; using QuantProject.Business.Strategies.InSample.InSampleFitnessDistributionEstimation; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies.Logging; using QuantProject.Business.Strategies.ReturnsManagement; using QuantProject.Business.Strategies.ReturnsManagement.Time; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies.TickersRelationships; using QuantProject.Business.Strategies.Eligibles; using QuantProject.Business.Strategies.Optimizing.Decoding; using QuantProject.Data; using QuantProject.Data.DataProviders; using QuantProject.Data.Selectors; using QuantProject.Data.DataTables; using QuantProject.ADT.Optimizing.Genetic; using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.InSampleChoosers; using QuantProject.Scripts.TickerSelectionTesting.OTC; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; using QuantProject.Scripts.WalkForwardTesting.LinearCombination; namespace QuantProject.Scripts.TickerSelectionTesting.OTC.OTC_Intraday { /// <summary> /// Implements the open to close strategy (buy a portfolio at open /// and sell it at close) next to opening and closing time, /// using intraday bars /// </summary> [Serializable] public class OTCIntradayStrategy : IStrategyForBacktester { public event NewLogItemEventHandler NewLogItem; public event NewMessageEventHandler NewMessage; //initialized by the constructor protected int inSampleDays; protected int numDaysBetweenEachOptimization; protected int numDaysBeforeCurrentDateForRetrievingInSampleData; protected IInSampleChooser inSampleChooser; protected GeneticChooser geneticChooserForFitnessDistributionEstimator; protected double numberOfMinStdDeviationForOpeningPositions; protected double numberOfMaxStdDeviationForOpeningPositions; protected IEligiblesSelector eligiblesSelector; protected IInSampleFitnessDistributionEstimator estimator; protected int sampleLength; protected Benchmark benchmark; protected HistoricalMarketValueProvider historicalMarketValueProviderForInSample; protected HistoricalMarketValueProvider historicalMarketValueProviderForOutOfSample; //initialized after constructor's call protected int numDaysElapsedSinceLastOptimization; protected ReturnsManager returnsManager; protected TestingPositions[] chosenOTCPositions; //chosen in sample by the chooser or passed //directly by the user using a form: //these are the positions to test out of sample protected PortfolioType portfolioType; protected DateTime lastOptimizationDateTime; protected Account account; public Account Account { get { return this.account; } set { this.account = value; } } private int minimumNumberOfEligiblesForValidOptimization; // private bool optimalPositionsHaveBeenUpdated; protected int idxForBestPositionsCompatibleWithPortfolioType; protected EligibleTickers currentEligibles; protected bool stopLossConditionReached; protected bool takeProfitConditionReached; protected double maxOpeningLengthInMinutes; protected Time lastEntryTime; protected Time lastProfitOrLossTime; protected List<Time> openingTimesForAvailableBars; protected Time nearToOpeningTimeFrom; protected Time nearToOpeningTimeTo; protected Time nearToClosingTimeFrom; protected Time nearToClosingTimeTo; protected double currentAccountValue; protected double previousAccountValue; protected double stopLoss; protected double takeProfit; private string description_GetDescriptionForChooser() { if(this.inSampleChooser == null) return "NoChooserDefined"; else return this.inSampleChooser.Description; } private Time getLastEventTimeWithCachedBars() { return this.openingTimesForAvailableBars[openingTimesForAvailableBars.Count - 1]; } private Time getFirstEventTimeWithCachedBars() { return this.openingTimesForAvailableBars[0]; } public string Description { get { string description = "OTC_Intraday"; return description; } } public bool StopBacktestIfMaxRunningHoursHasBeenReached { get { return true; } } private void otcIntradayStrategy_commonInitialization(IEligiblesSelector eligiblesSelector, int minimumNumberOfEligiblesForValidOptimization, int inSampleDays, Benchmark benchmark, int numDaysBetweenEachOptimization, int numDaysBeforeCurrentDateForRetrievingInSampleData, HistoricalMarketValueProvider historicalMarketValueProviderForInSample, HistoricalMarketValueProvider historicalMarketValueProviderForOutOfSample, List<Time> openingTimesForAvailableBars, Time nearToOpeningTimeFrom, Time nearToOpeningTimeTo, Time nearToClosingTimeFrom, Time nearToClosingTimeTo, double stopLoss, double takeProfit, PortfolioType portfolioType, GeneticChooser geneticChooserForFitnessDistributionEstimator, double numberOfMinimumStdDeviationForOpeningPositions, double numberOfMaxStdDeviationForOpeningPositions, IInSampleFitnessDistributionEstimator estimator, int sampleLength) { this.eligiblesSelector = eligiblesSelector; this.minimumNumberOfEligiblesForValidOptimization = minimumNumberOfEligiblesForValidOptimization; this.inSampleDays = inSampleDays; this.benchmark = benchmark; this.idxForBestPositionsCompatibleWithPortfolioType = 0; this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; this.numDaysBeforeCurrentDateForRetrievingInSampleData = numDaysBeforeCurrentDateForRetrievingInSampleData; this.historicalMarketValueProviderForInSample = historicalMarketValueProviderForInSample; this.historicalMarketValueProviderForOutOfSample = historicalMarketValueProviderForOutOfSample; this.openingTimesForAvailableBars = openingTimesForAvailableBars; this.nearToOpeningTimeFrom = nearToOpeningTimeFrom; this.nearToOpeningTimeTo = nearToOpeningTimeTo; this.nearToClosingTimeFrom = nearToClosingTimeFrom; this.nearToClosingTimeTo = nearToClosingTimeTo; this.stopLoss = stopLoss; this.takeProfit = takeProfit; this.portfolioType = portfolioType; this.geneticChooserForFitnessDistributionEstimator = geneticChooserForFitnessDistributionEstimator; this.numberOfMinStdDeviationForOpeningPositions = numberOfMinimumStdDeviationForOpeningPositions; this.numberOfMaxStdDeviationForOpeningPositions = numberOfMaxStdDeviationForOpeningPositions; this.estimator = estimator; this.sampleLength = sampleLength; } public OTCIntradayStrategy(IEligiblesSelector eligiblesSelector, int minimumNumberOfEligiblesForValidOptimization, IInSampleChooser inSampleChooser, int inSampleDays, Benchmark benchmark, int numDaysBetweenEachOptimization, int numDaysBeforeCurrentDateForRetrievingInSampleData, HistoricalMarketValueProvider historicalMarketValueProviderForInSample, HistoricalMarketValueProvider historicalMarketValueProviderForOutOfSample, List<Time> openingTimesForAvailableBars, Time nearToOpeningTimeFrom, Time nearToOpeningTimeTo, Time nearToClosingTimeFrom, Time nearToClosingTimeTo, double stopLoss, double takeProfit, PortfolioType portfolioType, GeneticChooser geneticChooserForFitnessDistributionEstimator, double numberOfMinimumStdDeviationForOpeningPositions, double numberOfMaxStdDeviationForOpeningPositions, IInSampleFitnessDistributionEstimator estimator, int sampleLength) { this.inSampleChooser = inSampleChooser; this.otcIntradayStrategy_commonInitialization(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, inSampleDays, benchmark, numDaysBetweenEachOptimization, numDaysBeforeCurrentDateForRetrievingInSampleData, historicalMarketValueProviderForInSample, historicalMarketValueProviderForOutOfSample, openingTimesForAvailableBars, nearToOpeningTimeFrom, nearToOpeningTimeTo, nearToClosingTimeFrom, nearToClosingTimeTo, stopLoss, takeProfit, portfolioType, geneticChooserForFitnessDistributionEstimator, numberOfMinimumStdDeviationForOpeningPositions, numberOfMaxStdDeviationForOpeningPositions, estimator, sampleLength); } public OTCIntradayStrategy(IEligiblesSelector eligiblesSelector, TestingPositions[] chosenOTCPositions, Benchmark benchmark, HistoricalMarketValueProvider historicalMarketValueProviderForInSample, HistoricalMarketValueProvider historicalMarketValueProviderForOutOfSample, List<Time> openingTimesForAvailableBars, double stopLoss, double takeProfit, PortfolioType portfolioType, GeneticChooser geneticChooserForFitnessDistributionEstimator, double numberOfMinimumStdDeviationForOpeningPositions, double numberOfMaxStdDeviationForOpeningPositions, IInSampleFitnessDistributionEstimator estimator, int sampleLength) { this.chosenOTCPositions = chosenOTCPositions; this.otcIntradayStrategy_commonInitialization(eligiblesSelector, 5, 5, benchmark, numDaysBetweenEachOptimization, 0, historicalMarketValueProviderForInSample, historicalMarketValueProviderForOutOfSample, openingTimesForAvailableBars, nearToOpeningTimeFrom, nearToOpeningTimeTo, nearToClosingTimeFrom, nearToClosingTimeTo, stopLoss, takeProfit, portfolioType, geneticChooserForFitnessDistributionEstimator, numberOfMinimumStdDeviationForOpeningPositions, numberOfMaxStdDeviationForOpeningPositions, estimator, sampleLength); } private bool allTickersAreExchanged(DateTime dateTime, string[] tickers) { bool returnValue = true; try{ for( int i = 0; i < tickers.Length; i++ ) { if(!this.historicalMarketValueProviderForOutOfSample.WasExchanged( tickers[i], dateTime ) ) { returnValue = false; i = tickers.Length; //exit from for } } } catch(Exception ex){ string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; returnValue = false; } return returnValue; } private bool allTickersAreExchangedInTheLastFiveMinutelyBars(DateTime fromDateTime, string[] tickers) { bool returnValue = true; Time lastFiveMinutelyBarTime = this.openingTimesForAvailableBars[this.openingTimesForAvailableBars.Count-1].AddMinutes(-5); try{ for( int i = 0; i < tickers.Length; i++ ) { Bars currentTickerBars = new Bars(tickers[i], Time.GetDateTimeFromMerge(fromDateTime, lastFiveMinutelyBarTime), Time.GetDateTimeFromMerge(fromDateTime, this.openingTimesForAvailableBars[this.openingTimesForAvailableBars.Count-1]), 60); if( currentTickerBars.Rows.Count < 1 ) { returnValue = false; i = tickers.Length; //exit from for } } } catch(Exception ex){ string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; returnValue = false; } return returnValue; } #region newDateTimeEventHandler_closePositions private void newDateTimeEventHandler_closePositions(Time currentDailyTime) { if( allTickersAreExchanged( this.now(), AccountManager.GetTickersInOpenedPositions(this.account) ) ) { AccountManager.ClosePositions( this.account ); this.lastEntryTime = new Time("00:00:00"); } } #endregion newDateTimeEventHandler_closePositions #region newDateTimeEventHandler_openPositions private bool newDateTimeEventHandler_openPositions_bestFitnessIsSignificantlyHigh() { bool returnValue = false; double average, stdDev; if( this.geneticChooserForFitnessDistributionEstimator != null && this.estimator != null ) { average = estimator.GetAverage(this.geneticChooserForFitnessDistributionEstimator, this.currentEligibles, this.returnsManager, 100); stdDev = Math.Sqrt(estimator.GetVariance(this.geneticChooserForFitnessDistributionEstimator, this.currentEligibles, this.returnsManager, 100)); double bestFitness = this.chosenOTCPositions[idxForBestPositionsCompatibleWithPortfolioType].FitnessInSample; double minNumOfStdDev = this.numberOfMinStdDeviationForOpeningPositions; double maxNumOfStdDev = this.numberOfMaxStdDeviationForOpeningPositions; returnValue = bestFitness >= (average + minNumOfStdDev * stdDev) && bestFitness <= (average + maxNumOfStdDev * stdDev); } return returnValue; } private void newDateTimeEventHandler_openPositions(Time currentDailyTime) { if( this.chosenOTCPositions != null && this.allTickersAreExchanged( this.now(), this.chosenOTCPositions[idxForBestPositionsCompatibleWithPortfolioType].WeightedPositions.SignedTickers.Tickers) && this.newDateTimeEventHandler_openPositions_bestFitnessIsSignificantlyHigh() ) // && // this.allTickersAreExchangedInTheLastFiveMinutelyBars( this.now(), this.chosenOTCPositions[idxForBestPositionsCompatibleWithPortfolioType].WeightedPositions.SignedTickers.Tickers ) // ) { try { AccountManager.OpenPositions( this.chosenOTCPositions[idxForBestPositionsCompatibleWithPortfolioType].WeightedPositions, this.account ); this.lastEntryTime = currentDailyTime; this.previousAccountValue = this.account.GetMarketValue(); } catch(Exception ex) { string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; } } } #endregion newDateTimeEventHandler_openPositions #region newDateTimeEventHandler_updateStopLossAndTakeProfitConditions protected virtual void newDateTimeEventHandler_updateStopLossAndTakeProfitConditions(Time currentDailyTime) { //this.previousAccountValue has been set at opening positions this.stopLossConditionReached = false; this.takeProfitConditionReached = false; this.lastProfitOrLossTime = new Time("00:00:00"); if(this.account.Portfolio.Count > 0) { try{ this.currentAccountValue = this.account.GetMarketValue(); double portfolioGainOrLoss = (this.currentAccountValue - this.previousAccountValue) /this.previousAccountValue; if(!double.IsInfinity(portfolioGainOrLoss) && portfolioGainOrLoss <= -this.stopLoss ) { this.stopLossConditionReached = true; this.lastProfitOrLossTime = currentDailyTime; } else if (!double.IsInfinity(portfolioGainOrLoss) && portfolioGainOrLoss >= this.takeProfit ) { this.takeProfitConditionReached = true; this.lastProfitOrLossTime = currentDailyTime; } } catch(Exception ex) {string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + "";} } } #endregion newDateTimeEventHandler_updateStopLossAndTakeProfitConditions public virtual void NewDateTimeEventHandler( Object sender , DateTime dateTime ) { Time currentTime = new Time( dateTime ); bool nearToOpeningTime = currentTime >= this.nearToOpeningTimeFrom && currentTime <= this.nearToOpeningTimeTo; bool nearToClosingTime = currentTime >= this.nearToClosingTimeFrom && currentTime <= this.nearToClosingTimeTo; this.newDateTimeEventHandler_updateStopLossAndTakeProfitConditions(currentTime); bool timeToProfitOrToStopLoss = this.takeProfitConditionReached || this.stopLossConditionReached; if( this.account.Portfolio.Count == 0 && nearToOpeningTime ) this.newDateTimeEventHandler_openPositions(currentTime); if( this.account.Portfolio.Count > 0 && ( nearToClosingTime || timeToProfitOrToStopLoss ) ) this.newDateTimeEventHandler_closePositions(currentTime); if( currentTime == getLastEventTimeWithCachedBars() ) //it's time for new optimization, if the case this.newDateTimeEventHandler_updateTestingPositions( dateTime ); } #region FindPositionsForToday // private double[] findPositionsForToday_writePositionsToLogFile_getFitnesses(TestingPositions[] positionsToWrite) // { // int numOfValidFitnesses = 0; // double[] validFitnesses; // for(int i=0; i<positionsToWrite.Length; i++) // if( positionsToWrite[i] != null && // !double.IsInfinity(positionsToWrite[i].FitnessInSample) && // !double.IsNaN(positionsToWrite[i].FitnessInSample) && // positionsToWrite[i].FitnessInSample != double.MinValue ) // numOfValidFitnesses++; // // validFitnesses = new double[numOfValidFitnesses]; // int addedValidFitnesses = 0; // for(int i=0; i<positionsToWrite.Length; i++) // if( positionsToWrite[i] != null && // !double.IsInfinity(positionsToWrite[i].FitnessInSample) && // !double.IsNaN(positionsToWrite[i].FitnessInSample) && // positionsToWrite[i].FitnessInSample != double.MinValue ) // { // validFitnesses[addedValidFitnesses] = positionsToWrite[i].FitnessInSample; // addedValidFitnesses++; // } // return validFitnesses; // } private void findPositionsForToday_writePositionsToLogFile(DateTime today, EligibleTickers eligibles, TestingPositions[] positionsToWrite) { double averageRandomFitness = estimator.GetAverage(this.geneticChooserForFitnessDistributionEstimator, this.currentEligibles, this.returnsManager, this.sampleLength); double stdDevForRandomFitness = Math.Sqrt(estimator.GetVariance(this.geneticChooserForFitnessDistributionEstimator, this.currentEligibles, this.returnsManager, this.sampleLength)); string pathFile = System.Configuration.ConfigurationManager.AppSettings["LogArchive"] + "\\PositionsForOTCStrategyOn" + today.Day.ToString() + "_" + today.Month.ToString() + "_" + today.Year.ToString() + ".txt"; StreamWriter w = File.AppendText(pathFile); w.WriteLine ("\n----------------------------------------------\r\n"); w.Write("\r\nPositions for OTC Strategy on date: {0}\r", today.ToLongDateString() ); w.Write("\r\nNum days for optimization {0}\r", this.inSampleDays.ToString()); w.Write("\r\nEligibles: {0}\r", eligibles.Count.ToString() ); w.WriteLine ("\n----------------------------------------------"); // for(int i = 0; i<positionsToWrite.Length; i++) if(positionsToWrite[i] != null && positionsToWrite[i].FitnessInSample != double.MinValue) w.WriteLine("\n{0}-Positions: {1} --> fitness {2}", i.ToString(), positionsToWrite[i].WeightedPositions.Description, positionsToWrite[i].FitnessInSample.ToString() ); w.WriteLine ("\n\n----------------------------------------------"); w.WriteLine ("\n\nBest testing positions is {0}", this.idxForBestPositionsCompatibleWithPortfolioType.ToString()); w.Write("\r\nAverage random fitness (sample: {0}): {1}\r", this.sampleLength.ToString(), averageRandomFitness.ToString() ); w.Write("\r\nStandard deviation for random fitness: {0}\r", stdDevForRandomFitness.ToString()); w.Write("\r\nAverage + min num of std deviation ({0}) is: {1}\r", this.numberOfMinStdDeviationForOpeningPositions.ToString() , (averageRandomFitness + this.numberOfMinStdDeviationForOpeningPositions * stdDevForRandomFitness).ToString()); w.Write("\r\nAverage + max num of std deviation ({0}) is: {1}\r", this.numberOfMaxStdDeviationForOpeningPositions.ToString() , (averageRandomFitness + this.numberOfMaxStdDeviationForOpeningPositions * stdDevForRandomFitness).ToString()); // Update the underlying file. w.Flush(); w.Close(); } public void FindPositionsForToday(DateTime today, DateTime lastMarketDay) { History history = this.benchmark.GetEndOfDayHistory( HistoricalEndOfDayTimer.GetMarketOpen( lastMarketDay.AddDays( -this.inSampleDays ) ) , HistoricalEndOfDayTimer.GetMarketClose( lastMarketDay ) ); this.currentEligibles = this.eligiblesSelector.GetEligibleTickers(history); this.updateReturnsManager(history.FirstDateTime, history.LastDateTime); if( ( this.eligiblesSelector is DummyEligibleSelector && this.inSampleChooser != null ) || ( this.currentEligibles.Count > this.minimumNumberOfEligiblesForValidOptimization && this.inSampleChooser != null ) ) { this.chosenOTCPositions = (TestingPositions[])inSampleChooser.AnalyzeInSample(this.currentEligibles, this.returnsManager); this.updateTestingPositions_updateIdxForBestPositionsCompatibleWithPortfolioType(); this.findPositionsForToday_writePositionsToLogFile(today, this.currentEligibles, this.chosenOTCPositions); this.chosenOTCPositions = null; } } #endregion FindPositionsForToday #region UpdateTestingPositions private void updateTestingPositions_updateIdxForBestPositionsCompatibleWithPortfolioType() { for(int i = 0; i<this.chosenOTCPositions.Length; i++) { if(this.chosenOTCPositions[i] != null && ( (this.chosenOTCPositions[i].BothLongAndShortPositions && (this.portfolioType == PortfolioType.ShortAndLong || this.portfolioType == PortfolioType.OnlyMixed) ) || (this.chosenOTCPositions[i].OnlyLongPositions && this.portfolioType == PortfolioType.OnlyLong) || (this.chosenOTCPositions[i].OnlyShortPositions && this.portfolioType == PortfolioType.OnlyShort) ) ) { this.idxForBestPositionsCompatibleWithPortfolioType = i; i = this.chosenOTCPositions.Length;//exit from for } } } protected void updateTestingPositions(DateTime currentDateTime) { History history = this.benchmark.GetEndOfDayHistory( HistoricalEndOfDayTimer.GetMarketOpen( currentDateTime.AddDays( -this.inSampleDays ) ) , HistoricalEndOfDayTimer.GetMarketClose( currentDateTime.AddDays(-this.numDaysBeforeCurrentDateForRetrievingInSampleData) ) ); this.currentEligibles = this.eligiblesSelector.GetEligibleTickers(history); this.updateReturnsManager(history.FirstDateTime, history.LastDateTime); if( ( this.eligiblesSelector is DummyEligibleSelector && this.inSampleChooser != null ) || ( this.currentEligibles.Count > this.minimumNumberOfEligiblesForValidOptimization && this.inSampleChooser != null ) ) { this.chosenOTCPositions = (TestingPositions[])inSampleChooser.AnalyzeInSample(this.currentEligibles, this.returnsManager); this.updateTestingPositions_updateIdxForBestPositionsCompatibleWithPortfolioType(); this.logOptimizationInfo(this.currentEligibles); } } private bool optimalTestingPositionsAreToBeUpdated() { bool areToBeUpdated = false; if(this.inSampleChooser != null) { DateTime dateTimeForNextOptimization = this.lastOptimizationDateTime.AddDays( this.numDaysBetweenEachOptimization ); areToBeUpdated = ( ( ( this.account.Portfolio.Count == 0 ) && ( ( this.lastOptimizationDateTime == DateTime.MinValue ) ) ) || ( this.now() >= dateTimeForNextOptimization ) ); } return areToBeUpdated; } private void newDateTimeEventHandler_updateTestingPositions( DateTime dateTime ) { if ( this.optimalTestingPositionsAreToBeUpdated() ) { this.updateTestingPositions( dateTime ); this.lastOptimizationDateTime = this.now(); } } #endregion UpdateTestingPositions private DateTime now() { return this.account.Timer.GetCurrentDateTime(); } protected virtual void updateReturnsManager(DateTime firstDateTime, DateTime lastDayDateTime) { ReturnIntervals returnIntervals = // new DailyOpenToCloseIntervals( firstDateTime, lastDayDateTime, // this.benchmark.Ticker ); new OpenToCloseCloseToOpenIntervals( firstDateTime, lastDayDateTime, this.benchmark.Ticker ); this.returnsManager = new ReturnsManager( returnIntervals , this.historicalMarketValueProviderForInSample); } private OTCIntradayLogItem getLogItem( EligibleTickers eligibleTickers ) { OTCIntradayLogItem logItem = new OTCIntradayLogItem(this.now(), this.inSampleDays); logItem.BestOTCPositionsInSample = this.chosenOTCPositions; logItem.NumberOfEligibleTickers = eligibleTickers.Count; logItem.Fitness = this.chosenOTCPositions[idxForBestPositionsCompatibleWithPortfolioType].FitnessInSample; logItem.Generation = ((IGeneticallyOptimizable)this.chosenOTCPositions[idxForBestPositionsCompatibleWithPortfolioType]).Generation; logItem.Tickers = this.chosenOTCPositions[idxForBestPositionsCompatibleWithPortfolioType].HashCodeForTickerComposition; return logItem; } private void raiseNewLogItem( EligibleTickers eligibleTickers ) { OTCIntradayLogItem logItem = this.getLogItem( eligibleTickers ); NewLogItemEventArgs newLogItemEventArgs = new NewLogItemEventArgs( logItem ); this.NewLogItem( this , newLogItemEventArgs ); } private void notifyMessage( EligibleTickers eligibleTickers ) { string message = "Number of Eligible tickers: " + eligibleTickers.Count; NewMessageEventArgs newMessageEventArgs = new NewMessageEventArgs( message ); if ( this.NewMessage != null ) this.NewMessage( this , newMessageEventArgs ); } private void logOptimizationInfo( EligibleTickers eligibleTickers ) { if(eligibleTickers.Count > 0) this.raiseNewLogItem( eligibleTickers ); this.notifyMessage( eligibleTickers ); } } } --- NEW FILE: OTCIntradayMain.cs --- /* QuantProject - Quantitative Finance Library OTCIntradayMain.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections; using System.Collections.Generic; using System.IO; using QuantProject.ADT; using QuantProject.ADT.Statistics.Combinatorial; using QuantProject.ADT.FileManaging; using QuantProject.ADT.Timing; using QuantProject.Business.DataProviders; using QuantProject.Data.DataProviders.Bars.Caching; using QuantProject.Business.Strategies; using QuantProject.Business.Financial.Accounting.AccountProviding; using QuantProject.Business.Strategies.Eligibles; using QuantProject.Business.Strategies.EquityEvaluation; using QuantProject.Business.Strategies.InSample; using QuantProject.Business.Strategies.InSample.InSampleFitnessDistributionEstimation; using QuantProject.Business.Strategies.Logging; using QuantProject.Business.Strategies.Optimizing.Decoding; using QuantProject.Business.Strategies.Optimizing.GenomeManagers; using QuantProject.Business.Strategies.Optimizing.FitnessEvaluation; using QuantProject.Business.Strategies.ReturnsManagement; using QuantProject.Business.Strategies.ReturnsManagement.Time; using QuantProject.Business.Strategies.ReturnsManagement.Time.IntervalsSelectors; using QuantProject.Business.Timing; using QuantProject.Presentation; //using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.InSampleChoosers; //using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.Decoding; //using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.FitnessEvaluators; using QuantProject.Scripts.TickerSelectionTesting.OTC.InSampleChoosers; using QuantProject.Scripts.General; using QuantProject.Scripts.General.Logging; using QuantProject.Scripts.General.Reporting; using QuantProject.Scripts.General.Strategies.Optimizing.FitnessEvaluation; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; using QuantProject.Scripts.TickerSelectionTesting.OTC.InSampleChoosers.Genetic; using QuantProject.Scripts.TickerSelectionTesting.OTC.InSampleChoosers.BruteForce; namespace QuantProject.Scripts.TickerSelectionTesting.OTC.OTC_Intraday { /// <summary> /// Entry point for the OTCIntradayMain. If any strategy /// parameter had to be changed, this is the place where it should /// be done /// </summary> public class OTCIntradayMain : BasicScriptForBacktesting { private int numberOfPortfolioPositions; private PortfolioType portfolioType; private int maxNumberOfEligiblesToBeChosen; private Benchmark benchmark; private DateTime firstDateTime; private DateTime lastDateTime; List<Time> dailyTimes;//intraday times for barCache and Timer private Time nearToOpeningTimeFrom; private Time nearToOpeningTimeTo; private Time nearToClosingTimeFrom; private Time nearToClosingTimeTo; private HistoricalMarketValueProvider historicalMarketValueProviderForInSample; private HistoricalMarketValueProvider historicalMarketValueProviderForOutOfSample; private HistoricalMarketValueProvider historicalMarketValueProviderForTheBackTester; //private int stepInMinutesForTimer; private Timer timerForBackTester; private int intervalFrameInSeconds; private GenomeManagerType genomeManagerType; #region main public OTCIntradayMain() { this.numberOfPortfolioPositions = 2; // this.benchmark = new Benchmark( "CCE" ); this.portfolioType = PortfolioType.ShortAndLong;//filter for out of sample this.genomeManagerType = GenomeManagerType.ShortAndLong;//filter for the genetic chooser this.benchmark = new Benchmark( "ENI.MI" ); this.firstDateTime = new DateTime( 2000 , 1 , 1 ); this.lastDateTime = new DateTime( 2009 , 8, 27 ); //this.stepInMinutesForTimer = 1; this.intervalFrameInSeconds = 60; // this.dailyTimes = Time.GetIntermediateTimes(new Time("09:30:00"), // new Time("16:00:00"), // this.stepInMinutesForTimer); this.dailyTimes = new List<Time>(); this.dailyTimes.Add(new Time("09:30:00")); this.dailyTimes.Add(new Time("09:31:00")); this.dailyTimes.Add(new Time("09:32:00")); this.dailyTimes.Add(new Time("09:33:00")); this.dailyTimes.Add(new Time("09:34:00")); this.dailyTimes.Add(new Time("09:35:00")); this.dailyTimes.Add(new Time("15:55:00")); this.dailyTimes.Add(new Time("15:56:00")); this.dailyTimes.Add(new Time("15:57:00")); this.dailyTimes.Add(new Time("15:58:00")); this.dailyTimes.Add(new Time("15:59:00")); this.dailyTimes.Add(new Time("16:00:00")); this.nearToOpeningTimeFrom = new Time("09:30:00"); this.nearToOpeningTimeTo = new Time("09:35:00"); this.nearToClosingTimeFrom = new Time("15:55:00"); this.nearToClosingTimeTo = new Time("16:00:00"); this.historicalMarketValueProviderForInSample = // new HistoricalRawQuoteProvider(); new HistoricalAdjustedQuoteProvider(); this.historicalMarketValueProviderForOutOfSample = // this.getHistoricalBarProvider(); // this.historicalMarketValueProviderForInSample; new HistoricalRawQuoteProvider(); this.historicalMarketValueProviderForTheBackTester = this.historicalMarketValueProviderForOutOfSample; //new HistoricalBarProvider( //new SimpleBarCache( intervalFrameInSeconds, //BarComponent.Open ) ); //ricordarsi di togliere - mettere //commento nel gestore evento tempo this.timerForBackTester = new IndexBasedEndOfDayTimer( this.firstDateTime, this.lastDateTime, this.benchmark.Ticker); // new IndexBasedHistoricalTimer(this.benchmark.Ticker, // this.firstDateTime, // this.lastDateTime , // this.dailyTimes, // this.intervalFrameInSeconds); } #endregion main #region eligiblesSelector protected override IEligiblesSelector getEligiblesSelector() { this.maxNumberOfEligiblesToBeChosen = 50; // string tickersGroupId = "SP500"; string tickersGroupId = "STOCKMI"; bool temporizedGroup = true; int numDaysForAverageRawOpenPriceComputation = 10; double minPrice = 0.10; double maxPrice = 2000; // int maxNumberOfMostLiquidTickersToBeChosen = 150; // int numDaysForVolatility = 10; // IEligiblesSelector eligiblesSelector = // new ByPriceMostLiquidQuotedAtEachDateTime( // tickersGroupId , temporizedGroup , // maxNumberOfEligiblesToBeChosen , // numDaysForAverageRawOpenPriceComputation , // minPrice , maxPrice, intervalFrameInSeconds , // this.benchmark.Ticker ); // // IEligiblesSelector eligiblesSelector = // new ByPriceMostLiquidLessVolatileOTCAlwaysQuoted( // tickersGroupId , temporizedGroup , // maxNumberOfEligiblesToBeChosen , // maxNumberOfMostLiquidTickersToBeChosen , // numDaysForAverageRawOpenPriceComputation , // numDaysForVolatility , // minPrice , maxPrice ); IEligiblesSelector eligiblesSelector = new ByPriceMostLiquidAlwaysQuoted( tickersGroupId , temporizedGroup , maxNumberOfEligiblesToBeChosen , numDaysForAverageRawOpenPriceComputation , minPrice , maxPrice ); // IEligiblesSelector eligiblesSelector = // new ByPriceLessVolatileOTCAlwaysQuoted( // tickersGroupId , temporizedGroup , // maxNumberOfEligiblesToBeChosen , // numDaysForAverageRawOpenPriceComputation , // minPrice , maxPrice ); // eligiblesSelector = // new DummyEligibleSelector(); // return eligiblesSelector; } #endregion eligiblesSelector #region inSampleChooser protected override IInSampleChooser getInSampleChooser() { // Combination combinations; // if(this.genomeManagerType == GenomeManagerType.ShortAndLong || // this.genomeManagerType == GenomeManagerType.OnlyMixed) // combinations = new Combination(-this.maxNumberOfEligiblesToBeChosen, this.maxNumberOfEligiblesToBeChosen - 1, this.numberOfPortfolioPositions); // else // combinations = new Combination(0, this.maxNumberOfEligiblesToBeChosen - 1, this.numberOfPortfolioPositions); // // int numberOfBestTestingPositionsToBeReturned = // (int)combinations.TotalNumberOfCombinations; int numberOfBestTestingPositionsToBeReturned = 20; // parameters for the genetic optimizer // double crossoverRate = 0.85; // double mutationRate = 0.02; // double elitismRate = 0.001; // int populationSizeForGeneticOptimizer = 5000; // int generationNumberForGeneticOptimizer = 0; // int seedForRandomGenerator = // QuantProject.ADT.ConstantsProvider.SeedForRandomGenerator; IDecoderForTestingPositions decoderForTestingPositions = new BasicDecoderForOTCPositions(); IFitnessEvaluator fitnessEvaluator = // new OTCFitnessEvaluator( new SharpeRatio() ); new OTCCTOFitnessEvaluator( new SharpeRatio() ); // new DummyRandomFitnessEvaluator(); // IInSampleChooser inSampleChooser = // new OTCIntradayGeneticChooser(numberOfBestTestingPositionsToBeReturned, // 1 , balancedWeightsOnVolatilityBase, // minimumAbsoluteReturnValue , maximumAbsoluteReturnValue, this.benchmark.Ticker); // IInSampleChooser inSampleChooser = // new OTCEndOfDayGeneticChooser(this.numberOfPortfolioPositions, numberOfBestTestingPositionsToBeReturned, // benchmark, decoderForTestingPositions , // this.genomeManagerType , // fitnessEvaluator , // historicalMarketValueProviderForInSample, crossoverRate, // mutationRate, elitismRate , populationSizeForGeneticOptimizer, // generationNumberForGeneticOptimizer, seedForRandomGenerator); IInSampleChooser inSampleChooser = new OTCEndOfDayBruteForceChooser(this.portfolioType, this.numberOfPortfolioPositions, numberOfBestTestingPositionsToBeReturned, this.benchmark, decoderForTestingPositions , fitnessEvaluator, historicalMarketValueProviderForInSample); // //office // inSampleChooser = // new PVOChooserFromSavedBackTestLog( // @"C:\Utente\MarcoVarie\Vari\qP\LogArchive\2009_01_05_13_40_28_PVOIntraday_from_2006_01_01_to_2007_12_31_annlRtrn_17,84_maxDD_4,52\2009_01_05_13_40_28_PVOIntraday_from_2006_01_01_to_2007_12_31_annlRtrn_17,84_maxDD_4,52.qpL", // numberOfBestTestingPositionsToBeReturned); //home // inSampleChooser = // new PVOChooserFromSavedBackTestLog( // @"C:\Utente\MarcoVarie\Vari\qP\LogArchive\2009_01_01_18_57_36_PVOIntraday_from_2006_01_01_to_2007_12_31_annlRtrn_17.11_maxDD_3.80\2009_01_01_18_57_36_PVOIntraday_from_2006_01_01_to_2007_12_31_annlRtrn_17.11_maxDD_3.80.qpL", // numberOfBestTestingPositionsToBeReturned); return inSampleChooser; } #endregion inSampleChooser #region strategy protected override IStrategyForBacktester getStrategyForBacktester() { //int inSampleDays = 90; int inSampleDays = 45; int numDaysBetweenEachOptimization = 1; int numDaysBeforeCurrentDateForRetrievingInSampleData = 0; int minNumOfEligiblesForValidOptimization = 10; int sampleLengthForFitnessDistributionEstimation = 100; double minimumNumberOfStdDevForSignificantFitness = 0.0; double maximumNumberOfStdDevForSignificantFitness = 8.0; IInSampleFitnessDistributionEstimator estimator = new BasicInSampleFitnessDistributionEstimator(); GeneticChooser geneticChooserForEstimator = new OTCEndOfDayGeneticChooser(this.numberOfPortfolioPositions, 50, this.benchmark, new BasicDecoderForOTCPositions(), this.genomeManagerType , new OTCCTOFitnessEvaluator( new SharpeRatio() ) , historicalMarketValueProviderForInSample, 0.85, 0.01, 0.001 , 100, 0, QuantProject.ADT.ConstantsProvider.SeedForRandomGenerator); double stopLoss = 0.015; double takeProfit = 0.03; IStrategyForBacktester strategyForBacktester // = new PVO_OTCStrategyLessCorrelated(eligiblesSelector ,inSampleChooser , // inSampleDays , benchmark , numDaysBetweenEachOptimization , // oversoldThreshold , overboughtThreshold , historicalQuoteProvider); // = new OTCIntradayStrategy(eligiblesSelector , minNumOfEligiblesForValidOptimization, inSampleChooser , inSampleDays , benchmark , numDaysBetweenEachOptimization , numDaysBeforeCurrentDateForRetrievingInSampleData, historicalMarketValueProviderForInSample, historicalMarketValueProviderForOutOfSample, this.dailyTimes, this.nearToOpeningTimeFrom, this.nearToOpeningTimeTo, this.nearToClosingTimeFrom, this.nearToClosingTimeTo, stopLoss , takeProfit, this.portfolioType, geneticChooserForEstimator, minimumNumberOfStdDevForSignificantFitness, maximumNumberOfStdDevForSignificantFitness, estimator, sampleLengthForFitnessDistributionEstimation); ((OTCIntradayStrategy)strategyForBacktester).FindPositionsForToday( new DateTime(2009,8,31), new DateTime(2009,8,28) ); return strategyForBacktester; } #endregion strategy #region backTester protected override EndOfDayStrategyBackTester getEndOfDayStrategyBackTester() { string backTestId = "OTCIntradayStrategy"; IAccountProvider accountProvider; accountProvider = new SimpleAccountProvider(); // double fixedPercentageSlippage = 0.05; // accountProvider = // new InteractiveBrokerAccountProvider(fixedPercentageSlippage); double cashToStart = 10000; double maxRunningHours = 15; EndOfDayStrategyBackTester endOfDayStrategyBackTester = new EndOfDayStrategyBackTester( backTestId , this.timerForBackTester, this.strategyForBacktester , this.historicalMarketValueProviderForTheBackTester , accountProvider , firstDateTime , lastDateTime , this.benchmark , cashToStart , maxRunningHours ); return endOfDayStrategyBackTester; } #endregion backTester #region getHistoricalBarProvider #region getBarCache private IBarCache getBarCache() { IBarCache barCache = new DailyBarCache( this.intervalFrameInSeconds , dailyTimes ); return barCache; } #endregion getBarCache private HistoricalBarProvider getHistoricalBarProvider() { IBarCache barCache = getBarCache(); HistoricalBarProvider historicalBarProvider = new HistoricalBarProvider( barCache ); return historicalBarProvider; } #endregion getHistoricalBarProvider protected override string getCustomSmallTextForFolderName() { return "OTCIntradayStrategy"; } protected override string getPathForTheMainFolderWhereScriptsResultsAreToBeSaved() { string pathForTheMainFolderWhereScriptsResultsAreToBeSaved = System.Configuration.ConfigurationManager.AppSettings["LogArchive"]; return pathForTheMainFolderWhereScriptsResultsAreToBeSaved; } protected override string getFullPathFileNameForMain() { string returnValue; string fullPathFileNameForMainAtHome = @"C:\Quant\QuantProject\b7_Scripts\TickerSelectionTesting\OTC\OTC_Intraday\OTCIntradayMain.cs"; if( File.Exists(fullPathFileNameForMainAtHome) ) returnValue = fullPathFileNameForMainAtHome; else returnValue = @"C:\Utente\MarcoVarie\Vari\qP\QuantProject\b7_Scripts\TickerSelectionTesting\OTC\OTC_Intraday\OTCIntradayMain.cs"; return returnValue; } } } --- NEW FILE: OTCIntradayLogItem.cs --- /* QuantProject - Quantitative Finance Library OTCIntradayLogItem.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections.Generic; using System.Windows.Forms; using QuantProject.ADT; using QuantProject.ADT.Timing; using QuantProject.Data.DataProviders.Bars.Caching; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies; using QuantProject.Business.Financial.Accounting.AccountProviding; using QuantProject.Business.Strategies.Eligibles; using QuantProject.Business.Strategies.InSample; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies.Logging; using QuantProject.Business.Strategies.ReturnsManagement.Time; using QuantProject.Business.Strategies.ReturnsManagement.Time.IntervalsSelectors; using QuantProject.Scripts.TickerSelectionTesting.OTC.OTC_Intraday; using QuantProject.Business.Timing; using QuantProject.Presentation; using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; using QuantProject.Scripts.General.Reporting; using QuantProject.Scripts.General.Logging; namespace QuantProject.Scripts.TickerSelectionTesting.OTC.OTC_Intraday { /// <summary> /// LogItem for the Open To Close strategy /// It is "intraday" because the run method /// uses intraday bars /// </summary> [Serializable] public class OTCIntradayLogItem : LogItem { protected DummyTesterForTestingPositions[] dummyTestersForBestTestingPositionsInSample; protected TestingPositions[] bestOTCPositionsInSample; protected int numberOfEligibleTickers; protected double fitness; protected int generation; protected string tickers; protected int numberOfInSampleDays; public TestingPositions[] BestOTCPositionsInSample { get { if ( this.bestOTCPositionsInSample == null ) throw new Exception( "This property has not " + "been assigned yet! If you are loading the LogItem from " + "a log, this property was not set before logging the LogItem." ); return this.bestOTCPositionsInSample; } set { this.bestOTCPositionsInSample = value; } } public int NumberOfEligibleTickers { get { if ( this.numberOfEligibleTickers == int.MinValue ) throw new Exception( "This property has not " + "been assigned yet! If you are loading the LogItem from " + "a log, this property was not set before logging the LogItem." ); return this.numberOfEligibleTickers; } set { this.numberOfEligibleTickers = value; } } public double Fitness { get { if ( this.fitness == double.MinValue ) throw new Exception( "This property has not " + "been assigned yet! If you are loading the LogItem from " + "a log, this property was not set before logging the LogItem." ); return this.fitness; } set { this.fitness = value; } } public int Generation { get{return this.generation;} set{this.generation = value;} } public string Tickers { get{return this.tickers;} set{this.tickers = value;} } public OTCIntradayLogItem(DateTime dateTime, int numberOfInSampleDays) : base( dateTime ) { this.numberOfInSampleDays = numberOfInSampleDays; this.numberOfEligibleTickers = int.MinValue; this.fitness = double.MinValue; } protected virtual void runStrategyClickEventHandler(object sender, System.EventArgs e) { //general DateTime firstDateTime = this.SimulatedCreationDateTime.AddDays(-this.numberOfInSampleDays); DateTime lastDateTime = this.SimulatedCreationDateTime; double maxRunningHours = 1; Benchmark benchmark = new Benchmark( "CCE" ); int numberOfPortfolioPositions = this.BestOTCPositionsInSample[0].WeightedPositions.Count; //cash and portfolio type double cashToStart = 30000; HistoricalMarketValueProvider historicalQuoteProviderForBackTester, historicalQuoteProviderForInSampleChooser, historicalQuoteProviderForStrategy; historicalQuoteProviderForBackTester = // new HistoricalBarProvider( // new SimpleBarCache( 60, // BarComponent.Open ) ); new HistoricalRawQuoteProvider(); historicalQuoteProviderForInSampleChooser = historicalQuoteProviderForBackTester; historicalQuoteProviderForStrategy = historicalQuoteProviderForInSampleChooser; IEligiblesSelector eligiblesSelector = new DummyEligibleSelector(); //strategyParameters TestingPositions[] positionsToTest = this.BestOTCPositionsInSample; // new PreviousPeriodsWereEfficientEntryCondition(1, historicalQuoteProviderForStrategy, // numDaysForOscillatingPeriodForOutOfSample * 24 *60, // new MarketDateTimeManager(benchmark, firstDateTime, lastDateTime, 60) ); OTCIntradayStrategy strategy = new OTCIntradayStrategy(eligiblesSelector, positionsToTest, benchmark, historicalQuoteProviderForInSampleChooser, historicalQuoteProviderForBackTester, Time.GetIntermediateTimes(new Time("09:30:00"), new Time("16:00:00"), 1 ), 0.5, 0.5, PortfolioType.ShortAndLong, null, 2, 3, null, 100); QuantProject.Business.Timing.Timer timer = // new QuantProject.Business.Timing.IndexBasedHistoricalTimer( // benchmark.Ticker, firstDateTime , lastDateTime, // Time.GetIntermediateTimes(new Time("09:30:00"), // new Time("16:00:00"), 1 ) , // 60); new IndexBasedEndOfDayTimer( firstDateTime, lastDateTime.AddDays(10), benchmark.Ticker); EndOfDayStrategyBackTester endOfDayStrategyBackTester = new EndOfDayStrategyBackTester( "OTC" , timer , strategy, historicalQuoteProviderForBackTester , new SimpleAccountProvider(), firstDateTime , lastDateTime , benchmark , cashToStart , maxRunningHours ); endOfDayStrategyBackTester.Run(); BackTesterReportViewer.ShowReport( lastDateTime , endOfDayStrategyBackTester ); } private void showTestingPositionsClickEventHandler_setDummyTesters_setTester( int currentIndex , TestingPositions testingPositions , DateTime simulatedCreationDateTime ) { this.dummyTestersForBestTestingPositionsInSample[ currentIndex ] = new DummyTesterForTestingPositions( testingPositions , this.numberOfInSampleDays , simulatedCreationDateTime ); } private void showTestingPositionsClickEventHandler_setDummyTesters() { this.dummyTestersForBestTestingPositionsInSample = new DummyTesterForTestingPositions[this.BestOTCPositionsInSample.Length]; for ( int i = 0 ; i < BestOTCPositionsInSample.Length; i++ ) this.showTestingPositionsClickEventHandler_setDummyTesters_setTester( i , BestOTCPositionsInSample[ i ] , this.SimulatedCreationDateTime ); } protected virtual void showTestingPositionsClickEventHandler(object sender, System.EventArgs e) { this.showTestingPositionsClickEventHandler_setDummyTesters(); QuantProject.Presentation.ExecutablesListViewer executablesListViewer = new ExecutablesListViewer( this.dummyTestersForBestTestingPositionsInSample ); executablesListViewer.Show(); } protected virtual void createAndShowContextMenu() { MenuItem[] menuItems = new MenuItem[2]; menuItems[0] = new MenuItem("Run Strategy"); menuItems[1] = new MenuItem("Show TestingPositions"); menuItems[0].Click += new System.EventHandler(this.runStrategyClickEventHandler); menuItems[1].Click += new System.EventHandler(this.showTestingPositionsClickEventHandler); ContextMenu contextMenu = new ContextMenu(menuItems); contextMenu.Show(Form.ActiveForm, Form.MousePosition); } public override void Run() { this.createAndShowContextMenu(); } } } |