Home /Modules/advantage/TaUse02TrimRealizedPnl.sleela

TaUse02TrimRealizedPnl.sleela

lib/advantage/trading-actions-on-verb/02-trim-realized-pnl/TaUse02TrimRealizedPnl.sleela · 107 line(s) · sleela

<< lib/advantage/trading-actions-on-verb/02-trim-realized-pnl/TaUse02TrimRealizedPnl.sleela
   SLeeLa trading-action use case.
   Definition: Trading long, trim: a partial sell consumes the oldest lot first and realizes profit on exactly that cost. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 2 (style). Trim a long position. The sell takes the oldest lot first >
< (FIFO), so the realized profit uses the true cost of those shares, not an >
< average. The leftover lots keep their own prices and days. >
class TaUse02TrimRealizedPnl {
  < ---- 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 = 39302;
  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 2: trim and realized profit ==");
    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));
    act(book, "sell 80 @ 5500 on day 60, fee 100", book.sell(60, 80, 5500, 100));
    print("-- the sale --");
    expect("cost of the 80 shares sold", book.lastSaleCost, 400000);
    expect("profit of that sale", book.lastSalePnl, 40000);
    print("-- what remains --");
    expect("shares held", book.totalQty(), 70);
    expect("lots", book.lotCount(), 2);
    expect("first lot shares", book.lotQtyAt(0), 20);
    expect("first lot price", book.lotPriceAt(0), 5000);
    expect("cost basis cents", book.costBasis(), 360000);
    expect("realized gross", book.realizedGross(), 40000);
    expect("realized net of all fees", book.realizedNet(), 39700);
    expectTrue("invariants hold", book.invariantsHold());
    summary();
  }
}