File size: 1,549 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.rods.backtestingstrategies.entity;

import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDate;

@Entity
@Table(
        name = "crossovers",
        indexes = {
                @Index(name = "idx_crossover_date", columnList = "date"),
                @Index(name = "idx_crossover_type", columnList = "type")
        }
)
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED) // JPA requirement
@AllArgsConstructor(access = AccessLevel.PRIVATE)  // force factory usage
@ToString
@EqualsAndHashCode(of = {"date", "type", "price"})
public class CrossOver {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    // Date when crossover occurred
    @Column(nullable = false)
    private LocalDate date;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 10)
    private CrossOverType type; // BULLISH or BEARISH

    // Price at crossover
    @Column(nullable = false)
    private double price;

    /* ==========================
       Factory Methods
       ========================== */

    public static CrossOver bullish(Candle candle) {
        return new CrossOver(
                null,
                candle.getDate(),
                CrossOverType.BULLISH,
                candle.getClosePrice()
        );
    }

    public static CrossOver bearish(Candle candle) {
        return new CrossOver(
                null,
                candle.getDate(),
                CrossOverType.BEARISH,
                candle.getClosePrice()
        );
    }
}