1use jiff::Timestamp;
19use nautilus_core::{
20 nanos::UnixNanos,
21 python::{to_pyruntime_err, to_pyvalue_err},
22};
23use nautilus_model::{
24 data::BarType,
25 enums::{AccountType, OrderSide, OrderType, TimeInForce, TriggerType},
26 identifiers::{AccountId, ClientOrderId, InstrumentId, VenueOrderId},
27 python::instruments::{instrument_any_to_pyobject, pyobject_to_instrument_any},
28 types::{Price, Quantity},
29};
30use pyo3::{
31 conversion::IntoPyObjectExt,
32 prelude::*,
33 types::{PyDict, PyList},
34};
35use rust_decimal::Decimal;
36use ustr::Ustr;
37
38use crate::{
39 common::{credential::KrakenCredential, enums::KrakenEnvironment},
40 http::KrakenSpotHttpClient,
41};
42
43#[pymethods]
44#[pyo3_stub_gen::derive::gen_stub_pymethods]
45impl KrakenSpotHttpClient {
46 #[new]
52 #[pyo3(signature = (api_key=None, api_secret=None, base_url=None, timeout_secs=60, max_retries=None, retry_delay_ms=None, retry_delay_max_ms=None, proxy_url=None, max_requests_per_second=5))]
53 #[expect(clippy::too_many_arguments)]
54 fn py_new(
55 api_key: Option<String>,
56 api_secret: Option<String>,
57 base_url: Option<String>,
58 timeout_secs: u64,
59 max_retries: Option<u32>,
60 retry_delay_ms: Option<u64>,
61 retry_delay_max_ms: Option<u64>,
62 proxy_url: Option<String>,
63 max_requests_per_second: u32,
64 ) -> PyResult<Self> {
65 let environment = KrakenEnvironment::Live;
66
67 if let Some(cred) = KrakenCredential::resolve_spot(api_key, api_secret) {
68 let (k, s) = cred.into_parts();
69 Self::with_credentials(
70 k,
71 s,
72 environment,
73 base_url,
74 timeout_secs,
75 max_retries,
76 retry_delay_ms,
77 retry_delay_max_ms,
78 proxy_url,
79 max_requests_per_second,
80 )
81 .map_err(to_pyvalue_err)
82 } else {
83 Self::new(
84 environment,
85 base_url,
86 timeout_secs,
87 max_retries,
88 retry_delay_ms,
89 retry_delay_max_ms,
90 proxy_url,
91 max_requests_per_second,
92 )
93 .map_err(to_pyvalue_err)
94 }
95 }
96
97 #[getter]
98 #[pyo3(name = "base_url")]
99 #[must_use]
100 pub fn py_base_url(&self) -> String {
101 self.inner.base_url().to_string()
102 }
103
104 #[getter]
105 #[pyo3(name = "api_key")]
106 #[must_use]
107 pub fn py_api_key(&self) -> Option<&str> {
108 self.inner.credential().map(|c| c.api_key())
109 }
110
111 #[getter]
112 #[pyo3(name = "api_key_masked")]
113 #[must_use]
114 pub fn py_api_key_masked(&self) -> Option<String> {
115 self.inner.credential().map(|c| c.api_key_masked())
116 }
117
118 #[pyo3(name = "cache_instrument")]
120 fn py_cache_instrument(&self, py: Python, instrument: Py<PyAny>) -> PyResult<()> {
121 let inst_any = pyobject_to_instrument_any(py, instrument)?;
122 self.cache_instrument(inst_any);
123 Ok(())
124 }
125
126 #[pyo3(name = "cancel_all_requests")]
128 fn py_cancel_all_requests(&self) {
129 self.cancel_all_requests();
130 }
131
132 #[pyo3(name = "get_server_time")]
133 fn py_get_server_time<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
134 let client = self.clone();
135
136 pyo3_async_runtimes::tokio::future_into_py(py, async move {
137 let server_time = client
138 .inner
139 .get_server_time()
140 .await
141 .map_err(to_pyruntime_err)?;
142
143 let json_string = serde_json::to_string(&server_time)
144 .map_err(|e| to_pyruntime_err(format!("Failed to serialize response: {e}")))?;
145
146 Ok(json_string)
147 })
148 }
149
150 #[pyo3(name = "request_instruments")]
155 #[pyo3(signature = (pairs=None))]
156 fn py_request_instruments<'py>(
157 &self,
158 py: Python<'py>,
159 pairs: Option<Vec<String>>,
160 ) -> PyResult<Bound<'py, PyAny>> {
161 let client = self.clone();
162
163 pyo3_async_runtimes::tokio::future_into_py(py, async move {
164 let instruments = client
165 .request_instruments(pairs)
166 .await
167 .map_err(to_pyruntime_err)?;
168
169 Python::attach(|py| {
170 let py_instruments: PyResult<Vec<_>> = instruments
171 .into_iter()
172 .map(|inst| instrument_any_to_pyobject(py, inst))
173 .collect();
174 let pylist = PyList::new(py, py_instruments?)?;
175 Ok(pylist.unbind())
176 })
177 })
178 }
179
180 #[pyo3(name = "request_instrument_statuses")]
186 #[pyo3(signature = (pairs=None))]
187 fn py_request_instrument_statuses<'py>(
188 &self,
189 py: Python<'py>,
190 pairs: Option<Vec<String>>,
191 ) -> PyResult<Bound<'py, PyAny>> {
192 let client = self.clone();
193
194 pyo3_async_runtimes::tokio::future_into_py(py, async move {
195 let statuses = client
196 .request_instrument_statuses(pairs)
197 .await
198 .map_err(to_pyruntime_err)?;
199
200 Python::attach(|py| {
201 let dict = PyDict::new(py);
202 for (instrument_id, action) in statuses {
203 dict.set_item(
204 instrument_id.into_bound_py_any(py)?,
205 action.into_bound_py_any(py)?,
206 )?;
207 }
208 Ok(dict.into_any().unbind())
209 })
210 })
211 }
212
213 #[pyo3(name = "request_trades")]
215 #[pyo3(signature = (instrument_id, start=None, end=None, limit=None))]
216 fn py_request_trades<'py>(
217 &self,
218 py: Python<'py>,
219 instrument_id: InstrumentId,
220 start: Option<Timestamp>,
221 end: Option<Timestamp>,
222 limit: Option<u64>,
223 ) -> PyResult<Bound<'py, PyAny>> {
224 let client = self.clone();
225
226 pyo3_async_runtimes::tokio::future_into_py(py, async move {
227 let trades = client
228 .request_trades(instrument_id, start, end, limit)
229 .await
230 .map_err(to_pyruntime_err)?;
231
232 Python::attach(|py| {
233 let py_trades: PyResult<Vec<_>> = trades
234 .into_iter()
235 .map(|trade| trade.into_py_any(py))
236 .collect();
237 let pylist = PyList::new(py, py_trades?)?.into_any().unbind();
238 Ok(pylist)
239 })
240 })
241 }
242
243 #[pyo3(name = "request_book_snapshot")]
245 #[pyo3(signature = (instrument_id, depth=None))]
246 fn py_request_book_snapshot<'py>(
247 &self,
248 py: Python<'py>,
249 instrument_id: InstrumentId,
250 depth: Option<u32>,
251 ) -> PyResult<Bound<'py, PyAny>> {
252 let client = self.clone();
253
254 pyo3_async_runtimes::tokio::future_into_py(py, async move {
255 let book = client
256 .request_book_snapshot(instrument_id, depth)
257 .await
258 .map_err(to_pyruntime_err)?;
259
260 Python::attach(|py| book.into_py_any(py))
261 })
262 }
263
264 #[pyo3(name = "request_bars")]
266 #[pyo3(signature = (bar_type, start=None, end=None, limit=None))]
267 fn py_request_bars<'py>(
268 &self,
269 py: Python<'py>,
270 bar_type: BarType,
271 start: Option<Timestamp>,
272 end: Option<Timestamp>,
273 limit: Option<u64>,
274 ) -> PyResult<Bound<'py, PyAny>> {
275 let client = self.clone();
276
277 pyo3_async_runtimes::tokio::future_into_py(py, async move {
278 let bars = client
279 .request_bars(bar_type, start, end, limit)
280 .await
281 .map_err(to_pyruntime_err)?;
282
283 Python::attach(|py| {
284 let py_bars: PyResult<Vec<_>> =
285 bars.into_iter().map(|bar| bar.into_py_any(py)).collect();
286 let pylist = PyList::new(py, py_bars?)?.into_any().unbind();
287 Ok(pylist)
288 })
289 })
290 }
291
292 #[pyo3(name = "request_account_state")]
303 #[pyo3(signature = (account_id, account_type = AccountType::Cash, margin_balance_asset = None))]
304 fn py_request_account_state<'py>(
305 &self,
306 py: Python<'py>,
307 account_id: AccountId,
308 account_type: AccountType,
309 margin_balance_asset: Option<String>,
310 ) -> PyResult<Bound<'py, PyAny>> {
311 let client = self.clone();
312
313 pyo3_async_runtimes::tokio::future_into_py(py, async move {
314 let account_state = client
315 .request_account_state(account_id, account_type, margin_balance_asset.as_deref())
316 .await
317 .map_err(to_pyruntime_err)?;
318
319 Python::attach(|py| account_state.into_pyobject(py).map(|o| o.unbind()))
320 })
321 }
322
323 #[pyo3(name = "request_margin_metrics")]
336 #[pyo3(signature = (asset = None))]
337 fn py_request_margin_metrics<'py>(
338 &self,
339 py: Python<'py>,
340 asset: Option<String>,
341 ) -> PyResult<Bound<'py, PyAny>> {
342 let client = self.clone();
343
344 pyo3_async_runtimes::tokio::future_into_py(py, async move {
345 let metrics = client
346 .request_margin_metrics(asset.as_deref())
347 .await
348 .map_err(to_pyruntime_err)?;
349
350 Python::attach(|py| {
351 let dict = pyo3::types::PyDict::new(py);
352 for (k, v) in metrics {
353 dict.set_item(k, v)?;
354 }
355 Ok::<_, PyErr>(dict.unbind().into_any())
356 })
357 })
358 }
359
360 #[pyo3(name = "request_account_state_with_metrics")]
376 #[pyo3(signature = (account_id, account_type = AccountType::Cash, margin_balance_asset = None))]
377 fn py_request_account_state_with_metrics<'py>(
378 &self,
379 py: Python<'py>,
380 account_id: AccountId,
381 account_type: AccountType,
382 margin_balance_asset: Option<String>,
383 ) -> PyResult<Bound<'py, PyAny>> {
384 let client = self.clone();
385
386 pyo3_async_runtimes::tokio::future_into_py(py, async move {
387 let (account_state, metrics) = client
388 .request_account_state_with_metrics(
389 account_id,
390 account_type,
391 margin_balance_asset.as_deref(),
392 )
393 .await
394 .map_err(to_pyruntime_err)?;
395
396 Python::attach(|py| {
397 let state_obj = account_state.into_pyobject(py)?.unbind();
398 let dict = pyo3::types::PyDict::new(py);
399 for (k, v) in metrics {
400 dict.set_item(k, v)?;
401 }
402 let tuple = pyo3::types::PyTuple::new(
403 py,
404 [state_obj.into_any(), dict.unbind().into_any()],
405 )?;
406 Ok::<_, PyErr>(tuple.unbind().into_any())
407 })
408 })
409 }
410
411 #[pyo3(name = "request_order_status_reports")]
413 #[pyo3(signature = (account_id, instrument_id=None, start=None, end=None, open_only=false))]
414 fn py_request_order_status_reports<'py>(
415 &self,
416 py: Python<'py>,
417 account_id: AccountId,
418 instrument_id: Option<InstrumentId>,
419 start: Option<Timestamp>,
420 end: Option<Timestamp>,
421 open_only: bool,
422 ) -> PyResult<Bound<'py, PyAny>> {
423 let client = self.clone();
424
425 pyo3_async_runtimes::tokio::future_into_py(py, async move {
426 let reports = client
427 .request_order_status_reports(account_id, instrument_id, start, end, open_only)
428 .await
429 .map_err(to_pyruntime_err)?;
430
431 Python::attach(|py| {
432 let py_reports: PyResult<Vec<_>> = reports
433 .into_iter()
434 .map(|report| report.into_py_any(py))
435 .collect();
436 let pylist = PyList::new(py, py_reports?)?.into_any().unbind();
437 Ok(pylist)
438 })
439 })
440 }
441
442 #[pyo3(name = "request_fill_reports")]
444 #[pyo3(signature = (account_id, instrument_id=None, start=None, end=None))]
445 fn py_request_fill_reports<'py>(
446 &self,
447 py: Python<'py>,
448 account_id: AccountId,
449 instrument_id: Option<InstrumentId>,
450 start: Option<Timestamp>,
451 end: Option<Timestamp>,
452 ) -> PyResult<Bound<'py, PyAny>> {
453 let client = self.clone();
454
455 pyo3_async_runtimes::tokio::future_into_py(py, async move {
456 let reports = client
457 .request_fill_reports(account_id, instrument_id, start, end)
458 .await
459 .map_err(to_pyruntime_err)?;
460
461 Python::attach(|py| {
462 let py_reports: PyResult<Vec<_>> = reports
463 .into_iter()
464 .map(|report| report.into_py_any(py))
465 .collect();
466 let pylist = PyList::new(py, py_reports?)?.into_any().unbind();
467 Ok(pylist)
468 })
469 })
470 }
471
472 #[pyo3(name = "request_position_status_reports")]
478 #[pyo3(signature = (account_id, instrument_id=None, account_type=AccountType::Cash, use_spot_position_reports=false, quote_currency="USDT"))]
479 fn py_request_position_status_reports<'py>(
480 &self,
481 py: Python<'py>,
482 account_id: AccountId,
483 instrument_id: Option<InstrumentId>,
484 account_type: AccountType,
485 use_spot_position_reports: bool,
486 quote_currency: &str,
487 ) -> PyResult<Bound<'py, PyAny>> {
488 let client = self.clone();
489 let quote_currency = Ustr::from(quote_currency);
490
491 pyo3_async_runtimes::tokio::future_into_py(py, async move {
492 let reports = client
493 .request_position_status_reports(
494 account_id,
495 instrument_id,
496 account_type,
497 use_spot_position_reports,
498 quote_currency,
499 )
500 .await
501 .map_err(to_pyruntime_err)?;
502
503 Python::attach(|py| {
504 let py_reports: PyResult<Vec<_>> = reports
505 .into_iter()
506 .map(|report| report.into_py_any(py))
507 .collect();
508 let pylist = PyList::new(py, py_reports?)?.into_any().unbind();
509 Ok(pylist)
510 })
511 })
512 }
513
514 #[pyo3(name = "submit_order")]
527 #[pyo3(signature = (account_id, instrument_id, client_order_id, order_side, order_type, quantity, time_in_force, expire_time=None, price=None, trigger_price=None, trigger_type=None, trailing_offset=None, limit_offset=None, reduce_only=false, post_only=false, quote_quantity=false, display_qty=None, leverage=None, account_type=AccountType::Cash))]
528 #[expect(clippy::too_many_arguments)]
529 fn py_submit_order<'py>(
530 &self,
531 py: Python<'py>,
532 account_id: AccountId,
533 instrument_id: InstrumentId,
534 client_order_id: ClientOrderId,
535 order_side: OrderSide,
536 order_type: OrderType,
537 quantity: Quantity,
538 time_in_force: TimeInForce,
539 expire_time: Option<u64>,
540 price: Option<Price>,
541 trigger_price: Option<Price>,
542 trigger_type: Option<TriggerType>,
543 trailing_offset: Option<String>,
544 limit_offset: Option<String>,
545 reduce_only: bool,
546 post_only: bool,
547 quote_quantity: bool,
548 display_qty: Option<Quantity>,
549 leverage: Option<u16>,
550 account_type: AccountType,
551 ) -> PyResult<Bound<'py, PyAny>> {
552 let client = self.clone();
553 let expire_time = expire_time.map(UnixNanos::from);
554 let trailing_offset = trailing_offset
555 .map(|s| {
556 Decimal::from_str_exact(&s)
557 .map_err(|e| to_pyvalue_err(format!("invalid trailing_offset: {e}")))
558 })
559 .transpose()?;
560 let limit_offset = limit_offset
561 .map(|s| {
562 Decimal::from_str_exact(&s)
563 .map_err(|e| to_pyvalue_err(format!("invalid limit_offset: {e}")))
564 })
565 .transpose()?;
566
567 pyo3_async_runtimes::tokio::future_into_py(py, async move {
568 let venue_order_id = client
569 .submit_order(
570 account_id,
571 instrument_id,
572 client_order_id,
573 order_side,
574 order_type,
575 quantity,
576 time_in_force,
577 expire_time,
578 price,
579 trigger_price,
580 trigger_type,
581 trailing_offset,
582 limit_offset,
583 reduce_only,
584 post_only,
585 quote_quantity,
586 display_qty,
587 leverage,
588 account_type,
589 )
590 .await
591 .map_err(to_pyruntime_err)?;
592
593 Python::attach(|py| venue_order_id.into_pyobject(py).map(|o| o.unbind()))
594 })
595 }
596
597 #[pyo3(name = "cancel_order")]
607 #[pyo3(signature = (account_id, instrument_id, client_order_id=None, venue_order_id=None))]
608 fn py_cancel_order<'py>(
609 &self,
610 py: Python<'py>,
611 account_id: AccountId,
612 instrument_id: InstrumentId,
613 client_order_id: Option<ClientOrderId>,
614 venue_order_id: Option<VenueOrderId>,
615 ) -> PyResult<Bound<'py, PyAny>> {
616 let client = self.clone();
617
618 pyo3_async_runtimes::tokio::future_into_py(py, async move {
619 client
620 .cancel_order(account_id, instrument_id, client_order_id, venue_order_id)
621 .await
622 .map_err(to_pyruntime_err)
623 })
624 }
625
626 #[pyo3(name = "cancel_all_orders")]
627 fn py_cancel_all_orders<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
628 let client = self.clone();
629
630 pyo3_async_runtimes::tokio::future_into_py(py, async move {
631 let response = client
632 .inner
633 .cancel_all_orders()
634 .await
635 .map_err(to_pyruntime_err)?;
636
637 Ok(response.count)
638 })
639 }
640
641 #[pyo3(name = "cancel_orders_batch")]
643 fn py_cancel_orders_batch<'py>(
644 &self,
645 py: Python<'py>,
646 venue_order_ids: Vec<VenueOrderId>,
647 ) -> PyResult<Bound<'py, PyAny>> {
648 let client = self.clone();
649
650 pyo3_async_runtimes::tokio::future_into_py(py, async move {
651 client
652 .cancel_orders_batch(venue_order_ids)
653 .await
654 .map_err(to_pyruntime_err)
655 })
656 }
657
658 #[pyo3(name = "modify_order")]
670 #[pyo3(signature = (instrument_id, client_order_id=None, venue_order_id=None, quantity=None, price=None, trigger_price=None))]
671 #[expect(clippy::too_many_arguments)]
672 fn py_modify_order<'py>(
673 &self,
674 py: Python<'py>,
675 instrument_id: InstrumentId,
676 client_order_id: Option<ClientOrderId>,
677 venue_order_id: Option<VenueOrderId>,
678 quantity: Option<Quantity>,
679 price: Option<Price>,
680 trigger_price: Option<Price>,
681 ) -> PyResult<Bound<'py, PyAny>> {
682 let client = self.clone();
683
684 pyo3_async_runtimes::tokio::future_into_py(py, async move {
685 let new_venue_order_id = client
686 .modify_order(
687 instrument_id,
688 client_order_id,
689 venue_order_id,
690 quantity,
691 price,
692 trigger_price,
693 )
694 .await
695 .map_err(to_pyruntime_err)?;
696
697 Python::attach(|py| new_venue_order_id.into_pyobject(py).map(|o| o.unbind()))
698 })
699 }
700}
701
702#[pymethods]
706impl KrakenSpotHttpClient {
707 #[pyo3(name = "submit_orders_batch", signature = (orders, leverage=None, account_type=AccountType::Cash, per_order_leverages=None, per_order_reduce_only=None))]
712 #[expect(clippy::type_complexity)]
713 fn py_submit_orders_batch<'py>(
714 &self,
715 py: Python<'py>,
716 orders: Vec<(
717 InstrumentId,
718 ClientOrderId,
719 OrderSide,
720 OrderType,
721 Quantity,
722 TimeInForce,
723 Option<Price>,
724 Option<Price>,
725 Option<TriggerType>,
726 bool,
727 bool,
728 Option<Quantity>,
729 )>,
730 leverage: Option<u16>,
731 account_type: AccountType,
732 per_order_leverages: Option<Vec<Option<u16>>>,
733 per_order_reduce_only: Option<Vec<bool>>,
734 ) -> PyResult<Bound<'py, PyAny>> {
735 let client = self.clone();
736 let n = orders.len();
737
738 if let Some(ref v) = per_order_leverages
739 && v.len() != n
740 {
741 return Err(to_pyvalue_err(format!(
742 "per_order_leverages length must equal orders length, was {} for {n} orders",
743 v.len(),
744 )));
745 }
746
747 if let Some(ref v) = per_order_reduce_only
748 && v.len() != n
749 {
750 return Err(to_pyvalue_err(format!(
751 "per_order_reduce_only length must equal orders length, was {} for {n} orders",
752 v.len(),
753 )));
754 }
755
756 let leverages: Vec<Option<u16>> = match per_order_leverages {
757 Some(per_leverages) => per_leverages.into_iter().map(|v| v.or(leverage)).collect(),
758 None => vec![leverage; n],
759 };
760 let reduce_only_flags = per_order_reduce_only.unwrap_or_else(|| vec![false; n]);
761 let expanded_orders = orders
762 .into_iter()
763 .zip(leverages)
764 .zip(reduce_only_flags)
765 .map(
766 |(
767 (
768 (
769 instrument_id,
770 client_order_id,
771 order_side,
772 order_type,
773 quantity,
774 time_in_force,
775 price,
776 trigger_price,
777 trigger_type,
778 post_only,
779 quote_quantity,
780 display_qty,
781 ),
782 order_leverage,
783 ),
784 order_reduce_only,
785 )| {
786 (
787 instrument_id,
788 client_order_id,
789 order_side,
790 order_type,
791 quantity,
792 time_in_force,
793 None,
794 price,
795 trigger_price,
796 trigger_type,
797 None,
798 None,
799 order_reduce_only,
800 post_only,
801 quote_quantity,
802 display_qty,
803 order_leverage,
804 )
805 },
806 )
807 .collect();
808
809 pyo3_async_runtimes::tokio::future_into_py(py, async move {
810 client
811 .submit_orders_batch(expanded_orders, account_type)
812 .await
813 .map_err(to_pyruntime_err)
814 })
815 }
816}