Exemplo n.º 1
0
        public void RemovesOldValues()
        {
            var kellyCriterionManager = new KellyCriterionManager();

            var start = new DateTime(2019, 1, 1);

            kellyCriterionManager.AddNewValue(10, start);
            kellyCriterionManager.AddNewValue(10, start);
            kellyCriterionManager.UpdateScores();

            Console.WriteLine($"Estimate {kellyCriterionManager.KellyCriterionEstimate}" +
                              $" - ProbabilityValue {kellyCriterionManager.KellyCriterionProbabilityValue}");
            Assert.AreEqual(0.1, kellyCriterionManager.KellyCriterionEstimate);
            Assert.AreEqual(0.5, kellyCriterionManager.KellyCriterionProbabilityValue);


            kellyCriterionManager.AddNewValue(-10, start.AddDays(365));
            kellyCriterionManager.AddNewValue(-10, start.AddDays(365));
            kellyCriterionManager.UpdateScores();

            Console.WriteLine($"Estimate {kellyCriterionManager.KellyCriterionEstimate}" +
                              $" - ProbabilityValue {kellyCriterionManager.KellyCriterionProbabilityValue}");
            Assert.AreEqual(-0.1, kellyCriterionManager.KellyCriterionEstimate);
            Assert.AreEqual(0.5, kellyCriterionManager.KellyCriterionProbabilityValue);
        }
        /// <summary>
        /// Computes an estimated value for the insight. This is intended to be invoked at the end of the
        /// insight period, i.e, when now == insight.GeneratedTimeUtc + insight.Period;
        /// </summary>
        /// <param name="context">Context whose insight has just closed</param>
        public void OnInsightClosed(InsightAnalysisContext context)
        {
            // increment closed insight counter
            Statistics.TotalInsightsClosed += 1;

            // tradable volume (purposefully includes fractional shares)
            var volume = _tradablePercentOfVolume * context.InitialValues.Volume;

            // value of the entering the trade in the account currency
            var enterValue = context.InitialValues.Price * context.InitialValues.QuoteCurrencyConversionRate;

            // value of exiting the trade in the account currency
            var exitValue = context.CurrentValues.Price * context.CurrentValues.QuoteCurrencyConversionRate;

            // total value delta between enter and exit values
            var insightValue = (int)context.Insight.Direction * (exitValue - enterValue);

            var insightValueFactoredByTradableVolume = insightValue * volume;

            context.Insight.EstimatedValue = insightValueFactoredByTradableVolume;
            Statistics.TotalAccumulatedEstimatedAlphaValue += insightValueFactoredByTradableVolume;

            // just in case..
            if (enterValue != 0)
            {
                _kellyCriterionManager.AddNewValue(
                    (int)context.Insight.Direction * (exitValue / enterValue - 1),
                    context.Insight.GeneratedTimeUtc);
            }
        }
Exemplo n.º 3
0
        public void ExtremeCasesBigNumbers(int extremeCase)
        {
            var extremePositive = new decimal[] { 1000, 2000, 3000, 1000, 5000, 1000, 1000 };
            var extremeNegative = new decimal[] { -1000, -2000, -3000, -1000, -5000, -1000, -1000 };

            decimal[] collection = extremeCase == 1 ? extremePositive : extremeNegative;

            var kellyCriterionManager = new KellyCriterionManager();

            foreach (var newValue in collection)
            {
                kellyCriterionManager.AddNewValue(newValue, DateTime.UtcNow);
                kellyCriterionManager.UpdateScores();
            }
            var estimate         = kellyCriterionManager.KellyCriterionEstimate;
            var probabilityValue = kellyCriterionManager.KellyCriterionProbabilityValue;

            Console.WriteLine($"Estimate {estimate} - ProbabilityValue {probabilityValue}");
            Assert.AreEqual(extremeCase == 1
                    ? 0.00031578947368421 : -0.00031578947368421, kellyCriterionManager.KellyCriterionEstimate);
            Assert.AreEqual(1, kellyCriterionManager.KellyCriterionProbabilityValue);
        }
Exemplo n.º 4
0
        public void ExtremeCasesSmallNumbers(int extremeCase)
        {
            var extremePositive = new decimal[] { 1, 2, 3, 1, 5, 1, 1 };
            var extremeNegative = new decimal[] { -1, -2, -3, -1, -5, -1, -1 };

            decimal[] collection = extremeCase == 1 ? extremePositive : extremeNegative;

            var kellyCriterionManager = new KellyCriterionManager();

            foreach (var newValue in collection)
            {
                kellyCriterionManager.AddNewValue(newValue, DateTime.UtcNow);
                kellyCriterionManager.UpdateScores();
            }

            var estimate         = kellyCriterionManager.KellyCriterionEstimate;
            var probabilityValue = kellyCriterionManager.KellyCriterionProbabilityValue;

            Log.Trace($"Estimate {estimate} - ProbabilityValue {probabilityValue}");
            Assert.AreEqual(extremeCase == 1
                ? 0.315789473684211m : -0.315789473684211m, kellyCriterionManager.KellyCriterionEstimate);
            Assert.AreEqual(1, kellyCriterionManager.KellyCriterionProbabilityValue);
        }
Exemplo n.º 5
0
        public void MiddleCase()
        {
            // values sum up to 0 -> average will be 0
            var middleCase = new decimal[] { 1000, -2000, -3000, 1000, 5000, -1000, -1000 };

            var kellyCriterionManager = new KellyCriterionManager();

            foreach (var newValue in middleCase)
            {
                kellyCriterionManager.AddNewValue(newValue, DateTime.UtcNow);
                kellyCriterionManager.UpdateScores();
            }

            var estimate         = kellyCriterionManager.KellyCriterionEstimate;
            var probabilityValue = kellyCriterionManager.KellyCriterionProbabilityValue;

            Console.WriteLine($"Estimate {estimate} - ProbabilityValue {probabilityValue}");

            Console.WriteLine($"Estimate {estimate} - ProbabilityValue {probabilityValue}");

            // compare with a delta
            Assert.Less(Math.Abs(0 - kellyCriterionManager.KellyCriterionEstimate), 0.000000000000001m);
            Assert.AreEqual(1, kellyCriterionManager.KellyCriterionProbabilityValue);
        }