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.


MACD Strategy is composed of one trading action.

                        .withQuantity((s) -> BigDecimal.ONE))

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>() {

        public void accept(TradeEvent tradeEvent) {

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

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


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