Home /Modules/advantage/SLTradingCheck.sleela

SLTradingCheck.sleela

lib/advantage/trading/SLTradingCheck.sleela · 135 line(s) · sleela

<< lib/advantage/trading/SLTradingCheck.sleela
   SLeeLa Standard Library Definition.
   Definition: Shared fail-closed validation helpers and finite bounds for the trading
   models (amount, positive, rate, probability, days, distribution). >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Validation shared by the trading models. Limits match the C core: amounts >
< up to 1e12, holding up to 36500 days, 16 outcome branches, 256 lots, 64 >
< components. NaN and infinity are always rejected. >
class SLTradingCheck {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 39001;
  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 = 7;
  < 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; }


  << Shared, fail-closed validation for the trading models. Every check is
     written so that NaN and infinity are rejected: NaN fails every ordered
     comparison, and infinity fails the finite test (v minus v is NaN). >>

  < ---- finite bounds (same limits as the C core) ---- >
  double maxAmount() { return 1.0e12; }
  double maxHoldDays() { return 36500.0; }
  double probTolerance() { return 0.000001; }
  int maxOutcomes() { return 16; }
  int maxLots() { return 256; }
  int maxComponents() { return 64; }

  boolean isFinite(double v) { return (v - v) == 0.0; }

  < finite and 0 up to the amount cap >
  boolean isAmount(double v) {
    return isFinite(v) && v >= 0.0 && v <= maxAmount();
  }

  < finite and strictly above 0, up to the amount cap >
  boolean isPositive(double v) {
    return isFinite(v) && v > 0.0 && v <= maxAmount();
  }

  < finite, 0 up to but not including 1 >
  boolean isRate(double v) {
    return isFinite(v) && v >= 0.0 && v < 1.0;
  }

  < finite, 0 up to and including 1 >
  boolean isProb(double v) {
    return isFinite(v) && v >= 0.0 && v <= 1.0;
  }

  < finite, 0 up to the maximum holding time >
  boolean isDays(double v) {
    return isFinite(v) && v >= 0.0 && v <= maxHoldDays();
  }

  << True when the first n entries are each a probability and together sum
     to 1 within the tolerance. >>
  boolean isDistribution(double[] p, int n) {
    double sum = 0.0;
    int i = 0;
    while (i < n) {
      if (isProb(p[i]) == false) { return false; }
      sum = sum + p[i];
      i = i + 1;
    }
    double d = sum - 1.0;
    if (d < 0.0) { d = 0.0 - d; }
    return d <= probTolerance();
  }

  SLTradingResult fail(String why) {
    SLTradingResult r = new SLTradingResult();
    r.failWith(why);
    return r;
  }

  < Standard messages, shared so every model words its rejections the same. >
  String rangeMessage() { return "value non-finite or outside its valid range"; }
  String distributionMessage() { return "outcome probabilities are not a distribution"; }
  String countMessage() { return "item count out of range"; }

  << Build the common result, re-checking that nothing overflowed. On any
     problem the result is rejected, never partially filled. >>
  SLTradingResult finish(double totalCost, double proceeds, double holdDays) {
    if (isFinite(totalCost) == false) { return fail("result was not finite"); }
    if (isFinite(proceeds) == false) { return fail("result was not finite"); }
    if (totalCost < 0.0 || proceeds < 0.0) { return fail("result was not finite"); }
    SLTradingResult r = new SLTradingResult();
    r.configure(totalCost, proceeds, holdDays);
    if (isFinite(r.getExpectedProfit()) == false) { return fail("result was not finite"); }
    if (isFinite(r.getReturnOnCost()) == false) { return fail("result was not finite"); }
    return r;
  }
}