[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag WFLagEndOfDayT
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2006-07-30 13:40:00
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv8338/b7_Scripts/WalkForwardTesting/WalkForwardLag Modified Files: WFLagEndOfDayTimerHandler.cs Log Message: WeightedPositions are used instead of SignedTickers Index: WFLagEndOfDayTimerHandler.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WFLagEndOfDayTimerHandler.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** WFLagEndOfDayTimerHandler.cs 18 Jun 2006 14:13:30 -0000 1.4 --- WFLagEndOfDayTimerHandler.cs 30 Jul 2006 13:39:56 -0000 1.5 *************** *** 29,32 **** --- 29,33 ---- using QuantProject.Business.Financial.Ordering; using QuantProject.Business.Financial.Instruments; + using QuantProject.Business.Strategies; using QuantProject.Business.Timing; using QuantProject.Scripts.SimpleTesting; *************** *** 61,64 **** --- 62,66 ---- private DateTime lastOptimizationDate; + private bool arePositionsUpToDateWithChosenTickers; public event InSampleNewProgressEventHandler InSampleNewProgress; *************** *** 120,141 **** } #region fiveMinutesBeforeMarketCloseEventHandler_openPositions ! private string getTicker( string signedTicker ) ! { ! string returnValue; ! if ( signedTicker.IndexOf( "-" ) == 0 ) ! returnValue = signedTicker.Substring( 1 , signedTicker.Length - 1 ); ! else ! returnValue = signedTicker; ! return returnValue; ! } ! private int getReturnMultiplier( string signedTicker ) ! { ! int returnValue; ! if ( signedTicker.IndexOf( "-" ) == 0 ) ! returnValue = -1; ! else ! returnValue = 1; ! return returnValue; ! } private double getTodayReturnForTicker( string ticker ) { --- 122,143 ---- } #region fiveMinutesBeforeMarketCloseEventHandler_openPositions ! // private string getTicker( string signedTicker ) ! // { ! // string returnValue; ! // if ( signedTicker.IndexOf( "-" ) == 0 ) ! // returnValue = signedTicker.Substring( 1 , signedTicker.Length - 1 ); ! // else ! // returnValue = signedTicker; ! // return returnValue; ! // } ! // private int getReturnMultiplier( string signedTicker ) ! // { ! // int returnValue; ! // if ( signedTicker.IndexOf( "-" ) == 0 ) ! // returnValue = -1; ! // else ! // returnValue = 1; ! // return returnValue; ! // } private double getTodayReturnForTicker( string ticker ) { *************** *** 154,162 **** return returnValue; } ! private double getTodayReturnForSignedTicker( string signedTicker ) { double todayReturnForTicker = this.getTodayReturnForTicker( ! WFLagGenomeManager.GetTicker( signedTicker ) ); ! int returnMultiplier = this.getReturnMultiplier( signedTicker ); return todayReturnForTicker * returnMultiplier; } --- 156,165 ---- return returnValue; } ! private double getTodayReturn( ! WeightedPosition weightedPosition ) { double todayReturnForTicker = this.getTodayReturnForTicker( ! weightedPosition.Ticker ); ! double returnMultiplier = weightedPosition.Weight; return todayReturnForTicker * returnMultiplier; } *************** *** 168,196 **** { double totalReturn = 0; ! foreach ( string signedTicker in this.chosenTickers.DrivingPositions.Keys ) ! totalReturn += this.getTodayReturnForSignedTicker( signedTicker ); return totalReturn < 0; } private OrderType fiveMinutesBeforeMarketCloseEventHandler_openPosition_getOrderType( ! string signedTicker , bool isToReverse ) { OrderType orderType = OrderType.MarketBuy; ! if ( ( signedTicker.StartsWith( "-" ) && !isToReverse ) || ! ( !signedTicker.StartsWith( "-" ) && isToReverse ) ) orderType = OrderType.MarketSellShort; return orderType; } private void fiveMinutesBeforeMarketCloseEventHandler_openPosition( ! string signedTicker , bool isToReverse ) { ! string ticker = this.getTicker( signedTicker ); OrderType orderType = this.fiveMinutesBeforeMarketCloseEventHandler_openPosition_getOrderType( ! signedTicker , isToReverse ); double maxPositionValue = this.account.GetMarketValue() / this.numberOfPositionsToBeChosen; ! long sharesToBeTraded = OneRank.MaxBuyableShares( ticker , ! maxPositionValue , this.account.DataStreamer ); this.account.AddOrder( new Order( orderType , new Instrument( ticker ) , sharesToBeTraded ) ); --- 171,207 ---- { double totalReturn = 0; ! foreach ( WeightedPosition weightedPosition in ! this.chosenTickers.DrivingWeightedPositions.Values ) ! totalReturn += this.getTodayReturn( weightedPosition ); return totalReturn < 0; } private OrderType fiveMinutesBeforeMarketCloseEventHandler_openPosition_getOrderType( ! WeightedPosition weightedPosition , bool isToReverse ) { OrderType orderType = OrderType.MarketBuy; ! if ( ( weightedPosition.IsShort && !isToReverse ) || ! ( weightedPosition.IsLong && isToReverse ) ) orderType = OrderType.MarketSellShort; return orderType; } + private long getMaxBuyableShares( WeightedPosition weightedPosition ) + { + double maxPositionValue = this.account.GetMarketValue() * + Math.Abs( weightedPosition.Weight ); + double currentAsk = + this.account.DataStreamer.GetCurrentAsk( weightedPosition.Ticker ); + return Convert.ToInt64( Math.Floor( maxPositionValue / currentAsk ) ); + } private void fiveMinutesBeforeMarketCloseEventHandler_openPosition( ! WeightedPosition weightedPosition , bool isToReverse ) { ! string ticker = weightedPosition.Ticker; OrderType orderType = this.fiveMinutesBeforeMarketCloseEventHandler_openPosition_getOrderType( ! weightedPosition , isToReverse ); double maxPositionValue = this.account.GetMarketValue() / this.numberOfPositionsToBeChosen; ! long sharesToBeTraded = this.getMaxBuyableShares( weightedPosition ); this.account.AddOrder( new Order( orderType , new Instrument( ticker ) , sharesToBeTraded ) ); *************** *** 199,210 **** { bool isToReverse = this.isToReverse(); ! foreach ( string signedTicker ! in this.chosenTickers.PortfolioPositions.Keys ) this.fiveMinutesBeforeMarketCloseEventHandler_openPosition( ! signedTicker , isToReverse ); } private void fiveMinutesBeforeMarketCloseEventHandler_openPositions() { ! if ( this.chosenTickers.DrivingPositions != null ) this.fiveMinutesBeforeMarketCloseEventHandler_openPositions_actually(); } --- 210,222 ---- { bool isToReverse = this.isToReverse(); ! foreach ( WeightedPosition weightedPosition ! in this.chosenTickers.PortfolioWeightedPositions.Values ) this.fiveMinutesBeforeMarketCloseEventHandler_openPosition( ! weightedPosition , isToReverse ); ! this.arePositionsUpToDateWithChosenTickers = true; } private void fiveMinutesBeforeMarketCloseEventHandler_openPositions() { ! if ( this.chosenTickers.DrivingWeightedPositions != null ) this.fiveMinutesBeforeMarketCloseEventHandler_openPositions_actually(); } *************** *** 212,218 **** { double totalReturn = 0; ! foreach ( string signedTicker in ! this.chosenTickers.DrivingPositions.Keys ) ! totalReturn += this.getTodayReturnForSignedTicker( signedTicker ); return totalReturn; } --- 224,230 ---- { double totalReturn = 0; ! foreach ( WeightedPosition weightedPosition in ! this.chosenTickers.DrivingWeightedPositions.Values ) ! totalReturn += this.getTodayReturn( weightedPosition ); return totalReturn; } *************** *** 225,293 **** return position; } ! private string getSignedTicker( string ticker ) ! { ! string signedTicker = ""; ! if ( this.chosenTickers.PortfolioPositions.ContainsKey( ticker ) ) ! signedTicker = ticker; ! if ( this.chosenTickers.PortfolioPositions.ContainsKey( "-" + ticker ) ) ! signedTicker = "-" + ticker; ! if ( signedTicker == "" ) ! throw new Exception( "Nor ticker, nor '-'+ticker are contained in " + ! "chosenTickers.PortfolioPositions ; this is an unexpected " + ! "situation, when this method is invoked." ); ! return signedTicker; ! } ! private bool isShort( string signedTicker ) ! { ! return ( signedTicker.StartsWith( "-" ) ); ! } ! private bool isLong( string signedTicker ) ! { ! return ( !this.isShort( signedTicker ) ); ! } ! private bool isContainedInPortfolio( string signedTicker ) ! { ! string ticker = this.getTicker( signedTicker ); ! bool isContained = ! ( this.account.Portfolio.ContainsKey( ticker ) ) ! && ! ( ( this.isLong( signedTicker ) && ! this.account.Portfolio.IsLong( ticker ) ) || ! ( !this.isLong( signedTicker ) && ! this.account.Portfolio.IsShort( ticker ) ) ); ! return isContained; ! } ! private bool doPositionsCorrespondTo( ICollection signedTickers ) ! { ! bool areUpTodate = true; ! foreach ( string signedTicker in signedTickers ) ! areUpTodate = areUpTodate && this.isContainedInPortfolio( signedTicker ); ! return areUpTodate; ! } ! private string reverse( string signedTicker ) ! { ! string reversedSignedTicker = ""; ! if ( this.isLong( signedTicker ) ) ! reversedSignedTicker = "-" + signedTicker; ! if ( !this.isLong( signedTicker ) ) ! // signedTicker starts with a "-" character ! reversedSignedTicker = signedTicker.Substring( 1 ); ! return reversedSignedTicker; ! } ! private ICollection reverse( ICollection signedTickers ) ! { ! Hashtable reversedCollection = new Hashtable(); ! foreach ( string signedTicker in signedTickers ) ! reversedCollection.Add( this.reverse( signedTicker ) , null ); ! return reversedCollection.Keys; ! } ! private bool arePositionsUpToDateWithChosenTickers() { ! bool areUpTodate = ! this.doPositionsCorrespondTo( ! this.chosenTickers.PortfolioPositions.Keys ) || ! this.doPositionsCorrespondTo( ! this.reverse( this.chosenTickers.PortfolioPositions.Keys ) ); ! return areUpTodate; } private bool isReversed() --- 237,317 ---- return position; } ! // private string getSignedTicker( string ticker ) ! // { ! // string signedTicker = ""; ! // if ( this.chosenTickers.PortfolioWeightedPositions.ContainsKey( ticker ) ) ! // signedTicker = ticker; ! // if ( this.chosenTickers.PortfolioWeightedPositions.ContainsKey( "-" + ticker ) ) ! // signedTicker = "-" + ticker; ! // if ( signedTicker == "" ) ! // throw new Exception( "Nor ticker, nor '-'+ticker are contained in " + ! // "chosenTickers.PortfolioPositions ; this is an unexpected " + ! // "situation, when this method is invoked." ); ! // return signedTicker; ! // } ! // private bool isShort( string signedTicker ) ! // { ! // return ( signedTicker.StartsWith( "-" ) ); ! // } ! // private bool isLong( string signedTicker ) ! // { ! // return ( !this.isShort( signedTicker ) ); ! // } ! // private bool isContainedInPortfolio( WeightedPosition weightedPosition ) ! // { ! // bool isContained = false; ! // if ( this.account.Portfolio.ContainsKey( weightedPosition.Ticker ) ) ! // { ! // Position position = ! // this.account.Portfolio.GetPosition( weightedPosition.Ticker ); ! // isContained = ( position.q ! // } ! // bool isContained = ! // ( this.account.Portfolio.ContainsKey( weightedPosition.Ticker ) ) ! // && ! // ( ( weightedPosition.IsLong && ! // this.account.Portfolio.IsLong( weightedPosition.Ticker ) ) || ! // ( weightedPosition.IsShort && ! // this.account.Portfolio.IsShort( weightedPosition.Ticker ) ) ); ! // return isContained; ! // } ! // private bool doPositionsCorrespondTo( ICollection signedTickers ) ! // { ! // bool areUpTodate = true; ! // foreach ( string signedTicker in signedTickers ) ! // areUpTodate = areUpTodate && ! // this.isContainedInPortfolio( signedTicker ); ! // return areUpTodate; ! // } ! // private string reverse( string signedTicker ) ! // { ! // string reversedSignedTicker = ""; ! // if ( this.isLong( signedTicker ) ) ! // reversedSignedTicker = "-" + signedTicker; ! // if ( !this.isLong( signedTicker ) ) ! // // signedTicker starts with a "-" character ! // reversedSignedTicker = signedTicker.Substring( 1 ); ! // return reversedSignedTicker; ! // } ! // private ICollection reverse( ICollection signedTickers ) ! // { ! // Hashtable reversedCollection = new Hashtable(); ! // foreach ( string signedTicker in signedTickers ) ! // reversedCollection.Add( this.reverse( signedTicker ) , null ); ! // return reversedCollection.Keys; ! // } ! // private bool arePositionsUpToDateWithChosenTickers() ! // { ! // bool areUpTodate = ! // this.doPositionsCorrespondTo( ! // this.chosenTickers.PortfolioWeightedPositions ) || ! // this.doPositionsCorrespondTo( ! // this.reverse( this.chosenTickers.PortfolioWeightedPositions ) ); ! // return areUpTodate; ! // } ! private WeightedPosition getWeightedPosition( string ticker ) { ! return this.chosenTickers.PortfolioWeightedPositions.GetWeightedPosition( ! ticker ); } private bool isReversed() *************** *** 295,304 **** Position position = this.getFirstPosition(); PositionType positionType = position.Type; ! string positionTicker = position.Instrument.Key; ! String signedTicker = this.getSignedTicker( positionTicker ); bool isReversedPosition = ! ( ( this.isLong( signedTicker )&& ( position.Type == PositionType.Short ) ) || ! ( !this.isLong( signedTicker ) && ( position.Type == PositionType.Long ) ) ); return isReversedPosition; --- 319,329 ---- Position position = this.getFirstPosition(); PositionType positionType = position.Type; ! string firstPositionTicker = position.Instrument.Key; ! WeightedPosition weightedPosition = ! this.getWeightedPosition( firstPositionTicker ); bool isReversedPosition = ! ( ( weightedPosition.IsLong && ( position.Type == PositionType.Short ) ) || ! ( weightedPosition.IsShort && ( position.Type == PositionType.Long ) ) ); return isReversedPosition; *************** *** 318,322 **** { bool areToBeClosed = ! (!this.arePositionsUpToDateWithChosenTickers()) || this.isReversingNeeded(); return areToBeClosed; --- 343,347 ---- { bool areToBeClosed = ! ( !this.arePositionsUpToDateWithChosenTickers ) || this.isReversingNeeded(); return areToBeClosed; *************** *** 368,372 **** // Console.WriteLine( "Number of Eligible tickers: " + // this.eligibleTickers.EligibleTickers.Rows.Count ); ! this.chosenTickers.SetSignedTickers( this.eligibleTickers ); this.NewChosenTickers( this , new WFLagNewChosenTickersEventArgs( this.chosenTickers ) ); --- 393,398 ---- // Console.WriteLine( "Number of Eligible tickers: " + // this.eligibleTickers.EligibleTickers.Rows.Count ); ! this.chosenTickers.SetWeightedPositions( this.eligibleTickers ); ! this.arePositionsUpToDateWithChosenTickers = false; this.NewChosenTickers( this , new WFLagNewChosenTickersEventArgs( this.chosenTickers ) ); |