Compare commits

...

2 Commits

Author SHA1 Message Date
077b723536 parse trading212 record fees 2025-11-13 12:12:24 +00:00
143bd844a7 add Fees and Taxes to the Record type 2025-11-13 11:27:52 +00:00
7 changed files with 212 additions and 22 deletions

View File

@@ -1,3 +1,3 @@
package internal package internal
//go:generate mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record //go:generate go tool mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record,ReportWriter

View File

@@ -1,9 +1,9 @@
// Code generated by MockGen. DO NOT EDIT. // Code generated by MockGen. DO NOT EDIT.
// Source: git.naterciomoniz.net/applications/broker2anexoj/internal (interfaces: RecordReader,Record) // Source: git.naterciomoniz.net/applications/broker2anexoj/internal (interfaces: RecordReader,Record,ReportWriter)
// //
// Generated by this command: // Generated by this command:
// //
// mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record // mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record,ReportWriter
// //
// Package mocks is a generated GoMock package. // Package mocks is a generated GoMock package.
@@ -105,6 +105,44 @@ func (m *MockRecord) EXPECT() *MockRecordMockRecorder {
return m.recorder return m.recorder
} }
// Fees mocks base method.
func (m *MockRecord) Fees() *big.Float {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Fees")
ret0, _ := ret[0].(*big.Float)
return ret0
}
// Fees indicates an expected call of Fees.
func (mr *MockRecordMockRecorder) Fees() *MockRecordFeesCall {
mr.mock.ctrl.T.Helper()
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Fees", reflect.TypeOf((*MockRecord)(nil).Fees))
return &MockRecordFeesCall{Call: call}
}
// MockRecordFeesCall wrap *gomock.Call
type MockRecordFeesCall struct {
*gomock.Call
}
// Return rewrite *gomock.Call.Return
func (c *MockRecordFeesCall) Return(arg0 *big.Float) *MockRecordFeesCall {
c.Call = c.Call.Return(arg0)
return c
}
// Do rewrite *gomock.Call.Do
func (c *MockRecordFeesCall) Do(f func() *big.Float) *MockRecordFeesCall {
c.Call = c.Call.Do(f)
return c
}
// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockRecordFeesCall) DoAndReturn(f func() *big.Float) *MockRecordFeesCall {
c.Call = c.Call.DoAndReturn(f)
return c
}
// Price mocks base method. // Price mocks base method.
func (m *MockRecord) Price() *big.Float { func (m *MockRecord) Price() *big.Float {
m.ctrl.T.Helper() m.ctrl.T.Helper()
@@ -257,6 +295,44 @@ func (c *MockRecordSymbolCall) DoAndReturn(f func() string) *MockRecordSymbolCal
return c return c
} }
// Taxes mocks base method.
func (m *MockRecord) Taxes() *big.Float {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Taxes")
ret0, _ := ret[0].(*big.Float)
return ret0
}
// Taxes indicates an expected call of Taxes.
func (mr *MockRecordMockRecorder) Taxes() *MockRecordTaxesCall {
mr.mock.ctrl.T.Helper()
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Taxes", reflect.TypeOf((*MockRecord)(nil).Taxes))
return &MockRecordTaxesCall{Call: call}
}
// MockRecordTaxesCall wrap *gomock.Call
type MockRecordTaxesCall struct {
*gomock.Call
}
// Return rewrite *gomock.Call.Return
func (c *MockRecordTaxesCall) Return(arg0 *big.Float) *MockRecordTaxesCall {
c.Call = c.Call.Return(arg0)
return c
}
// Do rewrite *gomock.Call.Do
func (c *MockRecordTaxesCall) Do(f func() *big.Float) *MockRecordTaxesCall {
c.Call = c.Call.Do(f)
return c
}
// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockRecordTaxesCall) DoAndReturn(f func() *big.Float) *MockRecordTaxesCall {
c.Call = c.Call.DoAndReturn(f)
return c
}
// Timestamp mocks base method. // Timestamp mocks base method.
func (m *MockRecord) Timestamp() time.Time { func (m *MockRecord) Timestamp() time.Time {
m.ctrl.T.Helper() m.ctrl.T.Helper()
@@ -294,3 +370,65 @@ func (c *MockRecordTimestampCall) DoAndReturn(f func() time.Time) *MockRecordTim
c.Call = c.Call.DoAndReturn(f) c.Call = c.Call.DoAndReturn(f)
return c return c
} }
// MockReportWriter is a mock of ReportWriter interface.
type MockReportWriter struct {
ctrl *gomock.Controller
recorder *MockReportWriterMockRecorder
isgomock struct{}
}
// MockReportWriterMockRecorder is the mock recorder for MockReportWriter.
type MockReportWriterMockRecorder struct {
mock *MockReportWriter
}
// NewMockReportWriter creates a new mock instance.
func NewMockReportWriter(ctrl *gomock.Controller) *MockReportWriter {
mock := &MockReportWriter{ctrl: ctrl}
mock.recorder = &MockReportWriterMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockReportWriter) EXPECT() *MockReportWriterMockRecorder {
return m.recorder
}
// Write mocks base method.
func (m *MockReportWriter) Write(arg0 internal.ReportItem) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Write", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// Write indicates an expected call of Write.
func (mr *MockReportWriterMockRecorder) Write(arg0 any) *MockReportWriterWriteCall {
mr.mock.ctrl.T.Helper()
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockReportWriter)(nil).Write), arg0)
return &MockReportWriterWriteCall{Call: call}
}
// MockReportWriterWriteCall wrap *gomock.Call
type MockReportWriterWriteCall struct {
*gomock.Call
}
// Return rewrite *gomock.Call.Return
func (c *MockReportWriterWriteCall) Return(arg0 error) *MockReportWriterWriteCall {
c.Call = c.Call.Return(arg0)
return c
}
// Do rewrite *gomock.Call.Do
func (c *MockReportWriterWriteCall) Do(f func(internal.ReportItem) error) *MockReportWriterWriteCall {
c.Call = c.Call.Do(f)
return c
}
// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockReportWriterWriteCall) DoAndReturn(f func(internal.ReportItem) error) *MockReportWriterWriteCall {
c.Call = c.Call.DoAndReturn(f)
return c
}

