[Quantproject-developers] Re: [Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTest
Brought to you by:
glauco_1
|
From: Glauco S. <gl...@my...> - 2005-12-28 23:49:36
|
Come faccio ad usarlo? C'e' una chiamata nel main da "scommentare"? Vedo dal log che si basa sui genomi. Credo sarebbe molto utile integrarlo con un sistema che ho fatto io per la one rank: si preme tasto destro sul DataGrid delle transazioni e automaticamente un oggetto si calcola l'array di signed tickers che sta "piu' vicino" al punto del click (in pratica risale al genoma). E' utile quando si recupera un account per il quale non sono stati salvati i genomi, oppure quando si vede un punto interessante nel grafico e si vuole analizzare l'andamento del genoma corrispondente: basta andare nel DataGrid delle transazioni e clickare col destro. Se mi fai vedere un esempio di come usare RunEfficientOTCTypes partendo da un genoma, aggiungo io il codice per fare il tutto (partendo dalle transazioni nel report). At 11:22 PM 12/28/2005 +0000, you wrote: >Update of >/cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/TestingOTCTypes >In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26303/TestingOTCTypes > >Added Files: > RunEfficientOTCTypes.cs EndOfDayTimerHandlerOTCTypes.cs >Log Message: >Added files for testing optimized OpenToClose genomes >in several manners (daily, multiday, OTC - CTO combined, only CTO, etc.) > >--- NEW FILE: RunEfficientOTCTypes.cs --- >/* >QuantProject - Quantitative Finance Library > >RunEfficientOTCTypes.cs >Copyright (C) 2003 >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.Data; >using QuantProject.ADT; >using QuantProject.ADT.Optimizing.Genetic; >using QuantProject.ADT.FileManaging; >using QuantProject.Business.DataProviders; >using QuantProject.Business.Financial.Accounting; >using QuantProject.Business.Financial.Accounting.Reporting; >using QuantProject.Business.Financial.Instruments; >using QuantProject.Business.Financial.Ordering; >using QuantProject.Business.Scripting; >using QuantProject.Business.Strategies; >using QuantProject.Business.Testing; >using QuantProject.Business.Timing; >using QuantProject.Business.Financial.Accounting.Commissions; >using QuantProject.Data.DataProviders; >using QuantProject.Data.Selectors; >using QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios; >using QuantProject.Presentation.Reporting.WindowsForm; >using QuantProject.Scripts.WalkForwardTesting.LinearCombination; > > >namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios >{ > > /// <summary> > /// Script to test OTC daily, OTC multiday and OTC - CTO > /// all together with one optimization and 3 accounts (each for > /// each type of strategy) > /// </summary> > [Serializable] > public class RunEfficientOTCTypes : RunEfficientPortfolio > { > protected int numDaysBetweenEachOptimization; > private Account[] accounts; > > public RunEfficientOTCTypes(string tickerGroupID, int > numberOfEligibleTickers, > int numberOfTickersToBeChosen, int numDaysForOptimizationPeriod, > int generationNumberForGeneticOptimizer, > int populationSizeForGeneticOptimizer, string benchmark, > DateTime startDate, DateTime endDate, double targetReturn, > PortfolioType portfolioType, double maxRunningHours, > int numDaysBetweenEachOptimization): > base(tickerGroupID, numberOfEligibleTickers, > numberOfTickersToBeChosen, numDaysForOptimizationPeriod, > generationNumberForGeneticOptimizer, > populationSizeForGeneticOptimizer, benchmark, > startDate, endDate, targetReturn, > portfolioType, maxRunningHours) > { > this.ScriptName = "OTC_Types"; > this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; > this.accounts = new Account[4]; > } > > #region auxiliary overriden methods for Run > > > protected override void run_initializeAccount() > { > for(int i = 0; i<this.accounts.Length; i++) > { > this.accounts[i] = new Account( this.ScriptName, this.endOfDayTimer , > new HistoricalEndOfDayDataStreamer( > this.endOfDayTimer , > this.historicalQuoteProvider ) , > new HistoricalEndOfDayOrderExecutor( > this.endOfDayTimer , > this.historicalQuoteProvider )); > } > > } > > > protected override void run_initializeEndOfDayTimerHandler() > { > this.endOfDayTimerHandler = new > EndOfDayTimerHandlerOTCTypes(this.tickerGroupID, > this.numberOfEligibleTickers, > this.numberOfTickersToBeChosen, > this.numDaysForOptimizationPeriod, > this.generationNumberForGeneticOptimizer, > this.populationSizeForGeneticOptimizer, > this.benchmark, > this.targetReturn, > this.portfolioType, this.numDaysBetweenEachOptimization, > this.accounts); > } > > protected override void run_initializeHistoricalQuoteProvider() > { > //this.historicalQuoteProvider = new HistoricalRawQuoteProvider(); > this.historicalQuoteProvider = new HistoricalAdjustedQuoteProvider(); > } > > protected override void run_addEventHandlers() > { > this.endOfDayTimer.MarketOpen += > new MarketOpenEventHandler( > this.endOfDayTimerHandler.MarketOpenEventHandler); > > this.endOfDayTimer.MarketClose += > new MarketCloseEventHandler( > this.endOfDayTimerHandler.MarketCloseEventHandler); > > this.endOfDayTimer.MarketClose += > new MarketCloseEventHandler( > this.checkDateForReport); > > this.endOfDayTimer.OneHourAfterMarketClose += > new OneHourAfterMarketCloseEventHandler( > this.endOfDayTimerHandler.OneHourAfterMarketCloseEventHandler ); > } > #endregion > > //necessary far calling RunEfficientPortfolio.Run() > //in classes that inherit from this class > public override void Run() > { > base.Run(); > } > public override void SaveScriptResults() > { > string fileName = "From"+this.numberOfEligibleTickers + > "OptDays" + this.numDaysForOptimizationPeriod + "Portfolio" + > this.numberOfTickersToBeChosen + "GenNum" + > this.generationNumberForGeneticOptimizer + > "PopSize" + this.populationSizeForGeneticOptimizer + > "Target" + Convert.ToString(this.targetReturn) + > Convert.ToString(this.portfolioType); > string dirNameWhereToSaveAccounts = > System.Configuration.ConfigurationSettings.AppSettings["AccountsArchive"] + > "\\" + this.ScriptName + "\\"; > string dirNameWhereToSaveTransactions = > System.Configuration.ConfigurationSettings.AppSettings["TransactionsArchive"] + > "\\" + this.ScriptName + "\\"; > string dirNameWhereToSaveBestGenomes = > System.Configuration.ConfigurationSettings.AppSettings["GenomesArchive"] + > "\\" + this.ScriptName + "\\"; > > >this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveAccounts); > >this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveTransactions); > >this.checkDateForReport_createDirIfNotPresent(dirNameWhereToSaveBestGenomes); > > for(int i = 0; i<this.accounts.Length; i++) > { > ObjectArchiver.Archive(accounts[i], > dirNameWhereToSaveAccounts + > fileName + "#" + i.ToString() + ".qPa"); > ObjectArchiver.Archive(this.accounts[i].Transactions, > dirNameWhereToSaveTransactions + > fileName + "#" + i.ToString() + ".qPt"); > } > OptimizationOutput optimizationOutput = new OptimizationOutput(); > foreach(GenomeRepresentation genomeRepresentation in > this.endOfDayTimerHandler.BestGenomes) > optimizationOutput.Add(genomeRepresentation); > ObjectArchiver.Archive(optimizationOutput, > dirNameWhereToSaveBestGenomes + > fileName + ".bgn"); > this.endOfDayTimer.Stop(); > } > } >} > >--- NEW FILE: EndOfDayTimerHandlerOTCTypes.cs --- >/* >QuantProject - Quantitative Finance Library > >EndOfDayTimerHandlerOTCTypes.cs >Copyright (C) 2003 >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 QuantProject.ADT; >using QuantProject.Business.Financial.Accounting; >using QuantProject.Business.Financial.Instruments; >using QuantProject.Business.Financial.Ordering; >using QuantProject.Business.Timing; >using QuantProject.Data.DataProviders; >using QuantProject.Data.Selectors; >using QuantProject.ADT.Optimizing.Genetic; > >namespace QuantProject.Scripts.TickerSelectionTesting.EfficientPortfolios >{ > > /// <summary> > /// Implements MarketOpenEventHandler, > /// MarketCloseEventHandler and OneHourAfterMarketCloseEventHandler > /// These handlers run all the > /// OTC strategy types after a common optimization, for testing purposes > /// </summary> > [Serializable] > public class EndOfDayTimerHandlerOTCTypes : EndOfDayTimerHandler > { > private int numDaysBetweenEachOptimization; > private int numDaysElapsedSinceLastOptimization; > private int seedForRandomGenerator; > private Account[] accounts; > //private ArrayList[] ordersForAccounts; > > public EndOfDayTimerHandlerOTCTypes(string tickerGroupID, int > numberOfEligibleTickers, > int numberOfTickersToBeChosen, int > numDaysForOptimizationPeriod, > int generationNumberForGeneticOptimizer, > int populationSizeForGeneticOptimizer, > string benchmark, double targetReturn, > PortfolioType portfolioType, int > numDaysBetweenEachOptimization, > Account[] accounts): > base(tickerGroupID, numberOfEligibleTickers, > numberOfTickersToBeChosen, > numDaysForOptimizationPeriod, accounts[0], > generationNumberForGeneticOptimizer, > populationSizeForGeneticOptimizer, > benchmark, targetReturn, > portfolioType) > { > this.numDaysBetweenEachOptimization = numDaysBetweenEachOptimization; > this.numDaysElapsedSinceLastOptimization = 0; > this.seedForRandomGenerator = > ConstantsProvider.SeedForRandomGenerator; > this.accounts = accounts; > //for(int i = 0; i<this.accounts.Length;i++) > //ordersForAccounts[i] = new ArrayList(); > } > >// protected override void addChosenTickersToOrderList() >// { >// for(int i = 0; i<this.accounts.Length; i++) >// { >// if(i==0)//OTC daily >// {} >// if(i==1) >// {} >// if(i==2) >// {} >// for(int j = 0; j<this.numberOfTickersToBeChosen; j++) >// { >// string ticker = this.chosenTickersForAccounts[i,j]; >// if( ticker != null) >// { >// this.addOrderForTickerForEachAccount(i, ticker ); >// this.lastOrderedTickersForAccounts[i,j] = >// GenomeManagerForEfficientPortfolio.GetCleanTickerCode(ticker); >// } >// } >// } >// } > private void openPositions_openWhenPortfolioIsEmpty(int accountNumber) > { > if(this.accounts[accountNumber].Portfolio.Count == 0) > { > foreach(object item in this.orders) > this.accounts[accountNumber].AddOrder((Order)item); > } > } > > protected override void openPositions() > { > this.addChosenTickersToOrderList(); > for(int i = 0; i<this.accounts.Length; i++) > { > //add cash first for each account > if(this.orders.Count == 0 && this.accounts[i].Transactions.Count > == 0) > this.accounts[i].AddCash(30000); > > if(i<=1)//daily classical, and multiday > this.openPositions_openWhenPortfolioIsEmpty(i); > else if(i==2)//for the CTO OTC > { > this.closePositions_close(i); > foreach(object item in this.orders) > this.accounts[i].AddOrder((Order)item); > } > else if(i==3)//for the CTO, only at night > { > this.closePositions_close(i); > } > } > } > > > private void closePositions_close(int accountNumber) > { > string ticker; > if(this.accounts[accountNumber].Portfolio.Count >0) > { > for(int j = 0; j<this.lastOrderedTickers.Length; j++) > { > ticker = this.lastOrderedTickers[j]; > if( ticker != null) > { > if(this.accounts[accountNumber].Portfolio[ticker]!=null) > this.accounts[accountNumber].ClosePosition(ticker); > } > } > } > } > > > > protected override void closePositions() > { > for(int i=0; i<this.accounts.Length; i++) > { > if(i==0)//OTC daily account > this.closePositions_close(i); > if(i==1)//OTC 2 days > { > if(this.numDaysElapsedSinceLastOptimization == > this.numDaysBetweenEachOptimization - 1) > this.closePositions_close(i); > } > if(i==2)//OTC-CTO > { > this.closePositions_close(i); > if(this.numDaysElapsedSinceLastOptimization < > this.numDaysBetweenEachOptimization - 1) > //open reverse positions at night > { > this.reverseSignOfChosenTickers(); > this.orders.Clear(); > this.addChosenTickersToOrderList(); > this.openPositions_openWhenPortfolioIsEmpty(i); > this.reverseSignOfChosenTickers(); > this.orders.Clear(); > } > } > if(i==3)//CTO, only at night > { >// this.closePositions_close(i); > if(this.numDaysElapsedSinceLastOptimization < > this.numDaysBetweenEachOptimization - 1) > //open reverse positions at night > { > this.reverseSignOfChosenTickers(); > this.orders.Clear(); > this.addChosenTickersToOrderList(); > this.openPositions_openWhenPortfolioIsEmpty(i); > this.reverseSignOfChosenTickers(); > this.orders.Clear(); > } > } > } > } > private void reverseSignOfChosenTickers() > { > for(int i = 0; i<this.chosenTickers.Length; i++) > { > if(this.chosenTickers[i] != null) > { > if(this.chosenTickers[i].StartsWith("-")) > this.chosenTickers[i] = > >GenomeManagerForEfficientPortfolio.GetCleanTickerCode(this.chosenTickers[i]); > else > this.chosenTickers[i] = "-" + this.chosenTickers[i]; > } > } > } > > /// <summary> > /// Handles a "Market Open" event. > /// </summary> > /// <param name="sender"></param> > /// <param name="eventArgs"></param> > public override void MarketOpenEventHandler( > Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) > { > //temporarily the if condition > //if(this.numDaysElapsedSinceLastOptimization == 0) > this.openPositions(); > } > > > public override void MarketCloseEventHandler( > Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) > { > > //temporarily > //if(this.numDaysElapsedSinceLastOptimization == > // this.numDaysBetweenEachOptimization) > this.closePositions(); > > } > > > > #region OneHourAfterMarketCloseEventHandler > > protected DataTable getSetOfTickersToBeOptimized(DateTime currentDate) > { > /* > SelectorByAverageRawOpenPrice selectorByOpenPrice = > new SelectorByAverageRawOpenPrice(this.tickerGroupID, > false, > currentDate.AddDays(-this.numDaysForLiquidity), > currentDate, > this.numberOfEligibleTickers, > this.minPriceForMinimumCommission, > this.maxPriceForMinimumCommission, 0, 2); > DataTable tickersByPrice = > selectorByOpenPrice.GetTableOfSelectedTickers(); > */ > > SelectorByGroup temporizedGroup = new > SelectorByGroup(this.tickerGroupID, currentDate); > SelectorByOpenCloseCorrelationToBenchmark > lessCorrelatedFromTemporizedGroup = > new > SelectorByOpenCloseCorrelationToBenchmark(temporizedGroup.GetTableOfSelectedTickers(), > this.benchmark,true, > >currentDate.AddDays(-this.numDaysForOptimizationPeriod ), > >currentDate, > >this.numberOfEligibleTickers); > > this.eligibleTickers = > lessCorrelatedFromTemporizedGroup.GetTableOfSelectedTickers(); > SelectorByQuotationAtEachMarketDay quotedAtEachMarketDayFromEligible = > new SelectorByQuotationAtEachMarketDay( this.eligibleTickers, > false, > currentDate.AddDays(-this.numDaysForOptimizationPeriod), > currentDate, > this.numberOfEligibleTickers, this.benchmark); > //SelectorByWinningOpenToClose winners = > // new > SelectorByWinningOpenToClose(quotedAtEachMarketDayFromMostLiquid.GetTableOfSelectedTickers(), > // false, > currentDate.AddDays(-2), > // currentDate, > this.numberOfEligibleTickers/4); > //return winners.GetTableOfSelectedTickers(); > //SelectorByOpenCloseCorrelationToBenchmark lessCorrelated = > // new > SelectorByOpenCloseCorrelationToBenchmark(quotedAtEachMarketDayFromEligible.GetTableOfSelectedTickers(), > // this.benchmark, true, > // > currentDate.AddDays(-this.numDaysForLiquidity), > // currentDate, > this.numberOfEligibleTickers/2); > return quotedAtEachMarketDayFromEligible.GetTableOfSelectedTickers(); > //return lessCorrelated.GetTableOfSelectedTickers(); > } > > protected virtual void setTickers(DateTime currentDate, > bool setGenomeCounter) > { > > DataTable setOfTickersToBeOptimized = > this.getSetOfTickersToBeOptimized(currentDate); > if(setOfTickersToBeOptimized.Rows.Count > this.chosenTickers.Length*2) > //the optimization process is possible only if the initial set of > tickers is > //as large as the number of tickers to be chosen > > { > IGenomeManager genManEfficientCTOPortfolio = > new > GenomeManagerForEfficientCTOPortfolio(setOfTickersToBeOptimized, > >currentDate.AddDays(-this.numDaysForOptimizationPeriod), > currentDate, > >this.numberOfTickersToBeChosen, > this.targetReturn, > >this.portfolioType); > > GeneticOptimizer GO = new > GeneticOptimizer(genManEfficientCTOPortfolio, > >this.populationSizeForGeneticOptimizer, > >this.generationNumberForGeneticOptimizer, > >this.seedForRandomGenerator); > if(setGenomeCounter) > this.genomeCounter = new GenomeCounter(GO); > > GO.Run(false); > >this.addGenomeToBestGenomes(GO.BestGenome,currentDate.AddDays(-this.numDaysForOptimizationPeriod), > currentDate); > this.chosenTickers = ((GenomeMeaning)GO.BestGenome.Meaning).Tickers; > this.chosenTickersPortfolioWeights = > ((GenomeMeaning)GO.BestGenome.Meaning).TickersPortfolioWeights; > > } > //else it will be buyed again the previous optimized portfolio > //that's it the actual chosenTickers member > } > > protected void oneHourAfterMarketCloseEventHandler_updatePrices() > { > //min price for minimizing commission amount > //according to IB Broker's commission scheme > this.minPriceForMinimumCommission = > this.account.CashAmount/(this.numberOfTickersToBeChosen*100); > this.maxPriceForMinimumCommission = this.maxPriceForMinimumCommission; > //just to avoid warning message > } > > /// <summary> > /// Handles a "One hour after market close" event. > /// </summary> > /// <param name="sender"></param> > /// <param name="eventArgs"></param> > public override void OneHourAfterMarketCloseEventHandler( > Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) > { > this.seedForRandomGenerator++; > this.orders.Clear(); > //this.oneHourAfterMarketCloseEventHandler_updatePrices(); > if(this.numDaysElapsedSinceLastOptimization == > this.numDaysBetweenEachOptimization - 1) > { > >this.setTickers(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime, false); > //sets tickers to be chosen at next Market Open event > this.numDaysElapsedSinceLastOptimization = 0; > } > else > { > this.numDaysElapsedSinceLastOptimization++; > } > > } > > #endregion > > } >} > > > >------------------------------------------------------- >This SF.net email is sponsored by: Splunk Inc. Do you grep through log files >for problems? Stop! Download the new AJAX search engine that makes >searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! >http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click >_______________________________________________ >Quantproject-developers mailing list >Qua...@li... >https://lists.sourceforge.net/lists/listinfo/quantproject-developers |