nautilus_infrastructure/sql/models/
positions.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::str::FromStr;
17
18use nautilus_core::UnixNanos;
19use nautilus_model::{
20    enums::{OrderSide, PositionSide},
21    events::PositionSnapshot,
22    identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TraderId},
23    types::{Currency, Money, Quantity},
24};
25use sqlx::{FromRow, Row, postgres::PgRow};
26
27pub struct PositionSnapshotModel(pub PositionSnapshot);
28
29impl<'r> FromRow<'r, PgRow> for PositionSnapshotModel {
30    fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
31        let id = row.try_get::<&str, _>("id").map(PositionId::from)?;
32        let trader_id = row.try_get::<&str, _>("trader_id").map(TraderId::from)?;
33        let strategy_id = row
34            .try_get::<&str, _>("strategy_id")
35            .map(StrategyId::from)?;
36        let instrument_id = row
37            .try_get::<&str, _>("instrument_id")
38            .map(InstrumentId::from)?;
39        let account_id = row.try_get::<&str, _>("account_id").map(AccountId::from)?;
40        let opening_order_id = row
41            .try_get::<&str, _>("opening_order_id")
42            .map(ClientOrderId::from)?;
43        let closing_order_id = row
44            .try_get::<Option<&str>, _>("closing_order_id")
45            .ok()
46            .and_then(|x| x.map(ClientOrderId::from));
47        let entry = row
48            .try_get::<&str, _>("entry")
49            .map(OrderSide::from_str)?
50            .expect("Invalid `OrderSide`");
51        let side = row
52            .try_get::<&str, _>("side")
53            .map(PositionSide::from_str)?
54            .expect("Invalid `PositionSide`");
55        let signed_qty = row.try_get::<f64, _>("signed_qty")?;
56        let quantity = row.try_get::<&str, _>("quantity").map(Quantity::from)?;
57        let peak_qty = row.try_get::<&str, _>("peak_qty").map(Quantity::from)?;
58        let quote_currency = row
59            .try_get::<&str, _>("quote_currency")
60            .map(Currency::from)?;
61        let base_currency = row
62            .try_get::<Option<&str>, _>("base_currency")
63            .ok()
64            .and_then(|x| x.map(Currency::from));
65        let settlement_currency = row
66            .try_get::<&str, _>("settlement_currency")
67            .map(Currency::from)?;
68        let avg_px_open = row.try_get::<f64, _>("avg_px_open")?;
69        let avg_px_close = row.try_get::<Option<f64>, _>("avg_px_close")?;
70        let realized_return = row.try_get::<Option<f64>, _>("realized_return")?;
71        let realized_pnl = row.try_get::<&str, _>("realized_pnl").map(Money::from)?;
72        let unrealized_pnl = row
73            .try_get::<Option<&str>, _>("unrealized_pnl")
74            .ok()
75            .and_then(|x| x.map(Money::from));
76        let commissions = row
77            .try_get::<Option<Vec<String>>, _>("commissions")?
78            .map_or_else(Vec::new, |c| {
79                c.into_iter().map(|s| Money::from(&s)).collect()
80            });
81        let duration_ns: Option<u64> = row
82            .try_get::<Option<i64>, _>("duration_ns")?
83            .map(|value| value as u64);
84        let ts_opened = row.try_get::<String, _>("ts_opened").map(UnixNanos::from)?;
85        let ts_closed: Option<UnixNanos> = row
86            .try_get::<Option<String>, _>("ts_closed")?
87            .map(UnixNanos::from);
88        let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
89        let ts_last = row.try_get::<String, _>("ts_last").map(UnixNanos::from)?;
90
91        let snapshot = PositionSnapshot {
92            trader_id,
93            strategy_id,
94            instrument_id,
95            position_id: id,
96            account_id,
97            opening_order_id,
98            closing_order_id,
99            entry,
100            side,
101            signed_qty,
102            quantity,
103            peak_qty,
104            quote_currency,
105            base_currency,
106            settlement_currency,
107            avg_px_open,
108            avg_px_close,
109            realized_return,
110            realized_pnl: Some(realized_pnl), // TODO: Standardize
111            unrealized_pnl,
112            commissions,
113            duration_ns,
114            ts_opened,
115            ts_closed,
116            ts_last,
117            ts_init,
118        };
119
120        Ok(PositionSnapshotModel(snapshot))
121    }
122}