TaUse01OpenAndAdd.sleela
lib/advantage/trading-actions-on-verb/01-open-and-add/TaUse01OpenAndAdd.sleela · 103 line(s) · sleela
<< lib/advantage/trading-actions-on-verb/01-open-and-add/TaUse01OpenAndAdd.sleela
SLeeLa trading-action use case.
Definition: Trading long, open then add: buy verbs build FIFO lots and an average cost with an exact remainder. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 1 (style). Open a long position, then add to it. The book keeps each >
< buy as its own lot. Average cost is rounded down and the leftover cents are >
< reported, so no cent is lost in the rounding. >
class TaUse01OpenAndAdd {
< ---- 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 = 39301;
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 book = new SLLongBook();
print("== Use case 1: open and add ==");
act(book, "buy 100 @ 5000 on day 0, fee 100", book.buy(0, 100, 5000, 100));
act(book, "buy 50 @ 5200 on day 30, fee 100", book.buy(30, 50, 5200, 100));
print("-- the book --");
expect("lots", book.lotCount(), 2);
expect("shares held", book.totalQty(), 150);
expect("cost basis cents", book.costBasis(), 760000);
expect("average cost floor", book.avgCostFloor(), 5066);
expect("average cost remainder", book.avgCostRemainder(), 100);
expect("fees paid", book.fees(), 200);
expectTrue("average * shares + remainder = cost", book.avgCostFloor() * book.totalQty() + book.avgCostRemainder() == book.costBasis());
expectTrue("invariants hold", book.invariantsHold());
summary();
}
}