sclass TradingPosition { settable S coin; settable bool isLong; settable int leverage; // How much we originally invest in USDT settable double marginInvestment; // How much USDT margin we end up with settable double margin; // How much crypto we bet on settable double amount; // Is position still open? settable bool ongoing; settable long openingTime; settable long closingTime; settable double openingPrice; // closing or current price settable double closingPrice; settable double openingFees; settable double closingFees; settable double profit = Double.NaN; // Actual price movement during position settable TradingCandle candle; // Signal we acted on (optional) settable TradingSignal afterSignal; double openingFeePercentage() { ret doubleRatio(openingFees, margin*leverage); } double priceDifference() { ret closingPrice-openingPrice; } double leveragedPriceDifference() { ret priceDifference()*leverage; } // PNL = Profit & Loss double expectedPNL aka relativeValue() { ret !isNaN(profit) ? profit : (isLong() ? 1 : -1)*leveragedPriceDifference()*amount; } bool isWinner() { ret relativeValue() >= 0; } bool isOpen() { ret ongoing; } int direction() { ret isLong() ? 1 : -1; } static TradingPosition fromStrategy(G22TradingStrategy.Position p) { ret new TradingPosition() .profit(p.profit()) .openingTime(p.openingTime()) .closingTime(p.closingTime()) .openingPrice(p.openingPrice()) .closingPrice(p.closingPrice()) .isLong(p.direction > 0) .leverage(iround(p.leverage)); } }