Home /Modules/advantage/SLTradingResult.sleela

SLTradingResult.sleela

lib/advantage/trading/SLTradingResult.sleela · 132 line(s) · sleela

<< lib/advantage/trading/SLTradingResult.sleela
   SLeeLa Standard Library Definition.
   Definition: The common evaluation result of every trading model: total cost, expected
   proceeds, expected profit, return on cost, holding time, plus the advantage
   bridge (baseline and advantage net of the opportunity cost of capital). >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Result of one trading-model evaluation. Models that involve chance report >
< EXPECTED values, so a single real trade lands above or below them. >
< A rejected evaluation has ok false, a reason in error, and all numbers zero. >
< advantage = expected proceeds minus (total cost plus the opportunity cost of >
< the capital tied up for the holding time at the yearly cost of capital). >
class SLTradingResult {
  < ---- 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 = 39000;
  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; }


  < ---- result state ---- >
  boolean ok = false;
  String error = "not evaluated";
  double totalCost = 0.0;
  double expectedProceeds = 0.0;
  double expectedProfit = 0.0;
  double returnOnCost = 0.0;
  double holdDays = 0.0;

  < Record a successful evaluation. Profit and return on cost are derived. >
  void configure(double cost, double proceeds, double days) {
    ok = true;
    error = "";
    totalCost = cost;
    expectedProceeds = proceeds;
    expectedProfit = proceeds - cost;
    returnOnCost = 0.0;
    if (cost > 0.0) { returnOnCost = (proceeds - cost) / cost; }
    holdDays = days;
  }

  < Record a rejected evaluation. All numeric fields are reset to zero so a >
  < rejected result can never be mistaken for a real one. >
  void failWith(String why) {
    ok = false;
    error = why;
    totalCost = 0.0;
    expectedProceeds = 0.0;
    expectedProfit = 0.0;
    returnOnCost = 0.0;
    holdDays = 0.0;
  }

  boolean isOk() { return ok; }
  String getError() { return error; }
  double getTotalCost() { return totalCost; }
  double getExpectedProceeds() { return expectedProceeds; }
  double getExpectedProfit() { return expectedProfit; }
  double getReturnOnCost() { return returnOnCost; }
  double getHoldDays() { return holdDays; }

  < A cost of capital is a yearly fraction between 0 and 10. NaN fails both >
  < comparisons, so it is rejected too. >
  boolean rateOk(double costOfCapital) {
    return costOfCapital >= 0.0 && costOfCapital <= 10.0;
  }

  << Baseline for the advantage: the total cost plus the opportunity cost of
     tying that capital up for holdDays at the given yearly cost of capital.
     Returns -1.0 when the result is rejected or the rate is invalid. >>
  double baseline(double costOfCapital) {
    if (ok == false) { return -1.0; }
    if (rateOk(costOfCapital) == false) { return -1.0; }
    return totalCost * (1.0 + costOfCapital * (holdDays / 365.0));
  }

  << Advantage of the trade: expected proceeds beyond its costs AND beyond
     what the capital could have earned elsewhere. Returns 0.0 for a rejected
     result or invalid rate; use isOk() and rateOk() to tell that apart. >>
  double advantage(double costOfCapital) {
    if (ok == false) { return 0.0; }
    if (rateOk(costOfCapital) == false) { return 0.0; }
    return expectedProceeds - baseline(costOfCapital);
  }

  String describe() {
    if (ok == false) { return "REJECTED: " + error; }
    return "cost=" + totalCost + " proceeds=" + expectedProceeds
         + " profit=" + expectedProfit + " returnOnCost=" + returnOnCost
         + " holdDays=" + holdDays;
  }
}