Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize spike detection #7

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DiscordClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private static async Task Main()
await client.LoginAsync(TokenType.Bot, ConfigManager.BotConfig.Token);
await client.StartAsync();

flipperThread.Start();

// Block the program until it is closed.
bool shouldExit = false;
while (!shouldExit)
Expand Down Expand Up @@ -149,8 +151,6 @@ private static async Task OnBotReady()
Logger.Info($"Found {channels.Count} channels to post dump updates on.\n");
Logger.Warn("Please do not close this window manually, but use the 'exit' command to close the bot gracefully.");
Logger.Warn("Type 'help' for a list of commands.");

flipperThread.Start();
}


Expand Down
10 changes: 5 additions & 5 deletions OsrsFlipper/Filtering/Filters/FlipFilters/SpikeRemovalFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ protected override bool CanPassFilter(CacheEntry itemData, ItemPriceHistory hist
// Get the latest high price.
int latestHighPrice = itemData.PriceLatest.HighestPrice;

// Check the last 30 minutes (6 * 5min data points).
for (int i = 0; i < 6; i++)
// Check the last 15 minutes (3 * 5min data points).
for (int i = 0; i < 3; i++)
{
// Get the price before the latest price.
ItemPriceHistoryEntry historyEntry = history5Min.Data[^(2 + i)];

//TODO: Maybe some check for the price being too old?

int? historicalPrice = historyEntry.AvgHighPrice;
int? historicalPrice = historyEntry.HighestPrice;

if (historicalPrice == null)
return false;

// Calculate the percentage increase from the previous price to the latest price.
double percentageIncreaseFromHistory = ((double)latestHighPrice / historicalPrice.Value - 1) * 100;
double percentageIncreaseFromHistorical = ((double)latestHighPrice / historicalPrice.Value * 100) - 100d;

// If the percentage increase is significantly high, consider it a price spike.
if (percentageIncreaseFromHistory > _maxHighPriceIncreasePercentage)
if (percentageIncreaseFromHistorical > _maxHighPriceIncreasePercentage)
return false;
}

Expand Down
15 changes: 8 additions & 7 deletions OsrsFlipper/Flipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace OsrsFlipper;

public sealed class Flipper : IDisposable
{
private const bool DEBUG_FILTERS = false;
private const bool DEBUG_FILTERS = true;
private const int MAX_PRUNE_PASS_COUNT = 75;

/// <summary>
/// The API controller used to fetch data from the OSRS API.
Expand Down Expand Up @@ -50,9 +51,9 @@ private Flipper(OsrsApiController apiController, ItemCache cache, int cooldownMi
// Prune filters are used to quickly discard items that are not worth considering, and to avoid fetching additional API data for them.
_filterCollection
.AddPruneFilter(new ItemCooldownFilter(_cooldownManager)) // Skip items that are on a cooldown.
.AddPruneFilter(new TransactionAgeFilter(2, 10)) // Skip items that have not been traded in the last X minutes.
.AddPruneFilter(new TransactionAgeFilter(2, 8)) // Skip items that have not been traded in the last X minutes.
.AddPruneFilter(new Item24HAveragePriceFilter(50, 50_000_000)) // Skip items with a 24-hour average price outside the range X - Y.
.AddPruneFilter(new TransactionVolumeFilter(2_000_000)) // Skip items with a transaction volume less than X gp.
.AddPruneFilter(new TransactionVolumeFilter(3_000_000)) // Skip items with a transaction volume less than X gp.
.AddPruneFilter(new ReturnOfInvestmentFilter(4)); // Skip items with a return of investment less than X%.
//.AddPruneFilter(new VolatilityFilter(12)) // Skip items with a price fluctuation of more than X% in the last 30 minutes.

Expand All @@ -67,7 +68,7 @@ private Flipper(OsrsApiController apiController, ItemCache cache, int cooldownMi
/// <summary>
/// Creates a new Flipper instance.
/// </summary>
public static async Task<Flipper> Create(int cooldownMinutes = 5)
public static async Task<Flipper> Create(int cooldownMinutes = 8)
{
OsrsApiController apiController = new();

Expand Down Expand Up @@ -95,10 +96,10 @@ public async Task<List<ItemDump>> FindDumps()
foreach (CacheEntry entry in _cache.Entries())
{
// Safety check: If the pruning filters somehow stop working (or there are none), we won't spam the user with messages and the API with requests.
if (itemsPassedPruneCount >= 50)
if (itemsPassedPruneCount >= MAX_PRUNE_PASS_COUNT)
{
Logger.Warn("Over 50 items passed all pruning filters." +
"This is not generally good, and will result in wasted resources." +
Logger.Warn($"Over {MAX_PRUNE_PASS_COUNT} items passed all pruning filters. " +
"This is generally not good, and will result in wasted resources. " +
"Stopping dump search.");
break;
}
Expand Down
Loading