nautilus_model/events/position/closed.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------
use nautilus_core::{
nanos::{DurationNanos, UnixNanos},
UUID4,
};
use crate::{
enums::{OrderSide, PositionSide},
events::OrderFilled,
identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TraderId},
position::Position,
types::{Currency, Money, Price, Quantity},
};
/// Represents an event where a position has been closed.
#[repr(C)]
#[derive(Clone, PartialEq, Debug)]
pub struct PositionClosed {
/// The trader ID associated with the event.
pub trader_id: TraderId,
/// The strategy ID associated with the event.
pub strategy_id: StrategyId,
/// The instrument ID associated with the event.
pub instrument_id: InstrumentId,
/// The position ID associated with the event.
pub position_id: PositionId,
/// The account ID associated with the position.
pub account_id: AccountId,
/// The client order ID for the order which opened the position.
pub opening_order_id: ClientOrderId,
/// The client order ID for the order which closed the position.
pub closing_order_id: Option<ClientOrderId>,
/// The position entry order side.
pub entry: OrderSide,
/// The position side.
pub side: PositionSide,
/// The current signed quantity (positive for position side `LONG`, negative for `SHORT`).
pub signed_qty: f64,
/// The current open quantity.
pub quantity: Quantity,
/// The peak directional quantity reached by the position.
pub peak_quantity: Quantity,
/// The last fill quantity for the position.
pub last_qty: Quantity,
/// The last fill price for the position.
pub last_px: Price,
/// The position quote currency.
pub currency: Currency,
/// The average open price.
pub avg_px_open: f64,
/// The average closing price.
pub avg_px_close: Option<f64>,
/// The realized return for the position.
pub realized_return: f64,
/// The realized PnL for the position (including commissions).
pub realized_pnl: Option<Money>,
/// The unrealized PnL for the position (including commissions).
pub unrealized_pnl: Money,
/// The total open duration (nanoseconds).
pub duration: DurationNanos,
/// The unique identifier for the event.
pub event_id: UUID4,
/// UNIX timestamp (nanoseconds) when the position was opened.
pub ts_opened: UnixNanos,
/// UNIX timestamp (nanoseconds) when the position was closed.
pub ts_closed: Option<UnixNanos>,
/// UNIX timestamp (nanoseconds) when the event occurred.
pub ts_event: UnixNanos,
/// UNIX timestamp (nanoseconds) when the event was initialized.
pub ts_init: UnixNanos,
}
impl PositionClosed {
pub fn create(
position: &Position,
fill: &OrderFilled,
event_id: UUID4,
ts_init: UnixNanos,
) -> PositionClosed {
PositionClosed {
trader_id: position.trader_id,
strategy_id: position.strategy_id,
instrument_id: position.instrument_id,
position_id: position.id,
account_id: position.account_id,
opening_order_id: position.opening_order_id,
closing_order_id: position.closing_order_id,
entry: position.entry,
side: position.side,
signed_qty: position.signed_qty,
quantity: position.quantity,
peak_quantity: position.peak_qty,
last_qty: fill.last_qty,
last_px: fill.last_px,
currency: position.quote_currency,
avg_px_open: position.avg_px_open,
avg_px_close: position.avg_px_close,
realized_return: position.realized_return,
realized_pnl: position.realized_pnl,
unrealized_pnl: Money::new(0.0, position.quote_currency),
duration: position.duration_ns,
event_id,
ts_opened: position.ts_opened,
ts_closed: position.ts_closed,
ts_event: fill.ts_event,
ts_init,
}
}
}