[Quantproject-developers] QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOs
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2009-08-30 21:42:42
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/FitnessEvaluators In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv20386/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/FitnessEvaluators Modified Files: PVOFitnessEvaluator.cs Log Message: GetFitnessValue method has been updated (it is now more similar to what should happen out of sample) Index: PVOFitnessEvaluator.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TechnicalAnalysisTesting/Oscillators/FixedLevelOscillators/PortfolioValueOscillator/FitnessEvaluators/PVOFitnessEvaluator.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PVOFitnessEvaluator.cs 9 Mar 2008 22:49:24 -0000 1.1 --- PVOFitnessEvaluator.cs 30 Aug 2009 21:42:33 -0000 1.2 *************** *** 35,41 **** /// Evaluates (in sample) the fitness for a given TestingPositions /// </summary> ! public class PVOFitnessEvaluator : IFitnessEvaluator { private IEquityEvaluator strategyEquityEvaluator; public string Description --- 35,44 ---- /// Evaluates (in sample) the fitness for a given TestingPositions /// </summary> ! [Serializable] ! public class PVOFitnessEvaluator : IFitnessEvaluator { private IEquityEvaluator strategyEquityEvaluator; + private float takeProfit; + private float stopLoss; public string Description *************** *** 49,60 **** } ! 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) { --- 52,66 ---- } ! public PVOFitnessEvaluator(IEquityEvaluator strategyEquityEvaluator, ! double takeProfit, double stopLoss) { this.strategyEquityEvaluator = strategyEquityEvaluator; + this.takeProfit = Convert.ToSingle(takeProfit); + this.stopLoss = Convert.ToSingle(stopLoss); } #region GetFitnessValue //returns true if the strategy gets effective positions on ! //the market for at least a quarter of the market days private bool getFitnessValue_getFitnessValueActually_strategyGetsSufficientPositions(float[] strategyReturns) { *************** *** 68,71 **** --- 74,92 ---- } + private bool getFitnessValue_getFitnessValueActually_itIsTimeToExit( + float[] strategyReturns, int currentIndex, int indexOfLastSignal ) + { + bool returnValue = false; + float gainOrLoss; + float equityValue = 1; + if(indexOfLastSignal >= 1 ) + for(int i = 0; i + indexOfLastSignal < currentIndex; i++) + equityValue = equityValue + equityValue * strategyReturns[indexOfLastSignal + 1 + i]; + gainOrLoss = equityValue - 1; + if(gainOrLoss >= this.takeProfit || gainOrLoss <= -this.stopLoss) + returnValue = true; + return returnValue; + } + private float getFitnessValue_getFitnessValueActually( TestingPositions testingPositions, ReturnsManager returnsManager ) *************** *** 78,95 **** 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]; } --- 99,135 ---- testingPositions.WeightedPositions.GetReturns(returnsManager); float[] strategyReturns = new float[ plainWeightedPositionsReturns.Length ]; ! strategyReturns[0] = 0; ! strategyReturns[1] = 0;//at the two first days the ! //strategy returns is equal to 0 because no position //has been entered float coefficient = 0; ! int indexOfLastSignal = 0;// the last position where a threshold has been reached ! for(int i = 1; i < strategyReturns.Length - 1; i++) { ! if( plainWeightedPositionsReturns[i] >= overboughtThreshold && ! (plainWeightedPositionsReturns[i - 1] > - oversoldThreshold && ! plainWeightedPositionsReturns[i - 1] < overboughtThreshold ) && ! indexOfLastSignal == 0 ) ! //portfolio has been overbought and in the previous period ! //it was efficient ! { coefficient = -1; ! indexOfLastSignal = i; ! } ! if( plainWeightedPositionsReturns[i] <= - oversoldThreshold && ! (plainWeightedPositionsReturns[i - 1] > - oversoldThreshold && ! plainWeightedPositionsReturns[i - 1] < overboughtThreshold ) && ! indexOfLastSignal == 0 ) ! //portfolio has been oversold and in the previous period ! //it was efficient ! { coefficient = 1; ! indexOfLastSignal = i; ! } ! if ( getFitnessValue_getFitnessValueActually_itIsTimeToExit(strategyReturns, i, indexOfLastSignal) ) ! { ! coefficient = 0; ! indexOfLastSignal = 0; ! } strategyReturns[i + 1] = coefficient * plainWeightedPositionsReturns[i + 1]; } |