Beispiel #1
0
        static void Main(string[] args)
        {
            string         symbol        = YSQSymbol.YSQIndex.SNP;
            DateTime       begin         = new DateTime(2003, 12, 1);
            DateTime       end           = new DateTime(2004, 2, 1);
            ISamplePackage samplePackage = new YSQProvider().GetHistory(new Asset(symbol, AssetType.Index), begin, end, null);
            IBarPackage    barPackage    = (IBarPackage)samplePackage;
            List <IBar>    bars          = barPackage.Samples;
            TaResult       ema13         = bars.EMA(13);
            BullPower      bullPower     = new BullPower(bars, 13);

            //var sampleValues = new List<IInstantValue<double>>();
            //sampleValues.Add(new InstantValue<double>(new DateTime(2004, 1, 1), 1.0));
            //sampleValues.Add(new InstantValue<double>(new DateTime(2004, 1, 2), -1.0));
            //sampleValues.Add(new InstantValue<double>(new DateTime(2004, 1, 3), 1.0));
            //sampleValues.Add(new InstantValue<double>(new DateTime(2004, 1, 4), -1.0));

            ChartPool.CreateChart();
            ChartPool.ClearSeries();
            ChartPool.AddSeries(
                new List <Series> {
                new Series("Prices", ChartType.Lines, Colors.Black, bars.Select(x => new Sample(x.DateTime, (double)x.Close))),
                new Series("EMA(13)", ChartType.Lines, Colors.Red, ema13.InstantValues.Select(x => new Sample(x.DateTime, x.Value))),
                new Series("BullPower(13)", ChartType.Columns, Colors.Blue, bullPower.InstantValues.Select(x => new Sample(x.DateTime, x.Value))),
            }
                );

            Console.WriteLine("Press a key to exit...");
            Console.ReadKey(true);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var socks = Provider.GetStocks();

            socks.ForEach(stock => {
                stock.Update();
                var symbol = stock.Symbol;
                var begin  = new DateTime(2014, 12, 1);
                var end    = new DateTime(2014, 12, 31);
                var asset  = new Asset(symbol, AssetType.Stock);
                try {
                    ISamplePackage samplePackage = new YSQProvider().GetHistory(asset, begin, end, null);
                    IBarPackage barPackage       = (IBarPackage)samplePackage;
                    stock.Update(barPackage);
                    var samples      = barPackage.Samples;
                    int samplesCount = samples.Count;
                    if (samplesCount > 0)
                    {
                        var lastValue = samples.Last().Close;
                        Console.WriteLine("Stock: {0} - Samples: {1} - LastValue: {2}", symbol, samplesCount, lastValue);
                    }
                    else
                    {
                        Console.WriteLine("Stock: {0} - Samples: {1}", symbol, samplesCount);
                    }
                }
                catch {
                    Console.WriteLine("Stock: {0} - NO DATA FOUND", symbol);
                }
                stock.Save();
            });
            Console.ReadKey(true);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var symbol        = YSQSymbol.YSQIndex.SNP;
            var begin         = new DateTime(2000, 1, 1);
            var end           = new DateTime(2015, 1, 1);
            var samplePackage = new YSQProvider().GetHistory(new Asset(symbol, AssetType.Index), begin, end, null);
            var barPackage    = (IBarPackage)samplePackage;
            var bars          = barPackage.Samples;
            //bars.ForEach(x => Console.WriteLine(x));

            IBar          lastBar = null;
            List <double> deltas  = new List <double>();

            foreach (var bar in bars)
            {
                if (lastBar != null)
                {
                    var delta = lastBar.Close - bar.Open;
                    deltas.Add((double)delta);
                }
                lastBar = bar;
            }

            // TODO solve this
            //var smoothDeltas = deltas.ToArray().SMA(6);

            //Console.WriteLine("Press a key to exit...");
            //Console.ReadKey(true);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string         symbol        = YSQSymbol.YSQIndex.SNP;
            DateTime       begin         = new DateTime(2013, 1, 1);
            DateTime       end           = new DateTime(2014, 1, 1);
            ISamplePackage samplePackage = new YSQProvider().GetHistory(new Asset(symbol, AssetType.Index), begin, end, null);
            IBarPackage    barPackage    = (IBarPackage)samplePackage;
            List <IBar>    bars          = barPackage.Samples;
            // simulation
            ISimulation bestSimulation         = default(ISimulation);
            TaResult    bestSimulationSmallEma = default(TaResult);
            TaResult    bestSimulationBigEma   = default(TaResult);

            ChartPool.CreateChart();
            int      configurationsCount = 0;
            DateTime simulationsBegin    = DateTime.Now;

            for (int small = 2; small < 14; small += 1)
            {
                for (int big = 30; big < 80; big += 1)
                {
                    for (int take = 10; take < 50; take += 2)
                    {
                        for (int stop = 10; stop < 50; stop += 2)
                        {
                            configurationsCount++;
                            var simulation = SmaSimulation.CreateLongOnly(2, take, stop);
                            simulation.SimulationInfo = string
                                                        .Format("EMA_SMALL: {0}, EMA_BIG: {1}, TakeProfit: {2}, StopLoss: {3}",
                                                                small, big, take, stop);
                            SimulationRunner simulationRunner = new SimulationRunner(bars, simulation);
                            var smallEma = bars.EMA(small);
                            var bigEma   = bars.EMA(big);
                            simulationRunner.AddSerie("EMA_SMALL", smallEma);
                            simulationRunner.AddSerie("EMA_BIG", bigEma);
                            simulationRunner.Execute();
                            Console.WriteLine(simulation.GetReport());
                            if (bestSimulation == default(ISimulation) || simulation.Earnings > bestSimulation.Earnings)
                            {
                                bestSimulation         = simulation;
                                bestSimulationSmallEma = smallEma;
                                bestSimulationBigEma   = bigEma;
                            }
                            //ShowSimulation(simulation, bars, smallEma, bigEma);
                            //Console.ReadKey(true);
                        }
                    }
                }
            }
            Console.WriteLine("{0} configurations tested in {1}", configurationsCount, DateTime.Now - simulationsBegin);
            Console.WriteLine("Best simulation");
            ShowSimulation(bestSimulation, bars, bestSimulationSmallEma, bestSimulationBigEma);
            Console.ReadKey(true);
        }
