TaUse14DifferentialStress.sleela
lib/advantage/trading-actions-on-verb/14-differential-stress/TaUse14DifferentialStress.sleela · 315 line(s) · sleela
<< lib/advantage/trading-actions-on-verb/14-differential-stress/TaUse14DifferentialStress.sleela
SLeeLa trading-action use case.
Definition: Trading long, differential stress: 1200 random verbs are run against SLLongBook and against an independent per-share shadow model, and every observable must agree after every step. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 14 (style and time). Four seeds of 300 random verbs each (the last is buy-heavy so the 16-lot capacity is reached), drawn from >
< a fixed linear congruential generator so the run is repeatable. The shadow >
< model stores one entry per share and sells by shifting the array, so it shares >
< no code or data structure with the lot-based book. After each step the codes, >
< shares, cost, profit, fees, day, lot count, weighted age and oldest age must >
< match, a refused verb must change nothing, and the book invariants must hold. >
< The expected tallies come from a third implementation in Python. >
class TaUse14DifferentialStress {
< ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
<< Eleven catalogued attributes with getter/setter pairs. A worker-class
lesson carries the baseline identity record. >>
String metaVersion = "1.0.0";
int metaId = 39314;
String metaAuthor = "Kiro Agent";
String metaDate = "2026-11-11";
String metaCopyright = "SLeeLa project";
String metaTrademark = "none";
String metaCorporation = "none";
< metaOrigin: National | <nation> | International | Galactic >
String metaOrigin = "International";
String metaBirthDate = "2026-11-11";
< metaScienceRating: maturity/robustness on a 1..10 scale >
int metaScienceRating = 5;
< metaNationalSchedule: a real, citable instrument or "none" >
String metaNationalSchedule = "none";
String getMetaVersion() { return metaVersion; }
void setMetaVersion(String v) { metaVersion = v; }
int getMetaId() { return metaId; }
void setMetaId(int v) { metaId = v; }
String getAuthor() { return metaAuthor; }
void setAuthor(String v) { metaAuthor = v; }
String getDate() { return metaDate; }
void setDate(String v) { metaDate = v; }
String getCopyright() { return metaCopyright; }
void setCopyright(String v) { metaCopyright = v; }
String getTrademark() { return metaTrademark; }
void setTrademark(String v) { metaTrademark = v; }
String getCorporation() { return metaCorporation; }
void setCorporation(String v) { metaCorporation = v; }
String getOrigin() { return metaOrigin; }
void setOrigin(String v) { metaOrigin = v; }
String getBirthDate() { return metaBirthDate; }
void setBirthDate(String v) { metaBirthDate = v; }
int getScienceRating() { return metaScienceRating; }
void setScienceRating(int v) { metaScienceRating = v; }
String getNationalSchedule() { return metaNationalSchedule; }
void setNationalSchedule(String v) { metaNationalSchedule = v; }
int passed = 0;
int failed = 0;
void expect(String label, int got, int want) {
if (got == want) {
print(" ok " + label + " = " + got);
passed = passed + 1;
} else {
print(" FAIL " + label + " got " + got + " want " + want);
failed = failed + 1;
}
}
void expectTrue(String label, boolean got) {
if (got) {
print(" ok " + label);
passed = passed + 1;
} else {
print(" FAIL " + label);
failed = failed + 1;
}
}
void summary() {
print(" checks passed=" + passed + " failed=" + failed);
}
void act(SLLongBook b, String text, int code) {
print(" " + text + " : " + b.reason(code));
}
int state = 1;
int[] sp = new int[6000];
int[] sd = new int[6000];
int[] sl = new int[6000];
int m = 0;
int shLast = 0;
int seq = 0;
int shGross = 0;
int shFees = 0;
int[] tally = new int[9];
int mismatches = 0;
SLLongBook book = new SLLongBook();
int nextRaw() {
state = (state * 1103515245 + 12345) % 2147483648;
return state;
}
int rnd(int n) { return (nextRaw() / 65536) % n; }
< Distinct buys that still hold shares, counted from the per-share ids. >
int shLots() {
int c = 0;
int prev = 0 - 1;
int i = 0;
while (i < m) {
if (sl[i] != prev) { c = c + 1; prev = sl[i]; }
i = i + 1;
}
return c;
}
int shBuy(int day, int qty, int price, int fee) {
if (qty <= 0) { return 1; }
if (price <= 0) { return 2; }
if (fee < 0) { return 6; }
if (day < shLast) { return 5; }
if (shLots() >= 16) { return 4; }
int k = 0;
while (k < qty) {
sp[m] = price;
sd[m] = day;
sl[m] = seq;
m = m + 1;
k = k + 1;
}
seq = seq + 1;
shLast = day;
shFees = shFees + fee;
return 0;
}
int shSell(int day, int qty, int price, int fee) {
if (qty <= 0) { return 1; }
if (price <= 0) { return 2; }
if (fee < 0) { return 6; }
if (day < shLast) { return 5; }
if (qty > m) { return 3; }
int cost = 0;
int k = 0;
while (k < qty) { cost = cost + sp[k]; k = k + 1; }
int i = 0;
while (i < m - qty) {
sp[i] = sp[i + qty];
sd[i] = sd[i + qty];
sl[i] = sl[i + qty];
i = i + 1;
}
m = m - qty;
shGross = shGross + qty * price - cost;
shFees = shFees + fee;
shLast = day;
return 0;
}
int shCost() {
int s = 0;
int i = 0;
while (i < m) { s = s + sp[i]; i = i + 1; }
return s;
}
int shAgeSum() {
int s = 0;
int i = 0;
while (i < m) { s = s + (shLast - sd[i]); i = i + 1; }
return s;
}
< After every step the real book and the shadow must agree on everything >
< observable, and a refused verb must have changed nothing. >
boolean agrees() {
if (book.totalQty() != m) { return false; }
if (book.costBasis() != shCost()) { return false; }
if (book.realizedGross() != shGross) { return false; }
if (book.fees() != shFees) { return false; }
if (book.currentDay() != shLast) { return false; }
if (book.lotCount() != shLots()) { return false; }
if (m > 0) {
if (book.weightedAgeFloor(shLast) != shAgeSum() / m) { return false; }
if (book.oldestAge(shLast) != shLast - sd[0]) { return false; }
}
return book.invariantsHold();
}
void runSeed(int seed, int steps, int bu, int su, int hu) {
book = new SLLongBook();
state = seed;
m = 0;
shLast = 0;
seq = 0;
shGross = 0;
shFees = 0;
mismatches = 0;
int c = 0;
while (c < 9) { tally[c] = 0; c = c + 1; }
int step = 0;
while (step < steps) {
int r = rnd(40);
int got = 0;
int want = 0;
int beforeQty = book.totalQty();
int beforeCost = book.costBasis();
int beforeFees = book.fees();
int beforeDay = book.currentDay();
if (r < bu) {
int day = shLast + rnd(6) - 1;
int q = rnd(35) - 2;
int p = rnd(100);
int f = rnd(8) - 1;
want = shBuy(day, q, p, f);
got = book.buy(day, q, p, f);
} else if (r < su) {
int day2 = shLast + rnd(6) - 1;
int q2 = rnd(70) - 3;
int p2 = rnd(100);
int f2 = rnd(8) - 1;
want = shSell(day2, q2, p2, f2);
got = book.sell(day2, q2, p2, f2);
} else if (r < hu) {
int day3 = shLast + rnd(8) - 1;
if (day3 < shLast) { want = 5; } else { shLast = day3; want = 0; }
got = book.hold(day3);
} else {
int day4 = shLast + rnd(5);
int p4 = rnd(100);
int f4 = rnd(8) - 1;
if (m == 0) { want = 7; } else { want = shSell(day4, m, p4, f4); }
got = book.flatten(day4, p4, f4);
}
if (got != want) { mismatches = mismatches + 1; }
if (got != 0) {
boolean untouched = (book.totalQty() == beforeQty && book.costBasis() == beforeCost && book.fees() == beforeFees && book.currentDay() == beforeDay);
if (!untouched) { mismatches = mismatches + 1; }
}
if (!agrees()) { mismatches = mismatches + 1; }
tally[got] = tally[got] + 1;
step = step + 1;
}
}
void main() {
print("== Use case 14: differential stress against a per-share shadow ==");
print("-- 300 random verbs per seed, 4 seeds; the shadow keeps one entry per share --");
runSeed(1, 300, 20, 32, 36);
expect("seed 1: steps ending in code 0", tally[0], 183);
expect("seed 1: steps ending in code 1", tally[1], 17);
expect("seed 1: steps ending in code 2", tally[2], 1);
expect("seed 1: steps ending in code 3", tally[3], 44);
expect("seed 1: steps ending in code 4", tally[4], 0);
expect("seed 1: steps ending in code 5", tally[5], 26);
expect("seed 1: steps ending in code 6", tally[6], 23);
expect("seed 1: steps ending in code 7", tally[7], 6);
expect("seed 1: steps ending in code 8", tally[8], 0);
expect("seed 1: shares held at the end", book.totalQty(), 16);
expect("seed 1: realized gross at the end", book.realizedGross(), 1058);
expect("seed 1: fees paid at the end", book.fees(), 476);
expect("seed 1: steps where book and shadow differ", mismatches, 0);
runSeed(7, 300, 20, 32, 36);
expect("seed 7: steps ending in code 0", tally[0], 166);
expect("seed 7: steps ending in code 1", tally[1], 29);
expect("seed 7: steps ending in code 2", tally[2], 3);
expect("seed 7: steps ending in code 3", tally[3], 28);
expect("seed 7: steps ending in code 4", tally[4], 0);
expect("seed 7: steps ending in code 5", tally[5], 38);
expect("seed 7: steps ending in code 6", tally[6], 28);
expect("seed 7: steps ending in code 7", tally[7], 8);
expect("seed 7: steps ending in code 8", tally[8], 0);
expect("seed 7: shares held at the end", book.totalQty(), 82);
expect("seed 7: realized gross at the end", book.realizedGross(), 124);
expect("seed 7: fees paid at the end", book.fees(), 374);
expect("seed 7: steps where book and shadow differ", mismatches, 0);
runSeed(12345, 300, 20, 32, 36);
expect("seed 12345: steps ending in code 0", tally[0], 163);
expect("seed 12345: steps ending in code 1", tally[1], 13);
expect("seed 12345: steps ending in code 2", tally[2], 3);
expect("seed 12345: steps ending in code 3", tally[3], 34);
expect("seed 12345: steps ending in code 4", tally[4], 0);
expect("seed 12345: steps ending in code 5", tally[5], 38);
expect("seed 12345: steps ending in code 6", tally[6], 41);
expect("seed 12345: steps ending in code 7", tally[7], 8);
expect("seed 12345: steps ending in code 8", tally[8], 0);
expect("seed 12345: shares held at the end", book.totalQty(), 43);
expect("seed 12345: realized gross at the end", book.realizedGross(), -201);
expect("seed 12345: fees paid at the end", book.fees(), 425);
expect("seed 12345: steps where book and shadow differ", mismatches, 0);
runSeed(31, 300, 32, 32, 38);
expect("seed 31: steps ending in code 0", tally[0], 145);
expect("seed 31: steps ending in code 1", tally[1], 23);
expect("seed 31: steps ending in code 2", tally[2], 3);
expect("seed 31: steps ending in code 3", tally[3], 0);
expect("seed 31: steps ending in code 4", tally[4], 73);
expect("seed 31: steps ending in code 5", tally[5], 38);
expect("seed 31: steps ending in code 6", tally[6], 18);
expect("seed 31: steps ending in code 7", tally[7], 0);
expect("seed 31: steps ending in code 8", tally[8], 0);
expect("seed 31: shares held at the end", book.totalQty(), 42);
expect("seed 31: realized gross at the end", book.realizedGross(), 6955);
expect("seed 31: fees paid at the end", book.fees(), 280);
expect("seed 31: steps where book and shadow differ", mismatches, 0);
summary();
}
}