Skip to content

Commit 263d656

Browse files
committed
add mark_price to positions
1 parent c4b441d commit 263d656

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ All notable changes to this project will be documented in this file.
1212
- `MarginMode` enum + `update_margin!` + `update_marks!` functions for margin calculations and extended `Position` struct and `Account` struct to support margin trading
1313
- `ContractKind` enum to distinguish spot, perpetual, and future instruments
1414
- New `Instrument` lifecycle fields (`contract_kind`, `start_time`, `expiry`) with helpers `has_expiry`, `is_expired`, and `is_active`
15-
16-
### Changed
17-
18-
- Quote cash lookup performance improvement by caching index in `Instrument` struct
15+
- `Position.mark_price` stores the latest valuation price, updated on fills and mark-to-market to support liquidation, expiry settlement, and borrow logic.
1916

2017
### Removed
2118

2219
- Metadata type parameters (`OData`, `IData`, `CData`) and associated metadata fields were removed from core types, Tables outputs, and printing to simplify the API.
2320

21+
### Changed
22+
23+
- Quote cash lookup performance improvement by caching index in `Instrument` struct
24+
2425
## [0.4.2] - 2025-09-29
2526

2627
### Changed

src/logic.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Updates valuation and margin for a position using the latest mark price.
8080
@inline function update_marks!(acc::Account, pos::Position, close_price)
8181
update_valuation!(acc, pos, close_price)
8282
update_margin!(acc, pos, close_price)
83+
pos.mark_price = close_price
8384
return
8485
end
8586

src/position.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mutable struct Position{TTime<:Dates.AbstractTime}
77
value_local::Price # position value contribution in local currency
88
margin_init_local::Price # initial margin used in local currency
99
margin_maint_local::Price # maintenance margin used in local currency
10+
mark_price::Price # last valuation price
1011
last_order::Union{Nothing,Order{TTime}}
1112
last_trade::Union{Nothing,Trade{TTime}}
1213

@@ -20,6 +21,7 @@ mutable struct Position{TTime<:Dates.AbstractTime}
2021
value_local::Price=0.0,
2122
margin_init_local::Price=0.0,
2223
margin_maint_local::Price=0.0,
24+
mark_price::Price=Price(NaN),
2325
last_order::Union{Nothing,Order{TTime}}=nothing,
2426
last_trade::Union{Nothing,Trade{TTime}}=nothing,
2527
) where {TTime<:Dates.AbstractTime}
@@ -32,6 +34,7 @@ mutable struct Position{TTime<:Dates.AbstractTime}
3234
value_local,
3335
margin_init_local,
3436
margin_maint_local,
37+
mark_price,
3538
last_order,
3639
last_trade
3740
)

test/position.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,30 @@ end
7474
# Test 9: no op
7575
@test calc_exposure_increase_quantity(0, 0) == 0
7676
end
77+
78+
@testitem "mark_price set on fills and marks" begin
79+
using Test, Fastback, Dates
80+
81+
acc = Account()
82+
deposit!(acc, Cash(:USD), 10_000.0)
83+
inst = register_instrument!(acc, Instrument(Symbol("MK/USD"), :MK, :USD))
84+
85+
# Fill long; mark should be fill price
86+
order = Order(oid!(acc), inst, DateTime(2026, 1, 1), 100.0, 1.0)
87+
trade = fill_order!(acc, order, order.date, order.price)
88+
pos = get_position(acc, inst)
89+
@test pos.mark_price == 100.0
90+
91+
# Update with bid/ask; long uses bid as close
92+
update_pnl!(acc, inst, 101.0, 102.0)
93+
@test pos.mark_price == 101.0
94+
95+
# Flip to short; mark on fill should update
96+
order2 = Order(oid!(acc), inst, DateTime(2026, 1, 2), 103.0, -2.0)
97+
fill_order!(acc, order2, order2.date, order2.price)
98+
@test pos.mark_price == 103.0
99+
100+
# For short, close price comes from ask
101+
update_pnl!(acc, inst, 98.0, 99.0)
102+
@test pos.mark_price == 99.0
103+
end

0 commit comments

Comments
 (0)