Compare commits

...

3 Commits

Author SHA1 Message Date
a274718f0b use new internal package types and logic 2025-11-10 14:04:51 +00:00
dca43fe014 renamed direction to side 2025-11-10 14:04:18 +00:00
4686e36501 separate trading212 logic 2025-11-10 13:38:49 +00:00
6 changed files with 368 additions and 105 deletions

18
internal/record.go Normal file
View File

@@ -0,0 +1,18 @@
package internal
import (
"math/big"
"time"
)
type Record interface {
Symbol() string
Price() *big.Float
Quantity() *big.Float
Side() Side
Timestamp() time.Time
}
type RecordReader interface {
ReadRecord() (Record, error)
}

28
internal/side.go Normal file
View File

@@ -0,0 +1,28 @@
package internal
type Side uint
const (
SideUnknown Side = iota
SideBuy
SideSell
)
func (d Side) String() string {
switch d {
case SideBuy:
return "buy"
case SideSell:
return "sell"
default:
return "unknown"
}
}
func (d Side) IsBuy() bool {
return d == SideBuy
}
func (d Side) IsSell() bool {
return d == SideSell
}

60
internal/side_test.go Normal file
View File

@@ -0,0 +1,60 @@
package internal
import "testing"
func TestSide_String(t *testing.T) {
tests := []struct {
name string
side Side
want string
}{
{"buy", SideBuy, "buy"},
{"sell", SideSell, "sell"},
{"unknown", SideUnknown, "unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.side.String(); got != tt.want {
t.Errorf("want Side.String() to be %v but got %v", tt.want, got)
}
})
}
}
func TestSide_IsBuy(t *testing.T) {
tests := []struct {
name string
side Side
want bool
}{
{"buy", SideBuy, true},
{"sell", SideSell, false},
{"unknown", SideUnknown, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.side.IsBuy(); got != tt.want {
t.Errorf("want Side.IsBuy() to be %v but got %v", tt.want, got)
}
})
}
}
func TestSide_IsSell(t *testing.T) {
tests := []struct {
name string
side Side
want bool
}{
{"buy", SideBuy, false},
{"sell", SideSell, true},
{"unknown", SideUnknown, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.side.IsSell(); got != tt.want {
t.Errorf("want Side.IsSell() to be %v but got %v", tt.want, got)
}
})
}
}

View File

@@ -0,0 +1,108 @@
package trading212
import (
"encoding/csv"
"fmt"
"io"
"math/big"
"strings"
"time"
"git.naterciomoniz.net/applications/broker2anexoj/internal"
)
type Record struct {
symbol string
side internal.Side
quantity *big.Float
price *big.Float
timestamp time.Time
}
func (r Record) Symbol() string {
return r.symbol
}
func (r Record) Side() internal.Side {
return r.side
}
func (r Record) Quantity() *big.Float {
return r.quantity
}
func (r Record) Price() *big.Float {
return r.price
}
func (r Record) Timestamp() time.Time {
return r.timestamp
}
type RecordReader struct {
reader *csv.Reader
}
func NewRecordReader(r io.Reader) *RecordReader {
return &RecordReader{
reader: csv.NewReader(r),
}
}
const (
MarketBuy = "market buy"
MarketSell = "market sell"
LimitBuy = "limit buy"
LimitSell = "limit sell"
)
func (rr RecordReader) ReadRecord() (Record, error) {
for {
raw, err := rr.reader.Read()
if err != nil {
return Record{}, fmt.Errorf("read record: %w", err)
}
var side internal.Side
switch strings.ToLower(raw[0]) {
case MarketBuy, LimitBuy:
side = internal.SideBuy
case MarketSell, LimitSell:
side = internal.SideSell
case "action", "stock split open", "stock split close":
continue
default:
return Record{}, fmt.Errorf("parse record type: %s", raw[0])
}
qant, err := parseDecimal(raw[6])
if err != nil {
return Record{}, fmt.Errorf("parse record quantity: %w", err)
}
price, err := parseDecimal(raw[7])
if err != nil {
return Record{}, fmt.Errorf("parse record price: %w", err)
}
ts, err := time.Parse(time.DateTime, raw[1])
if err != nil {
return Record{}, fmt.Errorf("parse record timestamp: %w", err)
}
return Record{
symbol: raw[2],
side: side,
quantity: qant,
price: price,
timestamp: ts,
}, nil
}
}
// parseFloat attempts to parse a string using a standard precision and rounding mode.
// Using this function helps avoid issues around converting values due to sligh parameter changes.
func parseDecimal(s string) (*big.Float, error) {
f, _, err := big.ParseFloat(s, 10, 128, big.ToZero)
return f, err
}

