Pure code in src/Anubis/Baskets for the basket reversion family of the new route, so the engine of phase 14 and the eToro Demo of phase 20 share one home for the arithmetic of the research document: - ServerClock: New York + 7 h, the server day changing at 17:00 New York, half-open weekend rule, hourly window and daily venue pause. - Relations: chain, ratio and disjoint couples (56 + 112 + 210 = 378), appendix B in the document's row order, the eight documented spreads, the index sign and the 14-pair list. - BasketIndex: pip and log basis-point index, sample z-score, Rvol with its filter, correlation of log returns and the OLS hedge ratio; every value at bar t reads bars up to t only. - BasketMath (decimal): pip sum, common exit, distance scale with no addition after the last level, stop excursion of table 4.12, pip value, equal units raised to the venue minimum, exposure, three-costs rule. - Styles: pair states, the four styles, the Fedele and Prudente profiles. Every acceptance criterion of the item has a named test with the document's numbers, plus a fixed-seed property test that nothing at a closed bar depends on later bars. refs #119 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
11 KiB
C#
226 lines
11 KiB
C#
using Anubis.Baskets;
|
||
|
||
namespace Anubis.Tests;
|
||
|
||
/// <summary>
|
||
/// The relations among the 28 pairs (Phase 12, issue #119): the counts of section 4.1, appendix B row by row, the eight
|
||
/// documented spreads of section 3.3, the sign of the index and the 14-pair list.
|
||
/// </summary>
|
||
[TestClass]
|
||
public sealed class BasketRelationTests
|
||
{
|
||
/// <summary>Appendix B, written out by hand from the document: row, leg A, leg B, synthetic cross.</summary>
|
||
private static readonly string[][] AppendixB =
|
||
[
|
||
["EURGBP", "GBPAUD", "EURAUD"], ["EURGBP", "GBPNZD", "EURNZD"], ["EURGBP", "GBPUSD", "EURUSD"],
|
||
["EURGBP", "GBPCAD", "EURCAD"], ["EURGBP", "GBPCHF", "EURCHF"], ["EURGBP", "GBPJPY", "EURJPY"],
|
||
["EURAUD", "AUDNZD", "EURNZD"], ["EURAUD", "AUDUSD", "EURUSD"], ["EURAUD", "AUDCAD", "EURCAD"],
|
||
["EURAUD", "AUDCHF", "EURCHF"], ["EURAUD", "AUDJPY", "EURJPY"], ["GBPAUD", "AUDNZD", "GBPNZD"],
|
||
["GBPAUD", "AUDUSD", "GBPUSD"], ["GBPAUD", "AUDCAD", "GBPCAD"], ["GBPAUD", "AUDCHF", "GBPCHF"],
|
||
["GBPAUD", "AUDJPY", "GBPJPY"], ["EURNZD", "NZDUSD", "EURUSD"], ["EURNZD", "NZDCAD", "EURCAD"],
|
||
["EURNZD", "NZDCHF", "EURCHF"], ["EURNZD", "NZDJPY", "EURJPY"], ["GBPNZD", "NZDUSD", "GBPUSD"],
|
||
["GBPNZD", "NZDCAD", "GBPCAD"], ["GBPNZD", "NZDCHF", "GBPCHF"], ["GBPNZD", "NZDJPY", "GBPJPY"],
|
||
["AUDNZD", "NZDUSD", "AUDUSD"], ["AUDNZD", "NZDCAD", "AUDCAD"], ["AUDNZD", "NZDCHF", "AUDCHF"],
|
||
["AUDNZD", "NZDJPY", "AUDJPY"], ["EURUSD", "USDCAD", "EURCAD"], ["EURUSD", "USDCHF", "EURCHF"],
|
||
["EURUSD", "USDJPY", "EURJPY"], ["GBPUSD", "USDCAD", "GBPCAD"], ["GBPUSD", "USDCHF", "GBPCHF"],
|
||
["GBPUSD", "USDJPY", "GBPJPY"], ["AUDUSD", "USDCAD", "AUDCAD"], ["AUDUSD", "USDCHF", "AUDCHF"],
|
||
["AUDUSD", "USDJPY", "AUDJPY"], ["NZDUSD", "USDCAD", "NZDCAD"], ["NZDUSD", "USDCHF", "NZDCHF"],
|
||
["NZDUSD", "USDJPY", "NZDJPY"], ["EURCAD", "CADCHF", "EURCHF"], ["EURCAD", "CADJPY", "EURJPY"],
|
||
["GBPCAD", "CADCHF", "GBPCHF"], ["GBPCAD", "CADJPY", "GBPJPY"], ["AUDCAD", "CADCHF", "AUDCHF"],
|
||
["AUDCAD", "CADJPY", "AUDJPY"], ["NZDCAD", "CADCHF", "NZDCHF"], ["NZDCAD", "CADJPY", "NZDJPY"],
|
||
["USDCAD", "CADCHF", "USDCHF"], ["USDCAD", "CADJPY", "USDJPY"], ["EURCHF", "CHFJPY", "EURJPY"],
|
||
["GBPCHF", "CHFJPY", "GBPJPY"], ["AUDCHF", "CHFJPY", "AUDJPY"], ["NZDCHF", "CHFJPY", "NZDJPY"],
|
||
["USDCHF", "CHFJPY", "USDJPY"], ["CADCHF", "CHFJPY", "CADJPY"],
|
||
];
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void TheCouplesOfTheTwentyEightPairsAre56Chains112RatiosAnd210Disjoint()
|
||
{
|
||
var couples = Relations.Couples;
|
||
|
||
Assert.AreEqual(378, couples.Count);
|
||
Assert.AreEqual(378, couples.Select(c => (c.A.Name, c.B.Name)).Distinct().Count());
|
||
Assert.AreEqual(56, couples.Count(c => c.Kind == RelationKind.Chain));
|
||
Assert.AreEqual(112, couples.Count(c => c.Kind == RelationKind.Ratio));
|
||
Assert.AreEqual(210, couples.Count(c => c.Kind == RelationKind.Disjoint));
|
||
|
||
// Every pair is the synthetic cross of exactly six couples that share a currency, one per other currency (section 4.1).
|
||
foreach (var pair in FxPairs.All)
|
||
{
|
||
Assert.AreEqual(6, couples.Count(c => c.Cross == pair), pair.Name);
|
||
}
|
||
|
||
Assert.IsTrue(couples.Where(c => c.Kind == RelationKind.Disjoint).All(c => c.Common is null && c.Cross is null));
|
||
Assert.IsTrue(couples.Where(c => c.Kind == RelationKind.Chain).All(c => !c.CrossInverted));
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void AppendixBIsReproducedRowByRow()
|
||
{
|
||
var rows = Relations.AppendixB;
|
||
int[] listed = [3, 9, 17, 29, 30, 35];
|
||
|
||
Assert.AreEqual(56, rows.Count);
|
||
for (var i = 0; i < AppendixB.Length; i++)
|
||
{
|
||
var row = rows[i];
|
||
var expected = AppendixB[i];
|
||
Assert.AreEqual(i + 1, row.Row);
|
||
Assert.AreEqual(expected[0], row.A.Name, $"riga {i + 1}");
|
||
Assert.AreEqual(expected[1], row.B.Name, $"riga {i + 1}");
|
||
Assert.AreEqual(expected[2], row.Cross.Name, $"riga {i + 1}");
|
||
Assert.AreEqual(listed.Contains(i + 1), row.Listed, $"riga {i + 1}");
|
||
|
||
// The common currency is the quote of A and the base of B; the classification agrees with the row.
|
||
Assert.AreEqual(row.A.Quote, row.B.Base);
|
||
var relation = Relations.Classify(row.A, row.B);
|
||
Assert.AreEqual(RelationKind.Chain, relation.Kind);
|
||
Assert.AreEqual(row.Cross, relation.Cross);
|
||
Assert.AreEqual(row.A.Quote, relation.Common);
|
||
}
|
||
|
||
// The 56 rows are the 56 chains of the 378 couples, each once.
|
||
var chains = Relations.Couples.Where(c => c.Kind == RelationKind.Chain).Select(c => Key(c.A, c.B)).ToHashSet();
|
||
CollectionAssert.AreEquivalent(chains.ToList(), rows.Select(r => Key(r.A, r.B)).ToList());
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void TheEightDocumentedSpreadsAreSixChainsAndTwoDisjointWhateverTheLegOrder()
|
||
{
|
||
(string A, string B, RelationKind Kind, string? Cross)[] expected =
|
||
[
|
||
("EURUSD", "USDCHF", RelationKind.Chain, "EURCHF"),
|
||
("AUDUSD", "USDCAD", RelationKind.Chain, "AUDCAD"),
|
||
("NZDUSD", "EURNZD", RelationKind.Chain, "EURUSD"),
|
||
("USDCAD", "EURUSD", RelationKind.Chain, "EURCAD"),
|
||
("EURAUD", "AUDCAD", RelationKind.Chain, "EURCAD"),
|
||
("GBPUSD", "EURGBP", RelationKind.Chain, "EURUSD"),
|
||
("NZDUSD", "AUDCHF", RelationKind.Disjoint, null),
|
||
("EURCHF", "AUDCAD", RelationKind.Disjoint, null),
|
||
];
|
||
|
||
var spreads = Relations.DocumentedSpreads;
|
||
Assert.AreEqual(8, spreads.Count);
|
||
for (var i = 0; i < expected.Length; i++)
|
||
{
|
||
var (a, b, kind, cross) = expected[i];
|
||
Assert.AreEqual(a, spreads[i].A.Name);
|
||
Assert.AreEqual(b, spreads[i].B.Name);
|
||
|
||
var forward = Relations.Classify(FxPairs.Get(a), FxPairs.Get(b));
|
||
var backward = Relations.Classify(FxPairs.Get(b), FxPairs.Get(a));
|
||
Assert.AreEqual(kind, forward.Kind, $"{a}–{b}");
|
||
Assert.AreEqual(kind, backward.Kind, $"{b}–{a}");
|
||
Assert.AreEqual(cross, forward.Cross?.Name, $"{a}–{b}");
|
||
Assert.AreEqual(cross, backward.Cross?.Name, $"{b}–{a}");
|
||
Assert.AreEqual(forward.Common, backward.Common);
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void NoClassificationDependsOnTheOrderOfTheLegs()
|
||
{
|
||
foreach (var couple in Relations.Couples)
|
||
{
|
||
var swapped = Relations.Classify(couple.B, couple.A);
|
||
Assert.AreEqual(couple.Kind, swapped.Kind);
|
||
Assert.AreEqual(couple.Cross, swapped.Cross);
|
||
Assert.AreEqual(couple.Common, swapped.Common);
|
||
|
||
// Only the orientation of a ratio's synthetic A/B turns over with the legs.
|
||
if (couple.Kind == RelationKind.Ratio)
|
||
{
|
||
Assert.AreNotEqual(couple.CrossInverted, swapped.CrossInverted);
|
||
}
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void ARatioNamesItsSyntheticCrossAndWhetherItIsTheInverse()
|
||
{
|
||
// Both quote USD: EURUSD / GBPUSD = EURGBP (section 4.1); swapped it is 1/EURGBP.
|
||
var both = Relations.Classify(FxPairs.Get("EURUSD"), FxPairs.Get("GBPUSD"));
|
||
Assert.AreEqual(RelationKind.Ratio, both.Kind);
|
||
Assert.AreEqual("USD", both.Common);
|
||
Assert.AreEqual("EURGBP", both.Cross?.Name);
|
||
Assert.IsFalse(both.CrossInverted);
|
||
Assert.IsTrue(Relations.Classify(FxPairs.Get("GBPUSD"), FxPairs.Get("EURUSD")).CrossInverted);
|
||
|
||
// Both base EUR: EURUSD / EURGBP = USD per GBP = GBPUSD.
|
||
var bases = Relations.Classify(FxPairs.Get("EURUSD"), FxPairs.Get("EURGBP"));
|
||
Assert.AreEqual(RelationKind.Ratio, bases.Kind);
|
||
Assert.AreEqual("GBPUSD", bases.Cross?.Name);
|
||
Assert.IsFalse(bases.CrossInverted);
|
||
Assert.IsTrue(Relations.Classify(FxPairs.Get("EURGBP"), FxPairs.Get("EURUSD")).CrossInverted);
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void TheSignIsPlusForChainsMinusForRatiosAndAgainstTheCorrelationForDisjoint()
|
||
{
|
||
Assert.AreEqual(1, Relations.Sign(RelationKind.Chain, null));
|
||
Assert.AreEqual(1, Relations.Sign(RelationKind.Chain, 0.9));
|
||
Assert.AreEqual(-1, Relations.Sign(RelationKind.Ratio, null));
|
||
Assert.AreEqual(-1, Relations.Sign(RelationKind.Ratio, -0.9));
|
||
Assert.AreEqual(-1, Relations.Sign(RelationKind.Disjoint, 0.3));
|
||
Assert.AreEqual(1, Relations.Sign(RelationKind.Disjoint, -0.3));
|
||
|
||
// With no correlation, or a zero one, a disjoint couple has no sign and cannot be traded.
|
||
Assert.IsNull(Relations.Sign(RelationKind.Disjoint, 0));
|
||
Assert.IsNull(Relations.Sign(RelationKind.Disjoint, null));
|
||
Assert.IsNull(Relations.Sign(RelationKind.Disjoint, double.NaN));
|
||
|
||
var disjoint = Relations.Classify(FxPairs.Get("NZDUSD"), FxPairs.Get("AUDCHF"));
|
||
Assert.AreEqual(-1, disjoint.Sign(0.5));
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void ACoupleOfAPairWithItselfIsRefused()
|
||
{
|
||
var pair = FxPairs.Get("EURUSD");
|
||
Assert.ThrowsExactly<ArgumentException>(() => Relations.Classify(pair, pair));
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void TheFourteenPairListIsTheTwelveDocumentedSymbolsPlusEurcadAndAudnzd()
|
||
{
|
||
string[] expected =
|
||
[
|
||
"EURGBP", "EURAUD", "EURNZD", "EURUSD", "EURCAD", "EURCHF", "GBPUSD",
|
||
"AUDNZD", "AUDUSD", "AUDCAD", "AUDCHF", "NZDUSD", "USDCAD", "USDCHF",
|
||
];
|
||
|
||
var list = Relations.FourteenPairs;
|
||
CollectionAssert.AreEqual(expected, list.Select(p => p.Name).ToArray());
|
||
|
||
// Appendix A order, no yen: the order of the 28 with the others left out.
|
||
CollectionAssert.AreEqual(FxPairs.All.Where(list.Contains).ToArray(), list.ToArray());
|
||
Assert.IsFalse(list.Any(p => p.Quote == Currencies.Jpy));
|
||
|
||
// Every leg of the eight documented spreads is in the list.
|
||
Assert.IsTrue(Relations.DocumentedSpreads.All(s => list.Contains(s.A) && list.Contains(s.B)));
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void ThePipIsOneHundredthForYenPairsAndOneTenThousandthForTheOthers()
|
||
{
|
||
string[] yen = ["EURJPY", "GBPJPY", "AUDJPY", "NZDJPY", "USDJPY", "CADJPY", "CHFJPY"];
|
||
foreach (var pair in FxPairs.All)
|
||
{
|
||
Assert.AreEqual(yen.Contains(pair.Name) ? 0.01m : 0.0001m, pair.Pip, pair.Name);
|
||
}
|
||
|
||
Assert.AreEqual(7, FxPairs.All.Count(p => p.Pip == 0.01m));
|
||
}
|
||
|
||
private static string Key(FxPair a, FxPair b) => string.CompareOrdinal(a.Name, b.Name) < 0 ? a.Name + b.Name : b.Name + a.Name;
|
||
}
|