public void getOutcomeCodeFirst_Negative_Test()
        {
            double[][] inputs = new double[100][];
            DateTime   dt     = DateTime.Now.ToUniversalTime();

            for (int i = 0; i < inputs.Length; i++)
            {
                //Date bid ask volume
                dt        = dt.AddMilliseconds(1000);
                inputs[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), 2, 2, 0 };
            }

            double successRatio;

            bool[][] outcomeCodes = OutcomeGenerator.getOutcomeCodeFirst(inputs, 1000 * 10, 100, out successRatio);

            int notAssignedCount = 0;

            foreach (bool[] row in outcomeCodes)
            {
                if (row != null)
                {
                    Assert.IsFalse(row[(int)OutcomeCodeMatrixIndices.Buy]);
                    Assert.IsFalse(row[(int)OutcomeCodeMatrixIndices.Sell]);
                }
                else
                {
                    notAssignedCount++;
                }
            }
            Assert.AreEqual(10, notAssignedCount);
        }
Exemplo n.º 2
0
        public void TestGenerateAllPossibleGroupAssignmentsForDistinctGroupsAndDistinctMembers(int numOutcomes, int numTrials)
        {
            var outcomeGenerator = new OutcomeGenerator();
            var outcomes         = outcomeGenerator.GenerateAllOutcomes(numTrials, numOutcomes, '1');

            Assert.AreEqual(outcomes.Count, Math.Pow(numOutcomes, numTrials));
            var maxScore = 0;

            // note, this only works for n < 10
            foreach (var outcome in outcomes)
            {
                var score = int.Parse(outcome.Aggregate("", (score, i) => $"{score}{i}"));
                Assert.Greater(score, maxScore);
                maxScore = score;
            }
        }
        public static double optimizeOutcomeCodePercentage(int rounds, out double outcomeCodePercent, double[][] priceData, double[][] outcomes, out double buyDist, out double sellDist)
        {
            outcomeCodePercent = 0.5;

            buyDist  = double.NaN;
            sellDist = double.NaN;

            double lastScore = double.MinValue;
            double direction = -0.01;

            int round;

            for (round = 1; round < rounds; round++)
            {
                double   successRatioCode;
                bool[][] outcomeCodes = OutcomeGenerator.getOutcomeCode(priceData, outcomes, outcomeCodePercent, out successRatioCode);

                if (successRatioCode < 0.9)
                {
                    throw new TooLittleValidDataException("Too low success ratio: " + successRatioCode);
                }

                DistributionHelper.getOutcomeCodeDistribution(outcomeCodes, out buyDist, out sellDist);

                double score = ((buyDist + sellDist) / 2) * outcomeCodePercent;
                if (score < lastScore) //Wrong direction
                {
                    direction = direction * (-1);
                    Logger.log("New opt. direction: " + direction);
                }

                if (outcomeCodePercent <= 0 && direction <= 0)
                {
                    direction = Math.Abs(direction);
                }

                outcomeCodePercent += (direction / (1 + (round / 20)));

                Logger.log("PercDist OPT. Round " + round + " -> " + outcomeCodePercent + "% = |s" + Math.Round(score, 4) + "| b" + Math.Round(buyDist, 4) + " s" + Math.Round(sellDist, 4));

                lastScore = score;
                round++;
            }

            return(outcomeCodePercent);
        }
        public void getOutcomeCodeFirst_Positive_Test()
        {
            double[][] inputs = new double[100][];
            DateTime   dt     = DateTime.Now.ToUniversalTime();

            for (int i = 0; i < inputs.Length; i++)
            {
                //Date bid ask volume
                dt        = dt.AddMilliseconds(1000);
                inputs[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), 2, 2, 0 };
            }

            inputs[50][1] = inputs[50][2] = 5;

            double successRatio;

            bool[][] outcomeCodes = OutcomeGenerator.getOutcomeCodeFirst(inputs, 1000 * 10, 100, out successRatio);

            int notAssignedCount = 0;

            for (int i = 0; i < outcomeCodes.Length; i++)
            {
                if (outcomeCodes[i] != null)
                {
                    if (i > 40 && i < 50)
                    {
                        Assert.IsTrue(outcomeCodes[i][(int)OutcomeCodeMatrixIndices.Buy], i + " Buy: " + outcomeCodes[i][(int)OutcomeCodeMatrixIndices.Buy]);
                    }
                    else
                    {
                        Assert.IsFalse(outcomeCodes[i][(int)OutcomeCodeMatrixIndices.Buy], i + " Buy: " + outcomeCodes[i][(int)OutcomeCodeMatrixIndices.Buy]);
                    }

                    Assert.IsFalse(outcomeCodes[i][(int)OutcomeCodeMatrixIndices.Sell], i + " Sell: " + outcomeCodes[i][(int)OutcomeCodeMatrixIndices.Sell]);
                }
                else
                {
                    notAssignedCount++;
                }
            }
            Assert.AreEqual(10, notAssignedCount);
        }
        public static double findOutcomeCodeForDesiredDistribution(double desiredDistribution, double tolerance, double[][] priceData, double[][] outcomeMatrix, out double buyDist, out double sellDist)
        {
            double outcomeCodePercent = 0.5;
            int    round = 0;

            while (true)
            {
                double   successRatioCode;
                bool[][] outcomeCodes = OutcomeGenerator.getOutcomeCode(priceData, outcomeMatrix, outcomeCodePercent, out successRatioCode);

                if (successRatioCode < 0.9)
                {
                    throw new TooLittleValidDataException("Too few outcome codes: " + successRatioCode);
                }

                DistributionHelper.getOutcomeCodeDistribution(outcomeCodes, out buyDist, out sellDist);

                double score = (buyDist + sellDist) / 2;
                if (score > desiredDistribution - tolerance && score < desiredDistribution + tolerance)
                {
                    break;
                }
                else if (score > desiredDistribution + tolerance)
                {
                    outcomeCodePercent += (outcomeCodePercent / (10 + round));
                }
                else if (score < desiredDistribution - tolerance)
                {
                    outcomeCodePercent -= (outcomeCodePercent / (10 + round));
                }

                Logger.log("SetDist OPT. Round " + round + " -> " + outcomeCodePercent + "% = b" + Math.Round(buyDist, 4) + " s" + Math.Round(sellDist, 4) + " =" + Math.Round(score, 4));

                round++;
            }

            return(outcomeCodePercent);
        }
        public void getOutcome_Test_MoreComplex_Negative()
        {
            //Generate a input
            double[][] inputs = new double[100][];
            DateTime   dt     = DateTime.Now.ToUniversalTime();

            for (int i = 0; i < inputs.Length; i++)
            {
                //Date bid ask volume
                dt        = dt.AddMilliseconds(1000);
                inputs[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), inputs.Length - i, inputs.Length - i, 0 };
            }

            double successRate;

            double[][] outcomes = OutcomeGenerator.getOutcome(inputs, 1000 * 10, out successRate);

            int foundNaNs = 0;

            for (int i = 0; i < outcomes.Length; i++)
            {
                if (outcomes[i] != null)
                {
                    Assert.AreEqual(inputs.Length - (i + 9), outcomes[i][(int)OutcomeMatrixIndices.Actual]);
                    Assert.AreEqual(inputs.Length - (i + 9), outcomes[i][(int)OutcomeMatrixIndices.Min]);
                    Assert.AreEqual(inputs.Length - (i + 1), outcomes[i][(int)OutcomeMatrixIndices.Max]);
                }
                else
                {
                    foundNaNs++;
                }
            }

            Assert.AreEqual(10, foundNaNs);
            Assert.AreEqual(null, outcomes[outcomes.Length - 1]);
        }
        public void getOutcome_Test_OnlyOneValue()
        {
            //Generate a input
            double[][] inputs = new double[100][];
            DateTime   dt     = DateTime.Now.ToUniversalTime();

            for (int i = 0; i < inputs.Length; i++)
            {
                //Date bid ask volume
                dt        = dt.AddMilliseconds(1000);
                inputs[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), 2, 2, 0 };
            }

            double successRate;

            double[][] outcomes = OutcomeGenerator.getOutcome(inputs, 1000 * 10, out successRate);

            int foundNaNs = 0;

            foreach (double[] row in outcomes)
            {
                if (row != null)
                {
                    Assert.AreEqual(2, row[(int)OutcomeMatrixIndices.Actual]);
                    Assert.AreEqual(2, row[(int)OutcomeMatrixIndices.Min]);
                    Assert.AreEqual(2, row[(int)OutcomeMatrixIndices.Max]);
                }
                else
                {
                    foundNaNs++;
                }
            }

            Assert.AreEqual(10, foundNaNs);
            Assert.AreEqual(null, outcomes[outcomes.Length - 1]);
        }
