-
Notifications
You must be signed in to change notification settings - Fork 0
feat: sim snapshot per-position marks and margin-aware buying power #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package account | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| runtimeorder "github.com/rustyeddy/trader/internal/order" | ||
| "github.com/rustyeddy/trader/num" | ||
| ) | ||
|
|
||
| // PositionMark is the current valuation price of one open position's | ||
| // listing, and when it was observed (ADR-066). | ||
| // | ||
| // A mark is "as of the reporter's last observation of that listing" — | ||
| // for the simulator, the last fill or bar close it saw — not a live | ||
| // price. In a multi-instrument account, different listings' marks can | ||
| // have different AsOf times; each is the latest observation at or | ||
| // before the snapshot's own AsOf. | ||
| type PositionMark struct { | ||
| // Listing identifies the open position this mark values. | ||
| Listing ListingKey | ||
| // Price is the valuation price. It must be positive. | ||
| Price num.Price | ||
| // AsOf is when Price was observed. It must be set and must not be | ||
| // after the snapshot's AsOf. | ||
| AsOf time.Time | ||
| } | ||
|
|
||
| // checkMarks validates marks against the snapshot's positions and | ||
| // returns them in positions order. Every mark must name an open | ||
| // position, at most once. A position may have no mark: marks are | ||
| // optional for a reporter that has none, and a consumer that needs one | ||
| // treats its absence as an error, never as zero or AvgPrice. | ||
| func checkMarks(positions []runtimeorder.Position, marks []PositionMark, asOf time.Time) ([]PositionMark, error) { | ||
| if len(marks) == 0 { | ||
| return nil, nil | ||
| } | ||
| open := make(map[ListingKey]struct{}, len(positions)) | ||
| for _, p := range positions { | ||
| open[KeyOf(p.Listing)] = struct{}{} | ||
| } | ||
| byKey := make(map[ListingKey]PositionMark, len(marks)) | ||
| for i, m := range marks { | ||
| if _, ok := open[m.Listing]; !ok { | ||
| return nil, fmt.Errorf("entry %d: no open position in listing %s/%s/%s", | ||
| i, m.Listing.InstrumentID, m.Listing.Provider, m.Listing.Venue) | ||
| } | ||
| if _, dup := byKey[m.Listing]; dup { | ||
| return nil, fmt.Errorf("entry %d: duplicate mark for listing %s/%s/%s", | ||
| i, m.Listing.InstrumentID, m.Listing.Provider, m.Listing.Venue) | ||
| } | ||
| if m.Price.IsZero() { | ||
| return nil, fmt.Errorf("entry %d: price must be positive", i) | ||
| } | ||
| if m.AsOf.IsZero() { | ||
| return nil, fmt.Errorf("entry %d: as-of time must be set", i) | ||
| } | ||
| if m.AsOf.After(asOf) { | ||
| return nil, fmt.Errorf("entry %d: as-of %s is after snapshot as-of %s", i, m.AsOf, asOf) | ||
| } | ||
| byKey[m.Listing] = m | ||
| } | ||
| ordered := make([]PositionMark, 0, len(byKey)) | ||
| for _, p := range positions { | ||
| if m, ok := byKey[KeyOf(p.Listing)]; ok { | ||
| ordered = append(ordered, m) | ||
| } | ||
| } | ||
| return ordered, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package account | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| runtimeorder "github.com/rustyeddy/trader/internal/order" | ||
| "github.com/rustyeddy/trader/num" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // twoPositionParams is validParams with open EUR/USD and GBP/USD | ||
| // positions, in that order. | ||
| func twoPositionParams(t *testing.T) SnapshotParams { | ||
| t.Helper() | ||
| p := validParams(t) | ||
| gbp := mustListing(t, "GBP", "USD", "OANDA", "GBP_USD") | ||
| p.Positions = append(p.Positions, mustPosition(t, p.AccountID, gbp)) | ||
| return p | ||
| } | ||
|
|
||
| func markFor(p runtimeorder.Position, price string, at time.Time) PositionMark { | ||
| return PositionMark{Listing: KeyOf(p.Listing), Price: num.MustParsePrice(price), AsOf: at} | ||
| } | ||
|
|
||
| func TestSnapshotMarks(t *testing.T) { | ||
| p := twoPositionParams(t) | ||
| earlier := p.AsOf.Add(-time.Hour) | ||
| eurMark := markFor(p.Positions[0], "1.1", p.AsOf) | ||
| gbpMark := markFor(p.Positions[1], "1.3", earlier) | ||
| // Supplied out of Positions order; returned in Positions order. | ||
| p.Marks = []PositionMark{gbpMark, eurMark} | ||
|
|
||
| s, err := NewSnapshot(p) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []PositionMark{eurMark, gbpMark}, s.Marks()) | ||
|
|
||
| got, ok := s.Mark(KeyOf(p.Positions[1].Listing)) | ||
| require.True(t, ok) | ||
| assert.Equal(t, gbpMark, got, "each mark keeps its own AsOf") | ||
|
|
||
| // Marks returns a copy. | ||
| s.Marks()[0].Price = num.MustParsePrice("99") | ||
| assert.Equal(t, eurMark, s.Marks()[0]) | ||
| } | ||
|
|
||
| func TestSnapshotMarksOptional(t *testing.T) { | ||
| p := twoPositionParams(t) | ||
| p.Marks = []PositionMark{markFor(p.Positions[1], "1.3", p.AsOf)} | ||
| s, err := NewSnapshot(p) | ||
| require.NoError(t, err) | ||
| _, ok := s.Mark(KeyOf(p.Positions[0].Listing)) | ||
| assert.False(t, ok, "a position may have no mark") | ||
|
|
||
| s, err = NewSnapshot(validParams(t)) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, s.Marks()) | ||
| } | ||
|
|
||
| func TestSnapshotRejectsInvalidMarks(t *testing.T) { | ||
| other := mustListing(t, "AUD", "USD", "OANDA", "AUD_USD") | ||
| cases := []struct { | ||
| name string | ||
| mutate func(p *SnapshotParams) | ||
| }{ | ||
| {"no open position in listing", func(p *SnapshotParams) { | ||
| p.Marks = []PositionMark{{Listing: KeyOf(other), Price: num.MustParsePrice("1"), AsOf: p.AsOf}} | ||
| }}, | ||
| {"duplicate", func(p *SnapshotParams) { | ||
| m := markFor(p.Positions[0], "1", p.AsOf) | ||
| p.Marks = []PositionMark{m, m} | ||
| }}, | ||
| {"zero price", func(p *SnapshotParams) { | ||
| p.Marks = []PositionMark{{Listing: KeyOf(p.Positions[0].Listing), AsOf: p.AsOf}} | ||
| }}, | ||
| {"zero as-of", func(p *SnapshotParams) { | ||
| p.Marks = []PositionMark{{Listing: KeyOf(p.Positions[0].Listing), Price: num.MustParsePrice("1")}} | ||
| }}, | ||
| {"as-of after snapshot", func(p *SnapshotParams) { | ||
| p.Marks = []PositionMark{markFor(p.Positions[0], "1", p.AsOf.Add(time.Second))} | ||
| }}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| p := validParams(t) | ||
| tc.mutate(&p) | ||
| _, err := NewSnapshot(p) | ||
| assert.ErrorIs(t, err, ErrInvalidSnapshot) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestKeyOf(t *testing.T) { | ||
| l := mustEurUsdListing(t) | ||
| k := KeyOf(l) | ||
| assert.Equal(t, l.InstrumentID(), k.InstrumentID) | ||
| assert.Equal(t, l.Provider(), k.Provider) | ||
| assert.Equal(t, l.Venue(), k.Venue) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in c67b0c1:
accountWiregainsmarks(markWire: instrument/provider/venue, price, as_of, with omitempty so older entries still decode). On read, each mark's listing is resolved against the entry's own decoded positions; a mark naming no position isErrCorruptEntry.TestWriterReaderRoundTripsAccountMarksround-trips two listings, one with a venue, with differentAsOfvalues.