TaUse10WeightedAverageAge.sleela
lib/advantage/trading-actions-on-verb/10-weighted-average-age/TaUse10WeightedAverageAge.sleela · 109 line(s) · sleela
<< lib/advantage/trading-actions-on-verb/10-weighted-average-age/TaUse10WeightedAverageAge.sleela
SLeeLa trading-action use case.
Definition: Time on long, weighted age: the quantity-weighted average holding period is exact when divisible and a floor otherwise. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 10 (time). Average age is sum(shares * age) / shares, rounded down. >
< Case one divides exactly (110 days). Case two does not: (3*10 + 4*5) / 7 = >
< 50 / 7 = 7.14, so the floor is 7. A long old lot pulls the average up in >
< proportion to its size, not its count. >
class TaUse10WeightedAverageAge {
< ---- 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 = 39310;
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));
}
void main() {
SLLongBook one = new SLLongBook();
print("== Use case 10: weighted average age ==");
act(one, "buy 100 on day 0", one.buy(0, 100, 5000, 0));
act(one, "buy 50 on day 60", one.buy(60, 50, 5000, 0));
act(one, "buy 150 on day 120", one.buy(120, 150, 5000, 0));
expect("weighted age on day 180", one.weightedAgeFloor(180), 110);
expect("oldest age on day 180", one.oldestAge(180), 180);
SLLongBook two = new SLLongBook();
act(two, "buy 3 on day 0", two.buy(0, 3, 5000, 0));
act(two, "buy 4 on day 5", two.buy(5, 4, 5000, 0));
expect("weighted age on day 10 (floor of 50/7)", two.weightedAgeFloor(10), 7);
expectTrue("floor property: 7*7 <= 50 and 50 < 8*7", 7 * 7 <= 50 && 50 < 8 * 7);
print("-- size, not count --");
SLLongBook three = new SLLongBook();
act(three, "buy 1 on day 0", three.buy(0, 1, 5000, 0));
act(three, "buy 99 on day 99", three.buy(99, 99, 5000, 0));
expect("weighted age on day 100", three.weightedAgeFloor(100), 1);
expectTrue("invariants hold", one.invariantsHold() && two.invariantsHold() && three.invariantsHold());
summary();
}
}