pub struct Trade {Show 15 fields
pub id: Uuid,
pub action: Action,
pub side: Side,
pub option_style: OptionStyle,
pub fee: Positive,
pub symbol: Option<String>,
pub strike: Positive,
pub expiry: DateTime<Utc>,
pub timestamp: i64,
pub quantity: Positive,
pub premium: Positive,
pub contract_size: Positive,
pub underlying_price: Positive,
pub notes: Option<String>,
pub status: TradeStatus,
}Expand description
Represents a trade with detailed information such as action, side, option style, associated fees, and various metadata.
§Contract size
A trade carries its own contract_size, so the record stays
self-contained once the position it came from is gone. premium is
quoted per unit of the underlying and fee per contract: the trade’s
premium is premium × contract_size × quantity and its fees
fee × quantity. Trade::cost, Trade::income and Trade::net
combine them with checked arithmetic and return a
TradeError::ArithmeticOverflow when the result leaves the
representable range.
Fields§
§id: Uuidid- A universally unique identifier (UUID) for the trade.
action: Actionaction- The action associated with the trade (e.g., Buy or Sell).
side: Sideside- Indicates whether the trade is on the Long or Short side.
option_style: OptionStyleSpecifies the style of an options contract Call or Put.
fee: Positivefee- The transaction fee associated with the trade, represented as a positive value.
symbol: Option<String>symbol- An optional ticker symbol for the trade (e.g., “AAPL” for Apple Inc.).
strike: Positivestrike- The strike price of the option, represented as a positive value (e.g., 180.0).
expiry: DateTime<Utc>expiry- The expiration date of the Option, represented as aDateTime<Utc>(e.g., 2025-06-20T00:00:00Z).
timestamp: i64timestamp- The trade execution time in nanoseconds since the Unix epoch (1970-01-01 00:00:00 UTC). Serialized using nanoseconds withserde.
quantity: Positivequantity- The number of contracts traded, represented as a positive value.
premium- The premium per unit of the underlying, as quoted. One contract payspremium × contract_size.
contract_size: Positivecontract_size- The contract multiplier: units of the underlying covered by one contract (100 for a standard US equity option). Defaults to 1, so a trade recorded before the field existed deserializes unchanged. The trade carries its own multiplier so the record stays self-contained after the position it came from is gone.
underlying_price: Positiveunderlying_price- The price of the underlying asset, represented as a positive value.
notes: Option<String>notes- Optional free-form notes associated with the trade.
status: TradeStatusRepresents the current status of a trade.
This status field indicates the state of a trade during its lifecycle.
It uses the TradeStatus enum to define the possible statuses.
This field is essential for tracking and managing the lifecycle of a trade.
Implementations§
Source§impl Trade
impl Trade
Sourcepub fn new(
id: Uuid,
action: Action,
side: Side,
option_style: OptionStyle,
fee: Positive,
symbol: Option<String>,
strike: Positive,
expiry: DateTime<Utc>,
quantity: Positive,
premium: Positive,
underlying_price: Positive,
notes: Option<String>,
status: TradeStatus,
) -> Result<Self, TradeError>
pub fn new( id: Uuid, action: Action, side: Side, option_style: OptionStyle, fee: Positive, symbol: Option<String>, strike: Positive, expiry: DateTime<Utc>, quantity: Positive, premium: Positive, underlying_price: Positive, notes: Option<String>, status: TradeStatus, ) -> Result<Self, TradeError>
Creates a new instance of the struct with the provided parameters.
§Parameters
id(uuid::Uuid): The unique identifier for the entity.action(Action): The action being performed (e.g., buy, sell).side(Side): The side of the transaction (e.g., long, short).option_style(OptionStyle): The style of the option (e.g., American, European).fee(Positive): The fee associated with the transaction. It must be a positive value.symbol(Option<String>): The symbol of the underlying asset, if applicable.strike(Positive): The strike price of the option. This must be a positive value.expiry(DateTime<Utc>): The expiration date and time of the option in UTC.quantity(Positive): The quantity involved in the transaction. This must be a positive value.premium(Positive): The premium per unit of the underlying.underlying_price(Positive): The price of the underlying asset. This must be a positive value.notes(Option<String>): Any additional notes or metadata for the transaction.
§Returns
An instance of the struct initialized with the provided parameters, along with a timestamp
(i64) in nanoseconds representing the moment of creation. The contract size is 1; chain
Trade::with_contract_size to record a larger multiplier.
§Errors
Returns TradeError::ArithmeticOverflow when the current time in nanoseconds since the
Unix epoch leaves the i64 range, after 2262, even when truncated to whole seconds.
§Remarks
- Ensure all
Positivevalues are validated and created using the appropriate constructors. - The
symbolandnotesparameters are optional and can be set toNoneif not applicable.
Sourcepub fn with_contract_size(self, contract_size: Positive) -> Self
pub fn with_contract_size(self, contract_size: Positive) -> Self
Returns the trade with its contract multiplier set to contract_size.
Trade::new records a contract that covers one unit of the
underlying; chain this to record market contracts, for example
.with_contract_size(Positive::HUNDRED) for a standard US equity
option.
Sourcepub fn datetime(&self) -> DateTime<Utc>
pub fn datetime(&self) -> DateTime<Utc>
Convert back to DateTime<Utc> when you need it for pretty printing.
Sourcepub fn set_timestamp(
&mut self,
datetime: DateTime<Utc>,
) -> Result<(), TradeError>
pub fn set_timestamp( &mut self, datetime: DateTime<Utc>, ) -> Result<(), TradeError>
Sets the timestamp for the current instance using the provided DateTime<Utc> value.
§Parameters
datetime: ADateTime<Utc>object representing the new timestamp to be set.
§Note
If the nanosecond representation overflows, falls back to the whole seconds in nanoseconds.
§Errors
Returns TradeError::ArithmeticOverflow when datetime in nanoseconds since the Unix
epoch leaves the i64 range even when truncated to whole seconds (before 1677 or after
2262); the timestamp is left unchanged then.
Sourcepub fn cost(&self) -> Result<Positive, TradeError>
pub fn cost(&self) -> Result<Positive, TradeError>
Calculates the total cost associated with a transaction based on the action, side, fee, and premium.
§Returns
A Positive type, which represents the total cost of the transaction.
§Logic
- The total cost is determined by the transaction’s
action(Buy/Sell),side(Long/Short),fee, andpremium, all adjusted by thequantity. - The
feeis per contract and is multiplied by thequantity. Thepremiumis per unit of the underlying and is multiplied bycontract_size × quantity. - Depending on the combination of
actionandside, the following rules are applied:(Action::Buy, Side::Long)or(Action::Sell, Side::Short): The cost includes bothfeesandpremium.(Action::Buy, Side::Short)or(Action::Sell, Side::Long): The cost includes onlyfees.
§Errors
Returns TradeError::ArithmeticOverflow when the fees, the premium
or their sum leaves the Positive range. Every field is a public
Positive, so the products can overflow even though each value is
valid on its own.
Sourcepub fn income(&self) -> Result<Positive, TradeError>
pub fn income(&self) -> Result<Positive, TradeError>
Computes the income generated based on the action and side of a trade.
§Description
This function calculates the income by determining the premium associated
with the current object’s quantity, contract_size and premium values. The resulting
income depends on the combination of the action and side values:
- If the action is
Buyand the side isLong, or if the action isSelland the side isShort, the income is0. - If the action is
Buyand the side isShort, or if the action isSelland the side isLong, the function returns the computedpremium.
§Returns
Positive::ZERO: If theactionandsidecombination does not result in an income.- A
Positivevalue (equal to the computedpremium): For combinations where income is generated.
§Errors
Returns TradeError::ArithmeticOverflow when premium × contract_size × quantity leaves the Positive range.
§Dependencies
This method relies on:
Actionenum (expected values:Buy,Sell)Sideenum (expected values:Long,Short)Positivetype for representing non-negative values.
Sourcepub fn net(&self) -> Result<Decimal, TradeError>
pub fn net(&self) -> Result<Decimal, TradeError>
Calculates the net value by subtracting the cost from the income.
§Returns
A Decimal value representing the net result of income minus cost.
§Behavior
- The
income()method is called to obtain the income value, which is then converted to aDecimalusingto_dec(). - The
cost()method is called to obtain the cost value, which is also converted to aDecimalusingto_dec(). - The net value is computed as:
income.to_dec() - cost.to_dec().
§Errors
Propagates Trade::cost and Trade::income, and returns
TradeError::ArithmeticOverflow when the difference leaves the
Decimal range.
Sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
Checks if the current trade status is Open.
§Returns
true- If the trade’s status isOpen.false- If the trade’s status is notOpen.
This method is commonly used to determine whether a trade is still active and can be interacted with.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Determines whether the trade is closed.
This method checks the status of a trade and returns true
if the trade’s status is TradeStatus::Closed, otherwise returns false.
§Returns
bool-trueif the trade’s status isTradeStatus::Closed,falseotherwise.
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Checks if the trade has expired.
This method compares the current status of the trade with TradeStatus::Expired
and returns true if the trade is expired, otherwise false.
§Returns
true- If the trade’s status isTradeStatus::Expired.false- If the trade’s status is notTradeStatus::Expired.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Trade
impl<'de> Deserialize<'de> for Trade
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Trade
impl StructuralPartialEq for Trade
Source§impl TradeAble for Trade
impl TradeAble for Trade
Auto Trait Implementations§
impl Freeze for Trade
impl RefUnwindSafe for Trade
impl Send for Trade
impl Sync for Trade
impl Unpin for Trade
impl UnsafeUnpin for Trade
impl UnwindSafe for Trade
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.