View File

@@ -12,6 +12,8 @@ type Record interface {
Price() *big.Float Price() *big.Float
Quantity() *big.Float Quantity() *big.Float
Timestamp() time.Time Timestamp() time.Time
Fees() *big.Float
Taxes() *big.Float
} }
type RecordQueue struct { type RecordQueue struct {

15
internal/report.go Normal file
View File

@@ -0,0 +1,15 @@
package internal
import (
"errors"
)
type RecordReader interface {
// ReadRecord should return Records until an error is found.
ReadRecord() (Record, error)
}
type ReportWriter interface {
// ReportWriter writes report items
Write(ReportItem) error
}

View File

@@ -9,11 +9,6 @@ import (
"sync" "sync"
) )
type RecordReader interface {
// ReadRecord should return Records until an error is found.
ReadRecord() (Record, error)
}
// Reporter consumes each record to produce ReportItem. // Reporter consumes each record to produce ReportItem.
type Reporter struct { type Reporter struct {
reader RecordReader reader RecordReader

View File

@@ -17,6 +17,8 @@ type Record struct {
quantity *big.Float quantity *big.Float
price *big.Float price *big.Float
timestamp time.Time timestamp time.Time
fees *big.Float
taxes *big.Float
} }
func (r Record) Symbol() string { func (r Record) Symbol() string {
@@ -39,6 +41,14 @@ func (r Record) Timestamp() time.Time {
return r.timestamp return r.timestamp
} }
func (r Record) Fees() *big.Float {
return r.fees
}
func (r Record) Taxes() *big.Float {
return r.taxes
}
type RecordReader struct { type RecordReader struct {
reader *csv.Reader reader *csv.Reader
} }
@@ -90,12 +100,29 @@ func (rr RecordReader) ReadRecord() (internal.Record, error) {
return Record{}, fmt.Errorf("parse record timestamp: %w", err) return Record{}, fmt.Errorf("parse record timestamp: %w", err)
} }
convertionFee, err := parseOptinalDecimal(raw[16])
if err != nil {
return Record{}, fmt.Errorf("parse record convertion fee: %w", err)
}
stampDutyTax, err := parseOptinalDecimal(raw[14])
if err != nil {
return Record{}, fmt.Errorf("parse record stamp duty tax: %w", err)
}
frenchTxTax, err := parseOptinalDecimal(raw[18])
if err != nil {
return Record{}, fmt.Errorf("parse record french transaction tax: %w", err)
}
return Record{ return Record{
symbol: raw[2], symbol: raw[2],
side: side, side: side,
quantity: qant, quantity: qant,
price: price, price: price,
timestamp: ts, timestamp: ts,
fees: convertionFee,
taxes: new(big.Float).Add(stampDutyTax, frenchTxTax),
}, nil }, nil
} }
} }
@@ -106,3 +133,14 @@ func parseDecimal(s string) (*big.Float, error) {
f, _, err := big.ParseFloat(s, 10, 128, big.ToZero) f, _, err := big.ParseFloat(s, 10, 128, big.ToZero)
return f, err return f, err
} }
// parseOptinalDecimal behaves the same as parseDecimal but returns 0 when len(s) is 0 instead of
// error.
// Using this function helps avoid issues around converting values due to sligh parameter changes.
func parseOptinalDecimal(s string) (*big.Float, error) {
if len(s) == 0 {
return new(big.Float), nil
}
return parseDecimal(s)
}

