nomy-data-models 0.35.5

Data model definitions for Nomy wallet analysis data processing
Documentation
#![allow(clippy::too_many_arguments, unused_imports, non_camel_case_types)]
//! TradeMatch model definition.
//!
//! This file is generated automatically from the Python SQLAlchemy model.
//! Do not edit this file manually.

use serde::{Deserialize, Serialize};

// Standard imports that may be needed
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde_json::Value as JsonValue;
use uuid::Uuid;

// Additional imports specific to this model
// No additional imports needed

/// Model representing a realized PnL event from matching trades.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradeMatch {
    pub position_id: Uuid,
    pub opening_trade_id: Uuid,
    pub closing_trade_id: Uuid,
    pub matched_amount: Decimal,
    pub entry_price: Decimal,
    pub exit_price: Decimal,
    pub pnl: Decimal,
    pub pnl_usd: Decimal,
    pub roi: Option<Decimal>,
    pub holding_duration: Option<chrono::Duration>,
    pub match_created_at: DateTime<Utc>,
    pub id: Uuid,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub created_by: Option<String>,
    pub updated_by: Option<String>,
}

impl TradeMatch {
    /// Create a new TradeMatch.
    pub fn new(
        position_id: Uuid,
        opening_trade_id: Uuid,
        closing_trade_id: Uuid,
        matched_amount: Decimal,
        entry_price: Decimal,
        exit_price: Decimal,
        pnl: Decimal,
        pnl_usd: Decimal,
        roi: Decimal,
        holding_duration: chrono::Duration,
        match_created_at: DateTime<Utc>,
        id: Uuid,
        created_at: DateTime<Utc>,
        updated_at: DateTime<Utc>,
        created_by: String,
        updated_by: String,
    ) -> Self {
        Self {
            position_id,
            opening_trade_id,
            closing_trade_id,
            matched_amount,
            entry_price,
            exit_price,
            pnl,
            pnl_usd,
            roi: Some(roi),
            holding_duration: Some(holding_duration),
            match_created_at,
            id,
            created_at,
            updated_at,
            created_by: Some(created_by),
            updated_by: Some(updated_by),
        }
    }

    /// Convert to a JSON string.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Convert from a JSON string.
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }

    /// Convert to a dictionary-like structure.
    pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
        let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
        if let serde_json::Value::Object(map) = json {
            map
        } else {
            serde_json::Map::new()
        }
    }
}