File size: 2,182 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.rods.backtestingstrategies.entity;

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

import java.time.LocalDate;

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

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

    // Execution date
    @Column(nullable = false)
    private LocalDate date;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 10)
    private SignalType type; // BUY or SELL

    // Execution price
    @Column(nullable = false)
    private double price;

    // Number of shares traded
    @Column(nullable = false)
    private long shares;

    // Cash remaining after execution
    @Column(nullable = false)
    private double cashAfter;

    // Total equity after execution
    @Column(nullable = false)
    private double equityAfter;

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

    public static Transaction buy(
            Candle candle,
            double price,
            long shares,
            double cashAfter,
            double equityAfter
    ) {
        return new Transaction(
                null,
                candle.getDate(),
                SignalType.BUY,
                price,
                shares,
                cashAfter,
                equityAfter
        );
    }

    public static Transaction sell(
            Candle candle,
            double price,
            long shares,
            double cashAfter,
            double equityAfter
    ) {
        return new Transaction(
                null,
                candle.getDate(),
                SignalType.SELL,
                price,
                shares,
                cashAfter,
                equityAfter
        );
    }
}