[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOs
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2008-03-09 22:49:27
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/FitnessEvaluators In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv6639/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/FitnessEvaluators Added Files: PVOFitnessEvaluator.cs Log Message: Added a new implementation for the PVO strategy (now common objects recently added at business layer by Glauco are used). At the moment, these files perform a PVO strategy using close to close returns, on a multiday - minimum 1 day - basis. Take profit and stop loss levels can be set. --- NEW FILE: PVOFitnessEvaluator.cs --- /* QuantProject - Quantitative Finance Library PVOFitnessEvaluator.cs Copyright (C) 2008 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.ADT; using QuantProject.Business.Strategies; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies.Optimizing.FitnessEvaluation; using QuantProject.Business.Strategies.EquityEvaluation; using QuantProject.Business.Strategies.ReturnsManagement; namespace QuantProject.Scripts.TechnicalAnalysisTesting.Oscillators.FixedLevelOscillators.PortfolioValueOscillator.FitnessEvaluators { /// <summary> /// Evaluates (in sample) the fitness for a given TestingPositions /// </summary> public class PVOFitnessEvaluator : IFitnessEvaluator { private IEquityEvaluator strategyEquityEvaluator; public string Description { get { string description = "FitnessEvaluatedWith_" + this.strategyEquityEvaluator.GetType().ToString(); return description; } } public PVOFitnessEvaluator(IEquityEvaluator strategyEquityEvaluator) { this.strategyEquityEvaluator = strategyEquityEvaluator; } #region GetFitnessValue //returns true if the strategy gets effective positions on //the market for at least half the market days private bool getFitnessValue_getFitnessValueActually_strategyGetsSufficientPositions(float[] strategyReturns) { int daysOnTheMarket = 0; foreach(float strategyReturn in strategyReturns) if(strategyReturn != 0) //the applied strategy gets positions on the market daysOnTheMarket++; return daysOnTheMarket >= strategyReturns.Length / 2; } private float getFitnessValue_getFitnessValueActually( TestingPositions testingPositions, ReturnsManager returnsManager ) { float fitnessValue = -0.5f; PVOPositions pvoPositions = (PVOPositions)testingPositions; float oversoldThreshold = Convert.ToSingle(pvoPositions.OversoldThreshold); float overboughtThreshold = Convert.ToSingle(pvoPositions.OverboughtThreshold); float[] plainWeightedPositionsReturns = testingPositions.WeightedPositions.GetReturns(returnsManager); float[] strategyReturns = new float[ plainWeightedPositionsReturns.Length ]; strategyReturns[0] = 0; //a the very first day the //strategy return is equal to 0 because no position //has been entered float coefficient = 0; for(int i = 0; i < strategyReturns.Length - 1; i++) { if( plainWeightedPositionsReturns[i] >= overboughtThreshold ) //portfolio has been overbought coefficient = -1; else if( plainWeightedPositionsReturns[i] <= - oversoldThreshold ) //portfolio has been oversold coefficient = 1; //else the previous coeff is kept or, if no threshold has been //reached, then no positions will be opened (coefficient = 0) strategyReturns[i + 1] = coefficient * plainWeightedPositionsReturns[i + 1]; } if(this.getFitnessValue_getFitnessValueActually_strategyGetsSufficientPositions(strategyReturns)) fitnessValue = this.strategyEquityEvaluator.GetReturnsEvaluation(strategyReturns); return fitnessValue; } public double GetFitnessValue(object meaning , ReturnsManager returnsManager ) { float fitnessValue = -0.5f; TestingPositions testingPositions = (TestingPositions)meaning; if(testingPositions.WeightedPositions != null) fitnessValue = this.getFitnessValue_getFitnessValueActually( testingPositions, returnsManager ); return fitnessValue; } #endregion } } |