From 220fa4e5ccf5a20648b18146ccbbd5538889bbc5 Mon Sep 17 00:00:00 2001 From: Dev Date: Thu, 29 Jun 2023 12:36:11 +0100 Subject: [PATCH] use MathNet lib to calc standard dev when choosing average price --- .../Helpers/MarketPricesHelper.cs | 38 +++++++++++++------ .../Helpers/TarkovDevResponse.cs | 2 +- .../MarketPriceLookup.Common.csproj | 4 ++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/MarketPriceLookup.Common/Helpers/MarketPricesHelper.cs b/MarketPriceLookup.Common/Helpers/MarketPricesHelper.cs index 0b7e95e..ab46ecc 100644 --- a/MarketPriceLookup.Common/Helpers/MarketPricesHelper.cs +++ b/MarketPriceLookup.Common/Helpers/MarketPricesHelper.cs @@ -7,6 +7,8 @@ using System.Linq; using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; +using MathNet.Numerics.Statistics; +using System.Data; namespace MarketPriceLookup.Common.Helpers { @@ -101,28 +103,40 @@ namespace MarketPriceLookup.Common.Helpers } /// - /// Get items average flea price from all readings taken over the past 7 days + /// Get items average flea price from all readings taken over the past 14 days /// /// private static int GetAveragedPrice(Item item) { - var sevenDaysAgoTimestamp = DateTimeOffset.UtcNow.AddDays(-7).ToUnixTimeSeconds(); - var pricesWithinLast7days = new List(); - foreach (var historicalPrice in item.historicalPrices) + var fourteenDaysAgoTimestamp = DateTimeOffset.UtcNow.AddDays(-14).ToUnixTimeSeconds(); + var filteredPrices = item.historicalPrices.Where(x => long.Parse(x.timestamp) > fourteenDaysAgoTimestamp).OrderBy(x=> x.price).ToList(); + + if (filteredPrices.Count == 0) { - if (long.Parse(historicalPrice.timestamp) > sevenDaysAgoTimestamp) - { - pricesWithinLast7days.Add(historicalPrice.price); - } + // x day filter means no prices, use all data + filteredPrices = item.historicalPrices.ToList(); } - if (pricesWithinLast7days.Count == 0) + if (filteredPrices.Count == 1) { - LoggingHelpers.LogError($"No prices found for item {item.name} in last 7 days, using any availible"); - pricesWithinLast7days.AddRange(item.historicalPrices.Select(x => x.price)); + return filteredPrices[0].price; } - return (int)Math.Round(pricesWithinLast7days.Average()); + var prices = filteredPrices.Select(x => (double)x.price).ToArray(); + + var avgMean = prices.Average(); + var standardDev = prices.StandardDeviation(); + + var upperCutoff = standardDev * 1.5; + var lowerCutoff = standardDev * 2; + + var lowerBound = avgMean - lowerCutoff; + var upperBound = avgMean + upperCutoff; + + //var outliers = prices.Where(x => x < lowerBound || x > upperBound).ToList(); + var pricesWithOutliersRemoved = prices.Where(x => x >= lowerBound && x <= upperBound).ToList(); + + return (int)Math.Round(pricesWithOutliersRemoved.Average()); } public static Prices GetItemPrice(string key) diff --git a/MarketPriceLookup.Common/Helpers/TarkovDevResponse.cs b/MarketPriceLookup.Common/Helpers/TarkovDevResponse.cs index ed3689f..a74a760 100644 --- a/MarketPriceLookup.Common/Helpers/TarkovDevResponse.cs +++ b/MarketPriceLookup.Common/Helpers/TarkovDevResponse.cs @@ -17,7 +17,7 @@ namespace MarketPriceLookup.Common.Helpers { public string name { get; set; } public int avg24hPrice { get; set; } - public double changeLast48hPercent { get; set; } + public double? changeLast48hPercent { get; set; } public string id { get; set; } public HistoricalPrice[] historicalPrices { get; set; } } diff --git a/MarketPriceLookup.Common/MarketPriceLookup.Common.csproj b/MarketPriceLookup.Common/MarketPriceLookup.Common.csproj index a60e2de..0a9cd21 100644 --- a/MarketPriceLookup.Common/MarketPriceLookup.Common.csproj +++ b/MarketPriceLookup.Common/MarketPriceLookup.Common.csproj @@ -4,6 +4,10 @@ net6.0 + + + +