[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOs
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-08-19 17:13:36
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv3611/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator Modified Files: PVO_CTOMain.cs PVO_CTOStrategy.cs PVO_OTCMain.cs PVO_OTCStrategy.cs PVO_OTCStrategyLessCorrelated.cs PVOMain.cs PVOStrategy.cs RunPVO.cs Log Message: - the code has been cleaned up to avoid warnings - standard indentation has been applied Index: PVOStrategy.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/PVOStrategy.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** PVOStrategy.cs 14 Aug 2008 23:28:06 -0000 1.5 --- PVOStrategy.cs 19 Aug 2008 17:13:01 -0000 1.6 *************** *** 3,7 **** PVOStrategy.cs ! Copyright (C) 2008 Marco Milletti --- 3,7 ---- PVOStrategy.cs ! Copyright (C) 2008 Marco Milletti *************** *** 19,23 **** 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; --- 19,23 ---- 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; *************** *** 51,108 **** { /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the Portfolio Value ! /// Oscillator ! /// </summary> ! [Serializable] ! public class PVOStrategy : IEndOfDayStrategyForBacktester ! { public event NewLogItemEventHandler NewLogItem; public event NewMessageEventHandler NewMessage; //initialized by the constructor ! protected int numberOfTickersToBeChosen; ! protected int inSampleDays; ! protected int numDaysBetweenEachOptimization; ! protected IInSampleChooser inSampleChooser; ! protected IEligiblesSelector eligiblesSelector; ! protected Benchmark benchmark; ! protected HistoricalQuoteProvider historicalQuoteProvider; ! protected double maxAcceptableCloseToCloseDrawdown; ! protected double minimumAcceptableGain; ! protected int numDaysForOscillatingPeriod; ! //initialized after constructor's call ! protected int numDaysElapsedSinceLastOptimization; ! protected ReturnsManager returnsManager; protected TestingPositions[] chosenPVOPositions; //chosen in sample: these are the eligible positions for out //of sample testing protected PVOPositions pvoPositionsForOutOfSample; ! protected DateTime lastCloseDate; ! protected bool portfolioHasBeenOverbought; ! protected bool portfolioHasBeenOversold; ! protected Account account; ! public Account Account { get { return this.account; } ! set { this.account = value; } } ! protected bool stopLossConditionReached; ! protected bool takeProfitConditionReached; ! protected double currentAccountValue; ! protected double previousAccountValue; ! protected double oversoldThreshold; ! protected double overboughtThreshold; ! protected double oversoldThresholdMAX; ! protected double overboughtThresholdMAX; ! ! private string description_GetDescriptionForChooser() ! { ! if(this.inSampleChooser == null) ! return "ConstantChooser"; ! else ! return this.inSampleChooser.Description; ! } ! public string Description { --- 51,108 ---- { /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the Portfolio Value ! /// Oscillator ! /// </summary> ! [Serializable] ! public class PVOStrategy : IEndOfDayStrategyForBacktester ! { public event NewLogItemEventHandler NewLogItem; public event NewMessageEventHandler NewMessage; //initialized by the constructor ! protected int numberOfTickersToBeChosen; ! protected int inSampleDays; ! protected int numDaysBetweenEachOptimization; ! protected IInSampleChooser inSampleChooser; ! protected IEligiblesSelector eligiblesSelector; ! protected Benchmark benchmark; ! protected HistoricalQuoteProvider historicalQuoteProvider; ! protected double maxAcceptableCloseToCloseDrawdown; ! protected double minimumAcceptableGain; ! protected int numDaysForOscillatingPeriod; ! //initialized after constructor's call ! protected int numDaysElapsedSinceLastOptimization; ! protected ReturnsManager returnsManager; protected TestingPositions[] chosenPVOPositions; //chosen in sample: these are the eligible positions for out //of sample testing protected PVOPositions pvoPositionsForOutOfSample; ! protected DateTime lastCloseDate; ! protected bool portfolioHasBeenOverbought; ! protected bool portfolioHasBeenOversold; ! protected Account account; ! public Account Account { get { return this.account; } ! set { this.account = value; } } ! protected bool stopLossConditionReached; ! protected bool takeProfitConditionReached; ! protected double currentAccountValue; ! protected double previousAccountValue; ! protected double oversoldThreshold; ! protected double overboughtThreshold; ! protected double oversoldThresholdMAX; ! protected double overboughtThresholdMAX; ! ! private string description_GetDescriptionForChooser() ! { ! if(this.inSampleChooser == null) ! return "ConstantChooser"; ! else ! return this.inSampleChooser.Description; ! } ! public string Description { *************** *** 128,143 **** private void pvoStrategy(IEligiblesSelector eligiblesSelector, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) { this.eligiblesSelector = eligiblesSelector; --- 128,143 ---- private void pvoStrategy(IEligiblesSelector eligiblesSelector, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) { this.eligiblesSelector = eligiblesSelector; *************** *** 150,154 **** this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; this.minimumAcceptableGain = minimumAcceptableGain; ! this.oversoldThreshold = oversoldThreshold; this.overboughtThreshold = overboughtThreshold; this.oversoldThresholdMAX = oversoldThresholdMAX; --- 150,154 ---- this.maxAcceptableCloseToCloseDrawdown = maxAcceptableCloseToCloseDrawdown; this.minimumAcceptableGain = minimumAcceptableGain; ! this.oversoldThreshold = oversoldThreshold; this.overboughtThreshold = overboughtThreshold; this.oversoldThresholdMAX = oversoldThresholdMAX; *************** *** 161,265 **** } ! public PVOStrategy(IEligiblesSelector eligiblesSelector, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.inSampleChooser = inSampleChooser; ! } ! public PVOStrategy(IEligiblesSelector eligiblesSelector, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! double.MaxValue, double.MaxValue, ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.inSampleChooser = inSampleChooser; } public PVOStrategy(IEligiblesSelector eligiblesSelector, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.chosenPVOPositions = chosenPVOPositions; } public PVOStrategy(IEligiblesSelector eligiblesSelector, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! double.MaxValue , double.MaxValue , ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.chosenPVOPositions = chosenPVOPositions; } ! public virtual void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ; ! } public void FiveMinutesBeforeMarketCloseEventHandler( --- 161,265 ---- } ! public PVOStrategy(IEligiblesSelector eligiblesSelector, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.inSampleChooser = inSampleChooser; ! } ! public PVOStrategy(IEligiblesSelector eligiblesSelector, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! double.MaxValue, double.MaxValue, ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.inSampleChooser = inSampleChooser; } public PVOStrategy(IEligiblesSelector eligiblesSelector, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.chosenPVOPositions = chosenPVOPositions; } public PVOStrategy(IEligiblesSelector eligiblesSelector, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! int numDaysForOscillatingPeriod, ! int numberOfTickersToBeChosen, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider, ! double maxAcceptableCloseToCloseDrawdown, ! double minimumAcceptableGain) ! { this.pvoStrategy(eligiblesSelector, inSampleDays , numDaysForOscillatingPeriod , ! numberOfTickersToBeChosen , benchmark , numDaysBetweenEachOptimization , ! oversoldThreshold, overboughtThreshold, ! double.MaxValue , double.MaxValue , ! historicalQuoteProvider , maxAcceptableCloseToCloseDrawdown , ! minimumAcceptableGain ); this.chosenPVOPositions = chosenPVOPositions; } ! public virtual void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! ; ! } public void FiveMinutesBeforeMarketCloseEventHandler( *************** *** 273,464 **** } ! #region MarketCloseEventHandler ! //forOutOfSampleTesting protected virtual EndOfDayDateTime getBeginOfOscillatingPeriod(IndexBasedEndOfDayTimer timer) { return new EndOfDayDateTime( (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition-this.numDaysForOscillatingPeriod]["quDate"], ! EndOfDaySpecificTime.MarketClose ); } ! private void marketCloseEventHandler_reverseIfNeeded_reverse(PVOPositionsStatus currentStatus) ! { ! if(currentStatus == PVOPositionsStatus.Overbought) ! { ! this.portfolioHasBeenOversold = false; ! this.portfolioHasBeenOverbought = true; ! } ! else if(currentStatus == PVOPositionsStatus.Oversold) ! { ! this.portfolioHasBeenOversold = true; ! this.portfolioHasBeenOverbought = false; ! } ! AccountManager.ReversePositions(this.account); ! this.previousAccountValue = this.account.GetMarketValue(); ! } ! private void marketCloseEventHandler_reverseIfNeeded(IndexBasedEndOfDayTimer timer) ! { ! EndOfDayDateTime today = timer.GetCurrentTime(); EndOfDayDateTime beginOfOscillatingPeriod = this.getBeginOfOscillatingPeriod(timer); ! PVOPositionsStatus pvoPositionsStatus = ! this.pvoPositionsForOutOfSample.GetStatus(beginOfOscillatingPeriod, today , this.benchmark.Ticker, this.historicalQuoteProvider, double.MaxValue, double.MaxValue); ! if(pvoPositionsStatus == PVOPositionsStatus.Overbought && ! this.portfolioHasBeenOversold) ! //open positions derive from an overSold period but now ! //the overbought threshold has been reached ! this.marketCloseEventHandler_reverseIfNeeded_reverse(pvoPositionsStatus); ! ! if(pvoPositionsStatus == PVOPositionsStatus.Oversold && ! this.portfolioHasBeenOverbought) ! //open positions derive from an overBought period but now ! //the overSold threshold has been reached ! this.marketCloseEventHandler_reverseIfNeeded_reverse(pvoPositionsStatus); ! } ! private PVOPositionsStatus marketCloseEventHandler_openPositions_getStatus(IndexBasedEndOfDayTimer timer) ! { ! EndOfDayDateTime today = timer.GetCurrentTime(); ! EndOfDayDateTime beginOfOscillatingPeriod = ! this.getBeginOfOscillatingPeriod(timer); ! PVOPositionsStatus currentStatus = ! PVOPositionsStatus.InTheMiddle; ! for(int i = 0; i<this.chosenPVOPositions.Length; i++) ! { ! if(this.chosenPVOPositions[i] != null) ! currentStatus = ! ((PVOPositions)this.chosenPVOPositions[i]).GetStatus(beginOfOscillatingPeriod, today, ! this.benchmark.Ticker, this.historicalQuoteProvider, ! double.MaxValue, double.MaxValue); ! if(currentStatus == PVOPositionsStatus.Oversold || ! currentStatus == PVOPositionsStatus.Overbought ) ! { this.pvoPositionsForOutOfSample = (PVOPositions)this.chosenPVOPositions[i]; i = this.chosenPVOPositions.Length;//exit from for } ! } ! return currentStatus; ! } ! ! protected void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! PVOPositionsStatus pvoPositionsStatus = PVOPositionsStatus.InTheMiddle; ! if(timer.CurrentDateArrayPosition >= this.numDaysForOscillatingPeriod) ! pvoPositionsStatus = ! this.marketCloseEventHandler_openPositions_getStatus(timer); switch (pvoPositionsStatus){ ! case PVOPositionsStatus.Overbought: ! { ! #region manage Overbought case ! this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); ! try{ ! AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, ! this.account ); ! this.portfolioHasBeenOverbought = true; ! this.portfolioHasBeenOversold = false; ! this.previousAccountValue = this.account.GetMarketValue(); ! } ! catch(Exception ex){ ! ex = ex; ! } ! finally{ ! this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); ! } ! #endregion ! break; ! } ! case PVOPositionsStatus.Oversold: ! { ! #region manage Oversold case ! AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, ! this.account ); ! this.portfolioHasBeenOverbought = false; ! this.portfolioHasBeenOversold = true; ! this.previousAccountValue = this.account.GetMarketValue(); ! #endregion ! break; ! } ! case PVOPositionsStatus.InTheMiddle://that is ! { //pvoPositionsForOutOfSample has not been set ! ! this.previousAccountValue = this.account.GetMarketValue(); ! break; ! } default: ! { ! //it should never been reached ! this.previousAccountValue = this.account.GetMarketValue(); ! break; ! } ! } ! } ! ! protected virtual void marketCloseEventHandler_closePositionsIfNeeded() ! { ! if(this.stopLossConditionReached || ! this.takeProfitConditionReached || ! this.numDaysElapsedSinceLastOptimization + 1 == this.numDaysBetweenEachOptimization ) ! { ! AccountManager.ClosePositions(this.account); ! this.portfolioHasBeenOverbought = false; ! this.portfolioHasBeenOversold = false; ! } ! } ! ! protected virtual void marketCloseEventHandler_updateStopLossAndTakeProfitConditions() ! { ! //this.previousAccountValue has been set at opening positions ! this.stopLossConditionReached = false; ! this.takeProfitConditionReached = false; ! this.currentAccountValue = this.account.GetMarketValue(); ! double portfolioGainOrLoss = (this.currentAccountValue - this.previousAccountValue) ! /this.previousAccountValue; ! ! if(!double.IsInfinity(portfolioGainOrLoss) && ! portfolioGainOrLoss <= -this.maxAcceptableCloseToCloseDrawdown ) ! { ! this.stopLossConditionReached = true; ! this.takeProfitConditionReached = false; ! } ! else if (!double.IsInfinity(portfolioGainOrLoss) && ! portfolioGainOrLoss >= this.minimumAcceptableGain ) ! ! { ! this.stopLossConditionReached = false; ! this.takeProfitConditionReached = true; ! } ! } ! ! public virtual void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! try{ ! this.marketCloseEventHandler_updateStopLossAndTakeProfitConditions(); ! //this.marketCloseEventHandler_closePositionsIfNeeded(); ! if( this.chosenPVOPositions != null ) ! //PVOPositions have been chosen by the chooser ! { ! if(this.account.Portfolio.Count == 0) ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! //positions are opened only if thresholds are reached ! else//there are some opened positions ! this.marketCloseEventHandler_closePositionsIfNeeded(); ! //this.marketCloseEventHandler_reverseIfNeeded((IndexBasedEndOfDayTimer)sender); ! } ! } ! catch(TickerNotExchangedException ex) ! {ex=ex;} ! } ! #endregion ! ! #region OneHourAfterMarketCloseEventHandler ! protected virtual void updateReturnsManager(EndOfDayDateTime firstEndOfDayDateTime, ! EndOfDayDateTime lastEndOfDayDateTime) { ! this.returnsManager = new ReturnsManager(new CloseToCloseIntervals(firstEndOfDayDateTime, lastEndOfDayDateTime, ! this.benchmark.Ticker, this.numDaysForOscillatingPeriod), ! this.historicalQuoteProvider); } --- 273,468 ---- } ! #region MarketCloseEventHandler ! //forOutOfSampleTesting protected virtual EndOfDayDateTime getBeginOfOscillatingPeriod(IndexBasedEndOfDayTimer timer) { return new EndOfDayDateTime( (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition-this.numDaysForOscillatingPeriod]["quDate"], ! EndOfDaySpecificTime.MarketClose ); } ! private void marketCloseEventHandler_reverseIfNeeded_reverse(PVOPositionsStatus currentStatus) ! { ! if(currentStatus == PVOPositionsStatus.Overbought) ! { ! this.portfolioHasBeenOversold = false; ! this.portfolioHasBeenOverbought = true; ! } ! else if(currentStatus == PVOPositionsStatus.Oversold) ! { ! this.portfolioHasBeenOversold = true; ! this.portfolioHasBeenOverbought = false; ! } ! AccountManager.ReversePositions(this.account); ! this.previousAccountValue = this.account.GetMarketValue(); ! } ! private void marketCloseEventHandler_reverseIfNeeded(IndexBasedEndOfDayTimer timer) ! { ! EndOfDayDateTime today = timer.GetCurrentTime(); EndOfDayDateTime beginOfOscillatingPeriod = this.getBeginOfOscillatingPeriod(timer); ! PVOPositionsStatus pvoPositionsStatus = ! this.pvoPositionsForOutOfSample.GetStatus(beginOfOscillatingPeriod, today , this.benchmark.Ticker, this.historicalQuoteProvider, double.MaxValue, double.MaxValue); ! if(pvoPositionsStatus == PVOPositionsStatus.Overbought && ! this.portfolioHasBeenOversold) ! //open positions derive from an overSold period but now ! //the overbought threshold has been reached ! this.marketCloseEventHandler_reverseIfNeeded_reverse(pvoPositionsStatus); ! ! if(pvoPositionsStatus == PVOPositionsStatus.Oversold && ! this.portfolioHasBeenOverbought) ! //open positions derive from an overBought period but now ! //the overSold threshold has been reached ! this.marketCloseEventHandler_reverseIfNeeded_reverse(pvoPositionsStatus); ! } ! private PVOPositionsStatus marketCloseEventHandler_openPositions_getStatus(IndexBasedEndOfDayTimer timer) ! { ! EndOfDayDateTime today = timer.GetCurrentTime(); ! EndOfDayDateTime beginOfOscillatingPeriod = ! this.getBeginOfOscillatingPeriod(timer); ! PVOPositionsStatus currentStatus = ! PVOPositionsStatus.InTheMiddle; ! for(int i = 0; i<this.chosenPVOPositions.Length; i++) ! { ! if(this.chosenPVOPositions[i] != null) ! currentStatus = ! ((PVOPositions)this.chosenPVOPositions[i]).GetStatus(beginOfOscillatingPeriod, today, ! this.benchmark.Ticker, this.historicalQuoteProvider, ! double.MaxValue, double.MaxValue); ! if(currentStatus == PVOPositionsStatus.Oversold || ! currentStatus == PVOPositionsStatus.Overbought ) ! { this.pvoPositionsForOutOfSample = (PVOPositions)this.chosenPVOPositions[i]; i = this.chosenPVOPositions.Length;//exit from for } ! } ! return currentStatus; ! } ! ! protected void marketCloseEventHandler_openPositions(IndexBasedEndOfDayTimer timer) ! { ! PVOPositionsStatus pvoPositionsStatus = PVOPositionsStatus.InTheMiddle; ! if(timer.CurrentDateArrayPosition >= this.numDaysForOscillatingPeriod) ! pvoPositionsStatus = ! this.marketCloseEventHandler_openPositions_getStatus(timer); switch (pvoPositionsStatus){ ! case PVOPositionsStatus.Overbought: ! { ! #region manage Overbought case ! this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); ! try{ ! AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, ! this.account ); ! this.portfolioHasBeenOverbought = true; ! this.portfolioHasBeenOversold = false; ! this.previousAccountValue = this.account.GetMarketValue(); ! } ! catch(Exception ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! ! finally{ ! this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); ! } ! #endregion ! break; ! } ! case PVOPositionsStatus.Oversold: ! { ! #region manage Oversold case ! AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, ! this.account ); ! this.portfolioHasBeenOverbought = false; ! this.portfolioHasBeenOversold = true; ! this.previousAccountValue = this.account.GetMarketValue(); ! #endregion ! break; ! } ! case PVOPositionsStatus.InTheMiddle://that is ! { //pvoPositionsForOutOfSample has not been set ! ! this.previousAccountValue = this.account.GetMarketValue(); ! break; ! } default: ! { ! //it should never been reached ! this.previousAccountValue = this.account.GetMarketValue(); ! break; ! } ! } ! } ! ! protected virtual void marketCloseEventHandler_closePositionsIfNeeded() ! { ! if(this.stopLossConditionReached || ! this.takeProfitConditionReached || ! this.numDaysElapsedSinceLastOptimization + 1 == this.numDaysBetweenEachOptimization ) ! { ! AccountManager.ClosePositions(this.account); ! this.portfolioHasBeenOverbought = false; ! this.portfolioHasBeenOversold = false; ! } ! } ! ! protected virtual void marketCloseEventHandler_updateStopLossAndTakeProfitConditions() ! { ! //this.previousAccountValue has been set at opening positions ! this.stopLossConditionReached = false; ! this.takeProfitConditionReached = false; ! this.currentAccountValue = this.account.GetMarketValue(); ! double portfolioGainOrLoss = (this.currentAccountValue - this.previousAccountValue) ! /this.previousAccountValue; ! ! if(!double.IsInfinity(portfolioGainOrLoss) && ! portfolioGainOrLoss <= -this.maxAcceptableCloseToCloseDrawdown ) ! { ! this.stopLossConditionReached = true; ! this.takeProfitConditionReached = false; ! } ! else if (!double.IsInfinity(portfolioGainOrLoss) && ! portfolioGainOrLoss >= this.minimumAcceptableGain ) ! ! { ! this.stopLossConditionReached = false; ! this.takeProfitConditionReached = true; ! } ! } ! ! public virtual void MarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! try{ ! this.marketCloseEventHandler_updateStopLossAndTakeProfitConditions(); ! //this.marketCloseEventHandler_closePositionsIfNeeded(); ! if( this.chosenPVOPositions != null ) ! //PVOPositions have been chosen by the chooser ! { ! if(this.account.Portfolio.Count == 0) ! this.marketCloseEventHandler_openPositions((IndexBasedEndOfDayTimer)sender); ! //positions are opened only if thresholds are reached ! else//there are some opened positions ! this.marketCloseEventHandler_closePositionsIfNeeded(); ! //this.marketCloseEventHandler_reverseIfNeeded((IndexBasedEndOfDayTimer)sender); ! } ! } ! catch(TickerNotExchangedException ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! } ! #endregion ! ! #region OneHourAfterMarketCloseEventHandler ! protected virtual void updateReturnsManager(EndOfDayDateTime firstEndOfDayDateTime, ! EndOfDayDateTime lastEndOfDayDateTime) { ! this.returnsManager = new ReturnsManager(new CloseToCloseIntervals(firstEndOfDayDateTime, lastEndOfDayDateTime, ! this.benchmark.Ticker, this.numDaysForOscillatingPeriod), ! this.historicalQuoteProvider); } *************** *** 471,491 **** logItem.NumberOfEligibleTickers = eligibleTickers.Count; ! logItem.FitnessOfFirst = this.chosenPVOPositions[0].FitnessInSample; ! logItem.FitnessOfLast = this.chosenPVOPositions[this.chosenPVOPositions.Length - 1].FitnessInSample; ! logItem.GenerationOfFirst = ((IGeneticallyOptimizable)this.chosenPVOPositions[0]).Generation; ! logItem.GenerationOfLast = ((IGeneticallyOptimizable)this.chosenPVOPositions[this.chosenPVOPositions.Length - 1]).Generation; ! logItem.ThresholdsOfFirst = ((PVOPositions)this.chosenPVOPositions[0]).OversoldThreshold.ToString() + ";" + ((PVOPositions)this.chosenPVOPositions[0]).OverboughtThreshold.ToString(); ! logItem.ThresholdsOfLast = ((PVOPositions)this.chosenPVOPositions[this.chosenPVOPositions.Length - 1]).OversoldThreshold.ToString() + ";" + ((PVOPositions)this.chosenPVOPositions[this.chosenPVOPositions.Length - 1]).OverboughtThreshold.ToString(); ! logItem.TickersOfFirst = this.chosenPVOPositions[0].HashCodeForTickerComposition; ! logItem.TickersOfLast = this.chosenPVOPositions[this.chosenPVOPositions.Length - 1].HashCodeForTickerComposition; return logItem; --- 475,495 ---- logItem.NumberOfEligibleTickers = eligibleTickers.Count; ! logItem.FitnessOfFirst = this.chosenPVOPositions[0].FitnessInSample; ! logItem.FitnessOfLast = this.chosenPVOPositions[this.chosenPVOPositions.Length - 1].FitnessInSample; ! logItem.GenerationOfFirst = ((IGeneticallyOptimizable)this.chosenPVOPositions[0]).Generation; ! logItem.GenerationOfLast = ((IGeneticallyOptimizable)this.chosenPVOPositions[this.chosenPVOPositions.Length - 1]).Generation; ! logItem.ThresholdsOfFirst = ((PVOPositions)this.chosenPVOPositions[0]).OversoldThreshold.ToString() + ";" + ((PVOPositions)this.chosenPVOPositions[0]).OverboughtThreshold.ToString(); ! logItem.ThresholdsOfLast = ((PVOPositions)this.chosenPVOPositions[this.chosenPVOPositions.Length - 1]).OversoldThreshold.ToString() + ";" + ((PVOPositions)this.chosenPVOPositions[this.chosenPVOPositions.Length - 1]).OverboughtThreshold.ToString(); ! logItem.TickersOfFirst = this.chosenPVOPositions[0].HashCodeForTickerComposition; ! logItem.TickersOfLast = this.chosenPVOPositions[this.chosenPVOPositions.Length - 1].HashCodeForTickerComposition; return logItem; *************** *** 524,536 **** } ! protected virtual void updateTestingPositions(DateTime currentDate) ! { EndOfDayHistory endOfDayHistory = this.benchmark.GetEndOfDayHistory( ! new EndOfDayDateTime(currentDate.AddDays(-this.inSampleDays), ! EndOfDaySpecificTime.MarketClose), ! new EndOfDayDateTime(currentDate, ! EndOfDaySpecificTime.MarketClose)); ! EligibleTickers eligibles = this.eligiblesSelector.GetEligibleTickers(endOfDayHistory); this.updateReturnsManager(endOfDayHistory.FirstEndOfDayDateTime, --- 528,540 ---- } ! protected virtual void updateTestingPositions(DateTime currentDate) ! { EndOfDayHistory endOfDayHistory = this.benchmark.GetEndOfDayHistory( ! new EndOfDayDateTime(currentDate.AddDays(-this.inSampleDays), ! EndOfDaySpecificTime.MarketClose), ! new EndOfDayDateTime(currentDate, ! EndOfDaySpecificTime.MarketClose)); ! EligibleTickers eligibles = this.eligiblesSelector.GetEligibleTickers(endOfDayHistory); this.updateReturnsManager(endOfDayHistory.FirstEndOfDayDateTime, *************** *** 540,566 **** this.updateTestingPositions_updateThresholds(); this.logOptimizationInfo(eligibles); ! } ! ! /// <summary> ! /// Handles a "One hour after market close" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public virtual void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! this.lastCloseDate = endOfDayTimingEventArgs.EndOfDayDateTime.DateTime; ! this.numDaysElapsedSinceLastOptimization++; ! if((this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization)) ! //num days without optimization has elapsed ! { ! this.updateTestingPositions(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime); ! //sets tickers to be chosen next Market Close event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! ! } #endregion ! } } --- 544,570 ---- this.updateTestingPositions_updateThresholds(); this.logOptimizationInfo(eligibles); ! } ! ! /// <summary> ! /// Handles a "One hour after market close" event. ! /// </summary> ! /// <param name="sender"></param> ! /// <param name="eventArgs"></param> ! public virtual void OneHourAfterMarketCloseEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { ! this.lastCloseDate = endOfDayTimingEventArgs.EndOfDayDateTime.DateTime; ! this.numDaysElapsedSinceLastOptimization++; ! if((this.numDaysElapsedSinceLastOptimization == ! this.numDaysBetweenEachOptimization)) ! //num days without optimization has elapsed ! { ! this.updateTestingPositions(endOfDayTimingEventArgs.EndOfDayDateTime.DateTime); ! //sets tickers to be chosen next Market Close event ! this.numDaysElapsedSinceLastOptimization = 0; ! } ! ! } #endregion ! } } Index: PVO_OTCMain.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/PVO_OTCMain.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** PVO_OTCMain.cs 14 Aug 2008 23:28:06 -0000 1.6 --- PVO_OTCMain.cs 19 Aug 2008 17:12:59 -0000 1.7 *************** *** 208,212 **** { string pathForTheMainFolderWhereScriptsResultsAreToBeSaved = ! System.Configuration.ConfigurationSettings.AppSettings["LogArchive"]; return pathForTheMainFolderWhereScriptsResultsAreToBeSaved; } --- 208,212 ---- { string pathForTheMainFolderWhereScriptsResultsAreToBeSaved = ! System.Configuration.ConfigurationManager.AppSettings["LogArchive"]; return pathForTheMainFolderWhereScriptsResultsAreToBeSaved; } Index: PVO_OTCStrategy.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/PVO_OTCStrategy.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** PVO_OTCStrategy.cs 14 Aug 2008 23:28:06 -0000 1.5 --- PVO_OTCStrategy.cs 19 Aug 2008 17:13:00 -0000 1.6 *************** *** 3,7 **** PVO_OTCStrategy.cs ! Copyright (C) 2008 Marco Milletti --- 3,7 ---- PVO_OTCStrategy.cs ! Copyright (C) 2008 Marco Milletti *************** *** 19,23 **** 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; --- 19,23 ---- 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; *************** *** 52,104 **** { /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the Portfolio Value ! /// Oscillator ! /// </summary> ! [Serializable] ! public class PVO_OTCStrategy : IEndOfDayStrategyForBacktester ! { public event NewLogItemEventHandler NewLogItem; public event NewMessageEventHandler NewMessage; //initialized by the constructor ! protected int inSampleDays; ! protected int numDaysBetweenEachOptimization; ! protected IInSampleChooser inSampleChooser; ! protected IEligiblesSelector eligiblesSelector; ! protected Benchmark benchmark; ! protected HistoricalQuoteProvider historicalQuoteProvider; ! protected double oversoldThreshold; ! protected double overboughtThreshold; ! protected double oversoldThresholdMAX; ! protected double overboughtThresholdMAX; ! //initialized after constructor's call ! protected int numDaysElapsedSinceLastOptimization; ! protected ReturnsManager returnsManager; protected TestingPositions[] chosenPVOPositions; //chosen in sample: these are the eligible positions for out //of sample testing protected PVOPositions pvoPositionsForOutOfSample; ! protected DateTime lastCloseDate; protected DateTime lastOptimizationDateTime; ! protected Account account; ! public Account Account { ! get { return this.account; } ! set { this.account = value; } } ! private int numOfClosingsToCrossBeforeExit; ! private int numOfClosingsWithOpenPositions; ! private int minimumNumberOfEligiblesForValidOptimization; ! private bool optimalPositionsHaveBeenUpdated; ! ! private string description_GetDescriptionForChooser() ! { ! if(this.inSampleChooser == null) ! return "ConstantChooser"; ! else ! return this.inSampleChooser.Description; ! } ! public string Description { --- 52,104 ---- { /// <summary> ! /// Implements MarketOpenEventHandler and MarketCloseEventHandler ! /// These handlers contain the core strategy for the Portfolio Value ! /// Oscillator ! /// </summary> ! [Serializable] ! public class PVO_OTCStrategy : IEndOfDayStrategyForBacktester ! { public event NewLogItemEventHandler NewLogItem; public event NewMessageEventHandler NewMessage; //initialized by the constructor ! protected int inSampleDays; ! protected int numDaysBetweenEachOptimization; ! protected IInSampleChooser inSampleChooser; ! protected IEligiblesSelector eligiblesSelector; ! protected Benchmark benchmark; ! protected HistoricalQuoteProvider historicalQuoteProvider; ! protected double oversoldThreshold; ! protected double overboughtThreshold; ! protected double oversoldThresholdMAX; ! protected double overboughtThresholdMAX; ! //initialized after constructor's call ! protected int numDaysElapsedSinceLastOptimization; ! protected ReturnsManager returnsManager; protected TestingPositions[] chosenPVOPositions; //chosen in sample: these are the eligible positions for out //of sample testing protected PVOPositions pvoPositionsForOutOfSample; ! protected DateTime lastCloseDate; protected DateTime lastOptimizationDateTime; ! protected Account account; ! public Account Account { ! get { return this.account; } ! set { this.account = value; } } ! private int numOfClosingsToCrossBeforeExit; ! private int numOfClosingsWithOpenPositions; ! private int minimumNumberOfEligiblesForValidOptimization; ! private bool optimalPositionsHaveBeenUpdated; ! ! private string description_GetDescriptionForChooser() ! { ! if(this.inSampleChooser == null) ! return "ConstantChooser"; ! else ! return this.inSampleChooser.Description; ! } ! public string Description { *************** *** 127,140 **** private void pvo_otcStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider) { this.numOfClosingsWithOpenPositions = 0; --- 127,140 ---- private void pvo_otcStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider) { this.numOfClosingsWithOpenPositions = 0; *************** *** 154,238 **** } ! public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider) ! ! { ! this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider); this.inSampleChooser = inSampleChooser; ! } ! public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider) ! ! { this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, oversoldThreshold, overboughtThreshold, ! double.MaxValue, double.MaxValue, ! historicalQuoteProvider); this.inSampleChooser = inSampleChooser; ! } public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider) ! { this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, ! oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider); this.chosenPVOPositions = chosenPVOPositions; } public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider) ! { this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, ! oversoldThreshold, overboughtThreshold, ! double.MaxValue, double.MaxValue, ! historicalQuoteProvider); this.chosenPVOPositions = chosenPVOPositions; } --- 154,238 ---- } ! public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider) ! ! { ! this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider); this.inSampleChooser = inSampleChooser; ! } ! public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! IInSampleChooser inSampleChooser, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider) ! ! { this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, oversoldThreshold, overboughtThreshold, ! double.MaxValue, double.MaxValue, ! historicalQuoteProvider); this.inSampleChooser = inSampleChooser; ! } public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! double oversoldThresholdMAX, ! double overboughtThresholdMAX, ! HistoricalQuoteProvider historicalQuoteProvider) ! { this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, ! oversoldThreshold, overboughtThreshold, ! oversoldThresholdMAX, overboughtThresholdMAX, ! historicalQuoteProvider); this.chosenPVOPositions = chosenPVOPositions; } public PVO_OTCStrategy(IEligiblesSelector eligiblesSelector, ! int minimumNumberOfEligiblesForValidOptimization, ! TestingPositions[] chosenPVOPositions, ! int inSampleDays, ! Benchmark benchmark, ! int numDaysBetweenEachOptimization, ! int numOfClosingsToCrossBeforeExit, ! double oversoldThreshold, ! double overboughtThreshold, ! HistoricalQuoteProvider historicalQuoteProvider) ! { this.pvo_otcStrategy(eligiblesSelector, minimumNumberOfEligiblesForValidOptimization, ! inSampleDays , benchmark , numDaysBetweenEachOptimization , ! numOfClosingsToCrossBeforeExit, ! oversoldThreshold, overboughtThreshold, ! double.MaxValue, double.MaxValue, ! historicalQuoteProvider); this.chosenPVOPositions = chosenPVOPositions; } *************** *** 244,273 **** { return new EndOfDayDateTime( (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition-1]["quDate"], ! EndOfDaySpecificTime.MarketClose ); } private PVOPositionsStatus marketOpenEventHandler_openPositions_getStatus(IndexBasedEndOfDayTimer timer) ! { ! EndOfDayDateTime today = timer.GetCurrentTime(); ! EndOfDayDateTime beginOfOscillatingPeriod = ! this.getBeginOfOscillatingPeriod(timer); ! PVOPositionsStatus currentStatus = ! PVOPositionsStatus.InTheMiddle; ! for(int i = 0; i<this.chosenPVOPositions.Length; i++) ! { ! if(this.chosenPVOPositions[i] != null) ! currentStatus = ! ((PVOPositions)this.chosenPVOPositions[i]).GetStatus(beginOfOscillatingPeriod, today, ! this.benchmark.Ticker, this.historicalQuoteProvider, ! this.oversoldThresholdMAX, this.overboughtThresholdMAX); ! if(currentStatus == PVOPositionsStatus.Oversold || ! currentStatus == PVOPositionsStatus.Overbought ) ! { this.pvoPositionsForOutOfSample = (PVOPositions)this.chosenPVOPositions[i]; i = this.chosenPVOPositions.Length;//exit from for } ! } ! return currentStatus; ! } protected void marketOpenEventHandler_openPositions(IndexBasedEndOfDayTimer timer) --- 244,273 ---- { return new EndOfDayDateTime( (DateTime)timer.IndexQuotes.Rows[timer.CurrentDateArrayPosition-1]["quDate"], ! EndOfDaySpecificTime.MarketClose ); } private PVOPositionsStatus marketOpenEventHandler_openPositions_getStatus(IndexBasedEndOfDayTimer timer) ! { ! EndOfDayDateTime today = timer.GetCurrentTime(); ! EndOfDayDateTime beginOfOscillatingPeriod = ! this.getBeginOfOscillatingPeriod(timer); ! PVOPositionsStatus currentStatus = ! PVOPositionsStatus.InTheMiddle; ! for(int i = 0; i<this.chosenPVOPositions.Length; i++) ! { ! if(this.chosenPVOPositions[i] != null) ! currentStatus = ! ((PVOPositions)this.chosenPVOPositions[i]).GetStatus(beginOfOscillatingPeriod, today, ! this.benchmark.Ticker, this.historicalQuoteProvider, ! this.oversoldThresholdMAX, this.overboughtThresholdMAX); ! if(currentStatus == PVOPositionsStatus.Oversold || ! currentStatus == PVOPositionsStatus.Overbought ) ! { this.pvoPositionsForOutOfSample = (PVOPositions)this.chosenPVOPositions[i]; i = this.chosenPVOPositions.Length;//exit from for } ! } ! return currentStatus; ! } protected void marketOpenEventHandler_openPositions(IndexBasedEndOfDayTimer timer) *************** *** 275,329 **** PVOPositionsStatus pvoPositionsStatus = PVOPositionsStatus.InTheMiddle; if(timer.CurrentDateArrayPosition >= 1) ! pvoPositionsStatus = this.marketOpenEventHandler_openPositions_getStatus(timer); switch (pvoPositionsStatus) { case PVOPositionsStatus.Overbought: - { - #region manage Overbought case - this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); - try - { - AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, - this.account ); - } - catch(Exception ex) - { - ex = ex; - } - finally { this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); } - #endregion - break; - } case PVOPositionsStatus.Oversold: ! { ! AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, ! this.account ); ! break; ! } case PVOPositionsStatus.InTheMiddle://that is ! { //pvoPositionsForOutOfSample has not been set ! ! break; ! } default: ! { ! //it should never been reached ! break; ! } } } ! public virtual void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { if ( this.account.Portfolio.Count == 0 && ! this.chosenPVOPositions != null && ! this.optimalPositionsHaveBeenUpdated == true) ! //portfolio is empty and optimization has ! //been already successfully launched { try{ --- 275,329 ---- PVOPositionsStatus pvoPositionsStatus = PVOPositionsStatus.InTheMiddle; if(timer.CurrentDateArrayPosition >= 1) ! pvoPositionsStatus = this.marketOpenEventHandler_openPositions_getStatus(timer); switch (pvoPositionsStatus) { case PVOPositionsStatus.Overbought: { + #region manage Overbought case this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); + try + { + AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, + this.account ); + } + catch(Exception ex) + { + string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; + } + finally + { + this.pvoPositionsForOutOfSample.WeightedPositions.Reverse(); + } + #endregion + break; } case PVOPositionsStatus.Oversold: ! { ! AccountManager.OpenPositions( this.pvoPositionsForOutOfSample.WeightedPositions, ! this.account ); ! break; ! } case PVOPositionsStatus.InTheMiddle://that is ! { //pvoPositionsForOutOfSample has not been set ! ! break; ! } default: ! { ! //it should never been reached ! break; ! } } } ! public virtual void MarketOpenEventHandler( ! Object sender , EndOfDayTimingEventArgs endOfDayTimingEventArgs ) ! { if ( this.account.Portfolio.Count == 0 && ! this.chosenPVOPositions != null && ! this.optimalPositionsHaveBeenUpdated == true) ! //portfolio is empty and optimization has ! //been already successfully launched { try{ *************** *** 331,337 **** ... [truncated message content] |