protected void reportStatusAndAction(Status status) throws Exception { // Setup a Strategy.Status structure Strategy.Status sstatus = new Strategy.Status(status.getSymbol()); persistStatus(sstatus); }
@Override protected void onNewDay(LocalDate prevDay, LocalDate newDay) throws Exception { if (!newDay.isAfter(getTradingStart().toLocalDate())) return; if (newDay.isAfter(getTradingStop().toLocalDate())) return; if (delay > 0) { --delay; return; } // Manage assets on first day of the month open if (prevDay.getMonth().equals(newDay.getMonth())) return; // Manage assets only on Monday's open // if(Calendar.toWeek(prevDay).equals(Calendar.toWeek(newDay))) return; // Sort the instruments Status[] longStatuses = statuses_.values().toArray(new Status[statuses_.size()]); Arrays.sort( longStatuses, (s1, s2) -> -Double.compare(s1.longScores.get(0), s2.longScores.get(0))); // Reset statuses_ .values() .forEach( (s) -> { s.desiredPosition = 0; }); String longLine = ""; String shortLine = ""; int end = Math.min(ntop, longStatuses.length); for (int ii = 0; ii < end; ++ii) { Status status = longStatuses[ii]; double roc = status.roc.last(); String str = String.format("%s: %.2f", status.getSymbol(), roc); if (longLine.isEmpty()) longLine = str; else longLine += "; " + str; if (roc > 0) status.desiredPosition = 1; } if (!longLine.isEmpty()) { longLine = "Long: " + longLine + "; "; } // Cancel all active orders. broker.cancelAllOrders(); // Save the end equity double endEquity = getAccount().getEndEquity(); double positionSize = endEquity / end; for (Status ss : longStatuses) { long pos = 0; if (ss.position > 0) pos = 1; else if (ss.position < 0) pos = -1; if (pos != ss.desiredPosition) { if (pos == 0) { if (ss.desiredPosition > 0) { long qty = (long) (positionSize / ss.lastClose); enterLong(ss.getSymbol(), qty); } else { long qty = (long) (positionSize / ss.lastClose); enterShort(ss.getSymbol(), qty); } } else if (pos > 0) { exitLong(ss.getSymbol()); if (ss.desiredPosition < 0) { long qty = (long) (positionSize / ss.lastClose); enterShort(ss.getSymbol(), qty); } } else { exitShort(ss.getSymbol()); if (ss.desiredPosition > 0) { long qty = (long) (positionSize / ss.lastClose); enterLong(ss.getSymbol(), qty); } } } } if (!shortLine.isEmpty()) { shortLine = "Short: " + shortLine + "; "; } if (!longLine.isEmpty() || !shortLine.isEmpty()) { System.out.println( String.format("%1$tY-%1$tm-%1$td [%1$ta]: %2$s%3$s", newDay, longLine, shortLine)); } }