Implement A3, A4, A5, B3: Add MarketOutcomePriceSnapshot, implement Polymarket CLOB prices-history endpoint, improve strategy classification, introduce CopytradingScore, and decouple/optimize scoring pipeline into a separate worker
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Predictalytics.Application.Interfaces;
|
||||
using Predictalytics.Domain.Enums;
|
||||
@@ -13,11 +14,13 @@ public class PolymarketApiClient
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
private readonly HttpClient _gammaClient;
|
||||
private readonly HttpClient _clobClient;
|
||||
private readonly IRateLimiter _rateLimiter;
|
||||
private readonly ILogger<PolymarketApiClient> _logger;
|
||||
|
||||
private const string DataApiBase = "https://data-api.polymarket.com";
|
||||
private const string GammaApiBase = "https://gamma-api.polymarket.com";
|
||||
private const string ClobApiBase = "https://clob.polymarket.com";
|
||||
|
||||
public PolymarketApiClient(IHttpClientFactory httpFactory, IRateLimiter rateLimiter, ILogger<PolymarketApiClient> logger)
|
||||
{
|
||||
@@ -29,6 +32,10 @@ public class PolymarketApiClient
|
||||
_gammaClient.BaseAddress = new Uri(GammaApiBase);
|
||||
_gammaClient.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
|
||||
_clobClient = httpFactory.CreateClient("PolymarketClob");
|
||||
_clobClient.BaseAddress = new Uri(ClobApiBase);
|
||||
_clobClient.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
|
||||
_rateLimiter = rateLimiter;
|
||||
_logger = logger;
|
||||
}
|
||||
@@ -161,4 +168,22 @@ public class PolymarketApiClient
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<PriceHistoryEntry>> GetPricesHistoryAsync(string clobTokenId, string interval = "6h", CancellationToken ct = default)
|
||||
{
|
||||
var url = $"/prices-history?market={clobTokenId}&interval={interval}";
|
||||
var result = await ExecuteWithRetryAsync<PolymarketPriceHistoryResponse>(_clobClient, url, ct);
|
||||
return result?.History ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
public class PolymarketPriceHistoryResponse
|
||||
{
|
||||
[JsonPropertyName("history")] public List<PriceHistoryEntry> History { get; set; } = [];
|
||||
}
|
||||
|
||||
public class PriceHistoryEntry
|
||||
{
|
||||
[JsonPropertyName("t")] public long Timestamp { get; set; }
|
||||
[JsonPropertyName("p")] public double Price { get; set; }
|
||||
}
|
||||
|
||||
@@ -295,4 +295,20 @@ public class PolymarketProvider : IPlatformProvider
|
||||
|
||||
return TradeSide.Unknown;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<MarketOutcomePriceSnapshot>> GetPriceHistoryAsync(string tokenId, CancellationToken ct = default)
|
||||
{
|
||||
using var _ = PlatformLogContext.Push(PlatformName);
|
||||
_logger.LogDebug("Fetching price history for CLOB Token {TokenId}", tokenId);
|
||||
|
||||
var rawHistory = await _api.GetPricesHistoryAsync(tokenId, "6h", ct);
|
||||
_logger.LogInformation("Fetched {Count} price history entries for {TokenId}", rawHistory.Count, tokenId);
|
||||
|
||||
return rawHistory.Select(r => new MarketOutcomePriceSnapshot
|
||||
{
|
||||
Price = (decimal)r.Price,
|
||||
Timestamp = DateTimeOffset.FromUnixTimeSeconds(r.Timestamp).UtcDateTime,
|
||||
MarketOutcomeId = 0
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user