MACDStrategy

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

  • MACD line - difference between long term and short term average
  • Signal line - average of MACD Line on given period

In the moment when these 2 lines crosses with each other then it is a signal for MACD Strategy to make an order. If MACD line crosses below Signal line then it is a signal to sell shares and respectively when it crosses above Signal line it is a time to buy shares.

Strategy settings

MACD Strategy parameters
LENGTH OF SHORT TERM AVERAGE Number of trades included in short moving average window
LENGTH OF LONG TERM AVERAGE Number of trades included in long moving average window
LENGTH OF MACD SIGNAL LINE Number of trades included in MACD average window

Case scenario

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

MACD Example view

MACD Strategy executes alternately buy and sell orders in places where MACD and signal lines crosses. When MACD rises above Signal line it is time to place buy order and when MACD falls below Signal line it is time to signal to sell.

MACD Strategy statistics

As can be seen in strategy statistics, MACD Strategy brought net profit of $0.05.

Development

MACD Strategy is composed of one trading action.

action(tradingAction
        .condition(BUY_SIGNAL.or(SELL_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) {
                longTermData.add(tradeEvent.getPrice());
                shortTermData.add(tradeEvent.getPrice());

                if (longTermData.isFilled() && shortTermData.isFilled()) {
                        BigDecimal MACD = shortTermData.getMeanValue().subtract(longTermData.getMeanValue());
                        signalLine.add(MACD);

                        if (signalLine.isFilled()) {
                                MACDLineCurrent = MACD;
                                signalLineCurrent = signalLine.getMeanValue();

                                triggerTradingActions(tradeEvent.getMarketSymbol());
                        }
                }
        }
};

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