public void indicatorSamper_getStatistics_Test_SellCorrealation()
        {
            double[] values       = new double[100];
            bool[][] outcomeCodes = new bool[100][];
            Random   z            = new Random();

            for (int i = 0; i < values.Length; i++)
            {
                double d = z.NextDouble();
                if (d > 0.7)
                {
                    outcomeCodes[i] = new bool[] { z.NextDouble() > 0.5, true }
                }
                ;
                else
                {
                    outcomeCodes[i] = new bool[] { z.NextDouble() > 0.5, z.NextDouble() > 0.5 }
                };

                values[i] = d;
            }

            double spBuy, spSell, pBuy, pSell;

            IndicatorSampler.getStatisticsOutcomeCodes(values, outcomeCodes, out spBuy, out spSell, out pBuy, out pSell);

            Assert.IsTrue(spSell > spBuy - 0.2);
            Assert.IsTrue(pSell > pBuy - 0.2);
        }
Beispiel #2
0
        public void VisualizeOutcomeCodeSampling_Test()
        {
            double[] values       = new double[300];
            bool[][] outcomeCodes = new bool[300][];
            Random   z            = new Random();

            for (int i = 0; i < values.Length; i++)
            {
                double d = z.NextDouble();
                if (d > 0.7)
                {
                    outcomeCodes[i] = new bool[] { z.NextDouble() > 0.2, z.NextDouble() > 0.1 }
                }
                ;
                else
                {
                    outcomeCodes[i] = new bool[] { z.NextDouble() > 0.6, z.NextDouble() > 0.7 }
                };

                values[i] = d;
            }

            outcomeCodes[50] = null;

            double validRatio;

            double[][] samples = IndicatorSampler.sampleValuesOutcomeCode(values, outcomeCodes, 0, 1, 10, out validRatio);

            samples[3] = null;

            Image img = OutcomeSamplingVisualizer.visualizeOutcomeCodeSamplingTable(samples, 500, 300, 0.3d);
            //ArrayVisualizer.showImg(img); //Todo: Test somehow
        }
        public void sampleValuesOutcome_Test()
        {
            double[]   values   = new double[100];
            double[][] outcomes = new double[100][];
            double[][] prices   = new double[100][];
            DateTime   dt       = DateTime.Now.ToUniversalTime();

            Random z = new Random();

            for (int i = 0; i < values.Length; i++)
            {
                //Min max actual
                double d = z.NextDouble();
                if (d > 0.7)
                {
                    outcomes[i] = new double[] { 0, 2, 3 }
                }
                ;
                else
                {
                    outcomes[i] = new double[] { 0.8, 1.2, 4 }
                };

                values[i] = d;

                dt        = dt.AddMilliseconds(1000);
                prices[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), 1, 1, 0 };
            }

            double validOut;

            double[][] samples = IndicatorSampler.sampleValuesOutcome(values, prices, outcomes, 0, 1, out validOut, 10);

            Assert.AreEqual(1d, validOut);

            for (int i = 0; i < 10; i++)
            {
                if (samples[i] != null)
                {
                    Assert.AreEqual(0.1d * i, samples[i][((int)SampleValuesOutcomeIndices.Start)], 0.0001);

                    if (i >= 7)
                    {
                        Assert.AreEqual(100d, samples[i][((int)SampleValuesOutcomeIndices.MaxAvg)], 0.01);
                        Assert.AreEqual(-100d, samples[i][((int)SampleValuesOutcomeIndices.MinAvg)], 0.01);
                        Assert.AreEqual(200d, samples[i][((int)SampleValuesOutcomeIndices.ActualAvg)], 0.01);
                    }
                    else
                    {
                        Assert.AreEqual(20d, samples[i][((int)SampleValuesOutcomeIndices.MaxAvg)], 0.01);
                        Assert.AreEqual(-20d, samples[i][((int)SampleValuesOutcomeIndices.MinAvg)], 0.01);
                        Assert.AreEqual(300d, samples[i][((int)SampleValuesOutcomeIndices.ActualAvg)], 0.01);
                    }
                }
            }
        }
        public void sampleValuesOutcomeCode_Test()
        {
            double[] values       = new double[100];
            bool[][] outcomeCodes = new bool[100][];
            Random   z            = new Random();

            for (int i = 0; i < values.Length; i++)
            {
                double d = z.NextDouble();
                if (d > 0.7)
                {
                    outcomeCodes[i] = new bool[] { true, true }
                }
                ;
                else
                {
                    outcomeCodes[i] = new bool[] { true, false }
                };

                values[i] = d;
            }

            double validRatio;

            double[][] samples = IndicatorSampler.sampleValuesOutcomeCode(values, outcomeCodes, 0, 1, 10, out validRatio);
            Assert.AreEqual(1, validRatio);

            for (int i = 0; i < 10; i++)
            {
                if (samples[i] != null)
                {
                    Assert.AreEqual(1d, samples[i][((int)SampleValuesOutcomeCodesIndices.BuyRatio)]);
                    Assert.AreEqual(0.1d * i, samples[i][((int)SampleValuesOutcomeCodesIndices.Start)], 0.0001);

                    if (i < 7)
                    {
                        Assert.AreEqual(0d, samples[i][((int)SampleValuesOutcomeCodesIndices.SellRatio)]);
                    }
                    else
                    {
                        Assert.AreEqual(1d, samples[i][((int)SampleValuesOutcomeCodesIndices.SellRatio)]);
                    }
                }
            }
        }
