use MathNet lib to calc standard dev when choosing average price

This commit is contained in:
Dev 2023-06-29 12:36:11 +01:00
parent bfcff97fc5
commit 220fa4e5cc
3 changed files with 31 additions and 13 deletions

View File

@ -7,6 +7,8 @@ using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text.Json; using System.Text.Json;
using MathNet.Numerics.Statistics;
using System.Data;
namespace MarketPriceLookup.Common.Helpers namespace MarketPriceLookup.Common.Helpers
{ {
@ -101,28 +103,40 @@ namespace MarketPriceLookup.Common.Helpers
} }
/// <summary> /// <summary>
/// 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
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
private static int GetAveragedPrice(Item item) private static int GetAveragedPrice(Item item)
{ {
var sevenDaysAgoTimestamp = DateTimeOffset.UtcNow.AddDays(-7).ToUnixTimeSeconds(); var fourteenDaysAgoTimestamp = DateTimeOffset.UtcNow.AddDays(-14).ToUnixTimeSeconds();
var pricesWithinLast7days = new List<int>(); var filteredPrices = item.historicalPrices.Where(x => long.Parse(x.timestamp) > fourteenDaysAgoTimestamp).OrderBy(x=> x.price).ToList();
foreach (var historicalPrice in item.historicalPrices)
if (filteredPrices.Count == 0)
{ {
if (long.Parse(historicalPrice.timestamp) > sevenDaysAgoTimestamp) // x day filter means no prices, use all data
{ filteredPrices = item.historicalPrices.ToList();
pricesWithinLast7days.Add(historicalPrice.price);
}
} }
if (pricesWithinLast7days.Count == 0) if (filteredPrices.Count == 1)
{ {
LoggingHelpers.LogError($"No prices found for item {item.name} in last 7 days, using any availible"); return filteredPrices[0].price;
pricesWithinLast7days.AddRange(item.historicalPrices.Select(x => x.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) public static Prices GetItemPrice(string key)

View File

@ -17,7 +17,7 @@ namespace MarketPriceLookup.Common.Helpers
{ {
public string name { get; set; } public string name { get; set; }
public int avg24hPrice { get; set; } public int avg24hPrice { get; set; }
public double changeLast48hPercent { get; set; } public double? changeLast48hPercent { get; set; }
public string id { get; set; } public string id { get; set; }
public HistoricalPrice[] historicalPrices { get; set; } public HistoricalPrice[] historicalPrices { get; set; }
} }

View File

@ -4,6 +4,10 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MarketPriceLookup.Common.Models\MarketPriceLookup.Common.Models.csproj" /> <ProjectReference Include="..\MarketPriceLookup.Common.Models\MarketPriceLookup.Common.Models.csproj" />
</ItemGroup> </ItemGroup>