Exemplo n.º 8
0
 // problem: https://fivethirtyeight.com/features/what-are-the-odds-world-cup-teams-play-each-other-twice/
 // answer: https://fivethirtyeight.com/features/how-fast-can-you-deliver-pbjs-how-many-meerkats-can-survive/
 public ContinuousLockSolver()
 {
     this._outcomeGenerator = new OutcomeGenerator();
 }
Exemplo n.º 9
0
 public OlympicClimbingChampionSolver()
 {
     this._outcomeGenerator = new OutcomeGenerator();
 }
Exemplo n.º 10
0
        public void updateIndicators(long timeframeToLookBack, long timeframeToLookBackForIndicatorInit, IndicatorSelector indicatorSelector)
        {
            List <double[]> selectedPriceData = new List <double[]>();

            for (int i = priceData.Count - 1; i > 0; i--)
            {
                if (Convert.ToInt64(priceData[i][(int)PriceDataIndeces.Date]) > timestampNow - timeframeToLookBack)
                {
                    selectedPriceData.Insert(0, priceData[i]); //Todo: List direction correct?
                }
                else
                {
                    break;
                }
            }

            double[][] selectedPriceDataArray = selectedPriceData.ToArray();
            double     s;

            double[][] outcomeData = OutcomeGenerator.getOutcome(selectedPriceDataArray, outcomeTimeframe, out s);
            if (s < 0.6)
            {
                throw new Exception("s < o.6: " + s);
            }

            //bool[][] outcomeCodeData = OutcomeGenerator.getOutcomeCode(selectedPriceDataArray, outcomeData, outcomeCodePercent, out s);
            bool[][] outcomeCodeFirstData = OutcomeGenerator.getOutcomeCodeFirst(selectedPriceDataArray, outcomeTimeframe, outcomeCodePercent, out s);
            if (s < 0.6)
            {
                throw new Exception("s < o.6: " + s);
            }

            string[] indicatorIds;

            //This part can be skipped by caching todo: get from outside
            double hash = outcomeTimeframe + selectedPriceData[0].Sum() + selectedPriceData[selectedPriceData.Count - 1].Sum() + selectedPriceData[selectedPriceData.Count / 2].Sum();
            string optimalIndicatorsFileName = cachePath + "/" + "optimalIndicatorsIn_" + hash + "_" + selectedPriceData[selectedPriceData.Count - 1][(int)PriceDataIndeces.Date] + "_" + timeframeToLookBack + "_" + outcomeCodePercent + ".txt";

            if (cachePath != null && File.Exists(optimalIndicatorsFileName))
            {
                indicatorIds = File.ReadAllText(optimalIndicatorsFileName).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                Logger.log("Loaded optimal indicators from file: " + optimalIndicatorsFileName);
            }
            else
            {
                //Shuffle okay indicators? todo:
                Logger.log("Generated optimal indicators");
                IndicatorOptimizer optimizer = new IndicatorOptimizer(selectedPriceDataArray, outcomeData, outcomeCodeFirstData, outcomeTimeframe, outcomeCodePercent, minPercentThreshold, learningIndicatorSteps);
                indicatorIds = optimizer.getOptimizedIndicators(okayIndicators, indicatorSelector, 8);

                File.WriteAllLines(optimalIndicatorsFileName, indicatorIds);
            }

            Logger.log("Selected indicators: ");
            List <LearningIndicator> lis = new List <LearningIndicator>();

            foreach (string str in indicatorIds)
            {
                Logger.log(str);

                WalkerIndicator ind = IndicatorGenerator.getIndicatorByString(str);
                if (ind.getName() != str)
                {
                    throw new Exception(str + "!=" + ind.getName());
                }

                lis.Add(new LearningIndicator(ind, selectedPriceDataArray, outcomeCodeFirstData, outcomeData, outcomeTimeframe, outcomeCodePercent, minPercentThreshold, learningIndicatorSteps, false));
            }

            SignalMachine sm = new AlternativeSignalMachine(lis.ToArray()); //Todo: make accessable copy?

            Logger.log("SM STATE: ##################" + Environment.NewLine + sm.getStateMessage());

            //Make them up to date
            List <double[]> selectedPriceDataForIndicatorInit = new List <double[]>();

            for (int i = priceData.Count - 1; i > 0; i--)
            {
                if (Convert.ToInt64(priceData[i][(int)PriceDataIndeces.Date]) > timestampNow - timeframeToLookBackForIndicatorInit)
                {
                    selectedPriceDataForIndicatorInit.Insert(0, priceData[i]); //Todo: List direction correct?
                }
                else
                {
                    break;
                }
            }

            foreach (double[] row in selectedPriceDataForIndicatorInit)
            {
                sm.pushPrice(row);
            }

            this.signalMachine = sm;
        }
