bitmex_order/
order.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::env;
17
18use nautilus_bitmex::{
19    common::enums::{BitmexExecInstruction, BitmexOrderType, BitmexSide},
20    http::{
21        client::BitmexHttpClient,
22        query::{DeleteOrderParamsBuilder, GetOrderParamsBuilder, PostOrderParamsBuilder},
23    },
24};
25use tracing::level_filters::LevelFilter;
26
27#[tokio::main]
28async fn main() -> Result<(), Box<dyn std::error::Error>> {
29    tracing_subscriber::fmt()
30        .with_max_level(LevelFilter::TRACE)
31        .init();
32
33    let api_key = env::var("BITMEX_API_KEY").expect("environment variable should be set");
34    let api_secret = env::var("BITMEX_API_SECRET").expect("environment variable should be set");
35    let client = BitmexHttpClient::new(None, Some(api_key), Some(api_secret), false, None);
36
37    let cl_ord_id = "2024-12-03-002";
38
39    let params = PostOrderParamsBuilder::default()
40        .symbol("XBTUSD".to_string())
41        .cl_ord_id(cl_ord_id)
42        .ord_type(BitmexOrderType::Limit)
43        .side(BitmexSide::Sell)
44        .order_qty(100_u32)
45        .price(100_000.0)
46        .exec_inst(vec![BitmexExecInstruction::ParticipateDoNotInitiate])
47        .build()?;
48    match client.http_place_order(params).await {
49        Ok(resp) => tracing::debug!("{:?}", resp),
50        Err(e) => tracing::error!("{e:?}"),
51    }
52
53    let params = DeleteOrderParamsBuilder::default()
54        .cl_ord_id(vec![cl_ord_id.to_string()])
55        .build()?;
56    match client.http_cancel_orders(params).await {
57        Ok(resp) => tracing::debug!("{:?}", resp),
58        Err(e) => tracing::error!("{e:?}"),
59    }
60
61    let params = GetOrderParamsBuilder::default()
62        .symbol("XBTUSD".to_string())
63        .build()?;
64    match client.get_orders(params).await {
65        Ok(resp) => tracing::debug!("{:?}", resp),
66        Err(e) => tracing::error!("{e:?}"),
67    }
68
69    Ok(())
70}