[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination StrategyType.
Brought to you by:
glauco_1
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29203/b7_Scripts/WalkForwardTesting/LinearCombination Modified Files: TestDisplayer.cs LinearCombinationTest.cs Added Files: StrategyType.cs CloseToOpenDailyStrategy.cs Log Message: Close to open strategy type has been added to the TestDisplayer for examining in sample/outOfSample behaviour of genomes (for linear combination strategies) Index: LinearCombinationTest.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/LinearCombinationTest.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** LinearCombinationTest.cs 10 Aug 2005 16:51:31 -0000 1.3 --- LinearCombinationTest.cs 8 Jan 2006 23:33:52 -0000 1.4 *************** *** 44,48 **** private DateTime lastDate; private GenomeRepresentation genomeRepresentation; ! private bool openToCloseDaily; private IHistoricalQuoteProvider historicalQuoteProvider; --- 44,49 ---- private DateTime lastDate; private GenomeRepresentation genomeRepresentation; ! // private bool openToCloseDaily; ! private StrategyType strategyType; private IHistoricalQuoteProvider historicalQuoteProvider; *************** *** 52,61 **** public LinearCombinationTest( DateTime firstDate , DateTime lastDate , ! GenomeRepresentation genomeRepresentation , bool openToCloseDaily ) { this.firstDate = firstDate; this.lastDate = lastDate; this.genomeRepresentation = genomeRepresentation; ! this.openToCloseDaily = openToCloseDaily; } --- 53,63 ---- public LinearCombinationTest( DateTime firstDate , DateTime lastDate , ! GenomeRepresentation genomeRepresentation , StrategyType strategyType) { this.firstDate = firstDate; this.lastDate = lastDate; this.genomeRepresentation = genomeRepresentation; ! // this.openToCloseDaily = openToCloseDaily; ! this.strategyType = strategyType; } *************** *** 70,74 **** private void run_setHistoricalQuoteProvider() { ! if ( this.openToCloseDaily ) this.historicalQuoteProvider = new HistoricalRawQuoteProvider(); else --- 72,76 ---- private void run_setHistoricalQuoteProvider() { ! if ( this.strategyType == StrategyType.OpenToCloseDaily ) this.historicalQuoteProvider = new HistoricalRawQuoteProvider(); else *************** *** 79,88 **** string[] signedTickers = GenomeRepresentation.GetSignedTickers( this.genomeRepresentation.SignedTickers ); ! if ( this.openToCloseDaily ) ! this.endOfDayStrategy = new OpenToCloseDailyStrategy( ! this.account , signedTickers ); ! else ! this.endOfDayStrategy = new OpenToCloseWeeklyStrategy( ! this.account , signedTickers ); } private string getDateString( DateTime dateTime ) --- 81,99 ---- string[] signedTickers = GenomeRepresentation.GetSignedTickers( this.genomeRepresentation.SignedTickers ); ! switch (this.strategyType) ! { ! case StrategyType.OpenToCloseDaily: ! this.endOfDayStrategy = new OpenToCloseDailyStrategy( ! this.account , signedTickers ); ! break; ! case StrategyType.OpenToCloseWeekly: ! this.endOfDayStrategy = new OpenToCloseWeeklyStrategy( ! this.account , signedTickers ); ! break; ! case StrategyType.CloseToOpenDaily: ! this.endOfDayStrategy = new CloseToOpenDailyStrategy( ! this.account , signedTickers ); ! break; ! } } private string getDateString( DateTime dateTime ) *************** *** 126,129 **** --- 137,143 ---- new FiveMinutesBeforeMarketCloseEventHandler( this.endOfDayStrategy.FiveMinutesBeforeMarketCloseEventHandler ); + this.historicalEndOfDayTimer.MarketClose += + new MarketCloseEventHandler( + this.endOfDayStrategy.MarketCloseEventHandler ); this.historicalEndOfDayTimer.OneHourAfterMarketClose += new OneHourAfterMarketCloseEventHandler( --- NEW FILE: StrategyType.cs --- /* QuantProject - Quantitative Finance Library StrategyType.cs Copyright (C) 2004 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; namespace QuantProject.Scripts.WalkForwardTesting.LinearCombination { /// <summary> /// Enumeration for strategy types based on linear combination /// </summary> public enum StrategyType { OpenToCloseDaily, OpenToCloseWeekly, CloseToOpenDaily } } --- NEW FILE: CloseToOpenDailyStrategy.cs --- /* QuantProject - Quantitative Finance Library CloseToOpenDailyStrategy.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 QuantProject.Business.Financial.Accounting; using QuantProject.Business.Financial.Instruments; using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Strategies; using QuantProject.Business.Timing; namespace QuantProject.Scripts.WalkForwardTesting.LinearCombination { /// <summary> /// Close To Open daily strategy /// </summary> [Serializable] public class CloseToOpenDailyStrategy : IEndOfDayStrategy { private Account account; private string[] signedTickers; public CloseToOpenDailyStrategy( Account account , string[] signedTickers) { this.account = account; this.signedTickers = signedTickers; } private long marketCloseEventHandler_addOrder_getQuantity( string ticker ) { double accountValue = this.account.GetMarketValue(); double currentTickerAsk = this.account.DataStreamer.GetCurrentAsk( ticker ); double maxPositionValueForThisTicker = accountValue/this.signedTickers.Length; long quantity = Convert.ToInt64( Math.Floor( maxPositionValueForThisTicker / currentTickerAsk ) ); return quantity; } private void marketCloseEventHandler_addOrder( string signedTicker ) { OrderType orderType = GenomeRepresentation.GetOrderType( signedTicker ); string ticker = GenomeRepresentation.GetTicker( signedTicker ); long quantity = marketCloseEventHandler_addOrder_getQuantity( ticker ); Order order = new Order( orderType , new Instrument( ticker ) , quantity ); this.account.AddOrder( order ); } private void marketCloseEventHandler_addOrders() { foreach ( string signedTicker in this.signedTickers ) marketCloseEventHandler_addOrder( signedTicker ); } public void MarketOpenEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) { this.marketOpen_closePositions(); } private void marketOpen_closePositions() { ArrayList tickers = new ArrayList(); foreach ( Position position in this.account.Portfolio.Positions ) tickers.Add( position.Instrument.Key ); foreach ( string ticker in tickers ) this.account.ClosePosition( ticker ); } public void FiveMinutesBeforeMarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) { } public void MarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) { if ( ( this.account.CashAmount == 0 ) && ( this.account.Transactions.Count == 0 ) ) // cash has not been added yet this.account.AddCash( 30000 ); marketCloseEventHandler_addOrders(); } public void OneHourAfterMarketCloseEventHandler( Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs) { } } } Index: TestDisplayer.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/LinearCombination/TestDisplayer.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** TestDisplayer.cs 10 Aug 2005 16:53:45 -0000 1.5 --- TestDisplayer.cs 8 Jan 2006 23:33:52 -0000 1.6 *************** *** 48,52 **** --- 48,54 ---- private System.Windows.Forms.RadioButton radioButtonOpenToCloseDaily; private System.Windows.Forms.RadioButton radioButtonOpenToCloseWeekly; + private System.Windows.Forms.RadioButton radioButtonCloseToOpenDaily; private GenomeRepresentation lastSelectedGenomeRepresentation; + private StrategyType selectedStrategyType = StrategyType.OpenToCloseDaily; private void testdisplayer() *************** *** 98,178 **** private void InitializeComponent() { ! this.dgBestGenomes = new System.Windows.Forms.DataGrid(); ! this.dtpFirstDate = new System.Windows.Forms.DateTimePicker(); ! this.dtpLastDate = new System.Windows.Forms.DateTimePicker(); ! this.label1 = new System.Windows.Forms.Label(); ! this.radioButtonOpenToCloseDaily = new System.Windows.Forms.RadioButton(); ! this.radioButtonOpenToCloseWeekly = new System.Windows.Forms.RadioButton(); ! ((System.ComponentModel.ISupportInitialize)(this.dgBestGenomes)).BeginInit(); ! this.SuspendLayout(); ! // ! // dgBestGenomes ! // ! this.dgBestGenomes.DataMember = ""; ! this.dgBestGenomes.Dock = System.Windows.Forms.DockStyle.Bottom; ! this.dgBestGenomes.HeaderForeColor = System.Drawing.SystemColors.ControlText; ! this.dgBestGenomes.Location = new System.Drawing.Point(0, 125); ! this.dgBestGenomes.Name = "dgBestGenomes"; ! this.dgBestGenomes.Size = new System.Drawing.Size(520, 248); ! this.dgBestGenomes.TabIndex = 0; ! this.dgBestGenomes.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dgBestGenomes_MouseUp); ! // ! // dtpFirstDate ! // ! this.dtpFirstDate.Location = new System.Drawing.Point(16, 24); ! this.dtpFirstDate.Name = "dtpFirstDate"; ! this.dtpFirstDate.TabIndex = 1; ! // ! // dtpLastDate ! // ! this.dtpLastDate.Location = new System.Drawing.Point(264, 24); ! this.dtpLastDate.Name = "dtpLastDate"; ! this.dtpLastDate.Size = new System.Drawing.Size(208, 20); ! this.dtpLastDate.TabIndex = 2; ! // ! // label1 ! // ! this.label1.Location = new System.Drawing.Point(32, 64); ! this.label1.Name = "label1"; ! this.label1.Size = new System.Drawing.Size(400, 40); ! this.label1.TabIndex = 3; ! this.label1.Text = "Left click data grid rows to reset dates to the optimization period. Right click " + ! "to preserve date displacements and backtest."; ! // ! // radioButtonOpenToCloseDaily ! // ! this.radioButtonOpenToCloseDaily.Checked = true; ! this.radioButtonOpenToCloseDaily.Location = new System.Drawing.Point(64, 96); ! this.radioButtonOpenToCloseDaily.Name = "radioButtonOpenToCloseDaily"; ! this.radioButtonOpenToCloseDaily.Size = new System.Drawing.Size(144, 24); ! this.radioButtonOpenToCloseDaily.TabIndex = 4; ! this.radioButtonOpenToCloseDaily.TabStop = true; ! this.radioButtonOpenToCloseDaily.Text = "Open To Close Daily"; ! // ! // radioButtonOpenToCloseWeekly ! // ! this.radioButtonOpenToCloseWeekly.Location = new System.Drawing.Point(224, 96); ! this.radioButtonOpenToCloseWeekly.Name = "radioButtonOpenToCloseWeekly"; ! this.radioButtonOpenToCloseWeekly.Size = new System.Drawing.Size(144, 24); ! this.radioButtonOpenToCloseWeekly.TabIndex = 5; ! this.radioButtonOpenToCloseWeekly.Text = "Open To Close Weekly"; ! // ! // TestDisplayer ! // ! this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); ! this.ClientSize = new System.Drawing.Size(520, 373); ! this.Controls.AddRange(new System.Windows.Forms.Control[] { ! this.radioButtonOpenToCloseWeekly, ! this.radioButtonOpenToCloseDaily, ! this.label1, ! this.dtpLastDate, ! this.dtpFirstDate, ! this.dgBestGenomes}); ! this.Name = "TestDisplayer"; ! this.Text = "TestDisplayer"; ! ((System.ComponentModel.ISupportInitialize)(this.dgBestGenomes)).EndInit(); ! this.ResumeLayout(false); ! } #endregion --- 100,193 ---- private void InitializeComponent() { ! this.dgBestGenomes = new System.Windows.Forms.DataGrid(); ! this.dtpFirstDate = new System.Windows.Forms.DateTimePicker(); ! this.dtpLastDate = new System.Windows.Forms.DateTimePicker(); ! this.label1 = new System.Windows.Forms.Label(); ! this.radioButtonOpenToCloseDaily = new System.Windows.Forms.RadioButton(); ! this.radioButtonOpenToCloseWeekly = new System.Windows.Forms.RadioButton(); ! this.radioButtonCloseToOpenDaily = new System.Windows.Forms.RadioButton(); ! ((System.ComponentModel.ISupportInitialize)(this.dgBestGenomes)).BeginInit(); ! this.SuspendLayout(); ! // ! // dgBestGenomes ! // ! this.dgBestGenomes.DataMember = ""; ! this.dgBestGenomes.Dock = System.Windows.Forms.DockStyle.Bottom; ! this.dgBestGenomes.HeaderForeColor = System.Drawing.SystemColors.ControlText; ! this.dgBestGenomes.Location = new System.Drawing.Point(0, 157); ! this.dgBestGenomes.Name = "dgBestGenomes"; ! this.dgBestGenomes.Size = new System.Drawing.Size(584, 216); ! this.dgBestGenomes.TabIndex = 0; ! this.dgBestGenomes.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dgBestGenomes_MouseUp); ! // ! // dtpFirstDate ! // ! this.dtpFirstDate.Location = new System.Drawing.Point(16, 24); ! this.dtpFirstDate.Name = "dtpFirstDate"; ! this.dtpFirstDate.TabIndex = 1; ! // ! // dtpLastDate ! // ! this.dtpLastDate.Location = new System.Drawing.Point(264, 24); ! this.dtpLastDate.Name = "dtpLastDate"; ! this.dtpLastDate.Size = new System.Drawing.Size(208, 20); ! this.dtpLastDate.TabIndex = 2; ! // ! // label1 ! // ! this.label1.Location = new System.Drawing.Point(32, 64); ! this.label1.Name = "label1"; ! this.label1.Size = new System.Drawing.Size(400, 40); ! this.label1.TabIndex = 3; ! this.label1.Text = "Left click data grid rows to reset dates to the optimization period. Right click " + ! "to preserve date displacements and backtest."; ! // ! // radioButtonOpenToCloseDaily ! // ! this.radioButtonOpenToCloseDaily.Checked = true; ! this.radioButtonOpenToCloseDaily.Location = new System.Drawing.Point(64, 96); ! this.radioButtonOpenToCloseDaily.Name = "radioButtonOpenToCloseDaily"; ! this.radioButtonOpenToCloseDaily.Size = new System.Drawing.Size(144, 24); ! this.radioButtonOpenToCloseDaily.TabIndex = 4; ! this.radioButtonOpenToCloseDaily.TabStop = true; ! this.radioButtonOpenToCloseDaily.Text = "Open To Close Daily"; ! this.radioButtonOpenToCloseDaily.CheckedChanged += new System.EventHandler(this.radioButtonOpenToCloseDaily_CheckedChanged); ! // ! // radioButtonOpenToCloseWeekly ! // ! this.radioButtonOpenToCloseWeekly.Location = new System.Drawing.Point(224, 96); ! this.radioButtonOpenToCloseWeekly.Name = "radioButtonOpenToCloseWeekly"; ! this.radioButtonOpenToCloseWeekly.Size = new System.Drawing.Size(144, 24); ! this.radioButtonOpenToCloseWeekly.TabIndex = 5; ! this.radioButtonOpenToCloseWeekly.Text = "Open To Close Weekly"; ! this.radioButtonOpenToCloseWeekly.CheckedChanged += new System.EventHandler(this.radioButtonOpenToCloseWeekly_CheckedChanged); ! // ! // radioButtonCloseToOpenDaily ! // ! this.radioButtonCloseToOpenDaily.Location = new System.Drawing.Point(64, 128); ! this.radioButtonCloseToOpenDaily.Name = "radioButtonCloseToOpenDaily"; ! this.radioButtonCloseToOpenDaily.Size = new System.Drawing.Size(144, 24); ! this.radioButtonCloseToOpenDaily.TabIndex = 6; ! this.radioButtonCloseToOpenDaily.Text = "Close To Open Daily"; ! this.radioButtonCloseToOpenDaily.CheckedChanged += new System.EventHandler(this.radioButtonCloseToOpenDaily_CheckedChanged); ! // ! // TestDisplayer ! // ! this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); ! this.ClientSize = new System.Drawing.Size(584, 373); ! this.Controls.AddRange(new System.Windows.Forms.Control[] { ! this.radioButtonCloseToOpenDaily, ! this.radioButtonOpenToCloseWeekly, ! this.radioButtonOpenToCloseDaily, ! this.label1, ! this.dtpLastDate, ! this.dtpFirstDate, ! this.dgBestGenomes}); ! this.Name = "TestDisplayer"; ! this.Text = "TestDisplayer"; ! ((System.ComponentModel.ISupportInitialize)(this.dgBestGenomes)).EndInit(); ! this.ResumeLayout(false); ! } #endregion *************** *** 224,228 **** new LinearCombinationTest( this.dtpFirstDate.Value , this.dtpLastDate.Value , genomeRepresentation , ! this.radioButtonOpenToCloseDaily.Checked ); linearCombinationTest.Run(); this.lastSelectedGenomeRepresentation = genomeRepresentation; --- 239,243 ---- new LinearCombinationTest( this.dtpFirstDate.Value , this.dtpLastDate.Value , genomeRepresentation , ! this.selectedStrategyType); linearCombinationTest.Run(); this.lastSelectedGenomeRepresentation = genomeRepresentation; *************** *** 254,257 **** --- 269,296 ---- this.dgBestGenomes_MouseUp_leftButton( sender , e ); } + + private void update_selectedStrategyType() + { + if(this.radioButtonOpenToCloseDaily.Checked) + this.selectedStrategyType = StrategyType.OpenToCloseDaily; + else if(this.radioButtonOpenToCloseWeekly.Checked) + this.selectedStrategyType = StrategyType.OpenToCloseWeekly; + else if(this.radioButtonCloseToOpenDaily.Checked) + this.selectedStrategyType = StrategyType.CloseToOpenDaily; + } + private void radioButtonOpenToCloseDaily_CheckedChanged(object sender, System.EventArgs e) + { + this.update_selectedStrategyType(); + } + + private void radioButtonOpenToCloseWeekly_CheckedChanged(object sender, System.EventArgs e) + { + this.update_selectedStrategyType(); + } + + private void radioButtonCloseToOpenDaily_CheckedChanged(object sender, System.EventArgs e) + { + this.update_selectedStrategyType(); + } } } |