View File

@@ -0,0 +1,145 @@
package trading212
import (
"bytes"
"io"
"math/big"
"testing"
"time"
"git.naterciomoniz.net/applications/broker2anexoj/internal"
)
func TestRecordReader_ReadRecord(t *testing.T) {
tests := []struct {
name string
r io.Reader
want Record
wantErr bool
}{
{
name: "empty reader",
r: bytes.NewBufferString(""),
want: Record{},
wantErr: true,
},
{
name: "well formed buy",
r: bytes.NewBufferString(`Market buy,2025-07-03 10:44:29,SYM123456ABXY,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.3690000000,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{
symbol: "SYM123456ABXY",
side: internal.SideBuy,
quantity: ShouldParseDecimal(t, "2.4387014200"),
price: ShouldParseDecimal(t, "7.3690000000"),
timestamp: time.Date(2025, 7, 3, 10, 44, 29, 0, time.UTC),
},
wantErr: false,
},
{
name: "well formed sell",
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:30,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{
symbol: "IE000GA3D489",
side: internal.SideSell,
quantity: ShouldParseDecimal(t, "2.4387014200"),
price: ShouldParseDecimal(t, "7.9999999999"),
timestamp: time.Date(2025, 8, 4, 11, 45, 30, 0, time.UTC),
},
wantErr: false,
},
{
name: "malformed side",
r: bytes.NewBufferString(`Aljksdaf Balsjdkf,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
{
name: "empty side",
r: bytes.NewBufferString(`,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,0x1234,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
{
name: "malformed qantity",
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,0x1234,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
{
name: "empty qantity",
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
{
name: "malformed price",
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,0b101010,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
{
name: "empty price",
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
{
name: "malformed timestamp",
r: bytes.NewBufferString(`Market sell,2006-01-02T15:04:05Z07:00,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
{
name: "empty timestamp",
r: bytes.NewBufferString(`Market sell,,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
want: Record{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rr := NewRecordReader(tt.r)
got, gotErr := rr.ReadRecord()
if gotErr != nil {
if !tt.wantErr {
t.Fatalf("ReadRecord() failed: %v", gotErr)
}
return
}
if tt.wantErr {
t.Fatalf("ReadRecord() expected an error")
}
if got.symbol != tt.want.symbol {
t.Fatalf("want symbol %v but got %v", tt.want.symbol, got.symbol)
}
if got.side != tt.want.side {
t.Fatalf("want side %v but got %v", tt.want.side, got.side)
}
if got.price.Cmp(tt.want.price) != 0 {
t.Fatalf("want price %v but got %v", tt.want.price, got.price)
}
if got.quantity.Cmp(tt.want.quantity) != 0 {
t.Fatalf("want quantity %v but got %v", tt.want.quantity, got.quantity)
}
if !got.timestamp.Equal(tt.want.timestamp) {
t.Fatalf("want timestamp %v but got %v", tt.want.timestamp, got.timestamp)
}
})
}
}
func ShouldParseDecimal(t testing.TB, sf string) *big.Float {
t.Helper()
bf, err := parseDecimal(sf)
if err != nil {
t.Fatalf("parsing decimal: %s", sf)
}
return bf
}

114
main.go
View File

@@ -2,14 +2,15 @@ package main
import ( import (
"container/list" "container/list"
"encoding/csv"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"math/big" "math/big"
"os" "os"
"strings"
"git.naterciomoniz.net/applications/broker2anexoj/internal"
"git.naterciomoniz.net/applications/broker2anexoj/internal/trading212"
) )
func main() { func main() {
@@ -25,7 +26,7 @@ func run() error {
return fmt.Errorf("open statement: %w", err) return fmt.Errorf("open statement: %w", err)
} }
r := NewRecordReader(f) r := trading212.NewRecordReader(f)
assets := make(map[string]*list.List) assets := make(map[string]*list.List)
for { for {
@@ -37,8 +38,8 @@ func run() error {
return fmt.Errorf("read statement record: %w", err) return fmt.Errorf("read statement record: %w", err)
} }
switch record.Direction() { switch record.Side() {
case DirectionBuy: case internal.SideBuy:
lst, ok := assets[record.Symbol()] lst, ok := assets[record.Symbol()]
if !ok { if !ok {
lst = list.New() lst = list.New()
@@ -46,7 +47,7 @@ func run() error {
} }
lst.PushBack(record) lst.PushBack(record)
case DirectionSell: case internal.SideSell:
lst, ok := assets[record.Symbol()] lst, ok := assets[record.Symbol()]
if !ok { if !ok {
return ErrSellWithoutBuy return ErrSellWithoutBuy
@@ -61,7 +62,7 @@ func run() error {
return ErrSellWithoutBuy return ErrSellWithoutBuy
} }
next, ok := front.Value.(Record) next, ok := front.Value.(internal.Record)
if !ok { if !ok {
return fmt.Errorf("unexpected record type: %T", front) return fmt.Errorf("unexpected record type: %T", front)
} }
@@ -86,7 +87,7 @@ func run() error {
} }
default: default:
return fmt.Errorf("unknown direction: %s", record.Direction()) return fmt.Errorf("unknown side: %s", record.Side())
} }
} }
@@ -96,100 +97,3 @@ func run() error {
} }
var ErrSellWithoutBuy = fmt.Errorf("found sell without bought volume") var ErrSellWithoutBuy = fmt.Errorf("found sell without bought volume")
type Record struct {
symbol string
direction Direction
quantity *big.Float
price *big.Float
}
func (r Record) Symbol() string {
return r.symbol
}
func (r Record) Direction() Direction {
return r.direction
}
func (r Record) Quantity() *big.Float {
return r.quantity
}
func (r Record) Price() *big.Float {
return r.price
}
type RecordReader struct {
reader *csv.Reader
}
func NewRecordReader(r io.Reader) *RecordReader {
return &RecordReader{
reader: csv.NewReader(r),
}
}
func (rr RecordReader) ReadRecord() (Record, error) {
for {
raw, err := rr.reader.Read()
if err != nil {
return Record{}, fmt.Errorf("read record: %w", err)
}
var dir Direction
switch strings.ToLower(raw[0]) {
case "market buy":
dir = DirectionBuy
case "market sell":
dir = DirectionSell
case "action", "stock split open", "stock split close":
continue
default:
return Record{}, fmt.Errorf("unhandled record: %s", raw[0])
}
qant, _, err := big.ParseFloat(raw[6], 10, 20, big.ToZero)
if err != nil {
return Record{}, fmt.Errorf("parse quantity: %w", err)
}
price, _, err := big.ParseFloat(raw[7], 10, 20, big.ToZero)
if err != nil {
return Record{}, fmt.Errorf("parse price: %w", err)
}
return Record{
symbol: raw[2],
direction: dir,
quantity: qant,
price: price,
}, nil
}
}
type Direction uint
const (
DirectionUnknown Direction = 0
DirectionBuy = 1
DirectionSell = 2
)
func (d Direction) String() string {
switch d {
case 1:
return "buy"
case 2:
return "sell"
default:
return "unknown"
}
}
func (d Direction) IsBuy() bool {
return d == DirectionBuy
}
func (d Direction) IsSell() bool {
return d == DirectionSell
}