Beispiel #5
0
        public void TestActualPrice()
        {
            ISample sample = new YSQProvider().GetPrice(new Asset("MSFT", AssetType.Stock));

            Assert.IsTrue(sample is IQuote);
            IQuote quote = (IQuote)sample;

            Assert.IsNotNull(quote.Asset);
            Assert.IsNotNull(quote.Source);
            Assert.IsNotNull(quote.DateTime);
            Assert.IsTrue(quote.Ask > 0);
        }
Beispiel #6
0
        public void TestHistoricalPrices()
        {
            ISamplePackage samplePackage = new YSQProvider().GetHistory(new Asset("MSFT", AssetType.Stock), new DateTime(2014, 1, 1), new DateTime(2014, 7, 1), null);

            Assert.IsTrue(samplePackage is ISamplePackage <IBar>, "samplePackage no es instancia de ISamplePackage<IBar>");
            IBarPackage barPackage = (IBarPackage)samplePackage;

            Assert.IsNotNull(barPackage.Samples);
            Assert.IsTrue(barPackage.Samples.Count > 0);
            Assert.IsNotNull(barPackage.Asset);
            Assert.IsNotNull(barPackage.Source);
            Assert.IsNotNull(barPackage.Period);
        }
Beispiel #7
0
        public void TestMethod()
        {
            // data
            string         symbol        = YSQSymbol.YSQIndex.SNP;
            DateTime       begin         = new DateTime(2000, 1, 1);
            DateTime       end           = new DateTime(2015, 1, 1);
            ISamplePackage samplePackage = new YSQProvider()
                                           .GetHistory(new Asset(symbol, AssetType.Index), begin, end, null);
            IBarPackage barPackage = (IBarPackage)samplePackage;
            List <IBar> bars       = barPackage.Samples;
            // simulation
            var simulation = new TestSimulation(10, 5, true);
            SimulationRunner simulationRunner = new SimulationRunner(bars, simulation);

            simulationRunner.AddSerie("EMA_SMALL", bars.EMA(10));
            simulationRunner.AddSerie("EMA_BIG", bars.EMA(20));
            simulationRunner.Execute();
            Debug.WriteLine(simulation.GetReport());
        }
Beispiel #8
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);
        }