nautilus_model/events/position/opened.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 nautilus_core::{UnixNanos, UUID4};
17
18use crate::{
19 enums::{OrderSide, PositionSide},
20 events::OrderFilled,
21 identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TraderId},
22 position::Position,
23 types::{Currency, Price, Quantity},
24};
25
26/// Represents an event where a position has been opened.
27#[repr(C)]
28#[derive(Clone, PartialEq, Debug)]
29pub struct PositionOpened {
30 /// The trader ID associated with the event.
31 pub trader_id: TraderId,
32 /// The strategy ID associated with the event.
33 pub strategy_id: StrategyId,
34 /// The instrument ID associated with the event.
35 pub instrument_id: InstrumentId,
36 /// The position ID associated with the event.
37 pub position_id: PositionId,
38 /// The account ID associated with the position.
39 pub account_id: AccountId,
40 /// The client order ID for the order which opened the position.
41 pub opening_order_id: ClientOrderId,
42 /// The position entry order side.
43 pub entry: OrderSide,
44 /// The position side.
45 pub side: PositionSide,
46 /// The current signed quantity (positive for position side `LONG`, negative for `SHORT`).
47 pub signed_qty: f64,
48 /// The current open quantity.
49 pub quantity: Quantity,
50 /// The last fill quantity for the position.
51 pub last_qty: Quantity,
52 /// The last fill price for the position.
53 pub last_px: Price,
54 /// The position quote currency.
55 pub currency: Currency,
56 /// The average open price.
57 pub avg_px_open: f64,
58 /// The unique identifier for the event.
59 pub event_id: UUID4,
60 /// UNIX timestamp (nanoseconds) when the event occurred.
61 pub ts_event: UnixNanos,
62 /// UNIX timestamp (nanoseconds) when the event was initialized.
63 pub ts_init: UnixNanos,
64}
65
66impl PositionOpened {
67 pub fn create(
68 position: &Position,
69 fill: &OrderFilled,
70 event_id: UUID4,
71 ts_init: UnixNanos,
72 ) -> PositionOpened {
73 PositionOpened {
74 trader_id: position.trader_id,
75 strategy_id: position.strategy_id,
76 instrument_id: position.instrument_id,
77 position_id: position.id,
78 account_id: position.account_id,
79 opening_order_id: position.opening_order_id,
80 entry: position.entry,
81 side: position.side,
82 signed_qty: position.signed_qty,
83 quantity: position.quantity,
84 last_qty: fill.last_qty,
85 last_px: fill.last_px,
86 currency: position.quote_currency,
87 avg_px_open: position.avg_px_open,
88 event_id,
89 ts_event: fill.ts_event,
90 ts_init,
91 }
92 }
93}