-
Notifications
You must be signed in to change notification settings - Fork 0
/
positions.go
78 lines (59 loc) · 1.16 KB
/
positions.go
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
package models
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
)
type Positions []*Position
type PositionsAnalysis struct {
Winning, TotalPL float64
}
func (ps Positions) Localize() {
for _, ps := range ps {
ps.Localize()
}
}
func (ps Positions) Analyze() *PositionsAnalysis {
totalPL := 0.0
nWinning := 0
for _, pos := range ps {
if pos.ClosedPL > 0.0 {
nWinning++
}
totalPL += pos.ClosedPL
}
winning := float64(nWinning) / float64(len(ps))
return &PositionsAnalysis{
Winning: winning,
TotalPL: totalPL,
}
}
func (ps Positions) StoreToFile(fpath string) error {
f, err := os.Create(fpath)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", fpath, err)
}
defer f.Close()
w := bufio.NewWriter(f)
if err = ps.Store(w); err != nil {
return err
}
w.Flush()
return nil
}
func (ps Positions) Store(w io.Writer) error {
for i, p := range ps {
d, err := json.Marshal(p)
if err != nil {
return fmt.Errorf("failed to marshal position %d: %w", i+1, err)
}
d = append(d, byte('\n'))
_, err = w.Write(d)
if err != nil {
return fmt.Errorf("failed to write position %d: %w", i+1, err)
}
}
return nil
}