@Override public boolean isExpired(final TicketState ticketState) { if (ticketState == null) { return true; } final ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); final ZonedDateTime expirationTime = ticketState.getLastTimeUsed().plus(this.timeToKillInSeconds, ChronoUnit.SECONDS); return now.isAfter(expirationTime); }
@Override public List<Set<ComparableTenor>> visitAnnuityDefinition( AnnuityDefinition<? extends PaymentDefinition> annuity, ZonedDateTime date) { final int n = annuity.getNumberOfPayments(); final List<Set<ComparableTenor>> tenors = new ArrayList<>(); for (int i = 0; i < n; i++) { final PaymentDefinition payment = annuity.getNthPayment(i); if (!date.isAfter(payment.getPaymentDate())) { tenors.add(payment.accept(COUPON_VISITOR)); } } return tenors; }
/** * Method runStrategy. Note the current candle is just forming Enter a tier 1-3 gap in first 5min * bar direction, with a 3R target and stop @ 5min high/low. * * <p>TradeOrders create TradePositions that are associated to Contracts. TradeOrders are * associated to TradePositions and the Tradestrategy that created them. A TradePosition may have * TradeOrders from multiple Tradestrategies. * * <p>TradePositions are created when there is no open TradePosition and a TradeOrder is either * filled or partially filled. * * <p>Note TradePositions are closed when the open quantity is zero. The new TradePosition is * associated to the Contract with the 1 to 1 relationship from Contract to TradePosition. The * TradeOrder that opened the TradePosition is marked as the open order @see * org.trade.persistent.dao.TradeOrder.getIsOpenPosition() * * <p>TradePosition will have the Side set to either BOT/SLD i.e. Long/Short. If an open position * changes from Long to Short dues to an over Sell/Buy order the side will switch. * * @param candleSeries CandleSeries the series of candles that has been updated. * @param newBar boolean has a new bar just started. * @see org.trade.strategy.AbstractStrategyRule#runStrategy(CandleSeries, boolean) */ @SuppressWarnings("unused") public void runStrategy(CandleSeries candleSeries, boolean newBar) { _log.info( "Inside BreakEvenStrategy.runStrategy::" + this.getSymbol() + "_at_" + this.getCurrentCandle().getPeriod().getStart()); try { // Get the current candle CandleItem currentCandleItem = this.getCurrentCandle(); ZonedDateTime startPeriod = currentCandleItem.getPeriod().getStart(); // QuantityShares = this.getTradestrategy().getCodeValues().get(0).getCodeValue(); // QuantityShares = // this.getTradestrategy().getCodeValues().get(0).getCodeAttribute().getDefaultValue(); QuantityShares = ((Long) this.getTradestrategy().getValueCode("stockSharesQuantity")).intValue(); /* * Position is open kill this Strategy as its job is done. In this * example we would manage the position with a strategy manager. * This strategy is just used to create the order that would open * the position. */ if (this.isThereOpenPosition()) { _log.info( "Strategy complete open position filled symbol: " + getSymbol() + " startPeriod: " + startPeriod); /* * If the order is partial filled check if the risk goes beyond * 1 risk unit. If it does cancel the openPositionOrder this * will cause it to be marked as filled. */ if (OrderStatus.PARTIALFILLED.equals(this.getOpenPositionOrder().getStatus())) { if (isRiskViolated( currentCandleItem.getClose(), this.getTradestrategy().getRiskAmount(), this.getOpenPositionOrder().getQuantity(), this.getOpenPositionOrder().getAverageFilledPrice())) { this.cancelOrder(this.getOpenPositionOrder()); } } // this.cancel(); return; } else /* * Open position order was cancelled kill this Strategy as its job * is done. */ if (null != openPositionOrderKey && !this.getTradeOrder(openPositionOrderKey).isActive()) { _log.info( "Strategy complete open position cancelled symbol: " + getSymbol() + " startPeriod: " + startPeriod); updateTradestrategyStatus(TradestrategyStatus.CANCELLED); // this.cancel(); return; } else { /* * Create code here to create orders based on your conditions/rules. * * * Validamos que la estrategia solo se ejecute dentro del periodo de 9:30am a 15:00pm */ /* if (startPeriod.isAfter(this.getTradestrategy().getTradingday().getOpen().minusSeconds(1)) && startPeriod.isBefore(this.getTradestrategy().getTradingday().getClose().plusSeconds(1)) ) { // && newBar */ /* * Example On start of the second (9:35) candle check the 9:30 * candle and buy over under in the direction of the bar. * * * Validamos que no hayan transcurrido 25min (despues de la apertura) y que la estrategia * no se haya cumplido **/ if (startPeriod.isBefore(this.getTradestrategy().getTradingday().getOpen().plusMinutes(25)) && !StrategyOK) { Candle currentCandle = currentCandleItem.getCandle(); Candle prevDayCandle = getPreviousDayCandleFromDb( candleSeries, startPeriod); // Obtenemos el punto P, es decir el punto de apertura del día // anterior DecimalFormat df = new DecimalFormat("#.00"); BigDecimal currentClose = new BigDecimal(currentCandle.getClose().doubleValue()) .setScale(2, RoundingMode.HALF_EVEN); BigDecimal preDayClose = new BigDecimal(prevDayCandle.getClose().doubleValue()) .setScale(2, RoundingMode.HALF_EVEN); if (currentClose.doubleValue() == preDayClose .doubleValue()) { // Validamos que el valor actual de LOW sea igual al de P StrategyOK = Boolean.TRUE; if (!this.isThereOpenPosition()) { // Siempre que no haya una orden abierta ... // Money auxStopPrice = new Money(prevDayCandle.getLow()); Money auxStopPrice = new Money(prevDayCandle.getClose()).subtract(new Money(0.04)); Money limitPrice = new Money(prevDayCandle.getClose()); LastAuxStopPrice = auxStopPrice.doubleValue(); TradeOrder tradeOrder = this.createOrder( this.getTradestrategy().getContract(), Action.BUY, OrderType.STPLMT, limitPrice, auxStopPrice, QuantityShares, false, true); // Creamos y transmitimos una orden BUY, STPLMT = LOW - 4c } _log.info( "StrategyOK::" + StrategyOK + ", LastAuxStopPrice::" + LastAuxStopPrice + ", Symbol::" + this.getSymbol()); /* TradeOrder tradeOrder = new TradeOrder(this.getTradestrategy(), Action.BUY, OrderType.STPLMT, 100, price, price.add(new BigDecimal(0.02)), TradingCalendar.getDateTimeNowMarketTimeZone()); //tradeOrder.setClientId(clientId); tradeOrder.setTransmit(new Boolean(true)); //tradeOrder.setStatus(OrderStatus.UNSUBMIT); this.submitOrder(this.getTradestrategy().getContract(), tradeOrder); */ } } else if (startPeriod.isAfter( this.getTradestrategy().getTradingday().getOpen().plusMinutes(25)) && !StrategyOK) { // Si han pasado 25min y no se cumplio la estrategia, cancelamos la // ejecución // this.cancel(); return; } else if (StrategyOK) { // Si se ejecuto la estrategia... // Candle prevDayCandle = getPreviousDayCandleFromDb(candleSeries, startPeriod); /* * Is the candle in the direction of the Tradestrategy side i.e. * a long play should have a green 5min candle */ CandleItem prevCandleItem = null; if (getCurrentCandleCount() > 0) { prevCandleItem = (CandleItem) candleSeries.getDataItem(getCurrentCandleCount() - 1); // AbstractStrategyRule // .logCandle(this, prevCandleItem.getCandle()); } this.reFreshPositionOrders(); // double lastAuxStopPrice = this.getOpenPositionOrder().getAuxPrice().doubleValue(); if (currentCandleItem.getClose() > prevCandleItem.getClose() && currentCandleItem.getClose() >= addAPercentToANumber(LastAuxStopPrice, 50)) { /* * Validamos que haya un incremento en LOW entre la * posicion actual y la posicion anterior del dia; y * además, que el incremento de LOW del dia sea mayor o * igual a un 50% de LOW del dia anterior */ /* Money stopPrice = addPennyAndRoundStop(this .getOpenPositionOrder().getAverageFilledPrice() .doubleValue(), getOpenTradePosition() .getSide(), Action.BUY, 0.04); moveStopOCAPrice(stopPrice, true); */ Money auxStopPrice = new Money(addAPercentToANumber(LastAuxStopPrice, 50)).subtract(new Money(0.04)); Money limitPrice = new Money(addAPercentToANumber(LastAuxStopPrice, 50)); LastAuxStopPrice = auxStopPrice.doubleValue(); TradeOrder tradeOrder = this.updateOrder( this.getOpenPositionOrder().getOrderKey(), Action.BUY, OrderType.STPLMT, limitPrice, auxStopPrice, QuantityShares, false, true); // Creamos y transmitimos una orden BUY, STPLMT = (LOW + 50%) - 4c this.reFreshPositionOrders(); } _log.info( "StrategyOK::" + StrategyOK + ", LastAuxStopPrice::" + LastAuxStopPrice + ", Symbol::" + this.getSymbol()); } } /* * Close any opened positions with a market order at day end minus * one bar. * if (!currentCandleItem.getLastUpdateDate().isBefore( this.getTradestrategy() .getTradingday() .getClose() .minusMinutes( this.getTradestrategy().getBarSize() / 60))) { cancelOrdersClosePosition(true); _log.info("Rule 15:55:00 close all open positions: " + getSymbol() + " Time: " + startPeriod); this.cancel(); } */ } catch (StrategyRuleException | PersistentModelException | ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | IOException ex) { _log.error("Error runRule exception: " + ex.getMessage(), ex); error(1, 10, "Error runRule exception: " + ex.getMessage()); } catch (Exception ex) { _log.error("Error runRule exception: " + ex.getMessage(), ex); error(1, 10, "Error runRule exception: " + ex.getMessage()); } }
@Test public void http() throws InterruptedException { String tokenHeaderName = WERVAL.application().config().string(JWT.HTTP_HEADER_CONFIG_KEY); JWT jwt = WERVAL.application().plugin(JWT.class); // Unauthorized access to authenticated resource when().get("/authenticated").then().statusCode(UNAUTHORIZED_CODE); // Login String token = given() .body("{\"email\":\"[email protected]\",\"password\":\"admin-password\"}") .contentType(APPLICATION_JSON) .when() .post("/login") .then() .statusCode(OK_CODE) .header(tokenHeaderName, notNullValue()) .log() .all() .extract() .header(tokenHeaderName); // Authenticated access given().header(tokenHeaderName, token).when().get("/authenticated").then().statusCode(OK_CODE); // Authorized access given().header(tokenHeaderName, token).when().get("/authorized").then().statusCode(OK_CODE); // Gather time related claims from token ZoneId utc = ZoneId.of("UTC"); Map<String, Object> claims = jwt.claimsOfToken(token); ZonedDateTime iat = ZonedDateTime.ofInstant(Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_ISSUED_AT)), utc); ZonedDateTime nbf = ZonedDateTime.ofInstant( Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_NOT_BEFORE)), utc); ZonedDateTime exp = ZonedDateTime.ofInstant( Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_EXPIRATION)), utc); // Wait at least one second before renewal so new dates will be different Thread.sleep(1200); // Renew token String renewed = given() .header(tokenHeaderName, token) .when() .post("/renew") .then() .statusCode(OK_CODE) .header(tokenHeaderName, notNullValue()) .log() .all() .extract() .header(tokenHeaderName); // Gather time related claims from renewed token claims = jwt.claimsOfToken(renewed); ZonedDateTime renewedIat = ZonedDateTime.ofInstant(Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_ISSUED_AT)), utc); ZonedDateTime renewedNbf = ZonedDateTime.ofInstant( Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_NOT_BEFORE)), utc); ZonedDateTime renewedExp = ZonedDateTime.ofInstant( Instant.ofEpochSecond((Long) claims.get(JWT.CLAIM_EXPIRATION)), utc); // Assert renewed token time related claims are greater than the ones in the original token assertTrue(renewedIat.isAfter(iat)); assertTrue(renewedNbf.isAfter(nbf)); assertTrue(renewedExp.isAfter(exp)); }