예제 #1
0
        /// <summary>
        /// Runs a series of tests
        /// </summary>
        /// <param name="tests">The tests to run</param>
        public void Run(IEnumerable <Test> tests)
        {
            var grouping = tests.GroupBy(m => m.suite);

            foreach (var testCase in grouping)
            {
                var caseInstance = testCase.Key.type.GetConstructor(new Type[0]).Invoke(new object[0]);
                if (testCase.Key.URL != null)
                {
                    Navigate(testCase.Key.URL);
                }
                foreach (var test in testCase)
                {
                    try
                    {
                        test.RunTest(caseInstance, this);
                        OnTestComplete?.Invoke(test, null);
                    }
                    catch (TargetInvocationException er)
                    {
                        var ex = er.InnerException;
                        OnTestComplete?.Invoke(test, ex as AssertionFailure ?? new AssertionFailure(ex.Message, ex));
                    }
                }
            }
        }
예제 #2
0
 private void Test(IEnumerable <Digit> digits)
 {
     double[,] nn_result;
     foreach (var digit in digits)
     {
         nn_result = Run(MakeInput(digit.PixelSet));
         OnTestComplete?.Invoke(MakeOutput(nn_result) == digit.Label);
     }
 }
예제 #3
0
        private void CleanUpTest(TestBase _test)
        {
            try
            {
                _test.CleanUp();
            }
            catch (Exception ex)
            {
                _test.Exception = ex;
                OnException?.Invoke(_test, ex);
                throw ex;
            }

            OnTestComplete?.Invoke(_test);
        }
예제 #4
0
        /// <summary>
        /// Execute all tests and return IEnumerable with test output
        /// </summary>
        /// <param name="assembly">assembly with test methods</param>
        /// <param name="tasksCount">amount of parallel tests (use processorCount when -1)</param>
        /// <returns>IEnumerable with test output</returns>
        public static async Task <IEnumerable <TestResult> > RunTestsAsync(Assembly assembly = null,
                                                                           int tasksCount    = -1)
        {
            var testResults = new List <Task <TestResult> >();

            using var maxTasks = new SemaphoreSlim(tasksCount == -1 ? Environment.ProcessorCount : tasksCount);
            foreach (var test in LoadTests(assembly))
            {
                await maxTasks.WaitAsync();

                OnTestStart?.Invoke(test);

                var task = test.Execute();
                testResults.Add(task);
                task.ContinueWith(ta =>
                {
                    OnTestComplete?.Invoke(ta.Result);
                    return(maxTasks.Release());
                });
            }

            return(await Task.WhenAll(testResults));
        }
