Home /Modules/advantage/TaUse26MarginAndTime.sleela

TaUse26MarginAndTime.sleela

lib/advantage/trading-actions-on-verb/26-margin-and-time/TaUse26MarginAndTime.sleela · 124 line(s) · sleela

<< lib/advantage/trading-actions-on-verb/26-margin-and-time/TaUse26MarginAndTime.sleela
   SLeeLa trading-action use case.
   Definition: Time on short with margin: borrow cost drains collateral by shares times rate each day, so the first call day has a closed form, confirmed by stepping and by the book's own accrual. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 26 (time, short margin, illustrative). 100 shares short at 5000 with >
< collateral 750000 and a 30 percent requirement leave an excess of 100000. Borrow >
< cost of 3 cents per share per day drains 300 a day, so the excess first goes >
< negative on day floor(100000 / 300) + 1 = 334. The book accrual confirms day 333 >
< is still fine and day 334 is not. >
class TaUse26MarginAndTime {
  < ---- 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 = 39326;
  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 act2(SLShortBook b, String text, int code) {
    print("  " + text + " : " + b.reason(code));
  }

  void main() {
    SLShortBook book = new SLShortBook();
    SLShortMargin m = new SLShortMargin();
    print("== Use case 26: borrow cost wears the margin down ==");
    expect("set the borrow rate", book.setBorrowRate(3), 0);
    act2(book, "short 100 @ 5000 on day 0", book.shortSell(0, 100, 5000, 0));
    int collateral = book.proceedsHeld() + 250000;
    expect("collateral", collateral, 750000);
    expect("excess on day 0 (price stays 5000)", m.excess(collateral, 100, 5000, 3000), 100000);
    expect("borrow burn per day (100 shares * 3)", 100 * 3, 300);
    int first = m.daysToCall(collateral, 100, 5000, 3000, 3);
    expect("first day with a call (closed form)", first, 334);
    expect("first day with a call (stepping)", m.daysToCallScan(collateral, 100, 5000, 3000, 3), 334);
    print("-- check against the book itself --");
    act2(book, "hold to day 333", book.hold(333));
    expect("borrow accrued on day 333", book.borrowAccrued(333), 99900);
    expect("excess on day 333", m.excess(collateral, 100, 5000, 3000) - book.borrowAccrued(333), 100);
    expectTrue("no call yet on day 333", m.excess(collateral, 100, 5000, 3000) - book.borrowAccrued(333) >= 0);
    act2(book, "hold to day 334", book.hold(334));
    expect("borrow accrued on day 334", book.borrowAccrued(334), 100200);
    expect("excess on day 334", m.excess(collateral, 100, 5000, 3000) - book.borrowAccrued(334), -200);
    expectTrue("a call is due on day 334", m.excess(collateral, 100, 5000, 3000) - book.borrowAccrued(334) < 0);
    print("-- edge cases --");
    expect("nothing drains at rate 0", m.daysToCall(collateral, 100, 5000, 3000, 0), 0 - 2);
    expect("already due today", m.daysToCall(750000, 100, 7000, 3000, 3), 0);
    expect("rate above the bound is refused", m.daysToCall(collateral, 100, 5000, 3000, 1001), 0 - 1);
    expect("a horizon beyond the day bound", m.daysToCall(4000000000000000, 1, 1, 0, 1), 0 - 3);
    expectTrue("invariants hold", book.invariantsHold());
    summary();
  }
}