Both clients are written in house on HttpClient and ClientWebSocket, behind IMarketDataSource, with a rate limiter on the injected clock and retries that wait on it too. OKX: EUR pairs only (trap 14), rules from public/instruments, daily candles with bar=1Dutc and a refusal of any candle not opening at 00:00 UTC (trap 23), backwards paging of history-candles, errors read from the code field because OKX answers them with HTTP 200, tickers on the public stream and candles on the business stream with ping after silence. The HMAC signer and the demo header are ready for Phase 3. Alpaca: consolidated daily bars, real and adjusted, dated in New York and never newer than 15 minutes (trap 8), an explicit feed on every request, IEX for recent minutes, quotes and the stream (at most 30 symbols), calendar and clock converted to UTC, corporate actions. Without keys nothing is sent and the reason is stated. Tests use the documentation's examples and synthetic answers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
247 lines
13 KiB
C#
247 lines
13 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Anubis.Brokers;
|
|
using Anubis.Core;
|
|
using Anubis.Data;
|
|
|
|
namespace Anubis.Tests;
|
|
|
|
/// <summary>The data part of the OKX Europe adapter on recorded responses (issue #18).</summary>
|
|
[TestClass]
|
|
public sealed class OkxTests
|
|
{
|
|
private static readonly DateTimeOffset _now = new(2026, 11, 10, 13, 45, 0, TimeSpan.Zero);
|
|
|
|
private static (OkxClient Client, FakeHttp Http) Create(Action<FakeHttp> setup, IClock? clock = null, OkxOptions? options = null)
|
|
{
|
|
var http = new FakeHttp();
|
|
setup(http);
|
|
return (new OkxClient(http.Client(), clock ?? new FastClock(_now), options), http);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task InstrumentsAreOnlyEuroPairsWithTheirRules()
|
|
{
|
|
var (okx, http) = Create(h => h.On("/api/v5/public/instruments?instType=SPOT", Fixture.Text("okx/instruments-spot.json")));
|
|
var list = await okx.ListInstrumentsAsync(CancellationToken.None).ConfigureAwait(false);
|
|
|
|
// USDT is not tradable in Europe (trap 14); USDC and PLN pairs are not used (SPEC 8.1).
|
|
CollectionAssert.AreEqual(new[] { "BTC-EUR", "ETH-EUR", "SOL-EUR" }, list.Select(i => i.Symbol).ToArray());
|
|
var btc = list[0];
|
|
Assert.AreEqual(new InstrumentSpec(Venues.Okx, "BTC-EUR", "BTC/EUR", AssetClasses.Crypto, "EUR")
|
|
{
|
|
BaseCurrency = "BTC",
|
|
TickSize = 0.1m,
|
|
LotSize = 0.00000001m,
|
|
MinSize = 0.0001m,
|
|
Status = "live",
|
|
ListedAt = new DateTimeOffset(2023, 11, 21, 4, 11, 0, TimeSpan.Zero),
|
|
}, btc);
|
|
Assert.IsTrue(http.Requests.All(r => r.RequestUri!.Host == "eea.okx.com"), "the European host");
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task DailyCandlesCloseAtMidnightUtcAndSkipTheRunningDay()
|
|
{
|
|
var (okx, http) = Create(h => h.On("history-candles", Fixture.Text("okx/candles-1Dutc-btc-eur.json")));
|
|
var bars = await okx.FetchDailyBarsAsync("BTC-EUR", new DateOnly(2026, 11, 6), new DateOnly(2026, 11, 10), CancellationToken.None).ConfigureAwait(false);
|
|
|
|
var request = http.Requests.Single().RequestUri!.ToString();
|
|
StringAssert.Contains(request, "bar=1Dutc", "never 1D, which opens at 16:00 UTC (trap 23)");
|
|
StringAssert.Contains(request, "after=" + new DateTimeOffset(2026, 11, 11, 0, 0, 0, TimeSpan.Zero).ToUnixTimeMilliseconds());
|
|
|
|
// 10 November is still running (confirm 0) and is left out.
|
|
CollectionAssert.AreEqual(
|
|
new[] { new DateOnly(2026, 11, 6), new DateOnly(2026, 11, 7), new DateOnly(2026, 11, 8), new DateOnly(2026, 11, 9) },
|
|
bars.Select(b => b.Day).ToArray());
|
|
var last = bars[^1];
|
|
Assert.AreEqual((67102.9, 67730.8, 65957.5, 67058.9, 87.03419039), (last.Open, last.High, last.Low, last.Close, last.Volume));
|
|
Assert.AreEqual(5799147.867285542, last.QuoteVolume);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task HistoryPagesBackwardsUntilTheFirstCandle()
|
|
{
|
|
var first = new DateTimeOffset(2023, 11, 21, 0, 0, 0, TimeSpan.Zero).ToUnixTimeMilliseconds();
|
|
var (okx, http) = Create(h => h
|
|
.On("after=" + first, """{"code":"0","data":[],"msg":""}""")
|
|
.On("history-candles", Fixture.Text("okx/history-candles-1Dutc-btc-eur-first.json")));
|
|
var bars = await okx.FetchDailyBarsAsync("BTC-EUR", new DateOnly(2017, 1, 1), new DateOnly(2023, 11, 25), CancellationToken.None).ConfigureAwait(false);
|
|
|
|
Assert.HasCount(2, http.Requests, "the second page is empty: the history starts on 21 November 2023");
|
|
Assert.AreEqual(new DateOnly(2023, 11, 21), bars[0].Day);
|
|
Assert.AreEqual(new DateOnly(2023, 11, 25), bars[^1].Day);
|
|
Assert.HasCount(5, bars);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task ACandleThatDoesNotOpenAtMidnightIsRefused()
|
|
{
|
|
var sixteen = new DateTimeOffset(2026, 11, 8, 16, 0, 0, TimeSpan.Zero).ToUnixTimeMilliseconds();
|
|
var (okx, _) = Create(h => h.On("history-candles", $$"""{"code":"0","data":[["{{sixteen}}","1","2","0.5","1.5","3","4","4","1"]],"msg":""}"""));
|
|
var error = await Assert.ThrowsAsync<MarketDataException>(() =>
|
|
okx.FetchDailyBarsAsync("BTC-EUR", new DateOnly(2026, 11, 8), new DateOnly(2026, 11, 8), CancellationToken.None)).ConfigureAwait(false);
|
|
StringAssert.Contains(error.Message, "16:00");
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task MinuteCandlesUseTheRecentEndpointForTheLastDay()
|
|
{
|
|
var (okx, http) = Create(h => h.On("/market/candles?", Fixture.Text("okx/candles-1m-btc-eur.json")));
|
|
var start = DateTimeOffset.FromUnixTimeMilliseconds(1794318000000);
|
|
var bars = await okx.FetchMinuteBarsAsync("BTC-EUR", start, start.AddMinutes(5), CancellationToken.None).ConfigureAwait(false);
|
|
Assert.HasCount(4, bars, "the running minute is left out");
|
|
Assert.AreEqual(start, bars[0].OpensAt);
|
|
StringAssert.Contains(http.Requests[0].RequestUri!.ToString(), "bar=1m");
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task ErrorsArriveWithStatus200AndBusyAnswersAreRetried()
|
|
{
|
|
var calls = 0;
|
|
var clock = new FastClock(_now);
|
|
var (okx, _) = Create(h => h.On(_ => true, _ => ++calls == 1
|
|
? FakeHttp.Respond("""{"code":"50011","data":[],"msg":"Rate limit reached"}""")
|
|
: FakeHttp.Respond(Fixture.Text("okx/ticker-btc-eur.json"))), clock);
|
|
var quote = await okx.FetchQuoteAsync("BTC-EUR", CancellationToken.None).ConfigureAwait(false);
|
|
Assert.AreEqual(2, calls);
|
|
Assert.IsGreaterThanOrEqualTo(TimeSpan.FromSeconds(2), clock.Waited, "waits before trying again");
|
|
Assert.AreEqual((65986.1m, 65998.6m), (quote.Bid, quote.Ask));
|
|
Assert.AreEqual(0.02600011m, quote.BidSize);
|
|
Assert.AreEqual(DateTimeOffset.FromUnixTimeMilliseconds(1794318312368), quote.At);
|
|
Assert.AreEqual(1.894, quote.SpreadBp!.Value, 0.001);
|
|
|
|
var (unknown, _) = Create(h => h.On("ticker", """{"code":"51001","data":[],"msg":"Instrument ID, Instrument ID code, or Spread ID doesn't exist."}"""));
|
|
var error = await Assert.ThrowsAsync<MarketDataException>(() => unknown.FetchQuoteAsync("XXX-EUR", CancellationToken.None)).ConfigureAwait(false);
|
|
Assert.IsFalse(error.Retryable);
|
|
StringAssert.Contains(error.Message, "51001");
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task DemoAddsTheSimulatedTradingHeader()
|
|
{
|
|
var (okx, http) = Create(h => h.On("ticker", Fixture.Text("okx/ticker-btc-eur.json")), options: OkxOptions.ForDemo());
|
|
await okx.FetchQuoteAsync("BTC-EUR", CancellationToken.None).ConfigureAwait(false);
|
|
Assert.AreEqual("1", http.Requests[0].Headers.GetValues("x-simulated-trading").Single());
|
|
Assert.AreEqual("wseeapap.okx.com", OkxOptions.ForDemo().BusinessStreamUrl.Host);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task FundingRateAndOpenInterestOfBitcoin()
|
|
{
|
|
var (okx, _) = Create(h => h
|
|
.On("funding-rate", Fixture.Text("okx/funding-rate-btc.json"))
|
|
.On("open-interest", Fixture.Text("okx/open-interest-btc.json")));
|
|
var funding = await okx.FetchFundingRateAsync("BTC-USDT-SWAP", CancellationToken.None).ConfigureAwait(false);
|
|
Assert.AreEqual(-0.0000009390698834, funding.Rate);
|
|
Assert.AreEqual(TimeSpan.FromHours(8), funding.Interval);
|
|
var interest = await okx.FetchOpenInterestAsync("BTC-USDT-SWAP", CancellationToken.None).ConfigureAwait(false);
|
|
Assert.AreEqual(36006.7700000001675, interest.Coins, 1e-6);
|
|
Assert.AreEqual(2659809297.869, interest.Usd, 0.01);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public void SignatureIsHmacSha256OfTimestampMethodPathAndBody()
|
|
{
|
|
var at = new DateTimeOffset(2026, 11, 10, 13, 45, 1, 234, TimeSpan.Zero);
|
|
var credentials = new OkxCredentials("key-id", "secret-value", "pass-phrase");
|
|
var headers = OkxSigner.Headers(credentials, at, "get", "/api/v5/account/balance?ccy=EUR", "", demo: true);
|
|
Assert.AreEqual("2026-11-10T13:45:01.234Z", headers["OK-ACCESS-TIMESTAMP"]);
|
|
var expected = Convert.ToBase64String(HMACSHA256.HashData(Encoding.UTF8.GetBytes("secret-value"),
|
|
Encoding.UTF8.GetBytes("2026-11-10T13:45:01.234ZGET/api/v5/account/balance?ccy=EUR")));
|
|
Assert.AreEqual(expected, headers["OK-ACCESS-SIGN"]);
|
|
Assert.AreEqual("1", headers["x-simulated-trading"]);
|
|
Assert.AreEqual("OkxCredentials(***)", credentials.ToString(), "never printed");
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public async Task StreamSubscribesTickersAndCandlesOnTheirHostsAndPingsWhenSilent()
|
|
{
|
|
var clock = new ManualClock(_now);
|
|
var sockets = new List<FakeSocket>();
|
|
var okx = new OkxClient(new FakeHttp().Client(), clock, sockets: () =>
|
|
{
|
|
var socket = new FakeSocket();
|
|
lock (sockets)
|
|
{
|
|
sockets.Add(socket);
|
|
}
|
|
|
|
return socket;
|
|
});
|
|
|
|
using var stop = new CancellationTokenSource(TimeSpan.FromSeconds(20));
|
|
var events = new List<MarketEvent>();
|
|
var reading = Task.Run(async () =>
|
|
{
|
|
await foreach (var item in okx.StreamAsync(["BTC-EUR", "ETH-EUR"], stop.Token).ConfigureAwait(false))
|
|
{
|
|
lock (events)
|
|
{
|
|
events.Add(item);
|
|
}
|
|
}
|
|
});
|
|
|
|
await WaitForAsync(() => sockets.Count == 2 && sockets.All(s => s.Sent.Count == 1)).ConfigureAwait(false);
|
|
var publicSocket = sockets.Single(s => s.Url!.AbsolutePath.EndsWith("/public", StringComparison.Ordinal));
|
|
var businessSocket = sockets.Single(s => s.Url!.AbsolutePath.EndsWith("/business", StringComparison.Ordinal));
|
|
StringAssert.Contains(publicSocket.Sent[0], "\"channel\":\"tickers\",\"instId\":\"ETH-EUR\"");
|
|
StringAssert.Contains(businessSocket.Sent[0], "\"channel\":\"candle1m\",\"instId\":\"BTC-EUR\"");
|
|
|
|
publicSocket.Push("""{"id":"1","event":"subscribe","arg":{"channel":"tickers","instId":"BTC-EUR"},"connId":"53efe456"}""");
|
|
publicSocket.Push("""{"arg":{"channel":"tickers","instId":"BTC-EUR"},"data":[{"instType":"SPOT","instId":"BTC-EUR","last":"66148.9","lastSz":"0.00010133","askPx":"66134.7","askSz":"0.01847","bidPx":"66129.6","bidSz":"0.0193605","ts":"1794318514123"}]}""");
|
|
businessSocket.Push("""{"arg":{"channel":"candle1m","instId":"BTC-EUR"},"data":[["1794318180000","65999.8","66069.8","65999.8","66050.5","0.0523422","3457.08","3457.08","1"]]}""");
|
|
await WaitForAsync(() => events.Count == 2).ConfigureAwait(false);
|
|
var quote = events.OfType<QuoteEvent>().Single().Quote;
|
|
Assert.AreEqual((66129.6m, 66134.7m), (quote.Bid, quote.Ask));
|
|
var bar = events.OfType<BarEvent>().Single();
|
|
Assert.IsTrue(bar.Closed);
|
|
Assert.AreEqual(66050.5, bar.Bar.Close);
|
|
|
|
// Twenty seconds of silence: "ping". The public side answers, the business side does not and is dropped.
|
|
await WaitForAsync(() => clock.Waiting >= 2).ConfigureAwait(false);
|
|
clock.Advance(TimeSpan.FromSeconds(21));
|
|
await WaitForAsync(() => publicSocket.Sent.Contains("ping") && businessSocket.Sent.Contains("ping")).ConfigureAwait(false);
|
|
publicSocket.Push("pong");
|
|
await WaitForAsync(() => clock.Waiting >= 2).ConfigureAwait(false);
|
|
clock.Advance(TimeSpan.FromSeconds(21));
|
|
await reading.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(false);
|
|
Assert.IsTrue(sockets.All(s => s.Disposed), "both connections are closed when the feed ends");
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
public void StreamErrorsStopTheFeed()
|
|
{
|
|
var error = Assert.Throws<MarketDataException>(() => OkxClient.ParseStreamMessage(
|
|
"""{"event":"error","msg":"Subscribe failed, wrong URL or channel:candle1m,instId:BTC-EUR doesn't exist.","code":"60018","connId":"53efe456","id":"2"}"""));
|
|
StringAssert.Contains(error.Message, "60018");
|
|
Assert.IsEmpty(OkxClient.ParseStreamMessage("""{"event":"notice","msg":"WebSocket port 8443 will be retired soon.","code":"64010"}"""));
|
|
}
|
|
|
|
internal static async Task WaitForAsync(Func<bool> condition)
|
|
{
|
|
var deadline = DateTime.UtcNow.AddSeconds(10);
|
|
while (!condition())
|
|
{
|
|
if (DateTime.UtcNow > deadline)
|
|
{
|
|
Assert.Fail("Condition not reached in time.");
|
|
}
|
|
|
|
await Task.Delay(10).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|