Fase 12 — Dati e matematica dei panieri (#187)
Research-only data and maths for basket mean reversion (ADR 0028): a seam between the trial judge and the engines, the basket maths in src/Anubis/Baskets, sub-day research bars, ECB crosses and FX minute import. refs #118, refs #119, refs #120, refs #121 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #187.
This commit is contained in:
1 parent
93cdf7850d
commit
e0a438a0b2
40 files changed
+4464
-110
No files matched your search
@@ -0,0 +1,278 @@
|
||||
using System.Globalization;
|
||||
using Anubis.Baskets;
|
||||
|
||||
namespace Anubis.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The money side of the basket maths (Phase 12, issue #119): pip sum and common exit, table 4.5, the distance scale, the
|
||||
/// stop excursions of table 4.12 and section 6.9, pip value, units raised to the venue minimum, exposure and the three-costs
|
||||
/// rule. Every expected number is the document's, or worked out by hand in the comment next to it.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public sealed class BasketMathTests
|
||||
{
|
||||
private static readonly decimal[] AudusdEntries = [0.69456m, 0.69003m, 0.68739m];
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void ThreeAudusdBuysWithTarget69ExitAtTheCommonPrice()
|
||||
{
|
||||
var pair = FxPairs.Get("AUDUSD");
|
||||
|
||||
// mean = 2.07198 / 3 = 0.69066; target / n = 6.9 / 3 = 2.3 pips = 0.00023; P* = 0.69089.
|
||||
var exit = BasketMath.CommonExit(AudusdEntries, Direction.Buy, 6.9m, pair.Pip);
|
||||
Assert.IsTrue(Math.Abs(exit - 0.69089m) <= 0.1m * pair.Pip, exit.ToString(CultureInfo.InvariantCulture));
|
||||
|
||||
// At that bid each order has −36.7, +8.6 and +35.0 pips, +6.9 in all.
|
||||
var quote = new BidAsk(0.69089m, 0.69095m);
|
||||
var orders = AudusdEntries.Select(e => new BasketOrder(pair, Direction.Buy, e)).ToArray();
|
||||
CollectionAssert.AreEqual(new[] { -36.7m, 8.6m, 35.0m }, orders.Select(o => o.Pips(quote)).ToArray());
|
||||
Assert.AreEqual(6.9m, BasketMath.PipSum(orders, _ => quote));
|
||||
Assert.AreEqual(6.9m, BasketMath.PipSum(orders, _ => new BidAsk(exit, exit + 0.00006m)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void ShortOrdersExitBelowTheMeanAndCountPipsOnTheAsk()
|
||||
{
|
||||
var pair = FxPairs.Get("USDJPY");
|
||||
decimal[] entries = [157.20m, 157.60m, 158.00m];
|
||||
|
||||
// mean 157.60, target 9 over 3 orders = 3 pips = 0.03 below: 157.57.
|
||||
var exit = BasketMath.CommonExit(entries, Direction.Sell, 9m, pair.Pip);
|
||||
Assert.AreEqual(157.57m, exit);
|
||||
|
||||
var orders = entries.Select(e => new BasketOrder(pair, Direction.Sell, e)).ToArray();
|
||||
Assert.AreEqual(9m, BasketMath.PipSum(orders, _ => new BidAsk(157.55m, exit)));
|
||||
CollectionAssert.AreEqual(new[] { -37m, 3m, 43m }, orders.Select(o => o.Pips(new BidAsk(157.55m, exit))).ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
[DataRow(1, "10.00")]
|
||||
[DataRow(2, "5.00")]
|
||||
[DataRow(3, "3.33")]
|
||||
[DataRow(4, "2.50")]
|
||||
[DataRow(5, "2.00")]
|
||||
[DataRow(8, "1.25")]
|
||||
public void TheMovePerOrderFollowsTable45(int orders, string expected)
|
||||
{
|
||||
var move = BasketMath.MovePerOrder(10m, orders);
|
||||
Assert.AreEqual(decimal.Parse(expected, CultureInfo.InvariantCulture), Math.Round(move, 2));
|
||||
|
||||
// The same identity as the common exit: P* − mean = target / n pips.
|
||||
var entries = Enumerable.Range(0, orders).Select(i => 1.1000m - (i * 0.0040m)).ToArray();
|
||||
var exit = BasketMath.CommonExit(entries, Direction.Buy, 10m, 0.0001m);
|
||||
AssertClose(move * 0.0001m, exit - entries.Average(), 0.0000000000000000001m);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
[DataRow(0.8, "1.0", "40", "40", "40", "40")]
|
||||
[DataRow(0.8, "1.3", "40", "52", "67.6", "87.88")]
|
||||
[DataRow(1.5, "1.0", "60", "60", "60", "60")]
|
||||
[DataRow(1.5, "1.3", "60", "78", "101.4", "131.82")]
|
||||
[DataRow(4.0, "1.0", "120", "120", "120", "120")]
|
||||
[DataRow(4.0, "1.3", "120", "156", "202.8", "263.64")]
|
||||
public void TheDistanceScaleFollowsRvolAndTheGrowth(double rvol, string growth, string d1, string d2, string d3, string d4)
|
||||
{
|
||||
var g = decimal.Parse(growth, CultureInfo.InvariantCulture);
|
||||
decimal[] expected = [.. new[] { d1, d2, d3, d4 }.Select(d => decimal.Parse(d, CultureInfo.InvariantCulture))];
|
||||
|
||||
for (var k = 1; k <= 4; k++)
|
||||
{
|
||||
Assert.AreEqual(expected[k - 1], BasketMath.Distance(40m, rvol, g, k), $"k = {k}");
|
||||
}
|
||||
|
||||
CollectionAssert.AreEqual(expected, BasketMath.Distances(40m, rvol, g, maxReentries: 4).ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void NoLevelIsAddedAfterTheLast()
|
||||
{
|
||||
// Three re-entries: four levels at most; the k-th addition is the one made with k levels open.
|
||||
for (var levels = 1; levels <= 3; levels++)
|
||||
{
|
||||
Assert.IsTrue(BasketMath.CanAdd(levels, maxReentries: 3));
|
||||
Assert.AreEqual(BasketMath.Distance(40m, 1.0, 1.3m, levels), BasketMath.NextDistance(levels, 3, 40m, 1.0, 1.3m));
|
||||
}
|
||||
|
||||
Assert.IsFalse(BasketMath.CanAdd(4, maxReentries: 3));
|
||||
Assert.IsNull(BasketMath.NextDistance(4, 3, 40m, 1.0, 1.3m));
|
||||
Assert.IsFalse(BasketMath.ShouldAdd(-10_000m, levels: 4, maxReentries: 3, 40m, 1.0, 1.3m));
|
||||
|
||||
// With four re-entries (growth variant) the fifth level is the last.
|
||||
Assert.IsTrue(BasketMath.CanAdd(4, maxReentries: 4));
|
||||
Assert.IsFalse(BasketMath.CanAdd(5, maxReentries: 4));
|
||||
Assert.IsFalse(BasketMath.CanAdd(0, maxReentries: 3));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void ALevelIsAddedOnlyWhenTheLastLevelHasLostTheDistance()
|
||||
{
|
||||
// Second level (k = 1): 40 pips; third (k = 2) with growth 1.3: 52 pips, measured from the last level.
|
||||
Assert.IsFalse(BasketMath.ShouldAdd(-39.9m, levels: 1, maxReentries: 3, 40m, 1.0, 1.3m));
|
||||
Assert.IsTrue(BasketMath.ShouldAdd(-40m, levels: 1, maxReentries: 3, 40m, 1.0, 1.3m));
|
||||
Assert.IsFalse(BasketMath.ShouldAdd(-51.9m, levels: 2, maxReentries: 3, 40m, 1.0, 1.3m));
|
||||
Assert.IsTrue(BasketMath.ShouldAdd(-52m, levels: 2, maxReentries: 3, 40m, 1.0, 1.3m));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
[DataRow("0.01", "40", -2310)]
|
||||
[DataRow("0.02", "40", -1185)]
|
||||
[DataRow("0.05", "40", -510)]
|
||||
[DataRow("0.10", "40", -285)]
|
||||
[DataRow("0.01", "65", -2348)]
|
||||
[DataRow("0.02", "65", -1222)]
|
||||
[DataRow("0.05", "65", -548)]
|
||||
[DataRow("0.10", "65", -322)]
|
||||
public void TheEightExcursionsOfTable412(string lot, string distance, int expected)
|
||||
{
|
||||
var l = decimal.Parse(lot, CultureInfo.InvariantCulture);
|
||||
var d = decimal.Parse(distance, CultureInfo.InvariantCulture);
|
||||
|
||||
// Account 10,000 USD, stop at 9%, 10 USD per lot per pip, 1 + 3 levels; shown values rounded half to even.
|
||||
var excursion = BasketMath.StopExcursion(4, d, l, 10m, 0.09m, 10_000m);
|
||||
Assert.AreEqual((decimal)expected, Math.Round(excursion));
|
||||
|
||||
// At that excursion the pip sum loses exactly the 9%: 900 USD.
|
||||
Assert.AreEqual(-900m, BasketMath.PipSumAtExcursion(4, excursion, d) * l * 10m);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
[DataRow(500, -172)]
|
||||
[DataRow(1000, -285)]
|
||||
[DataRow(2500, -622)]
|
||||
[DataRow(5000, -1185)]
|
||||
[DataRow(10000, -2310)]
|
||||
[DataRow(25000, -5685)]
|
||||
public void TheCapitalTableOfSection69(int capital, int expected)
|
||||
{
|
||||
// Lot 0.01 (the pip sum is worth 0.1 USD), D = 40, four levels, 9% of capital.
|
||||
Assert.AreEqual((decimal)expected, Math.Round(BasketMath.StopExcursion(4, 40m, 0.01m, 10m, 0.09m, capital)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void TheLotForAGivenRiskInvertsTheExcursion()
|
||||
{
|
||||
// 0.09 × 10,000 / (10 × |4 × −285 + 6 × 40|) = 900 / 9,000 = 0.10.
|
||||
Assert.AreEqual(0.10m, BasketMath.LotForRisk(4, -285m, 40m, 10m, 0.09m, 10_000m));
|
||||
Assert.AreEqual(4 * -100m + (6 * 40m), BasketMath.PipSumAtExcursion(4, -100m, 40m));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void ThePipValueOfAYenPairIsUnitsTimesPipOverUsdjpy()
|
||||
{
|
||||
// The ECB test day of the plan: USDJPY = 176.79 / 1.1193.
|
||||
var usdjpy = 176.79m / 1.1193m;
|
||||
var prices = Prices(("USDJPY", usdjpy));
|
||||
|
||||
Assert.AreEqual(1m / usdjpy, BasketMath.UsdPerUnit(Currencies.Jpy, prices));
|
||||
AssertClose(10m / usdjpy, BasketMath.PipValue(FxPairs.Get("USDJPY"), 1000m, prices));
|
||||
AssertClose(9.21m / usdjpy, BasketMath.PipValue(FxPairs.Get("EURJPY"), 921m, prices));
|
||||
AssertClose(8.47m / usdjpy, BasketMath.PipValue(FxPairs.Get("CHFJPY"), 847m, prices));
|
||||
AssertClose(0.0633124m, BasketMath.PipValue(FxPairs.Get("USDJPY"), 1000m, prices), 0.0000001m);
|
||||
|
||||
// Appendix A, units per 1,000 USD with USDJPY about 160.3: 0.062, 0.057 and 0.053 USD per pip.
|
||||
var appendix = Prices(("USDJPY", 160.3m));
|
||||
Assert.AreEqual(0.062m, Math.Round(BasketMath.PipValue(FxPairs.Get("USDJPY"), 1000m, appendix), 3));
|
||||
Assert.AreEqual(0.057m, Math.Round(BasketMath.PipValue(FxPairs.Get("EURJPY"), 921m, appendix), 3));
|
||||
Assert.AreEqual(0.053m, Math.Round(BasketMath.PipValue(FxPairs.Get("CHFJPY"), 847m, appendix), 3));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void ThePipValueOfOtherQuotesUsesTheirDollarValue()
|
||||
{
|
||||
var prices = Prices(("EURUSD", 1.0856m), ("USDCHF", 0.847m), ("AUDUSD", 0.6623m));
|
||||
|
||||
Assert.AreEqual(1m, BasketMath.UsdPerUnit(Currencies.Usd, prices));
|
||||
Assert.AreEqual(1.0856m, BasketMath.UsdPerUnit(Currencies.Eur, prices));
|
||||
Assert.AreEqual(0.1m, BasketMath.PipValue(FxPairs.Get("EURUSD"), 1000m, prices));
|
||||
Assert.AreEqual(0.1m / 0.847m, BasketMath.PipValue(FxPairs.Get("USDCHF"), 1000m, prices));
|
||||
|
||||
// Appendix A: 1,510 AUDUSD units per 1,000 USD are worth 0.151 USD per pip.
|
||||
Assert.AreEqual(0.151m, BasketMath.PipValue(FxPairs.Get("AUDUSD"), 1510m, prices));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void BothLegsRiseToTheUnitsThatBringTheWeakestToTheVenueMinimum()
|
||||
{
|
||||
// 0.01 lot per 10,000 USD on 10,000 USD: 1,000 units, worth 662.3 USD on an AUD base, under the 1,000 USD minimum.
|
||||
// 1,000 / 0.6623 = 1,509.89, rounded up: 1,510 on both legs (appendix A).
|
||||
var aud = BasketMath.EqualUnits(0.01m, 10_000m, [1.0856m, 0.6623m]);
|
||||
Assert.AreEqual(1510m, aud.Units);
|
||||
Assert.IsTrue(aud.Raised);
|
||||
Assert.AreEqual(0.0151m, aud.Lot());
|
||||
|
||||
// NZD base: 1,000 / 0.54172 = 1,845.97, rounded up: 1,846. (Appendix A rounds to the nearest unit: at exactly 0.5417
|
||||
// the minimum needs 1,846.04, so 1,847 rounded up.)
|
||||
var nzd = BasketMath.EqualUnits(0.01m, 10_000m, [0.6623m, 0.54172m]);
|
||||
Assert.AreEqual(1846m, nzd.Units);
|
||||
Assert.IsTrue(nzd.Units * 0.54172m >= 1000m);
|
||||
Assert.AreEqual(1847m, BasketMath.EqualUnits(0.01m, 10_000m, [0.5417m]).Units);
|
||||
|
||||
// Above the minimum nothing moves: 2,000 units are 1,083 USD on the NZD leg.
|
||||
var enough = BasketMath.EqualUnits(0.01m, 20_000m, [0.6623m, 0.54172m]);
|
||||
Assert.AreEqual(2000m, enough.Units);
|
||||
Assert.IsFalse(enough.Raised);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void TheNetExposureOfEqualUnitsOnEurusdAndUsdchf()
|
||||
{
|
||||
// Long 1,000 EURUSD at 1.0856: +1,000 EUR, −1,085.6 USD. Long 1,000 USDCHF at 0.847: +1,000 USD, −847 CHF.
|
||||
var exposure = BasketMath.Exposure(
|
||||
[
|
||||
new ExposureLeg(FxPairs.Get("EURUSD"), Direction.Buy, 1000m, 1.0856m),
|
||||
new ExposureLeg(FxPairs.Get("USDCHF"), Direction.Buy, 1000m, 0.847m),
|
||||
]);
|
||||
|
||||
Assert.AreEqual(3, exposure.Count);
|
||||
Assert.AreEqual(1000m, exposure[Currencies.Eur]);
|
||||
Assert.AreEqual(-85.6m, exposure[Currencies.Usd]);
|
||||
Assert.AreEqual(-86m, Math.Round(exposure[Currencies.Usd]));
|
||||
Assert.AreEqual(-847m, exposure[Currencies.Chf]);
|
||||
|
||||
// Selling reverses every sign; in dollars the base is valued at its own price.
|
||||
var sold = BasketMath.Exposure([new ExposureLeg(FxPairs.Get("EURUSD"), Direction.Sell, 1000m, 1.0856m)]);
|
||||
Assert.AreEqual(-1000m, sold[Currencies.Eur]);
|
||||
Assert.AreEqual(1085.6m, sold[Currencies.Usd]);
|
||||
var dollars = BasketMath.UsdExposure(exposure, Prices(("EURUSD", 1.0856m), ("USDCHF", 0.847m)));
|
||||
Assert.AreEqual(1085.6m, dollars[Currencies.Eur]);
|
||||
AssertClose(-1000m, dollars[Currencies.Chf]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Unit)]
|
||||
public void TheThreeCostsRulePassesThreeOfTheFiveDocumentedSpreads()
|
||||
{
|
||||
decimal[] costs = [2.1m, 2.5m, 6.9m, 2.9m, 4.7m];
|
||||
var passing = costs.Where(c => BasketMath.PassesCostRule(10m, c)).ToArray();
|
||||
|
||||
CollectionAssert.AreEqual(new[] { 2.1m, 2.5m, 2.9m }, passing);
|
||||
Assert.IsFalse(BasketMath.PassesCostRule(10m, 6.9m));
|
||||
Assert.IsFalse(BasketMath.PassesCostRule(10m, 4.7m));
|
||||
|
||||
// Exactly three times the cost still passes.
|
||||
Assert.IsTrue(BasketMath.PassesCostRule(9m, 3m));
|
||||
Assert.IsFalse(BasketMath.PassesCostRule(8.99m, 3m));
|
||||
}
|
||||
|
||||
private static Func<FxPair, decimal> Prices(params (string Pair, decimal Price)[] prices)
|
||||
{
|
||||
var map = prices.ToDictionary(p => p.Pair, p => p.Price, StringComparer.Ordinal);
|
||||
return pair => map[pair.Name];
|
||||
}
|
||||
|
||||
private static void AssertClose(decimal expected, decimal actual, decimal tolerance = 0.000000000001m) =>
|
||||
Assert.IsTrue(Math.Abs(expected - actual) <= tolerance, $"{expected} ≠ {actual}");
|
||||
}
|
||||
Reference in new issue
Block a user