Skip to content

Commit

Permalink
portfolio done
Browse files Browse the repository at this point in the history
Signed-off-by: Pushkar Mishra <[email protected]>
  • Loading branch information
Pushkarm029 committed Nov 20, 2024
1 parent 7b36e7f commit 4917806
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 162 deletions.
15 changes: 15 additions & 0 deletions nautilus_core/common/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,12 @@ impl Cache {
self.accounts.get(account_id)
}

/// Returns a mutable reference to the account for the given `account_id` (if found).
#[must_use]
pub fn mut_account(&mut self, account_id: &AccountId) -> Option<&mut AccountAny> {
self.accounts.get_mut(account_id)
}

/// Returns a reference to the account for the given `venue` (if found).
#[must_use]
pub fn account_for_venue(&self, venue: &Venue) -> Option<&AccountAny> {
Expand All @@ -2646,6 +2652,15 @@ impl Cache {
.and_then(|account_id| self.accounts.get(account_id))
}

/// Returns a reference to the account for the given `venue` (if found).
#[must_use]
pub fn mut_account_for_venue(&mut self, venue: &Venue) -> Option<&mut AccountAny> {
self.index
.venue_account
.get(venue)
.and_then(|account_id| self.accounts.get_mut(account_id))
}

/// Returns a reference to the account ID for the given `venue` (if found).
#[must_use]
pub fn account_id(&self, venue: &Venue) -> Option<&AccountId> {
Expand Down
24 changes: 21 additions & 3 deletions nautilus_core/model/src/events/position/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use crate::events::position::{
changed::PositionChanged, closed::PositionClosed, opened::PositionOpened,
use crate::{
events::position::{changed::PositionChanged, closed::PositionClosed, opened::PositionOpened},
identifiers::{AccountId, InstrumentId},
};

pub mod changed;
pub mod closed;
pub mod opened;
Expand All @@ -27,3 +27,21 @@ pub enum PositionEvent {
PositionChanged(PositionChanged),
PositionClosed(PositionClosed),
}

impl PositionEvent {
pub fn instrument_id(&self) -> InstrumentId {
match self {
PositionEvent::PositionOpened(position) => position.instrument_id,
PositionEvent::PositionChanged(position) => position.instrument_id,
PositionEvent::PositionClosed(position) => position.instrument_id,
}
}

pub fn account_id(&self) -> AccountId {
match self {
PositionEvent::PositionOpened(position) => position.account_id,
PositionEvent::PositionChanged(position) => position.account_id,
PositionEvent::PositionClosed(position) => position.account_id,
}
}
}
22 changes: 13 additions & 9 deletions nautilus_core/portfolio/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct AccountsManager {
}

impl AccountsManager {
pub fn new(clock: Rc<RefCell<dyn Clock>>, cache: Rc<RefCell<Cache>>) -> Self {
Self { clock, cache }
}

#[must_use]
pub fn update_balances(
&self,
Expand Down Expand Up @@ -86,17 +90,17 @@ impl AccountsManager {
#[must_use]
pub fn update_orders(
&self,
account: AccountAny,
account: &mut AccountAny,
instrument: InstrumentAny,
orders_open: &[OrderAny],
orders_open: Vec<&OrderAny>,
ts_event: UnixNanos,
) -> Option<AccountState> {
match account {
AccountAny::Cash(mut cash_account) => {
self.update_balance_locked(&mut cash_account, instrument, orders_open, ts_event)
AccountAny::Cash(cash_account) => {
self.update_balance_locked(cash_account, instrument, orders_open, ts_event)
}
AccountAny::Margin(mut margin_account) => {
self.update_margin_init(&mut margin_account, instrument, orders_open, ts_event)
AccountAny::Margin(margin_account) => {
self.update_margin_init(margin_account, instrument, orders_open, ts_event)
}
}
}
Expand All @@ -107,7 +111,7 @@ impl AccountsManager {
&self,
account: &mut MarginAccount,
instrument: InstrumentAny,
positions: &[Position],
positions: Vec<&Position>,
ts_event: UnixNanos,
) -> Option<AccountState> {
// Initialize variables
Expand Down Expand Up @@ -241,7 +245,7 @@ impl AccountsManager {
&self,
account: &mut CashAccount,
instrument: InstrumentAny,
orders_open: &[OrderAny],
orders_open: Vec<&OrderAny>,
ts_event: UnixNanos,
) -> Option<AccountState> {
if orders_open.is_empty() {
Expand Down Expand Up @@ -336,7 +340,7 @@ impl AccountsManager {
&self,
account: &mut MarginAccount,
instrument: InstrumentAny,
orders_open: &[OrderAny],
orders_open: Vec<&OrderAny>,
ts_event: UnixNanos,
) -> Option<AccountState> {
let mut total_margin_init = Decimal::ZERO;
Expand Down
Loading

0 comments on commit 4917806

Please sign in to comment.