Пример #1
0
        static void TrainWithIndexData()
        {
            List <double[]> data = new List <double[]>();

            var symbol        = YSQSymbol.YSQIndex.SNP;
            var begin         = new DateTime(2004, 1, 1);
            var end           = new DateTime(2008, 1, 1);
            var samplePackage = new YSQProvider().GetHistory(new Asset(symbol, AssetType.Index), begin, end, null);
            var barPackage    = (IBarPackage)samplePackage;
            var bars          = barPackage.Samples;

            TaResult indicator0 = bars.SMA(7);
            TaResult indicator1 = bars.SMA(14);
            TaResult indicator2 = bars.SMA(24);
            TaResult indicator3 = bars.SMA(60);

            List <TaResult> indicatorsList = new List <TaResult> {
                indicator0, indicator1, indicator2, indicator3
            };

            // discard bars and indicators previous to valid series
            int firstValidSampleOnSeries = TaResult.GetFirstValidSample(indicatorsList);

            Console.WriteLine("First valid sample on series: {0}", firstValidSampleOnSeries);

            bars.RemoveRange(0, firstValidSampleOnSeries);
            TaResult.DiscardFirstSamples(indicatorsList, firstValidSampleOnSeries);
            Console.WriteLine("Invalid samples removed");

            // possible answers
            var stay    = new double[] { 1, 0, 0 };
            var goLong  = new double[] { 0, 1, 0 };
            var goShort = new double[] { 0, 0, 1 };

            var deltaPrices = bars.Select(x => x.Close - x.Open).ToList();
            var action      = deltaPrices.Select(deltaPrice => {
                var abs = Math.Abs(deltaPrice);
                if (abs < 5)
                {
                    return(stay);
                }
                if (deltaPrice < 0)
                {
                    return(goShort);
                }
                return(goLong);
            }).ToList();

            Console.WriteLine("Times to stay: {0}", action.Count(x => x == stay));
            Console.WriteLine("Times to go long: {0}", action.Count(x => x == goLong));
            Console.WriteLine("Times to go short: {0}", action.Count(x => x == goShort));

            int samplesCount = bars.Count;

            for (int i = 0; i < samplesCount; i++)
            {
                var entry = new double[] {
                    indicator0.InstantValues[i].Value,
                    indicator1.InstantValues[i].Value,
                    indicator2.InstantValues[i].Value,
                    indicator3.InstantValues[i].Value,
                    action[i][0],
                    action[i][1],
                    action[i][2]
                };
                data.Add(entry);
            }

            NeuralNetwork neuralNetwork = Trainer.Execute(data);

            ChartPool.CreateChart();

            int     takeProfitPoints   = 15;
            int     stopLossPoints     = 10;
            decimal intensityThreshold = 0.0m;

            indicator0.Normalize();
            indicator1.Normalize();
            indicator2.Normalize();
            indicator3.Normalize();

            var simulation = new NNSimulation(neuralNetwork, intensityThreshold, takeProfitPoints, stopLossPoints, true);
            SimulationRunner simulationRunner = new SimulationRunner(bars, simulation);

            simulationRunner.AddSerie("INDICATOR_0", indicator0);
            simulationRunner.AddSerie("INDICATOR_1", indicator1);
            simulationRunner.AddSerie("INDICATOR_2", indicator2);
            simulationRunner.AddSerie("INDICATOR_3", indicator3);
            simulationRunner.Execute();
            ShowSimulation(simulation, bars, indicator0, indicator1, indicator2, indicator3);
        }