Skip to content

Commit dd5c381

Browse files
committed
perf improvement: added Instrument.quote_cash_index
1 parent 08d78fb commit dd5c381

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
88

99
- Support for optional Instrument `multiplier` value
1010

11+
### Changed
12+
13+
- Quote cash lookup performance improvement by caching index in `Instrument` struct
14+
1115
## [0.4.2] - 2025-09-29
1216

1317
### Changed

src/account.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ function register_instrument!(
137137
throw(ArgumentError("Instrument $(inst.symbol) already registered"))
138138
end
139139

140+
quote_cash_index = cash_asset(acc, inst.quote_symbol).index
141+
140142
# set asset index for fast array indexing and hashing
141143
inst.index = length(acc.positions) + 1
144+
inst.quote_cash_index = quote_cash_index
142145

143146
push!(acc.positions, Position{TTime,OData}(inst.index, inst))
144147

src/instrument.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ mutable struct Instrument{IData}
1313
const quote_symbol::Symbol
1414
const quote_tick::Price # minimum price increment of base asset
1515
const quote_digits::Int # number of digits after the decimal point for display
16-
16+
quote_cash_index::Int
17+
1718
const multiplier::Float64
1819
const metadata::IData
1920

@@ -42,6 +43,7 @@ mutable struct Instrument{IData}
4243
quote_symbol,
4344
quote_tick,
4445
quote_digits,
46+
0, # quote_cash_index
4547
multiplier,
4648
metadata
4749
)

src/logic.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# update P&L and account equity using delta of old and new P&L
33
new_pnl = calc_pnl_local(pos, close_price)
44
pnl_delta = new_pnl - pos.pnl_local
5-
cash = cash_asset(acc, pos.inst.quote_symbol)
6-
@inbounds acc.equities[cash.index] += pnl_delta
5+
quote_cash_index = pos.inst.quote_cash_index
6+
@inbounds acc.equities[quote_cash_index] += pnl_delta
77
pos.pnl_local = new_pnl
88
return
99
end
@@ -24,8 +24,8 @@ end
2424
commission::Price=0.0, # fixed commission in quote (local) currency
2525
commission_pct::Price=0.0, # percentage commission of nominal order value, e.g. 0.001 = 0.1%
2626
)::Trade{TTime,OData,IData} where {TTime<:Dates.AbstractTime,OData,IData,CData}
27-
# get quote asset
28-
quote_cash = cash_asset(acc, order.inst.quote_symbol)
27+
# get quote asset index
28+
quote_cash_index = order.inst.quote_cash_index
2929

3030
# positions are netted using weighted average price,
3131
# hence only one static position per instrument is maintained
@@ -48,7 +48,7 @@ end
4848
realized_pnl = (fill_price - pos.avg_price) * realized_qty
4949

5050
# add realized P&L to account balance
51-
@inbounds acc.balances[quote_cash.index] += realized_pnl
51+
@inbounds acc.balances[quote_cash_index] += realized_pnl
5252

5353
# remove realized P&L from position P&L
5454
pos.pnl_local -= realized_pnl
@@ -98,8 +98,8 @@ end
9898
pos.quantity = new_exposure
9999

100100
# subtract paid commissions from account balance and equity
101-
@inbounds acc.balances[quote_cash.index] -= commission
102-
@inbounds acc.equities[quote_cash.index] -= commission
101+
@inbounds acc.balances[quote_cash_index] -= commission
102+
@inbounds acc.equities[quote_cash_index] -= commission
103103

104104
# update P&L of position and account equity (w/o commissions, already accounted for)
105105
update_pnl!(acc, pos, fill_price)

0 commit comments

Comments
 (0)