nautilus_infrastructure/sql/
queries.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::collections::HashMap;
17
18use nautilus_common::{custom::CustomData, signal::Signal};
19use nautilus_model::{
20    accounts::{any::AccountAny, base::Account},
21    data::{Bar, DataType, QuoteTick, TradeTick},
22    events::{
23        AccountState, OrderEvent, OrderEventAny, OrderSnapshot,
24        position::snapshot::PositionSnapshot,
25    },
26    identifiers::{AccountId, ClientId, ClientOrderId, InstrumentId, PositionId},
27    instruments::{Instrument, InstrumentAny},
28    orders::{Order, OrderAny},
29    types::{AccountBalance, Currency, MarginBalance},
30};
31use sqlx::{PgPool, Row};
32
33use super::models::{
34    orders::OrderSnapshotModel,
35    positions::PositionSnapshotModel,
36    types::{CustomDataModel, SignalModel},
37};
38use crate::sql::models::{
39    accounts::AccountEventModel,
40    data::{BarModel, QuoteTickModel, TradeTickModel},
41    enums::{
42        AggregationSourceModel, AggressorSideModel, AssetClassModel, BarAggregationModel,
43        CurrencyTypeModel, PriceTypeModel, TrailingOffsetTypeModel,
44    },
45    general::{GeneralRow, OrderEventOrderClientIdCombination},
46    instruments::InstrumentAnyModel,
47    orders::OrderEventAnyModel,
48    types::CurrencyModel,
49};
50
51#[derive(Debug)]
52pub struct DatabaseQueries;
53
54impl DatabaseQueries {
55    pub async fn truncate(pool: &PgPool) -> anyhow::Result<()> {
56        sqlx::query("SELECT truncate_all_tables()")
57            .execute(pool)
58            .await
59            .map(|_| ())
60            .map_err(|e| anyhow::anyhow!("Failed to truncate tables: {e}"))
61    }
62
63    pub async fn add(pool: &PgPool, key: String, value: Vec<u8>) -> anyhow::Result<()> {
64        sqlx::query("INSERT INTO general (id, value) VALUES ($1, $2)")
65            .bind(key)
66            .bind(value)
67            .execute(pool)
68            .await
69            .map(|_| ())
70            .map_err(|e| anyhow::anyhow!("Failed to insert into general table: {e}"))
71    }
72
73    pub async fn load(pool: &PgPool) -> anyhow::Result<HashMap<String, Vec<u8>>> {
74        sqlx::query_as::<_, GeneralRow>("SELECT * FROM general")
75            .fetch_all(pool)
76            .await
77            .map(|rows| {
78                let mut cache: HashMap<String, Vec<u8>> = HashMap::new();
79                for row in rows {
80                    cache.insert(row.id, row.value);
81                }
82                cache
83            })
84            .map_err(|e| anyhow::anyhow!("Failed to load general table: {e}"))
85    }
86
87    pub async fn add_currency(pool: &PgPool, currency: Currency) -> anyhow::Result<()> {
88        sqlx::query(
89            "INSERT INTO currency (id, precision, iso4217, name, currency_type) VALUES ($1, $2, $3, $4, $5::currency_type) ON CONFLICT (id) DO NOTHING"
90        )
91            .bind(currency.code.as_str())
92            .bind(currency.precision as i32)
93            .bind(currency.iso4217 as i32)
94            .bind(currency.name.as_str())
95            .bind(CurrencyTypeModel(currency.currency_type))
96            .execute(pool)
97            .await
98            .map(|_| ())
99            .map_err(|e| anyhow::anyhow!("Failed to insert into currency table: {e}"))
100    }
101
102    pub async fn load_currencies(pool: &PgPool) -> anyhow::Result<Vec<Currency>> {
103        sqlx::query_as::<_, CurrencyModel>("SELECT * FROM currency ORDER BY id ASC")
104            .fetch_all(pool)
105            .await
106            .map(|rows| rows.into_iter().map(|row| row.0).collect())
107            .map_err(|e| anyhow::anyhow!("Failed to load currencies: {e}"))
108    }
109
110    pub async fn load_currency(pool: &PgPool, code: &str) -> anyhow::Result<Option<Currency>> {
111        sqlx::query_as::<_, CurrencyModel>("SELECT * FROM currency WHERE id = $1")
112            .bind(code)
113            .fetch_optional(pool)
114            .await
115            .map(|currency| currency.map(|row| row.0))
116            .map_err(|e| anyhow::anyhow!("Failed to load currency: {e}"))
117    }
118
119    pub async fn add_instrument(
120        pool: &PgPool,
121        kind: &str,
122        instrument: Box<dyn Instrument>,
123    ) -> anyhow::Result<()> {
124        sqlx::query(r#"
125            INSERT INTO "instrument" (
126                id, kind, raw_symbol, base_currency, underlying, quote_currency, settlement_currency, isin, asset_class, exchange,
127                multiplier, option_kind, is_inverse, strike_price, activation_ns, expiration_ns, price_precision, size_precision,
128                price_increment, size_increment, maker_fee, taker_fee, margin_init, margin_maint, lot_size, max_quantity, min_quantity, max_notional,
129                min_notional, max_price, min_price, ts_init, ts_event, created_at, updated_at
130            ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::asset_class, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
131            ON CONFLICT (id)
132            DO UPDATE
133            SET
134                kind = $2, raw_symbol = $3, base_currency= $4, underlying = $5, quote_currency = $6, settlement_currency = $7, isin = $8, asset_class = $9, exchange = $10,
135                 multiplier = $11, option_kind = $12, is_inverse = $13, strike_price = $14, activation_ns = $15, expiration_ns = $16 , price_precision = $17, size_precision = $18,
136                 price_increment = $19, size_increment = $20, maker_fee = $21, taker_fee = $22, margin_init = $23, margin_maint = $24, lot_size = $25, max_quantity = $26,
137                 min_quantity = $27, max_notional = $28, min_notional = $29, max_price = $30, min_price = $31, ts_init = $32,  ts_event = $33, updated_at = CURRENT_TIMESTAMP
138            "#)
139            .bind(instrument.id().to_string())
140            .bind(kind)
141            .bind(instrument.raw_symbol().to_string())
142            .bind(instrument.base_currency().map(|x| x.code.as_str()))
143            .bind(instrument.underlying().map(|x| x.to_string()))
144            .bind(instrument.quote_currency().code.as_str())
145            .bind(instrument.settlement_currency().code.as_str())
146            .bind(instrument.isin().map(|x| x.to_string()))
147            .bind(AssetClassModel(instrument.asset_class()))
148            .bind(instrument.exchange().map(|x| x.to_string()))
149            .bind(instrument.multiplier().to_string())
150            .bind(instrument.option_kind().map(|x| x.to_string()))
151            .bind(instrument.is_inverse())
152            .bind(instrument.strike_price().map(|x| x.to_string()))
153            .bind(instrument.activation_ns().map(|x| x.to_string()))
154            .bind(instrument.expiration_ns().map(|x| x.to_string()))
155            .bind(instrument.price_precision() as i32)
156            .bind(instrument.size_precision() as i32)
157            .bind(instrument.price_increment().to_string())
158            .bind(instrument.size_increment().to_string())
159            .bind(instrument.maker_fee().to_string())
160            .bind(instrument.taker_fee().to_string())
161            .bind(instrument.margin_init().to_string())
162            .bind(instrument.margin_maint().to_string())
163            .bind(instrument.lot_size().map(|x| x.to_string()))
164            .bind(instrument.max_quantity().map(|x| x.to_string()))
165            .bind(instrument.min_quantity().map(|x| x.to_string()))
166            .bind(instrument.max_notional().map(|x| x.to_string()))
167            .bind(instrument.min_notional().map(|x| x.to_string()))
168            .bind(instrument.max_price().map(|x| x.to_string()))
169            .bind(instrument.min_price().map(|x| x.to_string()))
170            .bind(instrument.ts_init().to_string())
171            .bind(instrument.ts_event().to_string())
172            .execute(pool)
173            .await
174            .map(|_| ())
175            .map_err(|e| anyhow::anyhow!(format!("Failed to insert item {} into instrument table: {:?}", instrument.id().to_string(), e)))
176    }
177
178    pub async fn load_instrument(
179        pool: &PgPool,
180        instrument_id: &InstrumentId,
181    ) -> anyhow::Result<Option<InstrumentAny>> {
182        sqlx::query_as::<_, InstrumentAnyModel>("SELECT * FROM instrument WHERE id = $1")
183            .bind(instrument_id.to_string())
184            .fetch_optional(pool)
185            .await
186            .map(|instrument| instrument.map(|row| row.0))
187            .map_err(|e| {
188                anyhow::anyhow!("Failed to load instrument with id {instrument_id},error is: {e}")
189            })
190    }
191
192    pub async fn load_instruments(pool: &PgPool) -> anyhow::Result<Vec<InstrumentAny>> {
193        sqlx::query_as::<_, InstrumentAnyModel>("SELECT * FROM instrument")
194            .fetch_all(pool)
195            .await
196            .map(|rows| rows.into_iter().map(|row| row.0).collect())
197            .map_err(|e| anyhow::anyhow!("Failed to load instruments: {e}"))
198    }
199
200    pub async fn add_order(
201        pool: &PgPool,
202        _kind: &str,
203        updated: bool,
204        order: Box<dyn Order>,
205        client_id: Option<ClientId>,
206    ) -> anyhow::Result<()> {
207        if updated {
208            let exists =
209                DatabaseQueries::check_if_order_initialized_exists(pool, order.client_order_id())
210                    .await
211                    .unwrap();
212            if !exists {
213                panic!(
214                    "OrderInitialized event does not exist for order: {}",
215                    order.client_order_id()
216                );
217            }
218        }
219        match order.last_event().clone() {
220            OrderEventAny::Accepted(event) => {
221                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
222            }
223            OrderEventAny::CancelRejected(event) => {
224                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
225            }
226            OrderEventAny::Canceled(event) => {
227                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
228            }
229            OrderEventAny::Denied(event) => {
230                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
231            }
232            OrderEventAny::Emulated(event) => {
233                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
234            }
235            OrderEventAny::Expired(event) => {
236                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
237            }
238            OrderEventAny::Filled(event) => {
239                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
240            }
241            OrderEventAny::Initialized(event) => {
242                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
243            }
244            OrderEventAny::ModifyRejected(event) => {
245                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
246            }
247            OrderEventAny::PendingCancel(event) => {
248                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
249            }
250            OrderEventAny::PendingUpdate(event) => {
251                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
252            }
253            OrderEventAny::Rejected(event) => {
254                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
255            }
256            OrderEventAny::Released(event) => {
257                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
258            }
259            OrderEventAny::Submitted(event) => {
260                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
261            }
262            OrderEventAny::Updated(event) => {
263                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
264            }
265            OrderEventAny::Triggered(event) => {
266                DatabaseQueries::add_order_event(pool, Box::new(event), client_id).await
267            }
268        }
269    }
270
271    pub async fn add_order_snapshot(pool: &PgPool, snapshot: OrderSnapshot) -> anyhow::Result<()> {
272        let mut transaction = pool.begin().await?;
273
274        // Insert trader if it does not exist
275        // TODO remove this when node and trader initialization is implemented
276        sqlx::query(
277            r#"
278            INSERT INTO "trader" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING
279            "#,
280        )
281        .bind(snapshot.trader_id.to_string())
282        .execute(&mut *transaction)
283        .await
284        .map(|_| ())
285        .map_err(|e| anyhow::anyhow!("Failed to insert into trader table: {e}"))?;
286
287        sqlx::query(
288            r#"
289            INSERT INTO "order" (
290                id, trader_id, strategy_id, instrument_id, client_order_id, venue_order_id, position_id,
291                account_id, last_trade_id, order_type, order_side, quantity, price, trigger_price,
292                trigger_type, limit_offset, trailing_offset, trailing_offset_type, time_in_force,
293                expire_time, filled_qty, liquidity_side, avg_px, slippage, commissions, status,
294                is_post_only, is_reduce_only, is_quote_quantity, display_qty, emulation_trigger,
295                trigger_instrument_id, contingency_type, order_list_id, linked_order_ids,
296                parent_order_id, exec_algorithm_id, exec_algorithm_params, exec_spawn_id, tags, init_id, ts_init, ts_last,
297                created_at, updated_at
298            ) VALUES (
299                $1, $2, $3, $4, $1, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16,
300                $17::TRAILING_OFFSET_TYPE, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28,
301                $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42,
302                CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
303            )
304            ON CONFLICT (id)
305            DO UPDATE SET
306                trader_id = $2,
307                strategy_id = $3,
308                instrument_id = $4,
309                venue_order_id = $5,
310                position_id = $6,
311                account_id = $7,
312                last_trade_id = $8,
313                order_type = $9,
314                order_side = $10,
315                quantity = $11,
316                price = $12,
317                trigger_price = $13,
318                trigger_type = $14,
319                limit_offset = $15,
320                trailing_offset = $16,
321                trailing_offset_type = $17::TRAILING_OFFSET_TYPE,
322                time_in_force = $18,
323                expire_time = $19,
324                filled_qty = $20,
325                liquidity_side = $21,
326                avg_px = $22,
327                slippage = $23,
328                commissions = $24,
329                status = $25,
330                is_post_only = $26,
331                is_reduce_only = $27,
332                is_quote_quantity = $28,
333                display_qty = $29,
334                emulation_trigger = $30,
335                trigger_instrument_id = $31,
336                contingency_type = $32,
337                order_list_id = $33,
338                linked_order_ids = $34,
339                parent_order_id = $35,
340                exec_algorithm_id = $36,
341                exec_algorithm_params = $37,
342                exec_spawn_id = $38,
343                tags = $39,
344                init_id = $40,
345                ts_init = $41,
346                ts_last = $42,
347                updated_at = CURRENT_TIMESTAMP
348        "#)
349            .bind(snapshot.client_order_id.to_string())  // Used for both id and client_order_id
350            .bind(snapshot.trader_id.to_string())
351            .bind(snapshot.strategy_id.to_string())
352            .bind(snapshot.instrument_id.to_string())
353            .bind(snapshot.venue_order_id.map(|x| x.to_string()))
354            .bind(snapshot.position_id.map(|x| x.to_string()))
355            .bind(snapshot.account_id.map(|x| x.to_string()))
356            .bind(snapshot.last_trade_id.map(|x| x.to_string()))
357            .bind(snapshot.order_type.to_string())
358            .bind(snapshot.order_side.to_string())
359            .bind(snapshot.quantity.to_string())
360            .bind(snapshot.price.map(|x| x.to_string()))
361            .bind(snapshot.trigger_price.map(|x| x.to_string()))
362            .bind(snapshot.trigger_type.map(|x| x.to_string()))
363            .bind(snapshot.limit_offset.map(|x| x.to_string()))
364            .bind(snapshot.trailing_offset.map(|x| x.to_string()))
365            .bind(snapshot.trailing_offset_type.map(|x| x.to_string()))
366            .bind(snapshot.time_in_force.to_string())
367            .bind(snapshot.expire_time.map(|x| x.to_string()))
368            .bind(snapshot.filled_qty.to_string())
369            .bind(snapshot.liquidity_side.map(|x| x.to_string()))
370            .bind(snapshot.avg_px)
371            .bind(snapshot.slippage)
372            .bind(snapshot.commissions.iter().map(|x| x.to_string()).collect::<Vec<String>>())
373            .bind(snapshot.status.to_string())
374            .bind(snapshot.is_post_only)
375            .bind(snapshot.is_reduce_only)
376            .bind(snapshot.is_quote_quantity)
377            .bind(snapshot.display_qty.map(|x| x.to_string()))
378            .bind(snapshot.emulation_trigger.map(|x| x.to_string()))
379            .bind(snapshot.trigger_instrument_id.map(|x| x.to_string()))
380            .bind(snapshot.contingency_type.map(|x| x.to_string()))
381            .bind(snapshot.order_list_id.map(|x| x.to_string()))
382            .bind(snapshot.linked_order_ids.map(|x| x.iter().map(|x| x.to_string()).collect::<Vec<String>>()))
383            .bind(snapshot.parent_order_id.map(|x| x.to_string()))
384            .bind(snapshot.exec_algorithm_id.map(|x| x.to_string()))
385            .bind(snapshot.exec_algorithm_params.map(|x| serde_json::to_value(x).unwrap()))
386            .bind(snapshot.exec_spawn_id.map(|x| x.to_string()))
387            .bind(snapshot.tags.map(|x| x.iter().map(|x| x.to_string()).collect::<Vec<String>>()))
388            .bind(snapshot.init_id.to_string())
389            .bind(snapshot.ts_init.to_string())
390            .bind(snapshot.ts_last.to_string())
391            .execute(&mut *transaction)
392            .await
393            .map(|_| ())
394            .map_err(|e| anyhow::anyhow!("Failed to insert into order table: {e}"))?;
395
396        transaction
397            .commit()
398            .await
399            .map_err(|e| anyhow::anyhow!("Failed to commit transaction: {e}"))
400    }
401
402    pub async fn load_order_snapshot(
403        pool: &PgPool,
404        client_order_id: &ClientOrderId,
405    ) -> anyhow::Result<Option<OrderSnapshot>> {
406        sqlx::query_as::<_, OrderSnapshotModel>(
407            r#"SELECT * FROM "order" WHERE client_order_id = $1"#,
408        )
409        .bind(client_order_id.to_string())
410        .fetch_optional(pool)
411        .await
412        .map(|model| model.map(|m| m.0))
413        .map_err(|e| anyhow::anyhow!("Failed to load order snapshot: {e}"))
414    }
415
416    pub async fn add_position_snapshot(
417        pool: &PgPool,
418        snapshot: PositionSnapshot,
419    ) -> anyhow::Result<()> {
420        let mut transaction = pool.begin().await?;
421
422        // Insert trader if it does not exist
423        // TODO remove this when node and trader initialization is implemented
424        sqlx::query(
425            r#"
426            INSERT INTO "trader" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING
427        "#,
428        )
429        .bind(snapshot.trader_id.to_string())
430        .execute(&mut *transaction)
431        .await
432        .map(|_| ())
433        .map_err(|e| anyhow::anyhow!("Failed to insert into trader table: {e}"))?;
434
435        sqlx::query(r#"
436            INSERT INTO "position" (
437                id, trader_id, strategy_id, instrument_id, account_id, opening_order_id, closing_order_id, entry, side, signed_qty, quantity, peak_qty,
438                quote_currency, base_currency, settlement_currency, avg_px_open, avg_px_close, realized_return, realized_pnl, unrealized_pnl, commissions,
439                duration_ns, ts_opened, ts_closed, ts_init, ts_last, created_at, updated_at
440            ) VALUES (
441                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20,
442                $21, $22, $23, $24, $25, $26, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
443            )
444            ON CONFLICT (id)
445            DO UPDATE
446            SET
447                trader_id = $2, strategy_id = $3, instrument_id = $4, account_id = $5, opening_order_id = $6, closing_order_id = $7, entry = $8, side = $9, signed_qty = $10, quantity = $11,
448                peak_qty = $12, quote_currency = $13, base_currency = $14, settlement_currency = $15, avg_px_open = $16, avg_px_close = $17, realized_return = $18, realized_pnl = $19, unrealized_pnl = $20,
449                commissions = $21, duration_ns = $22, ts_opened = $23, ts_closed = $24, ts_init = $25, ts_last = $26, updated_at = CURRENT_TIMESTAMP
450        "#)
451            .bind(snapshot.position_id.to_string())
452            .bind(snapshot.trader_id.to_string())
453            .bind(snapshot.strategy_id.to_string())
454            .bind(snapshot.instrument_id.to_string())
455            .bind(snapshot.account_id.to_string())
456            .bind(snapshot.opening_order_id.to_string())
457            .bind(snapshot.closing_order_id.map(|x| x.to_string()))
458            .bind(snapshot.entry.to_string())
459            .bind(snapshot.side.to_string())
460            .bind(snapshot.signed_qty)
461            .bind(snapshot.quantity.to_string())
462            .bind(snapshot.peak_qty.to_string())
463            .bind(snapshot.quote_currency.to_string())
464            .bind(snapshot.base_currency.map(|x| x.to_string()))
465            .bind(snapshot.settlement_currency.to_string())
466            .bind(snapshot.avg_px_open)
467            .bind(snapshot.avg_px_close)
468            .bind(snapshot.realized_return)
469            .bind(snapshot.realized_pnl.map(|x| x.to_string()))
470            .bind(snapshot.unrealized_pnl.map(|x| x.to_string()))
471            .bind(snapshot.commissions.iter().map(|x| x.to_string()).collect::<Vec<String>>())
472            .bind(snapshot.duration_ns.map(|x| x.to_string()))
473            .bind(snapshot.ts_opened.to_string())
474            .bind(snapshot.ts_closed.map(|x| x.to_string()))
475            .bind(snapshot.ts_init.to_string())
476            .bind(snapshot.ts_last.to_string())
477            .execute(&mut *transaction)
478            .await
479            .map(|_| ())
480            .map_err(|e| anyhow::anyhow!("Failed to insert into position table: {e}"))?;
481        transaction
482            .commit()
483            .await
484            .map_err(|e| anyhow::anyhow!("Failed to commit transaction: {e}"))
485    }
486
487    pub async fn load_position_snapshot(
488        pool: &PgPool,
489        position_id: &PositionId,
490    ) -> anyhow::Result<Option<PositionSnapshot>> {
491        sqlx::query_as::<_, PositionSnapshotModel>(r#"SELECT * FROM "position" WHERE id = $1"#)
492            .bind(position_id.to_string())
493            .fetch_optional(pool)
494            .await
495            .map(|model| model.map(|m| m.0))
496            .map_err(|e| anyhow::anyhow!("Failed to load position snapshot: {e}"))
497    }
498
499    pub async fn check_if_order_initialized_exists(
500        pool: &PgPool,
501        client_order_id: ClientOrderId,
502    ) -> anyhow::Result<bool> {
503        sqlx::query(r#"
504            SELECT EXISTS(SELECT 1 FROM "order_event" WHERE client_order_id = $1 AND kind = 'OrderInitialized')
505        "#)
506            .bind(client_order_id.to_string())
507            .fetch_one(pool)
508            .await
509            .map(|row| row.get(0))
510            .map_err(|e| anyhow::anyhow!("Failed to check if order initialized exists: {e}"))
511    }
512
513    pub async fn check_if_account_event_exists(
514        pool: &PgPool,
515        account_id: AccountId,
516    ) -> anyhow::Result<bool> {
517        sqlx::query(
518            r#"
519            SELECT EXISTS(SELECT 1 FROM "account_event" WHERE account_id = $1)
520        "#,
521        )
522        .bind(account_id.to_string())
523        .fetch_one(pool)
524        .await
525        .map(|row| row.get(0))
526        .map_err(|e| anyhow::anyhow!("Failed to check if account event exists: {e}"))
527    }
528
529    pub async fn add_order_event(
530        pool: &PgPool,
531        order_event: Box<dyn OrderEvent>,
532        client_id: Option<ClientId>,
533    ) -> anyhow::Result<()> {
534        let mut transaction = pool.begin().await?;
535
536        // Insert trader if it does not exist
537        // TODO remove this when node and trader initialization is implemented
538        sqlx::query(
539            r#"
540            INSERT INTO "trader" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING
541        "#,
542        )
543        .bind(order_event.trader_id().to_string())
544        .execute(&mut *transaction)
545        .await
546        .map(|_| ())
547        .map_err(|e| anyhow::anyhow!("Failed to insert into trader table: {e}"))?;
548
549        // Insert client if it does not exist
550        // TODO remove this when client initialization is implemented
551        if let Some(client_id) = client_id {
552            sqlx::query(
553                r#"
554                INSERT INTO "client" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING
555            "#,
556            )
557            .bind(client_id.to_string())
558            .execute(&mut *transaction)
559            .await
560            .map(|_| ())
561            .map_err(|e| anyhow::anyhow!("Failed to insert into client table: {e}"))?;
562        }
563
564        sqlx::query(r#"
565            INSERT INTO "order_event" (
566                id, kind, client_order_id, order_type, order_side, trader_id, client_id, strategy_id, instrument_id, trade_id, currency, quantity, time_in_force, liquidity_side,
567                post_only, reduce_only, quote_quantity, reconciliation, price, last_px, last_qty, trigger_price, trigger_type, limit_offset, trailing_offset,
568                trailing_offset_type, expire_time, display_qty, emulation_trigger, trigger_instrument_id, contingency_type,
569                order_list_id, linked_order_ids, parent_order_id,
570                exec_algorithm_id, exec_spawn_id, venue_order_id, account_id, position_id, commission, ts_event, ts_init, created_at, updated_at
571            ) VALUES (
572                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20,
573                $21, $22, $23, $24, $25::trailing_offset_type, $26, $27, $28, $29, $30, $31, $32, $33, $34,
574                $35, $36, $37, $38, $39, $40, $41, $42,  CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
575            )
576            ON CONFLICT (id)
577            DO UPDATE
578            SET
579                kind = $2, client_order_id = $3, order_type = $4, order_side=$5, trader_id = $6, client_id = $7, strategy_id = $8, instrument_id = $9, trade_id = $10, currency = $11,
580                quantity = $12, time_in_force = $13, liquidity_side = $14, post_only = $15, reduce_only = $16, quote_quantity = $17, reconciliation = $18, price = $19, last_px = $20,
581                last_qty = $21, trigger_price = $22, trigger_type = $23, limit_offset = $24, trailing_offset = $25, trailing_offset_type = $26, expire_time = $27, display_qty = $28,
582                emulation_trigger = $29, trigger_instrument_id = $30, contingency_type = $31, order_list_id = $32, linked_order_ids = $33, parent_order_id = $34, exec_algorithm_id = $35,
583                exec_spawn_id = $36, venue_order_id = $37, account_id = $38, position_id = $39, commission = $40, ts_event = $41, ts_init = $42, updated_at = CURRENT_TIMESTAMP
584
585        "#)
586            .bind(order_event.id().to_string())
587            .bind(order_event.kind())
588            .bind(order_event.client_order_id().to_string())
589            .bind(order_event.order_type().map(|x| x.to_string()))
590            .bind(order_event.order_side().map(|x| x.to_string()))
591            .bind(order_event.trader_id().to_string())
592            .bind(client_id.map(|x| x.to_string()))
593            .bind(order_event.strategy_id().to_string())
594            .bind(order_event.instrument_id().to_string())
595            .bind(order_event.trade_id().map(|x| x.to_string()))
596            .bind(order_event.currency().map(|x| x.code.as_str()))
597            .bind(order_event.quantity().map(|x| x.to_string()))
598            .bind(order_event.time_in_force().map(|x| x.to_string()))
599            .bind(order_event.liquidity_side().map(|x| x.to_string()))
600            .bind(order_event.post_only())
601            .bind(order_event.reduce_only())
602            .bind(order_event.quote_quantity())
603            .bind(order_event.reconciliation())
604            .bind(order_event.price().map(|x| x.to_string()))
605            .bind(order_event.last_px().map(|x| x.to_string()))
606            .bind(order_event.last_qty().map(|x| x.to_string()))
607            .bind(order_event.trigger_price().map(|x| x.to_string()))
608            .bind(order_event.trigger_type().map(|x| x.to_string()))
609            .bind(order_event.limit_offset().map(|x| x.to_string()))
610            .bind(order_event.trailing_offset().map(|x| x.to_string()))
611            .bind(order_event.trailing_offset_type().map(TrailingOffsetTypeModel))
612            .bind(order_event.expire_time().map(|x| x.to_string()))
613            .bind(order_event.display_qty().map(|x| x.to_string()))
614            .bind(order_event.emulation_trigger().map(|x| x.to_string()))
615            .bind(order_event.trigger_instrument_id().map(|x| x.to_string()))
616            .bind(order_event.contingency_type().map(|x| x.to_string()))
617            .bind(order_event.order_list_id().map(|x| x.to_string()))
618            .bind(order_event.linked_order_ids().map(|x| x.iter().map(|x| x.to_string()).collect::<Vec<String>>()))
619            .bind(order_event.parent_order_id().map(|x| x.to_string()))
620            .bind(order_event.exec_algorithm_id().map(|x| x.to_string()))
621            .bind(order_event.exec_spawn_id().map(|x| x.to_string()))
622            .bind(order_event.venue_order_id().map(|x| x.to_string()))
623            .bind(order_event.account_id().map(|x| x.to_string()))
624            .bind(order_event.position_id().map(|x| x.to_string()))
625            .bind(order_event.commission().map(|x| x.to_string()))
626            .bind(order_event.ts_event().to_string())
627            .bind(order_event.ts_init().to_string())
628            .execute(&mut *transaction)
629            .await
630            .map(|_| ())
631            .map_err(|e| anyhow::anyhow!("Failed to insert into order_event table: {e}"))?;
632        transaction
633            .commit()
634            .await
635            .map_err(|e| anyhow::anyhow!("Failed to commit transaction: {e}"))
636    }
637
638    pub async fn load_order_events(
639        pool: &PgPool,
640        client_order_id: &ClientOrderId,
641    ) -> anyhow::Result<Vec<OrderEventAny>> {
642        sqlx::query_as::<_, OrderEventAnyModel>(r#"SELECT * FROM "order_event" event WHERE event.client_order_id = $1 ORDER BY created_at ASC"#)
643        .bind(client_order_id.to_string())
644        .fetch_all(pool)
645        .await
646        .map(|rows| rows.into_iter().map(|row| row.0).collect())
647        .map_err(|e| anyhow::anyhow!("Failed to load order events: {e}"))
648    }
649
650    pub async fn load_order(
651        pool: &PgPool,
652        client_order_id: &ClientOrderId,
653    ) -> anyhow::Result<Option<OrderAny>> {
654        let order_events = DatabaseQueries::load_order_events(pool, client_order_id).await;
655
656        match order_events {
657            Ok(order_events) => {
658                if order_events.is_empty() {
659                    return Ok(None);
660                }
661                let order = OrderAny::from_events(order_events).unwrap();
662                Ok(Some(order))
663            }
664            Err(e) => anyhow::bail!("Failed to load order events: {e}"),
665        }
666    }
667
668    pub async fn load_orders(pool: &PgPool) -> anyhow::Result<Vec<OrderAny>> {
669        let mut orders: Vec<OrderAny> = Vec::new();
670        let client_order_ids: Vec<ClientOrderId> = sqlx::query(
671            r#"
672            SELECT DISTINCT client_order_id FROM "order_event"
673        "#,
674        )
675        .fetch_all(pool)
676        .await
677        .map(|rows| {
678            rows.into_iter()
679                .map(|row| ClientOrderId::from(row.get::<&str, _>(0)))
680                .collect()
681        })
682        .map_err(|e| anyhow::anyhow!("Failed to load order ids: {e}"))?;
683        for id in client_order_ids {
684            let order = DatabaseQueries::load_order(pool, &id).await.unwrap();
685            match order {
686                Some(order) => {
687                    orders.push(order);
688                }
689                None => {
690                    continue;
691                }
692            }
693        }
694        Ok(orders)
695    }
696
697    pub async fn add_account(
698        pool: &PgPool,
699        kind: &str,
700        updated: bool,
701        account: Box<dyn Account>,
702    ) -> anyhow::Result<()> {
703        if updated {
704            let exists = DatabaseQueries::check_if_account_event_exists(pool, account.id())
705                .await
706                .unwrap();
707            if !exists {
708                panic!("Account event does not exist for account: {}", account.id());
709            }
710        }
711
712        let mut transaction = pool.begin().await?;
713
714        sqlx::query(
715            r#"
716            INSERT INTO "account" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING
717        "#,
718        )
719        .bind(account.id().to_string())
720        .execute(&mut *transaction)
721        .await
722        .map(|_| ())
723        .map_err(|e| anyhow::anyhow!("Failed to insert into account table: {e}"))?;
724
725        let account_event = account.last_event().unwrap();
726        sqlx::query(r#"
727            INSERT INTO "account_event" (
728                id, kind, account_id, base_currency, balances, margins, is_reported, ts_event, ts_init, created_at, updated_at
729            ) VALUES (
730                $1, $2, $3, $4, $5, $6, $7, $8, $9, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
731            )
732            ON CONFLICT (id)
733            DO UPDATE
734            SET
735                kind = $2, account_id = $3, base_currency = $4, balances = $5, margins = $6, is_reported = $7,
736                ts_event = $8, ts_init = $9, updated_at = CURRENT_TIMESTAMP
737        "#)
738            .bind(account_event.event_id.to_string())
739            .bind(kind.to_string())
740            .bind(account_event.account_id.to_string())
741            .bind(account_event.base_currency.map(|x| x.code.as_str()))
742            .bind(serde_json::to_value::<Vec<AccountBalance>>(account_event.balances).unwrap())
743            .bind(serde_json::to_value::<Vec<MarginBalance>>(account_event.margins).unwrap())
744            .bind(account_event.is_reported)
745            .bind(account_event.ts_event.to_string())
746            .bind(account_event.ts_init.to_string())
747            .execute(&mut *transaction)
748            .await
749            .map(|_| ())
750            .map_err(|e| anyhow::anyhow!("Failed to insert into account_event table: {e}"))?;
751        transaction
752            .commit()
753            .await
754            .map_err(|e| anyhow::anyhow!("Failed to commit add_account transaction: {e}"))
755    }
756
757    pub async fn load_account_events(
758        pool: &PgPool,
759        account_id: &AccountId,
760    ) -> anyhow::Result<Vec<AccountState>> {
761        sqlx::query_as::<_, AccountEventModel>(
762            r#"SELECT * FROM "account_event" WHERE account_id = $1 ORDER BY created_at ASC"#,
763        )
764        .bind(account_id.to_string())
765        .fetch_all(pool)
766        .await
767        .map(|rows| rows.into_iter().map(|row| row.0).collect())
768        .map_err(|e| anyhow::anyhow!("Failed to load account events: {e}"))
769    }
770
771    pub async fn load_account(
772        pool: &PgPool,
773        account_id: &AccountId,
774    ) -> anyhow::Result<Option<AccountAny>> {
775        let account_events = DatabaseQueries::load_account_events(pool, account_id).await;
776        match account_events {
777            Ok(account_events) => {
778                if account_events.is_empty() {
779                    return Ok(None);
780                }
781                let account = AccountAny::from_events(account_events).unwrap();
782                Ok(Some(account))
783            }
784            Err(e) => anyhow::bail!("Failed to load account events: {e}"),
785        }
786    }
787
788    pub async fn load_accounts(pool: &PgPool) -> anyhow::Result<Vec<AccountAny>> {
789        let mut accounts: Vec<AccountAny> = Vec::new();
790        let account_ids: Vec<AccountId> = sqlx::query(
791            r#"
792            SELECT DISTINCT account_id FROM "account_event"
793        "#,
794        )
795        .fetch_all(pool)
796        .await
797        .map(|rows| {
798            rows.into_iter()
799                .map(|row| AccountId::from(row.get::<&str, _>(0)))
800                .collect()
801        })
802        .map_err(|e| anyhow::anyhow!("Failed to load account ids: {e}"))?;
803        for id in account_ids {
804            let account = DatabaseQueries::load_account(pool, &id).await.unwrap();
805            match account {
806                Some(account) => {
807                    accounts.push(account);
808                }
809                None => {
810                    continue;
811                }
812            }
813        }
814        Ok(accounts)
815    }
816
817    pub async fn add_trade(pool: &PgPool, trade: &TradeTick) -> anyhow::Result<()> {
818        sqlx::query(r#"
819            INSERT INTO "trade" (
820                instrument_id, price, quantity, aggressor_side, venue_trade_id,
821                ts_event, ts_init, created_at, updated_at
822            ) VALUES (
823                $1, $2, $3, $4::aggressor_side, $5, $6, $7, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
824            )
825            ON CONFLICT (id)
826            DO UPDATE
827            SET
828                instrument_id = $1, price = $2, quantity = $3, aggressor_side = $4, venue_trade_id = $5,
829                ts_event = $6, ts_init = $7, updated_at = CURRENT_TIMESTAMP
830        "#)
831            .bind(trade.instrument_id.to_string())
832            .bind(trade.price.to_string())
833            .bind(trade.size.to_string())
834            .bind(AggressorSideModel(trade.aggressor_side))
835            .bind(trade.trade_id.to_string())
836            .bind(trade.ts_event.to_string())
837            .bind(trade.ts_init.to_string())
838            .execute(pool)
839            .await
840            .map(|_| ())
841            .map_err(|e| anyhow::anyhow!("Failed to insert into trade table: {e}"))
842    }
843
844    pub async fn load_trades(
845        pool: &PgPool,
846        instrument_id: &InstrumentId,
847    ) -> anyhow::Result<Vec<TradeTick>> {
848        sqlx::query_as::<_, TradeTickModel>(
849            r#"SELECT * FROM "trade" WHERE instrument_id = $1 ORDER BY ts_event ASC"#,
850        )
851        .bind(instrument_id.to_string())
852        .fetch_all(pool)
853        .await
854        .map(|rows| rows.into_iter().map(|row| row.0).collect())
855        .map_err(|e| anyhow::anyhow!("Failed to load trades: {e}"))
856    }
857
858    pub async fn add_quote(pool: &PgPool, quote: &QuoteTick) -> anyhow::Result<()> {
859        sqlx::query(r#"
860            INSERT INTO "quote" (
861                instrument_id, bid_price, ask_price, bid_size, ask_size, ts_event, ts_init, created_at, updated_at
862            ) VALUES (
863                $1, $2, $3, $4, $5, $6, $7, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
864            )
865            ON CONFLICT (id)
866            DO UPDATE
867            SET
868                instrument_id = $1, bid_price = $2, ask_price = $3, bid_size = $4, ask_size = $5,
869                ts_event = $6, ts_init = $7, updated_at = CURRENT_TIMESTAMP
870        "#)
871            .bind(quote.instrument_id.to_string())
872            .bind(quote.bid_price.to_string())
873            .bind(quote.ask_price.to_string())
874            .bind(quote.bid_size.to_string())
875            .bind(quote.ask_size.to_string())
876            .bind(quote.ts_event.to_string())
877            .bind(quote.ts_init.to_string())
878            .execute(pool)
879            .await
880            .map(|_| ())
881            .map_err(|e| anyhow::anyhow!("Failed to insert into quote table: {e}"))
882    }
883
884    pub async fn load_quotes(
885        pool: &PgPool,
886        instrument_id: &InstrumentId,
887    ) -> anyhow::Result<Vec<QuoteTick>> {
888        sqlx::query_as::<_, QuoteTickModel>(
889            r#"SELECT * FROM "quote" WHERE instrument_id = $1 ORDER BY ts_event ASC"#,
890        )
891        .bind(instrument_id.to_string())
892        .fetch_all(pool)
893        .await
894        .map(|rows| rows.into_iter().map(|row| row.0).collect())
895        .map_err(|e| anyhow::anyhow!("Failed to load quotes: {e}"))
896    }
897
898    pub async fn add_bar(pool: &PgPool, bar: &Bar) -> anyhow::Result<()> {
899        println!("Adding bar: {:?}", bar);
900        sqlx::query(r#"
901            INSERT INTO "bar" (
902                instrument_id, step, bar_aggregation, price_type, aggregation_source, open, high, low, close, volume, ts_event, ts_init, created_at, updated_at
903            ) VALUES (
904                $1, $2, $3::bar_aggregation, $4::price_type, $5::aggregation_source, $6, $7, $8, $9, $10, $11, $12, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
905            )
906            ON CONFLICT (id)
907            DO UPDATE
908            SET
909                instrument_id = $1, step = $2, bar_aggregation = $3::bar_aggregation, price_type = $4::price_type, aggregation_source = $5::aggregation_source,
910                open = $6, high = $7, low = $8, close = $9, volume = $10, ts_event = $11, ts_init = $12, updated_at = CURRENT_TIMESTAMP
911        "#)
912            .bind(bar.bar_type.instrument_id().to_string())
913            .bind(bar.bar_type.spec().step.get() as i32)
914            .bind(BarAggregationModel(bar.bar_type.spec().aggregation))
915            .bind(PriceTypeModel(bar.bar_type.spec().price_type))
916            .bind(AggregationSourceModel(bar.bar_type.aggregation_source()))
917            .bind(bar.open.to_string())
918            .bind(bar.high.to_string())
919            .bind(bar.low.to_string())
920            .bind(bar.close.to_string())
921            .bind(bar.volume.to_string())
922            .bind(bar.ts_event.to_string())
923            .bind(bar.ts_init.to_string())
924            .execute(pool)
925            .await
926            .map(|_| ())
927            .map_err(|e| anyhow::anyhow!("Failed to insert into bar table: {e}"))
928    }
929
930    pub async fn load_bars(
931        pool: &PgPool,
932        instrument_id: &InstrumentId,
933    ) -> anyhow::Result<Vec<Bar>> {
934        sqlx::query_as::<_, BarModel>(
935            r#"SELECT * FROM "bar" WHERE instrument_id = $1 ORDER BY ts_event ASC"#,
936        )
937        .bind(instrument_id.to_string())
938        .fetch_all(pool)
939        .await
940        .map(|rows| rows.into_iter().map(|row| row.0).collect())
941        .map_err(|e| anyhow::anyhow!("Failed to load bars: {e}"))
942    }
943
944    pub async fn load_distinct_order_event_client_ids(
945        pool: &PgPool,
946    ) -> anyhow::Result<HashMap<ClientOrderId, ClientId>> {
947        let mut map: HashMap<ClientOrderId, ClientId> = HashMap::new();
948        let result = sqlx::query_as::<_, OrderEventOrderClientIdCombination>(
949            r#"
950            SELECT DISTINCT
951                client_order_id AS "client_order_id",
952                client_id AS "client_id"
953            FROM "order_event"
954        "#,
955        )
956        .fetch_all(pool)
957        .await
958        .map_err(|e| anyhow::anyhow!("Failed to load account ids: {e}"))?;
959        for id in result {
960            map.insert(id.client_order_id, id.client_id);
961        }
962        Ok(map)
963    }
964
965    pub async fn add_signal(pool: &PgPool, signal: &Signal) -> anyhow::Result<()> {
966        sqlx::query(
967            r#"
968            INSERT INTO "signal" (
969                name, value, ts_event, ts_init, created_at, updated_at
970            ) VALUES (
971                $1, $2, $3, $4, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
972            )
973            ON CONFLICT (id)
974            DO UPDATE
975            SET
976                name = $1, value = $2, ts_event = $3, ts_init = $4,
977                updated_at = CURRENT_TIMESTAMP
978        "#,
979        )
980        .bind(signal.name.to_string())
981        .bind(signal.value.to_string())
982        .bind(signal.ts_event.to_string())
983        .bind(signal.ts_init.to_string())
984        .execute(pool)
985        .await
986        .map(|_| ())
987        .map_err(|e| anyhow::anyhow!("Failed to insert into signal table: {e}"))
988    }
989
990    pub async fn load_signals(pool: &PgPool, name: &str) -> anyhow::Result<Vec<Signal>> {
991        sqlx::query_as::<_, SignalModel>(
992            r#"SELECT * FROM "signal" WHERE name = $1 ORDER BY ts_init ASC"#,
993        )
994        .bind(name)
995        .fetch_all(pool)
996        .await
997        .map(|rows| rows.into_iter().map(|row| row.0).collect())
998        .map_err(|e| anyhow::anyhow!("Failed to load signals: {e}"))
999    }
1000
1001    pub async fn add_custom_data(pool: &PgPool, data: &CustomData) -> anyhow::Result<()> {
1002        sqlx::query(
1003            r#"
1004            INSERT INTO "custom" (
1005                data_type, metadata, value, ts_event, ts_init, created_at, updated_at
1006            ) VALUES (
1007                $1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
1008            )
1009            ON CONFLICT (id)
1010            DO UPDATE
1011            SET
1012                data_type = $1, metadata = $2, value = $3, ts_event = $4, ts_init = $5,
1013                updated_at = CURRENT_TIMESTAMP
1014        "#,
1015        )
1016        .bind(data.data_type.type_name().to_string())
1017        .bind(
1018            data.data_type
1019                .metadata()
1020                .as_ref()
1021                .map_or_else(|| Ok(serde_json::Value::Null), serde_json::to_value)?,
1022        )
1023        .bind(data.value.to_vec())
1024        .bind(data.ts_event.to_string())
1025        .bind(data.ts_init.to_string())
1026        .execute(pool)
1027        .await
1028        .map(|_| ())
1029        .map_err(|e| anyhow::anyhow!("Failed to insert into custom table: {e}"))
1030    }
1031
1032    pub async fn load_custom_data(
1033        pool: &PgPool,
1034        data_type: &DataType,
1035    ) -> anyhow::Result<Vec<CustomData>> {
1036        // TODO: This metadata JSON could be more efficient at some point
1037        let metadata_json = data_type
1038            .metadata()
1039            .as_ref()
1040            .map_or(Ok(serde_json::Value::Null), |metadata| {
1041                serde_json::to_value(metadata)
1042            })?;
1043
1044        sqlx::query_as::<_, CustomDataModel>(
1045            r#"SELECT * FROM "custom" WHERE data_type = $1 AND metadata = $2 ORDER BY ts_init ASC"#,
1046        )
1047        .bind(data_type.type_name())
1048        .bind(metadata_json)
1049        .fetch_all(pool)
1050        .await
1051        .map(|rows| rows.into_iter().map(|row| row.0).collect())
1052        .map_err(|e| anyhow::anyhow!("Failed to load custom data: {e}"))
1053    }
1054}