sclass Corridor { record noeq Position(settable double openingPrice, settable double direction) { settable double closingPrice = Double.NaN; void close { openPositions.remove(this); closingPrice = currentPrice(); closedPositions.add(this); } } settable double ladderStep = 1; gettable double currentPrice = 0; new L loops; new LinkedHashSet openPositions; new L closedPositions; record noeq Loop(settable double startingPrice, settable double direction) { Position position; Loop successor; selfType init { loops.add(this); addThreshold(startingPrice+ladderStep, direction, -> { position = openPosition(direction); if (successor == null) successor = new Loop(x+ladderStep, direction).init(); }); addThreshold(startingPrice-ladderStep, -direction, -> { position.close(); }); } } Position openPosition(double direction) { ret addAndReturn(openPositions, new Position(currentPrice(), direction)); } void start { double price = currentPrice(); new Loop(price-ladderStep, 1).init(); new Loop(price+ladderStep, -1).init(); } }