File size: 975 Bytes
f5cd2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.rods.backtestingstrategies.strategy;

import com.rods.backtestingstrategies.entity.Candle;
import com.rods.backtestingstrategies.entity.SignalType;
import com.rods.backtestingstrategies.entity.TradeSignal;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BuyAndHoldStrategy implements Strategy {

    @Override
    public TradeSignal evaluate(List<Candle> candles, int index) {

        Candle candle = candles.get(index);

        // BUY on first candle
        if (index == 0) {
            return TradeSignal.buy(candle);
        }

        // SELL on last candle
        if (index == candles.size() - 1) {
            return TradeSignal.sell(candle);
        }

        // Otherwise HOLD
        return TradeSignal.hold();
    }

    @Override
    public StrategyType getType() {
        return StrategyType.BUY_AND_HOLD;
    }

    @Override
    public String getName() {
        return "Buy & Hold";
    }
}