TaUse17CoverRealizedPnl.sleela
lib/advantage/trading-actions-on-verb/17-cover-realized-pnl/TaUse17CoverRealizedPnl.sleela · 113 line(s) · sleela
<< lib/advantage/trading-actions-on-verb/17-cover-realized-pnl/TaUse17CoverRealizedPnl.sleela
SLeeLa trading-action use case.
Definition: Trading short, cover: a partial cover takes the oldest lot first, profit is sale price minus cover price, and borrow cost is charged for the days short. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Use case 17 (style, short). Cover part of a short position. The oldest lot is >
< covered first (FIFO), so profit uses the true sale price of those shares. Borrow >
< cost is 2 cents per share per day for the days each covered share was short. >
class TaUse17CoverRealizedPnl {
< ---- 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 = 39317;
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();
print("== Use case 17: cover and realized profit ==");
expect("set the borrow rate while flat", book.setBorrowRate(2), 0);
act2(book, "short 100 @ 5000 on day 0, fee 100", book.shortSell(0, 100, 5000, 100));
act2(book, "short 50 @ 4800 on day 30, fee 100", book.shortSell(30, 50, 4800, 100));
act2(book, "cover 80 @ 4500 on day 60, fee 100", book.cover(60, 80, 4500, 100));
print("-- the cover --");
expect("sale proceeds of the 80 shares covered", book.lastCoverProceeds, 400000);
expect("profit of that cover", book.lastCoverPnl, 40000);
expect("borrow cost paid (80 shares * 60 days * 2)", book.borrowCostPaid(), 9600);
print("-- what remains --");
expect("shares short", book.totalShort(), 70);
expect("lots", book.lotCount(), 2);
expect("first lot shares", book.lotQtyAt(0), 20);
expect("first lot price", book.lotPriceAt(0), 5000);
expect("sale proceeds held", book.proceedsHeld(), 340000);
expect("realized gross", book.realizedGross(), 40000);
expect("realized net of fees and borrow", book.realizedNet(), 30100);
expectTrue("invariants hold", book.invariantsHold());
summary();
}
}