Beispiel #5
0
        public void OutcomeSamplingTableVisualizer_Test()
        {
            double[]   values   = new double[100];
            double[][] outcomes = new double[100][];
            double[][] prices   = new double[100][];
            DateTime   dt       = DateTime.Now.ToUniversalTime();

            Random z = new Random();

            for (int i = 0; i < values.Length; i++)
            {
                //Min max actual
                double d = z.NextDouble();
                if (d > 0.7)
                {
                    outcomes[i] = new double[] { 1 - z.NextDouble() * 1, 1 + z.NextDouble() * 1, z.NextDouble() * 2 }
                }
                ;
                else
                {
                    outcomes[i] = new double[] { 1 - z.NextDouble() * 0.5, 1 + z.NextDouble() * 0.5, z.NextDouble() * 1 + 0.5 }
                };

                values[i] = d;

                dt        = dt.AddMilliseconds(1000);
                prices[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), 1, 1, 0 };
            }

            double validOut;

            double[][] samples = IndicatorSampler.sampleValuesOutcome(values, prices, outcomes, 0, 1, out validOut, 10);

            samples[3] = null;

            Image image = OutcomeSamplingVisualizer.visualizeOutcomeSamplingTable(samples, 500, 300, 0.3);
            //ArrayVisualizer.showImg(image); //Todo: Test somehow
        }
    }
        public LearningIndicator(WalkerIndicator indicator, double[][] prices, bool[][] outcomeCodes, double[][] outcomes, long timeframe, double targetPercent, double minPercentThreshold, int steps, bool createStatistics)
        {
            this.targetPercent = targetPercent;
            this.timeframe     = timeframe;

            double validRatio;

            double[] values = IndicatorRunner.getIndicatorValues(prices, indicator.Clone(), out validRatio);
            if (validRatio < 0.5)
            {
                throw new TooLittleValidDataException("Not enough valid values: " + validRatio);
            }

            //May be does not work properly... todo:
            double min, max, usedValuesRatio;

            //DistributionHelper.getMinMax(values, 4, out min, out max);
            DistributionHelper.getMinMax(values, out min, out max);

            outcomeCodeSamplingTable = IndicatorSampler.sampleValuesOutcomeCode(values, outcomeCodes, min, max, steps, out usedValuesRatio);
            if (usedValuesRatio < 0.5)
            {
                throw new TooLittleValidDataException("Not enough sampling for outcomeCode: " + usedValuesRatio);
            }

            outcomeSamplingTable = IndicatorSampler.sampleValuesOutcome(values, prices, outcomes, min, max, out usedValuesRatio, 40);
            if (usedValuesRatio < 0.5)
            {
                throw new TooLittleValidDataException("Not enough sampling for outcome: " + usedValuesRatio);
            }

            this.usedValues = usedValuesRatio;

            if (createStatistics)
            {
                //Predictive power calculation
                predictivePower = new double[33];
                IndicatorSampler.getStatisticsOutcomeCodes(values, outcomeCodes, out predictivePower[0], out predictivePower[1], out predictivePower[2], out predictivePower[3]);
                IndicatorSampler.getStatisticsOutcomes(values, prices, outcomes, out predictivePower[4], out predictivePower[5], out predictivePower[6], out predictivePower[7], out predictivePower[8], out predictivePower[9]);

                DistributionHelper.getSampleOutcomeCodesBuyMaxSellMax(outcomeCodeSamplingTable, minPercentThreshold, out predictivePower[10], out predictivePower[11], out predictivePower[12], out predictivePower[13]);
                DistributionHelper.getSampleOutcomesMinMax(outcomeSamplingTable, minPercentThreshold, out predictivePower[14], out predictivePower[15], out predictivePower[16], out predictivePower[17], out predictivePower[18], out predictivePower[19], out predictivePower[20], out predictivePower[21], out predictivePower[22], out predictivePower[23]);

                //Outcome Code

                List <double> buyCodesDist        = new List <double>(),
                              sellCodesDist       = new List <double>(),
                              buySellDistanceDist = new List <double>(),
                              minMaxDistanceDist  = new List <double>(),
                              minDist             = new List <double>(),
                              maxDist             = new List <double>(),
                              actualDist          = new List <double>();

                double totalCodeSamples = 0;
                foreach (double[] row in outcomeCodeSamplingTable)
                {
                    totalCodeSamples += row[(int)SampleValuesOutcomeCodesIndices.SamplesCount];
                }

                int regardedStates = 0;
                foreach (double[] row in outcomeCodeSamplingTable)
                {
                    if ((row[(int)SampleValuesOutcomeCodesIndices.SamplesCount] / totalCodeSamples) * 100 >= minPercentThreshold) //minPercentThreshold
                    {
                        buyCodesDist.Add(row[(int)SampleValuesOutcomeCodesIndices.BuyRatio]);
                        sellCodesDist.Add(row[(int)SampleValuesOutcomeCodesIndices.SellRatio]);
                        buySellDistanceDist.Add(Math.Abs(row[(int)SampleValuesOutcomeCodesIndices.BuyRatio] - row[(int)SampleValuesOutcomeCodesIndices.SellRatio]));
                        regardedStates++;
                    }
                }

                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.valuesOverMinPercentRatioCode] = Convert.ToDouble(regardedStates) / outcomeCodeSamplingTable.Length;

                if (regardedStates <= 2)
                {
                    throw new TooLittleStatesException("Too little sates: " + regardedStates);
                }

                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buyCodeStD]             = buyCodesDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.sellCodeStD]            = sellCodesDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buySellCodeDistanceStD] = buySellDistanceDist.StandardDeviation();

                //Outcome

                double totalSamples = 0;
                foreach (double[] row in outcomeSamplingTable)
                {
                    totalSamples += row[(int)SampleValuesOutcomeIndices.SamplesCount];
                }

                //Avgs
                regardedStates = 0;
                foreach (double[] row in outcomeSamplingTable)
                {
                    if ((row[(int)SampleValuesOutcomeIndices.SamplesCount] / totalSamples) * 100 > minPercentThreshold) //minPercentThreshold
                    {
                        maxDist.Add(row[(int)SampleValuesOutcomeIndices.MaxAvg]);
                        minDist.Add(row[(int)SampleValuesOutcomeIndices.MinAvg]);
                        minMaxDistanceDist.Add(Math.Abs(row[(int)SampleValuesOutcomeIndices.MaxAvg]) + row[(int)SampleValuesOutcomeIndices.MinAvg]);
                        actualDist.Add(row[(int)SampleValuesOutcomeIndices.ActualAvg]);
                        regardedStates++;
                    }
                }

                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.valuesOverMinPercentRatioOutcome] += Convert.ToDouble(regardedStates) / outcomeSamplingTable.Length;

                //avg distances
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.maxStD]            = maxDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minStD]            = minDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minMaxDistanceStd] = minMaxDistanceDist.StandardDeviation();
                predictivePower[(int)LearningIndicatorPredictivePowerIndecies.actualStD]         = actualDist.StandardDeviation();

                if (double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buyCodeStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.sellCodeStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.buySellCodeDistanceStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.maxStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minStD]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.minMaxDistanceStd]) ||
                    double.IsNaN(predictivePower[(int)LearningIndicatorPredictivePowerIndecies.actualStD]))
                {
                    throw new Exception("Not a valid predictive power!");
                }

                //End predictive power calculation
            }

            this.indicator = indicator;
        }