BollingerBandStrategy

Bollinger Band Strategy is a strategy build to gain profit on alternately buying and selling shares. Bollinger Band Strategy calculates three lines of values:

  • Middle band - simple moving average (SMA)
  • Upper Band - middle band plus standard deviation of price multiplied by a variance impact parameter
  • Lower Band - middle band minus standard deviation of price multiplied by a variance impact parameter

In the moment when the middle band crosses the upper or lower band from the proper side it is a signal for a strategy to make an order.

Strategy settings

Bollinger Bands Strategy parameters
NUMBER OF PERIODS Number of trades included in moving averages computation
VARAINCE IMPACT Decides about importance of a variance value

Case scenario

For case scenario Bollinger Band Strategy is set to default values and is executed on daily tick data.

Bollinger Band Example view

Bollinger Band Strategy executes alternately buy and sell orders. If middle band crosses an upper band from the bottom side it is a signal for Bollinger Band Strategy to make a sell order. If middle band crosses a lower band from the top side it is a signal for Bollinger Band Strategy to make a buy order.

Bollinger Bands Strategy statistics

As can be seen in strategy statistics, Bollinger Band Strategy brought net profit of $9.250.

Development

Bollinger Band Strategy is composed of one trading action.

action(tradingAction
    .condition(TRADE_SIGNAL)
    .actionType(TradingActionType.NEW_ORDER)
    .createOrderWith(
        orderBuilder
            .withSymbol(marketSymbol)
                    .side(SIDE)
                    .withPriceLimit(PRICE)
                    .withQuantity((s) -> BigDecimal.ONE))
        .onNewOrderSent(ON_NEW_ORDER_SENT))

Action when fired checks conditions if it should buy or sell shares in the moment. If one of conditions is fulfilled then trading action makes order with proper side and price.

Additionally in strategy instance there is ON_TRADE consumer that is executed with every incoming trade.

private Consumer<TradeEvent> ON_TRADE = new Consumer<TradeEvent>() {
        @Override
        public void accept(TradeEvent tradeEvent) {
            bands.add(tradeEvent.getPrice());

            if (bands.isReady()) {
                triggerTradingActions(tradeEvent.getMarketSymbol());
            }
        }
};

With every trade strategy recalculates stored average values and when proper number of trades pass it starts triggering defined trading action.