parse trading212 record fees
This commit is contained in:
@@ -17,6 +17,8 @@ type Record struct {
|
||||
quantity *big.Float
|
||||
price *big.Float
|
||||
timestamp time.Time
|
||||
fees *big.Float
|
||||
taxes *big.Float
|
||||
}
|
||||
|
||||
func (r Record) Symbol() string {
|
||||
@@ -40,11 +42,11 @@ func (r Record) Timestamp() time.Time {
|
||||
}
|
||||
|
||||
func (r Record) Fees() *big.Float {
|
||||
return new(big.Float) // FIX:
|
||||
return r.fees
|
||||
}
|
||||
|
||||
func (r Record) Taxes() *big.Float {
|
||||
return new(big.Float) // FIX:
|
||||
return r.taxes
|
||||
}
|
||||
|
||||
type RecordReader struct {
|
||||
@@ -98,12 +100,29 @@ func (rr RecordReader) ReadRecord() (internal.Record, error) {
|
||||
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{
|
||||
symbol: raw[2],
|
||||
side: side,
|
||||
quantity: qant,
|
||||
price: price,
|
||||
timestamp: ts,
|
||||
fees: convertionFee,
|
||||
taxes: new(big.Float).Add(stampDutyTax, frenchTxTax),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
@@ -114,3 +133,14 @@ func parseDecimal(s string) (*big.Float, error) {
|
||||
f, _, err := big.ParseFloat(s, 10, 128, big.ToZero)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -24,75 +24,69 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
||||
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",,`),
|
||||
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.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),
|
||||
fees: ShouldParseDecimal(t, "0.02"),
|
||||
taxes: ShouldParseDecimal(t, "0.25"),
|
||||
},
|
||||
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",,`),
|
||||
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",0.1,"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),
|
||||
fees: ShouldParseDecimal(t, "0.02"),
|
||||
taxes: ShouldParseDecimal(t, "0.1"),
|
||||
},
|
||||
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,
|
||||
},
|
||||
}
|
||||
@@ -130,6 +124,14 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
||||
if !got.Timestamp().Equal(tt.want.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())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user