Exemplo n.º 11
0
        private void FindOkayIndicatorsForm_Load(object sender, EventArgs e)
        {
            timer1.Start();

            string okayIndicatorsFile = "okayIndicators" + outcomeTimeframe + ".txt";

            if (File.Exists(okayIndicatorsFile))
            {
                List <string> lines = File.ReadLines(okayIndicatorsFile).ToList();
                foreach (string line in lines)
                {
                    if (line != "" && line != null && line != " ")
                    {
                        found.Add(line, true);
                    }
                }
            }

            DataLoader dl = new DataLoader(Config.DataPath + pair);

            double[][] priceData = dl.getArray(1000 * 60 * 60 * 24 * 30l,
                                               timeframeToTest,
                                               minTimestep); //One month, but the second. Every 10 secs

            double success;

            bool[][] outcomeCodeFirstData = OutcomeGenerator.getOutcomeCodeFirst(priceData, outcomeTimeframe, outcomeCodePercent, out success);

            if (success < 0.7)
            {
                throw new Exception("OutcomeCode low success: " + success);
            }

            double[][] outcomeData = OutcomeGenerator.getOutcome(priceData, outcomeTimeframe, out success);

            if (success < 0.7)
            {
                throw new Exception("Outcome low success: " + success);
            }

            IndicatorGenerator generator = new IndicatorGenerator();

            new Thread(delegate() {
                while (true)
                {
                    WalkerIndicator ind = generator.getGeneratedIndicator(Convert.ToInt32((outcomeTimeframe / 1000) / 10), Convert.ToInt32((outcomeTimeframe / 1000) * 100));

                    try
                    {
                        if (found.ContainsKey(ind.getName()) == false)
                        {
                            tried++;
                            new LearningIndicator(ind, priceData, outcomeCodeFirstData, outcomeData, outcomeTimeframe, outcomeCodePercent, minPercentThreshold, learningIndicatorSteps, false);
                            File.AppendAllText(okayIndicatorsFile, ind.getName() + Environment.NewLine);
                            found.Add(ind.getName(), true);
                        }
                    }
                    catch (Exception) { }
                }
            }).Start();
        }
Exemplo n.º 12
0
		public CitizenRandomHatSolver()
		{
			this._outcomeGenerator = new OutcomeGenerator();
		}
 public TrianglesFromRandomThreadsSolver()
 {
     this._outcomeGenerator = new OutcomeGenerator();
 }
Exemplo n.º 14
0
 public DiceRerollOptimizer()
 {
     _outcomeGenerator = new OutcomeGenerator();
 }