Home /Modules/advantage/TaUse13FullLifecycle.sleela

TaUse13FullLifecycle.sleela

lib/advantage/trading-actions-on-verb/13-full-lifecycle/TaUse13FullLifecycle.sleela · 134 line(s) · sleela

<< lib/advantage/trading-actions-on-verb/13-full-lifecycle/TaUse13FullLifecycle.sleela
   SLeeLa trading-action use case.
   Definition: Trading long over time: buys, a hold, a trim, a long-term sale and a flatten, with invariants after every action and an independent reconciliation. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 13 (style and time). A full life of a long position over 500 days. >
< After every action the book invariants are asserted. At the end, an >
< independent tally kept by this class (proceeds, cost, fees) must equal what >
< the book reports. At day 400 the shares held more than 365 days are sold. >
class TaUse13FullLifecycle {
  < ---- 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 = 39313;
  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 13: full lifecycle ==");
    int tallyProceeds = 0;
    int tallyFees = 0;
    int tallyBoughtCost = 0;
    act(book, "buy 100 @ 4000 on day 0, fee 50", book.buy(0, 100, 4000, 50));
    tallyBoughtCost = tallyBoughtCost + 100 * 4000;
    tallyFees = tallyFees + 50;
    expectTrue("invariants after buy 1", book.invariantsHold());
    act(book, "buy 100 @ 4400 on day 100, fee 50", book.buy(100, 100, 4400, 50));
    tallyBoughtCost = tallyBoughtCost + 100 * 4400;
    tallyFees = tallyFees + 50;
    expectTrue("invariants after buy 2", book.invariantsHold());
    act(book, "hold to day 200", book.hold(200));
    expect("weighted age on day 200", book.weightedAgeFloor(200), 150);
    act(book, "sell 50 @ 4800 on day 250, fee 50", book.sell(250, 50, 4800, 50));
    tallyProceeds = tallyProceeds + 50 * 4800;
    tallyFees = tallyFees + 50;
    expect("profit of the trim (50 @ 4000 cost)", book.lastSalePnl, 40000);
    expectTrue("invariants after the trim", book.invariantsHold());
    act(book, "buy 40 @ 4300 on day 300, fee 50", book.buy(300, 40, 4300, 50));
    tallyBoughtCost = tallyBoughtCost + 40 * 4300;
    tallyFees = tallyFees + 50;
    print("-- day 400: sell what is long term (held more than 365 days) --");
    int old = book.qtyLongTerm(400, 365);
    expect("long-term shares on day 400", old, 50);
    act(book, "sell the long-term shares @ 4900 on day 400, fee 50", book.sell(400, old, 4900, 50));
    tallyProceeds = tallyProceeds + old * 4900;
    tallyFees = tallyFees + 50;
    expect("profit of that sale", book.lastSalePnl, 45000);
    expectTrue("invariants after the long-term sale", book.invariantsHold());
    expect("shares held on day 400", book.totalQty(), 140);
    int rest = book.totalQty();
    act(book, "flatten @ 4700 on day 500, fee 50", book.flatten(500, 4700, 50));
    tallyProceeds = tallyProceeds + rest * 4700;
    tallyFees = tallyFees + 50;
    expectTrue("invariants after the flatten", book.invariantsHold());
    print("-- reconcile against the independent tally --");
    expect("shares held", book.totalQty(), 0);
    expect("fees paid", book.fees(), tallyFees);
    expect("realized net", book.realizedNet(), tallyProceeds - tallyBoughtCost - tallyFees);
    expect("realized net (reference value)", book.realizedNet(), 130700);
    expect("realized gross (reference value)", book.realizedGross(), 131000);
    summary();
  }
}