dceapi_rs/services/
trade.rs

1//! Trade service for trading parameter APIs.
2
3use std::collections::HashMap;
4
5use crate::error::Result;
6use crate::http::{BaseClient, RequestOptions};
7use crate::models::{
8    ArbitrageContract, ArbitrageContractRequest, ContractInfo, ContractInfoRequest,
9    DayTradeParamRequest, MainSeriesInfo, MainSeriesInfoRequest, MarginArbiPerfPara,
10    MarginArbiPerfParaRequest, NewContractInfo, NewContractInfoRequest, TradeParam, TradingParam,
11    TradingParamRequest,
12};
13
14/// API endpoint for day trade parameters.
15const PATH_GET_DAY_TRADE_PARAM: &str = "/dceapi/forward/publicweb/tradepara/dayTradPara";
16
17/// API endpoint for month trade parameters.
18const PATH_GET_MONTH_TRADE_PARAM: &str = "/dceapi/forward/publicweb/tradepara/monthTradPara";
19
20/// API endpoint for contract information.
21const PATH_GET_CONTRACT_INFO: &str = "/dceapi/forward/publicweb/tradepara/contractInfo";
22
23/// API endpoint for arbitrage contracts.
24const PATH_GET_ARBITRAGE_CONTRACT: &str = "/dceapi/forward/publicweb/tradepara/arbitrageContract";
25
26/// API endpoint for trading parameters by variety.
27const PATH_GET_TRADING_PARAM: &str = "/dceapi/forward/publicweb/tradepara/tradingParam";
28
29/// API endpoint for margin arbitrage performance parameters.
30const PATH_GET_MARGIN_ARBI_PERF_PARA: &str =
31    "/dceapi/forward/publicweb/tradepara/marginArbiPerfPara";
32
33/// API endpoint for new contract information.
34const PATH_GET_NEW_CONTRACT_INFO: &str = "/dceapi/forward/publicweb/tradepara/newContractInfo";
35
36/// API endpoint for main series information (market maker contracts).
37const PATH_GET_MAIN_SERIES_INFO: &str = "/dceapi/forward/publicweb/tradepara/mainSeriesInfo";
38
39/// Trade service for accessing trading parameters.
40#[derive(Debug, Clone)]
41pub struct TradeService {
42    client: BaseClient,
43}
44
45impl TradeService {
46    /// Create a new trade service.
47    pub fn new(client: BaseClient) -> Self {
48        TradeService { client }
49    }
50
51    /// Get daily trading parameters.
52    ///
53    /// Returns margin rates, price limits, etc. for a variety.
54    ///
55    /// # Arguments
56    /// * `req` - Request with variety_id, trade_type, and lang
57    /// * `opts` - Optional request options
58    pub async fn get_day_trade_param(
59        &self,
60        req: &DayTradeParamRequest,
61        opts: Option<RequestOptions>,
62    ) -> Result<Vec<TradeParam>> {
63        self.client
64            .do_post(PATH_GET_DAY_TRADE_PARAM, req, opts)
65            .await
66    }
67
68    /// Get monthly trading parameters.
69    ///
70    /// # Arguments
71    /// * `opts` - Optional request options
72    pub async fn get_month_trade_param(
73        &self,
74        opts: Option<RequestOptions>,
75    ) -> Result<HashMap<String, serde_json::Value>> {
76        #[derive(serde::Serialize)]
77        struct EmptyRequest {}
78
79        self.client
80            .do_post(PATH_GET_MONTH_TRADE_PARAM, &EmptyRequest {}, opts)
81            .await
82    }
83
84    /// Get contract information.
85    ///
86    /// Returns contract details including trading dates, unit, tick, etc.
87    ///
88    /// # Arguments
89    /// * `req` - Request with variety_id, trade_type, and lang
90    /// * `opts` - Optional request options
91    pub async fn get_contract_info(
92        &self,
93        req: &ContractInfoRequest,
94        opts: Option<RequestOptions>,
95    ) -> Result<Vec<ContractInfo>> {
96        self.client.do_post(PATH_GET_CONTRACT_INFO, req, opts).await
97    }
98
99    /// Get arbitrage contracts.
100    ///
101    /// Returns available spread/arbitrage trading contracts.
102    ///
103    /// # Arguments
104    /// * `lang` - Language ("zh" or "en"), defaults to "zh"
105    /// * `opts` - Optional request options
106    pub async fn get_arbitrage_contract(
107        &self,
108        lang: Option<&str>,
109        opts: Option<RequestOptions>,
110    ) -> Result<Vec<ArbitrageContract>> {
111        let req = ArbitrageContractRequest {
112            lang: lang.unwrap_or("zh").to_string(),
113        };
114        self.client
115            .do_post(PATH_GET_ARBITRAGE_CONTRACT, &req, opts)
116            .await
117    }
118
119    /// Get trading parameters by variety.
120    ///
121    /// Returns comprehensive trading parameters including margins, fees, limits for all varieties.
122    ///
123    /// # Arguments
124    /// * `lang` - Language ("zh" or "en"), defaults to "zh"
125    /// * `opts` - Optional request options
126    pub async fn get_trading_param(
127        &self,
128        lang: Option<&str>,
129        opts: Option<RequestOptions>,
130    ) -> Result<Vec<TradingParam>> {
131        let req = TradingParamRequest {
132            lang: lang.unwrap_or("zh").to_string(),
133        };
134        self.client
135            .do_post(PATH_GET_TRADING_PARAM, &req, opts)
136            .await
137    }
138
139    /// Get margin arbitrage performance parameters.
140    ///
141    /// Returns margin requirements and fees for arbitrage strategies.
142    ///
143    /// # Arguments
144    /// * `req` - Request with variety_id
145    /// * `opts` - Optional request options
146    pub async fn get_margin_arbi_perf_para(
147        &self,
148        req: &MarginArbiPerfParaRequest,
149        opts: Option<RequestOptions>,
150    ) -> Result<Vec<MarginArbiPerfPara>> {
151        self.client
152            .do_post(PATH_GET_MARGIN_ARBI_PERF_PARA, req, opts)
153            .await
154    }
155
156    /// Get new contract information (newly listed contracts).
157    ///
158    /// Returns information about futures/options contracts that were recently added.
159    ///
160    /// # Arguments
161    /// * `req` - Request with variety_id, trade_type, and contract_month
162    /// * `opts` - Optional request options
163    pub async fn get_new_contract_info(
164        &self,
165        req: &NewContractInfoRequest,
166        opts: Option<RequestOptions>,
167    ) -> Result<Vec<NewContractInfo>> {
168        self.client
169            .do_post(PATH_GET_NEW_CONTRACT_INFO, req, opts)
170            .await
171    }
172
173    /// Get main series information (market maker continuous quote contracts).
174    ///
175    /// Returns contracts designated for market maker continuous quoting.
176    ///
177    /// # Arguments
178    /// * `req` - Request with variety_id and trade_type
179    /// * `opts` - Optional request options
180    pub async fn get_main_series_info(
181        &self,
182        req: &MainSeriesInfoRequest,
183        opts: Option<RequestOptions>,
184    ) -> Result<Vec<MainSeriesInfo>> {
185        self.client
186            .do_post(PATH_GET_MAIN_SERIES_INFO, req, opts)
187            .await
188    }
189}