Home /Modules/advantage/TaUse25MarginDifferential.sleela

TaUse25MarginDifferential.sleela

lib/advantage/trading-actions-on-verb/25-margin-differential/TaUse25MarginDifferential.sleela · 196 line(s) · sleela

<< lib/advantage/trading-actions-on-verb/25-margin-differential/TaUse25MarginDifferential.sleela
   SLeeLa trading-action use case.
   Definition: Margin by differential testing: the binary search for the fewest shares to cover and the closed form for the first call day are each checked against a slow reference over every combination of a grid. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 25 (style and time, short margin, illustrative). The fast routines are >
< compared with scans that are too simple to be wrong. For sharesToCover the >
< answer must also be minimal (one fewer share is not enough) and a result of -1 >
< must mean negative equity. Counts of each outcome come from a separate Python >
< model, so a loop that stopped early would show as a wrong count. >
class TaUse25MarginDifferential {
  < ---- 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 = 39325;
  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() {
    SLShortMargin m = new SLShortMargin();
    print("== Use case 25: binary search and closed form against slow references ==");
    int[] shares = new int[6];
    shares[0] = 0; shares[1] = 1; shares[2] = 2; shares[3] = 5; shares[4] = 13; shares[5] = 30;
    int[] prices = new int[4];
    prices[0] = 1; prices[1] = 7; prices[2] = 13; prices[3] = 100;
    int[] bps = new int[6];
    bps[0] = 0; bps[1] = 1; bps[2] = 3000; bps[3] = 5000; bps[4] = 10000; bps[5] = 15000;
    int[] cs = new int[8];
    int combos = 0;
    int bad = 0;
    int none = 0;
    int zero = 0;
    int some = 0;
    int a = 0;
    while (a < 6) {
      int b = 0;
      while (b < 4) {
        int n = shares[a];
        int p = prices[b];
        int v = n * p;
        int lessOne = v - 1;
        if (lessOne < 0) { lessOne = 0; }
        cs[0] = 0; cs[1] = 1; cs[2] = lessOne; cs[3] = v; cs[4] = v + 1; cs[5] = v * 3 / 2; cs[6] = v * 2; cs[7] = v * 3;
        int c = 0;
        while (c < 6) {
          int d = 0;
          while (d < 8) {
            int fast = m.sharesToCover(cs[d], n, p, bps[c]);
            int slow = m.sharesToCoverScan(cs[d], n, p, bps[c]);
            boolean ok = (fast == slow);
            if (ok && fast > 0) {
              ok = m.enoughAfterCover(cs[d], n, p, bps[c], fast) && !m.enoughAfterCover(cs[d], n, p, bps[c], fast - 1);
            }
            if (ok && fast == 0) { ok = m.enoughAfterCover(cs[d], n, p, bps[c], 0); }
            if (ok && fast < 0) { ok = (m.equity(cs[d], n, p) < 0); }
            if (!ok) { bad = bad + 1; }
            if (fast < 0) { none = none + 1; } else if (fast == 0) { zero = zero + 1; } else { some = some + 1; }
            combos = combos + 1;
            d = d + 1;
          }
          c = c + 1;
        }
        b = b + 1;
      }
      a = a + 1;
    }
    expect("sharesToCover combinations tested", combos, 1152);
    expect("combinations where the methods or the minimality test fail", bad, 0);
    expect("combinations where no cover can help", none, 354);
    expect("combinations where no cover is needed", zero, 547);
    expect("combinations where some shares must be covered", some, 251);
    print("-- daysToCall: closed form against stepping one day at a time --");
    int[] sh2 = new int[3];
    sh2[0] = 1; sh2[1] = 7; sh2[2] = 100;
    int[] pr2 = new int[2];
    pr2[0] = 1; pr2[1] = 50;
    int[] bp2 = new int[3];
    bp2[0] = 0; bp2[1] = 3000; bp2[2] = 10000;
    int[] rt = new int[3];
    rt[0] = 0; rt[1] = 3; rt[2] = 1000;
    int[] cs2 = new int[4];
    int combos2 = 0;
    int bad2 = 0;
    int never = 0;
    int already = 0;
    int later = 0;
    int daySum = 0;
    int e = 0;
    while (e < 3) {
      int f = 0;
      while (f < 2) {
        int g = 0;
        while (g < 3) {
          int h = 0;
          while (h < 3) {
            int n2 = sh2[e];
            int p2 = pr2[f];
            int v2 = n2 * p2;
            cs2[0] = v2; cs2[1] = v2 + 17; cs2[2] = 2 * v2; cs2[3] = 3 * v2 + 17;
            int k = 0;
            while (k < 4) {
              int fast2 = m.daysToCall(cs2[k], n2, p2, bp2[g], rt[h]);
              int slow2 = m.daysToCallScan(cs2[k], n2, p2, bp2[g], rt[h]);
              if (fast2 != slow2) { bad2 = bad2 + 1; }
              if (fast2 == 0 - 2) { never = never + 1; } else if (fast2 == 0) { already = already + 1; } else { later = later + 1; daySum = daySum + fast2; }
              combos2 = combos2 + 1;
              k = k + 1;
            }
            h = h + 1;
          }
          g = g + 1;
        }
        f = f + 1;
      }
      e = e + 1;
    }
    expect("daysToCall combinations tested", combos2, 216);
    expect("combinations where closed form and stepping differ", bad2, 0);
    expect("combinations where nothing drains (never)", never, 53);
    expect("combinations already due on day 0", already, 57);
    expect("combinations due on a later day", later, 106);
    expect("sum of those later days", daySum, 480);
    summary();
  }
}