Testing with Fake MarketSeries Data

Created at 03 Feb 2015, 20:43
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
GR

gregschr

Joined 17.08.2014

Testing with Fake MarketSeries Data
03 Feb 2015, 20:43


I am trying to set up unit tests for indicators that I am developing.  How can I use fake MarketSeries data in the Calculate method.

Below is an example of code where I would like to test with the fake MarketSeries data.

    public class _SwingStrength : Indicator
    {
        ...
        public override void Calculate(int newBarFormingIndex)
        {
         ...
         double potentialSwingBarHigh = CalculateBarValue(newBarFormingIndex, "High", false, false); 
         ...
         }

        private double CalculateBarValue(int barIndex, string highOrLow, bool useHeikenAshiBars, bool useOpenClose)
        {
            double returnError = -1;
            if (useHeikenAshiBars) {...}
            else if (useOpenClose) {...}
            else
            {
                if (highOrLow == "High")
                    return MarketSeries.High[barIndex];
                else if (highOrLow == "Low")
                    return MarketSeries.Low[barIndex];
                else
                    return returnError;
            }
            return returnError;
     }        
}

Any help you can give me is greatly appreciated!

Greg


@gregschr
Replies

gregschr
04 Feb 2015, 04:45

I think I figured this out. By introducing a seam between my indicator and the Indicator class I can now override the MarketSeries getter and setter (and any other properties or methods) in my unit tests.  When the indicator is running normally in cAlgo, the MarketSeries data from the Indicator class is returned as usual.

    public class IndicatorSeam : Indicator
    {
		private MarketSeries _marketSeries;
		public virtual new MarketSeries MarketSeries
		{
			get
			{
				if ( _marketSeries == null ) return base.MarketSeries;
				else return _marketSeries;
			}
			set { _marketSeries = value; }
		}
		public override void Calculate(int index)
		{
			// Do nothing here
		}
    }

    [Indicator("Swing Strength", IsOverlay = true, TimeZone = TimeZones.CentralStandardTime, AccessRights = AccessRights.FileSystem)]
    public class _SwingStrength : IndicatorSeam
    {
          ...
    }

 


@gregschr

AlexanderRC
04 Feb 2015, 22:23

You even try to not inherit from Indicator and reimplement the feauters you require from it as stubs

And do conditional compilation whether you are running as an externally invoked unit test or inside cAlgo compilation engine. But cAlgo compilation engine ignores symbols defined in Visual Studio properties. You may have to #define you conditional compilation manually or at the unit test project which would not depend on cAlgo API directly and could be run independently.

See /forum/calgo-support/3827 for more info.


@AlexanderRC