View File

@@ -24,75 +24,69 @@ func TestRecordReader_ReadRecord(t *testing.T) {
wantErr: true, wantErr: true,
}, },
{ {
name: "well formed buy", 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",,`), 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.25,"EUR",0.02,"EUR",,`),
want: Record{ want: Record{
symbol: "SYM123456ABXY", symbol: "SYM123456ABXY",
side: internal.SideBuy, side: internal.SideBuy,
quantity: ShouldParseDecimal(t, "2.4387014200"), quantity: ShouldParseDecimal(t, "2.4387014200"),
price: ShouldParseDecimal(t, "7.3690000000"), price: ShouldParseDecimal(t, "7.3690000000"),
timestamp: time.Date(2025, 7, 3, 10, 44, 29, 0, time.UTC), timestamp: time.Date(2025, 7, 3, 10, 44, 29, 0, time.UTC),
fees: ShouldParseDecimal(t, "0.02"),
taxes: ShouldParseDecimal(t, "0.25"),
}, },
wantErr: false,
}, },
{ {
name: "well formed sell", 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",,`), 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",0.1,"EUR"`),
want: Record{ want: Record{
symbol: "IE000GA3D489", symbol: "IE000GA3D489",
side: internal.SideSell, side: internal.SideSell,
quantity: ShouldParseDecimal(t, "2.4387014200"), quantity: ShouldParseDecimal(t, "2.4387014200"),
price: ShouldParseDecimal(t, "7.9999999999"), price: ShouldParseDecimal(t, "7.9999999999"),
timestamp: time.Date(2025, 8, 4, 11, 45, 30, 0, time.UTC), timestamp: time.Date(2025, 8, 4, 11, 45, 30, 0, time.UTC),
fees: ShouldParseDecimal(t, "0.02"),
taxes: ShouldParseDecimal(t, "0.1"),
}, },
wantErr: false,
}, },
{ {
name: "malformed side", 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",,`), 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, wantErr: true,
}, },
{ {
name: "empty side", 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",,`), 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, wantErr: true,
}, },
{ {
name: "malformed qantity", 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",,`), 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, wantErr: true,
}, },
{ {
name: "empty qantity", 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",,`), 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, wantErr: true,
}, },
{ {
name: "malformed price", 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",,`), 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, wantErr: true,
}, },
{ {
name: "empty price", 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",,`), 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, wantErr: true,
}, },
{ {
name: "malformed timestamp", 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",,`), 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, wantErr: true,
}, },
{ {
name: "empty timestamp", 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",,`), 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, wantErr: true,
}, },
} }
@@ -130,6 +124,14 @@ func TestRecordReader_ReadRecord(t *testing.T) {
if !got.Timestamp().Equal(tt.want.timestamp) { if !got.Timestamp().Equal(tt.want.timestamp) {
t.Fatalf("want timestamp %v but got %v", tt.want.timestamp, got.Timestamp()) t.Fatalf("want timestamp %v but got %v", tt.want.timestamp, got.Timestamp())
} }
if got.Fees().Cmp(tt.want.fees) != 0 {
t.Fatalf("want fees %v but got %v", tt.want.fees, got.Fees())
}
if got.Taxes().Cmp(tt.want.taxes) != 0 {
t.Fatalf("want taxes %v but got %v", tt.want.taxes, got.Taxes())
}
}) })
} }
} }