예제 #5
0
        public void Test(Queue <BackTestTask> tasks, OnTestComplete onComplete)
        {
            while (tasks.Count > 0)
            {
                BackTestTask task = tasks.Dequeue();

                Strategy strategy = task.Strategy;
                Asset    asset    = task.Asset;

                //Record start time of process for time taken message
                DateTime now = DateTime.Now;


                byte[] bytes = asset.Dataset;

                //add in the required minute bars with a 1 bar lookback
                //If a strategy needs these bars it will be overwritted in the next foreach loop
                strategy.InitBarData();

                //keep a pointer of the last added bar and date so this can be modified while building up the higher timeframes
                Dictionary <int, Bar>      lastAddedBars  = new Dictionary <int, Bar>();
                Dictionary <int, DateTime> lastAddedDates = new Dictionary <int, DateTime>();


                //Traverse the byte array to build the bars - this can be done on separate threads for each asset for maximum speed
                int i   = 0;
                Bar bar = null;
                while (i < bytes.Length)
                {
                    bar = DataBuilder.ReadBinaryBar(bytes, (i / 44));
                    //move to next line in byte array - 1 bar is 44 bytes
                    i += 44;

                    //skip edge of market bars like Zorro does

                    /*
                     * if ((bar.OpenTime.DayOfWeek == DayOfWeek.Friday && bar.OpenTime.Hour > 18) ||
                     *  (bar.OpenTime.DayOfWeek == DayOfWeek.Sunday && bar.OpenTime.Hour < 22))
                     *  continue;
                     */

                    //go through each timeframe and either create a new bar or increment the current bar
                    foreach (KeyValuePair <int, Bar[]> barSet in strategy.Datasets)
                    {
                        ///////////////////////////////////////////////////////////////////////////////////
                        /////This marked section takes about half the processing time
                        ///////////////////////////////

                        //create a bardate pegged to the nearest timeframe
                        DateTime barDate;
                        if (barSet.Key == 1)
                        {
                            barDate = bar.OpenTime;
                        }
                        else
                        {
                            //Peg the bar to the start of the timeframe
                            TimeSpan d = TimeSpan.FromMinutes(barSet.Key);
                            barDate = (new DateTime((bar.OpenTime.Ticks + d.Ticks) / d.Ticks * d.Ticks, bar.OpenTime.Kind)).AddMinutes(-barSet.Key);
                        }

                        //Keep a record of the last added bars date or set it to the current bar date on the first run
                        DateTime lastAddedDate;
                        if (lastAddedDates.ContainsKey(barSet.Key))
                        {
                            lastAddedDate = lastAddedDates[barSet.Key];
                        }
                        else
                        {
                            lastAddedDate = barDate;
                        }

                        DateTime nextBar = ((DateTime)lastAddedDate).AddMinutes(barSet.Key);
                        /////////////////////////////////////////////////////////////
                        /////////////
                        ///////////////////////////////////////////////////////////////

                        //add the bar to all timeframes if it doesnt exist
                        if ((nextBar <= barDate) || !lastAddedDates.ContainsKey(barSet.Key))
                        {
                            //need a new bar for each time frame so we don't have the same pointer in each timeframe
                            Bar setBar = new Bar(bar);

                            //shift the bar array
                            Array.Copy(barSet.Value, 0, barSet.Value, 1, barSet.Value.Length - 1);

                            barSet.Value[0]            = setBar;
                            lastAddedBars[barSet.Key]  = setBar;
                            lastAddedDates[barSet.Key] = barDate;

                            //Update the Trades
                            if (barSet.Key == 1)
                            {
                                strategy.UpdateTrades(strategy.Datasets[1][1]);
                            }

                            //run the strategy (only if this timeframe was in the required data
                            //eg. minute data is always used but strategy may not request it and therefore won't run code on that timeframe
                            if (strategy.RequiredData.ContainsKey(barSet.Key))
                            {
                                if (TestSummary.StartDate == null)
                                {
                                    TestSummary.StartDate = bar.OpenTime;
                                }

                                //run the bar
                                strategy.Run(barSet.Key, asset.Name);
                            }

                            strategy.BarIndices[barSet.Key]++;
                        }
                        //don't need to increment bars on the 1 minute time frame
                        else if (barSet.Key > 1)
                        {
                            //get the lastAdded bar which is the start of this timeframe
                            Bar lastAdded = lastAddedBars[barSet.Key];

                            //We adjust the bar for the max,min,vol and close
                            if (bar.BidHigh > lastAdded.BidHigh)
                            {
                                lastAdded.BidHigh = bar.BidHigh;
                            }
                            if (bar.BidLow < lastAdded.BidLow)
                            {
                                lastAdded.BidLow = bar.BidLow;
                            }
                            if (bar.AskHigh > lastAdded.AskHigh)
                            {
                                lastAdded.AskHigh = bar.AskHigh;
                            }
                            if (bar.AskLow < lastAdded.AskLow)
                            {
                                lastAdded.AskLow = bar.AskLow;
                            }
                            lastAdded.BidClose = bar.BidClose;
                            lastAdded.AskClose = bar.AskClose;
                            lastAdded.Volume  += bar.Volume;
                        }
                    }
                }


                if (TestSummary.EndDate == null)
                {
                    TestSummary.EndDate = bar.OpenTime;
                }

                if (strategy.CloseOnExit)
                {
                    strategy.BTCloseAllTrades(bar);
                }

                //create a performance report and add it to the list
                TestSet ts = new TestSet(asset.Name, strategy.Description, strategy.ClosedTrades);

                //clear the data ready for the nxt asset
                strategy.ResetData();

                onComplete.Invoke(ts);
            }
        }
예제 #6
0
        private void CompleteTest()
        {
            float result = Questions.Sum(x => x.GetScore());

            OnTestComplete?.Invoke(result);
        }