[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOs
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2009-08-30 22:08:26
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/EntryConditions In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv22818 Added Files: AlwaysTrueEntryCondition.cs IEntryCondition.cs InefficiencyMovingBackEntryCondition.cs PreviousPeriodsWereEfficientEntryCondition.cs PriceRatioEntryCondition.cs Log Message: Added IEntryCondition interface and some classes implementing it, for the PVO strategy --- NEW FILE: IEntryCondition.cs --- /* QuantProject - Quantitative Finance Library IEntryCondition.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using QuantProject.Business.Strategies.OutOfSample; namespace QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.EntryConditions { /// <summary> /// decides if current positions satisfy the given condition /// </summary> public interface IEntryCondition { bool IsConditionSatisfiedByGivenPVOPositions( DateTime dateTime , PVOPositions pvoPositions ); } } --- NEW FILE: InefficiencyMovingBackEntryCondition.cs --- /* QuantProject - Quantitative Finance Library InefficiencyMovingBackEntryCondition.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies; using QuantProject.Business.Strategies.TickersRelationships; using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator; namespace QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.EntryConditions { /// <summary> /// Description of InefficiencyMovingBackEntryCondition. /// </summary> [Serializable] public class InefficiencyMovingBackEntryCondition : IEntryCondition { private double coefficientForThresholdLevelComputationForMovingBackSignal; private HistoricalMarketValueProvider marketValueProvider; private Benchmark benchmark; public InefficiencyMovingBackEntryCondition(double coefficientForThresholdLevelComputationForMovingBackSignal, HistoricalMarketValueProvider marketValueProvider, Benchmark benchmark) { this.coefficientForThresholdLevelComputationForMovingBackSignal = coefficientForThresholdLevelComputationForMovingBackSignal; this.marketValueProvider = marketValueProvider; this.benchmark = benchmark; } public bool IsConditionSatisfiedByGivenPVOPositions(DateTime dateTime , PVOPositions pvoPositionsForOutOfSample ) { bool returnValue = false; DateTime beginOfOscillatingPeriod = pvoPositionsForOutOfSample.LastInefficiencyDateTime; DateTime endOfOscillatingPeriod = dateTime; PVOPositionsStatus currentStatusForCurrentPositions = PVOPositionsStatus.InTheMiddle; if( pvoPositionsForOutOfSample != null ) try{ currentStatusForCurrentPositions = pvoPositionsForOutOfSample.GetStatus( beginOfOscillatingPeriod, endOfOscillatingPeriod, this.benchmark.Ticker, this.marketValueProvider, pvoPositionsForOutOfSample.OversoldThreshold * coefficientForThresholdLevelComputationForMovingBackSignal, pvoPositionsForOutOfSample.OversoldThreshold, pvoPositionsForOutOfSample.OverboughtThreshold * coefficientForThresholdLevelComputationForMovingBackSignal, pvoPositionsForOutOfSample.OverboughtThreshold ); }catch{} returnValue = ( (currentStatusForCurrentPositions == PVOPositionsStatus.Overbought && pvoPositionsForOutOfSample.StatusAtLastInefficiencyTime == PVOPositionsStatus.Oversold) || (currentStatusForCurrentPositions == PVOPositionsStatus.Oversold && pvoPositionsForOutOfSample.StatusAtLastInefficiencyTime == PVOPositionsStatus.Overbought) ); return returnValue; } } } --- NEW FILE: PreviousPeriodsWereEfficientEntryCondition.cs --- /* QuantProject - Quantitative Finance Library PreviousPeriodsWereEfficientEntryCondition.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies; //using QuantProject.Business.Strategies.TickersRelationships; using QuantProject.Business.Timing; using QuantProject.Business.Timing.TimingManagement; using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator; namespace QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.EntryConditions { /// <summary> /// Description of PreviousPeriodsWereEfficientEntryCondition. /// </summary> [Serializable] public class PreviousPeriodsWereEfficientEntryCondition : IEntryCondition { private int numberOfPreviousPeriods; private HistoricalMarketValueProvider marketValueProvider; private double numberOfIntervalsForEachPeriod; private MarketIntervalsManager marketIntervalsManager; private string tickerForBenchmark; public PreviousPeriodsWereEfficientEntryCondition(int numberOfPreviousPeriods, HistoricalMarketValueProvider marketValueProvider, double numberOfIntervalsForEachPeriod, MarketIntervalsManager marketIntervalsManager) { this.numberOfPreviousPeriods = numberOfPreviousPeriods; this.marketValueProvider = marketValueProvider; this.numberOfIntervalsForEachPeriod = numberOfIntervalsForEachPeriod; this.marketIntervalsManager = marketIntervalsManager; this.tickerForBenchmark = marketIntervalsManager.Benchmark.Ticker; } // public PreviousPeriodsWereEfficientEntryCondition(int numberOfPreviousPeriods, // HistoricalMarketValueProvider marketValueProvider, // double numberOfIntervalsForEachPeriod, // Benchmark benchmark) // { // this.numberOfPreviousPeriods = numberOfPreviousPeriods; // this.marketValueProvider = marketValueProvider; // this.numberOfIntervalsForEachPeriod = numberOfIntervalsForEachPeriod; // this.tickerForBenchmark = benchmark.Ticker; // this.marketIntervalsManager = new MarketDaysManager(benchmark, // } private PVOPositionsStatus isConditionSatisfiedByGivenPVOPositions_getStatus( DateTime beginOfOscillatingPeriod, DateTime endOfOscillatingPeriod, PVOPositions pvoPositionsForOutOfSample) { PVOPositionsStatus returnValue = PVOPositionsStatus.NotComputable; if(pvoPositionsForOutOfSample != null) try{ returnValue = pvoPositionsForOutOfSample.GetStatus(beginOfOscillatingPeriod, endOfOscillatingPeriod, this.tickerForBenchmark, this.marketValueProvider); }catch{} return returnValue; } public bool IsConditionSatisfiedByGivenPVOPositions(DateTime dateTime , PVOPositions pvoPositionsForOutOfSample ) { bool returnValue = true; PVOPositionsStatus statusForCurrentPreviousPeriod; for( int i = 1; i <= this.numberOfPreviousPeriods; i++ ) { DateTime beginOfOscillatingPeriod = this.marketIntervalsManager.AddMarketIntervals(dateTime, -(i+1)*(int)this.numberOfIntervalsForEachPeriod); DateTime endOfOscillatingPeriod = this.marketIntervalsManager.AddMarketIntervals (dateTime, -i*(int)this.numberOfIntervalsForEachPeriod); statusForCurrentPreviousPeriod = this.isConditionSatisfiedByGivenPVOPositions_getStatus( beginOfOscillatingPeriod, endOfOscillatingPeriod, pvoPositionsForOutOfSample ); if( statusForCurrentPreviousPeriod != PVOPositionsStatus.InTheMiddle ) { returnValue = false; i = this.numberOfPreviousPeriods; } } return returnValue; } } } --- NEW FILE: PriceRatioEntryCondition.cs --- /* QuantProject - Quantitative Finance Library PriceRatioEntryCondition.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies; using QuantProject.Business.Strategies.TickersRelationships; using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator; namespace QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.EntryConditions { /// <summary> /// Description of PriceRatioEntryCondition. /// </summary> [Serializable] public class PriceRatioEntryCondition : IEntryCondition { private int lengthInDaysForAveragePriceRatioCalculation; private HistoricalMarketValueProvider marketValueProvider; private double numOfStdDevForSignificantPriceMovements; public PriceRatioEntryCondition(int lengthInDaysForAveragePriceRatioCalculation, HistoricalMarketValueProvider marketValueProvider, double numOfStdDevForSignificantPriceMovements) { this.lengthInDaysForAveragePriceRatioCalculation = lengthInDaysForAveragePriceRatioCalculation; this.marketValueProvider = marketValueProvider; this.numOfStdDevForSignificantPriceMovements = numOfStdDevForSignificantPriceMovements; } //da rifare usando marketIntervalsManager public bool IsConditionSatisfiedByGivenPVOPositions(DateTime dateTime , PVOPositions pvoPositionsForOutOfSample ) { bool returnValue = false; try{ if( pvoPositionsForOutOfSample.WeightedPositions.Count != 2 ) throw new Exception("Price ratio condition can be verified only by pairs"); SignedTickers signedTickers = pvoPositionsForOutOfSample.WeightedPositions.SignedTickers; double priceRatioAverage = PriceRatioProvider.GetPriceRatioAverage(signedTickers[0].Ticker, signedTickers[1].Ticker, dateTime.AddDays(-this.lengthInDaysForAveragePriceRatioCalculation), dateTime.AddDays(-1) ); double priceRatioStdDev = PriceRatioProvider.GetPriceRatioStandardDeviation(signedTickers[0].Ticker, signedTickers[1].Ticker, dateTime.AddDays(-this.lengthInDaysForAveragePriceRatioCalculation), dateTime.AddDays(-1) ); double currentPriceRatio = this.marketValueProvider.GetMarketValue(signedTickers[0].Ticker, dateTime ) / this.marketValueProvider.GetMarketValue(signedTickers[1].Ticker, dateTime ); if(currentPriceRatio > priceRatioAverage + this.numOfStdDevForSignificantPriceMovements * priceRatioStdDev || currentPriceRatio < priceRatioAverage - this.numOfStdDevForSignificantPriceMovements * priceRatioStdDev ) returnValue = true; } catch(Exception ex){ string s = ex.ToString(); } return returnValue; } } } --- NEW FILE: AlwaysTrueEntryCondition.cs --- /* QuantProject - Quantitative Finance Library AlwaysTrueEntryCondition.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using QuantProject.Business.DataProviders; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies; using QuantProject.Business.Strategies.TickersRelationships; using QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator; namespace QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.EntryConditions { /// <summary> /// Description of AlwaysTrueEntryCondition. /// </summary> [Serializable] public class AlwaysTrueEntryCondition : IEntryCondition { public AlwaysTrueEntryCondition() { } public bool IsConditionSatisfiedByGivenPVOPositions(DateTime dateTime , PVOPositions pvoPositionsForOutOfSample ) { return true; } } } |