P openPosition(P p, int direction) {
var price = digitizedPrice();
var realPrice = currentPrice();
if (isNaN(price)) price = digitizer.digitizeIndividually(currentPrice());
p.openingPrice(realPrice);
p.direction(direction);
p.digitizedOpeningPrice(price);
double cryptoAmount = marginPerPosition/realPrice*leverage;
cryptoAmount = roundTo(cryptoStep, cryptoAmount);
log(renderVars("openPosition", +marginPerPosition, +realPrice, +leverage, +cryptoAmount, +cryptoStep));
p.cryptoAmount = max(minCrypto, cryptoAmount);
p.open();
//print("Opening " + p);
//printPositions();
p.openOnMarket();
ret p;
}
LS status() {
double mulProf = multiplicativeProfit();
ret llNonNulls(
"Profit: " + marginCoin + " " + plusMinusFix(formatMarginPrice(coinProfit())),
//baseToString(),
!primed() ? null : "Primed",
!started() ? null : "Started. current price: " + formatPriceX(currentPrice) + ", digitized: " + formatPriceX(digitizedPrice()),
"Leverage: " + leverage + ", margin per position: " + marginCoin + " " + formatPrice(marginPerPosition),
"Cell size: " + formatPercentage(cellSize, 3),
"Step " + n2(stepCount) + (stepSince == 0 ? "" : " since " + formatHoursMinutesColonSeconds(currentTime()-stepSince)),
"Realized profit: " + marginCoin + " " + formatMarginProfit(realizedCoinProfit) + " from " + n2(closedPositions, "closed position")
+ " (" + formatMarginProfit(realizedCoinWins) + " from " + n2(realizedWins, "win")
+ ", " + formatMarginProfit(realizedCoinLosses) + " from " + n2(realizedLosses, "loss", "losses") + ")",
"Unrealized profit: " + marginCoin + " " + formatMarginProfit(unrealizedCoinProfit()) + " in " + n2(openPositions, "open position"),
isNaN(mulProf) ? null : "Multiplicative profit: " + formatProfit(mulProf) + "%",
//"Debt: " + marginCoin + " " + formatMarginProfit(debt()) + " (max seen: " + formatMarginProfit(maxDebt) + ")",
"Investment used: " + marginCoin + " " + formatMarginPrice(maxInvestment()),
);
}
LS fullStatus() {
ret listCombine(
status(),
"",
n2(openPositions, "open position") + ":",
reversed(openPositions),
"",
n2(closedPositions, "closed position") + ":",
showClosedPositionsBackwards ? reversed(closedPositions) : closedPositions,
);
}
void feed(TickerSequence ts) {
if (ts == null) ret;
for (pricePoint : ts.pricePoints()) {
currentTime = -> pricePoint.timestamp;
price(pricePoint.price);
}
}
public int compareTo(G22TradingStrategy s) {
ret s == null ? 1 : cmp(coinProfit(), s.coinProfit());
}
void closeAllPositions {
closePositions(openPositions(), "User close");
}
void reset {
resetFields(this, [[
globalID
archived primed usingLiveData doingRealTrades
log currentTime currentPrice
direction lastDirection
digitizer maxDebt direction oldPrice startingPrice
maxInvestment
realizedProfit realizedCoinProfit
realizedWins realizedCoinWins
realizedLosses realizedCoinLosses
stepCount stepSince
openPositions closedPositions
]]);
change();
}
selfType emptyClone() {
var clone = shallowCloneToUnlistedConcept(this);
clone.reset();
ret clone;
}
L