public Position perform(LocalDate minDate) throws Exception { OhlcvFetcher fetcher = new OhlcvFetcher(); fetcher.connect(); Ohlcv ohlcv = fetcher.fetchLatest(this.filled); fetcher.disconnect(); if (minDate.compareTo(ohlcv.getDate()) > 0) { return null; } if (this.fill(ohlcv)) { return new Position( tickValue, unit, target, type, filled, fillPrice, initialStop, stop, profit, premium); } else return null; }
protected boolean fill(Ohlcv ohlcv) { double open = ohlcv.getOpen(); double high = ohlcv.getHigh(); double low = ohlcv.getLow(); boolean traded = false; if (this.orderType == TradeConstants.BUY) { if (target == OhlcvConstants.LowestLow || open >= target) { this.fillPrice = open; this.target = open; traded = true; } else if (high > target) { this.fillPrice = target; traded = true; } if (traded) { this.filled = ohlcv.getDate(); this.stop = this.fillPrice - this.stop; this.initialStop = this.stop; } } else if (this.orderType == TradeConstants.SELL) { if (target == OhlcvConstants.HighestHigh || open <= target) { this.fillPrice = open; this.target = open; traded = true; } else if (low < target) { this.fillPrice = target; traded = true; } if (traded) { this.filled = ohlcv.getDate(); this.stop = this.fillPrice + this.stop; this.initialStop = this.stop; } } return traded; }