Nouzan / exc

An abstraction layer for exchanges
MIT License
37 stars 14 forks source link

Error: api error: code=50014 msg=Parameter ccy can not be empty #15

Closed jackbbhua closed 1 year ago

jackbbhua commented 1 year ago

hi, i am testing the trade on okx, and i run cargo run --bin okx-ws-trading from examples. the error is Error: api error: code=50014 msg=Parameter ccy can not be empty. can you guide me where can i set the paramaeter for order? thank you so much.

Nouzan commented 1 year ago

I think you are using the "single-currency margin" mode, which requires the ccy parameter to be present in the order request. We haven't added support to it yet, you can try to switch to the "multi-currency margin" mode.

jackbbhua commented 1 year ago

thank you @Nouzan. nice project and amazing job.

Nouzan commented 1 year ago

You are welcome @jackbbhua! Feel free to ask if you have more questions. 😊

jackbbhua commented 1 year ago

i had switched to multi-currency mode. and chang the endpoint api to test mode api wss://wspap.okx.com:8443/ws/v5/public , wss://wspap.okx.com:8443/ws/v5/private. it shows the this here:

protocol initialized 2022-09-22T11:09:40.509002Z DEBUG exc_okx::key: message to sign: 1663844980GET/users/self/verify 2022-09-22T11:09:40.510308Z TRACE exc_okx::websocket::transport::protocol::stream: stream 1; idle -> open 2022-09-22T11:09:40.897670Z TRACE exc_okx::websocket::transport::protocol::ping_pong: ping pong; timer reset 2022-09-22T11:09:40.899991Z TRACE exc_okx::websocket::transport::protocol::message: message layer; received event=Response(Login(CodeMessage { code: "0", msg: "" })) 2022-09-22T11:09:40.900247Z TRACE exc_okx::websocket::transport::protocol::stream: received a server frame: ServerFrame { stream_id: 1, inner: Response(Login(CodeMessage { code: "0", msg: "" })) } 2022-09-22T11:09:40.900279Z DEBUG exc_okx::websocket::transport::protocol::stream: streaming worker; received a remote close frame: id=1 2022-09-22T11:09:40.900294Z TRACE exc_okx::websocket::transport::protocol::stream: stream 1; open -> remote-closed 2022-09-22T11:09:40.900435Z TRACE exc_okx::websocket::transport::protocol: wait header; peeked Ok(ServerFrame { stream_id: 1, inner: Response(Login(CodeMessage { code: "0", msg: "" })) }) 2022-09-22T11:09:40.900480Z TRACE exc_okx::websocket::transport::connection: login; login success 2022-09-22T11:09:40.900770Z TRACE exc_okx::websocket::transport::protocol::stream: stream 2; idle -> open 2022-09-22T11:09:41.276704Z TRACE exc_okx::websocket::transport::protocol::ping_pong: ping pong; timer reset 2022-09-22T11:09:41.276891Z TRACE exc_okx::websocket::transport::protocol::message: message layer; received event=TradeResponse(Order { id: "a25438ea900f49dba06bffad6a74d83b", code: "0", msg: "", data: [OrderData { cl_ord_id: "", ord_id: "493136826102808576", tag: Some(""), s_code: "0", s_msg: "" }] }) 2022-09-22T11:09:41.276928Z TRACE exc_okx::websocket::transport::protocol::stream: received a server frame: ServerFrame { stream_id: 2, inner: TradeResponse(Order { id: "a25438ea900f49dba06bffad6a74d83b", code: "0", msg: "", data: [OrderData { cl_ord_id: "", ord_id: "493136826102808576", tag: Some(""), s_code: "0", s_msg: "" }] }) } 2022-09-22T11:09:41.277071Z TRACE exc_okx::websocket::transport::protocol: wait header; peeked Ok(ServerFrame { stream_id: 2, inner: TradeResponse(Order { id: "a25438ea900f49dba06bffad6a74d83b", code: "0", msg: "", data: [OrderData { cl_ord_id: "", ord_id: "493136826102808576", tag: Some(""), s_code: "0", s_msg: "" }] }) }) 2022-09-22T11:09:41.279083Z DEBUG exc_okx::key: message to sign: 2022-09-22T11:09:41.279ZGET/api/v5/trade/order?instId=OKB-USDT&ordId=493136826102808576 2022-09-22T11:09:42.569908Z TRACE exc_okx::http::layer: http response; status: 401 Error: api: code=50101 msg=APIKey does not match current environment.

Nouzan commented 1 year ago

@jackbbhua The support to the OKX testing environment will be added in #16.

jackbbhua commented 1 year ago

thank you so much.

jackbbhua commented 1 year ago

hi,@Nouzan , can we easy to supprt "single-currency margin" mode, case most new traders don't have enough money to go to "multi-currency margin" level. thanks.

Nouzan commented 1 year ago

@jackbbhua To support the "single-currency margin" mode, we need to make some changes to the high-level API (something like Place::with_margin(self, &str) is needed). So maybe we will consider it in v0.5.

jackbbhua commented 1 year ago

@Nouzan hope can use the v0.5 very soon. 👍 thanks for the nice job.

jackbbhua commented 1 year ago

the 0.5 beta 2 is ready, pretty nice, can i have your IM acoount?like twitter, telegram?thanks.

jackbbhua commented 1 year ago

hi, @Nouzan can you give me some guide of how to place perpetual order? now i just can place a coin-coin order and margin orders. thank you.

Nouzan commented 1 year ago

hi, @Nouzan can you give me some guide of how to place perpetual order?

now i just can place a coin-coin order and margin orders. thank you.

You can use "COIN-USD-SWAP" or "COIN-USDT-SWAP" as the instrument names.

Nouzan commented 1 year ago

the 0.5 beta 2 is ready, pretty nice, can i have your IM acoount?like twitter, telegram?thanks.

Maybe we will have a discord channel in the future. But for now..., GitHub is just fine. 😊

jackbbhua commented 1 year ago

thank you. @Nouzan

jackbbhua commented 1 year ago

@Nouzan can you give me a demo of setPositionMode and Set Leverage please?

Nouzan commented 1 year ago

@Nouzan can you give me a demo of setPositionMode and Set Leverage please?

The APIs for setting position mode and leverage are not supported yet. Using "net_mode" makes things more consistent across different instruments and exchanges, as well as the "cross" margin mode which does not have to respect the leverage setting.

jackbbhua commented 1 year ago

ok.i had works that part out. Can you please give a sell order demo please? there has no demo is really hard to do right now. thank you so much.

Nouzan commented 1 year ago

ok.i had works that part out. Can you please give a sell order demo please? there has no demo is really hard to do right now. thank you so much.

Sell orders are the orders with negative size:

// Sell 10 DOGE at the price of 300 USDT/DOGE.
let sell = Place::with_size(dec!(-10)).limit(dec!(300));
exchange.place("DOGE-USDT", &sell, None).await?;
jackbbhua commented 1 year ago

wow.I got it . I try to find in docs but not success. Thanks!

jackbbhua commented 1 year ago

2022-10-11T09:52:35.347369Z INFO okx_streams: ts=2022-10-11 9:52:35.362 +00:00:00, last=(1281.39, 1), bid=(Some(1281.33), Some(24)), ask=(Some(1281.4), Some(543)) 1281.38 2022-10-11T09:52:35.483693Z INFO okx_streams: ts=2022-10-11 9:52:35.485 +00:00:00, last=(1281.38, 7), bid=(Some(1281.31), Some(530)), ask=(Some(1281.38), Some(32)) 1281.37 2022-10-11T09:52:35.764228Z INFO okx_streams: ts=2022-10-11 9:52:35.77 +00:00:00, last=(1281.37, 22), bid=(Some(1281.37), Some(47)), ask=(Some(1281.38), Some(593)) 1281.31 2022-10-11T09:52:35.873153Z INFO okx_streams: ts=2022-10-11 9:52:35.878 +00:00:00, last=(1281.31, 34), bid=(Some(1281.3), Some(540)), ask=(Some(1281.31), Some(77)) 1281.37 2022-10-11T09:52:35.983842Z INFO okx_streams: ts=2022-10-11 9:52:35.99 +00:00:00, last=(1281.37, 1), bid=(Some(1281.36), Some(364)), ask=(Some(1281.37), Some(126)) 1281.37 2022-10-11T09:52:36.088075Z INFO okx_streams: ts=2022-10-11 9:52:36.091 +00:00:00, last=(1281.37, 72), bid=(Some(1281.36), Some(723)), ask=(Some(1281.37), Some(15)) 1281.37

what's the inter mean after bid or ask like 723 and 15?

Nouzan commented 1 year ago

They are bid_size and ask_size.

jackbbhua commented 1 year ago

@Nouzan do you have backtest library of rust recommendation?

jackbbhua commented 1 year ago

and do you have this api alreay? https://www.okx.com/docs-v5/en/#rest-api-trade-close-positions

Nouzan commented 1 year ago

and do you have this api alreay? https://www.okx.com/docs-v5/en/#rest-api-trade-close-positions

No, but it is not hard to add.

Nouzan commented 1 year ago

@Nouzan do you have backtest library of rust recommendation?

As far as I know none, so we built one ourselves. It is private for now. I am planning to add a virtual exchange to exc though, with which can easily build a backtesting framework with exc's API.

jackbbhua commented 1 year ago

thanks @Nouzan. hope i can very good at this library.

jackbbhua commented 1 year ago

i tried long time not complete subscribe_orders ws function of okx, can you please give a demo please? thank you in advance.

jackbbhua commented 1 year ago

@Nouzan good morning sir.

Nouzan commented 1 year ago

i tried long time not complete subscribe_orders ws function of okx, can you please give a demo please? thank you in advance.

The order subscription API of OKX is also planned, but it seems that you are going to implement it yourself? Would you please describe your problem concretely in a new issue?

jackbbhua commented 1 year ago

i see. not very good at rust for now. if i can join that is cool, this is my code ` let exc = Okx::endpoint() .ws_ping_timeout(Duration::from_secs(5)) .ws_connection_timeout(Duration::from_secs(5)) .private(key) .connect_exc();

                let mut orders = ExcService::<SubscribeOrders>::into_retry(exc, Duration::from_secs(5));
                match { orders.subscribe_orders(inst).await } {
                    Ok(mut stream) => {
                        while let Some(c) = stream.next().await {
                            match c {
                                Ok(c) => {
                                    // tracing::info!("{c}");
                                },
                                Err(err) => {
                                    tracing::error!("{err}");
                                }
                            }
                        }
                        tracing::warn!("stream is dead; reconnecting..");
                    }
                    Err(err) => {
                        tracing::error!("request error: {err}; retrying..");
                    }
                }`
Nouzan commented 1 year ago

The SubscribeOrders request for OKX is not supported yet (but will be added soon). You can use TradingService::check instead. Polling state of orders is inefficient but is always needed.

jackbbhua commented 1 year ago

sure. thank you. @Nouzan

jackbbhua commented 1 year ago

one more question, is our place order api can support stop order paramaters? effect like https://www.okx.com/docs-v5/en/#rest-api-trade-place-algo-order to set Take-profit order price and Stop-loss order price

Nouzan commented 1 year ago

one more question, is our place order api can support stop order paramaters?

effect like https://www.okx.com/docs-v5/en/#rest-api-trade-place-algo-order to set Take-profit order price and Stop-loss order price

No. But I will consider it. In fact, stop-orders can be implemented by using the combination of TradingService and SubscribeTickerService.

jackbbhua commented 1 year ago

yes. then i will use the TradingService now.

jackbbhua commented 1 year ago

when i switch to Long/Short Mode, it show the Some(Other(api error: code=51000 msg=Parameter posSide error)) i had inset Parameters `

let request = okxclient .place_with_opts( &Place::with_size(Decimal::new((amount as i64) * 1, 0)).limit(Decimal::new(price as i64 + 10, 0)), PlaceOrderOptions::new(_inst).insert("tdMode", "isolated").insert("side", side).insert("posSide", _pos_side.to_string()).insert("tag", "2232323"), ) .await;

`

Nouzan commented 1 year ago

when i switch to Long/Short Mode, it show the Some(Other(api error: code=51000 msg=Parameter posSide error)) i had inset Parameters `

let request = okxclient .place_with_opts( &Place::with_size(Decimal::new((amount as i64) * 1, 0)).limit(Decimal::new(price as i64 + 10, 0)), PlaceOrderOptions::new(_inst).insert("tdMode", "isolated").insert("side", side).insert("posSide", _pos_side.to_string()).insert("tag", "2232323"), ) .await;

`

Sorry, the document isn't clear enough. Only the supported extra fields in PlaceOrderOptions will be recognized. Currently, exc only supports buy/sell mode for the most consistency, so posSide will be ignored.

Could you please describe the concrete use cases you want exc (or maybe just exc_okx ) to support in a new issue? I may work on them next week.

jackbbhua commented 1 year ago

i want use Long/Short Mode to open long and short at same time. jut need exc_okx trade in OKX for now. thank you so much.

jackbbhua commented 1 year ago

hi, @Nouzan do you ever face this issue before thread 'thread '<unnamed>' panicked at 'dispatch dropped without returning error', /Users/bigmouncehaha/.cargo/registry/src/github.com-1ecc6299db9ec823/hyper-0.14.20/src/client/conn.rs:329:35

Nouzan commented 1 year ago

hi, @Nouzan do you ever face this issue before

`

thread 'thread '' panicked at 'dispatch dropped without returning error', /Users/bigmouncehaha/.cargo/registry/src/github.com-1ecc6299db9ec823/hyper-0.14.20/src/client/conn.rs:329:35

`

No, it seems to be a problem in hyper. Or maybe you can open a new issue for it if it is reproducible.

jackbbhua commented 1 year ago

i think this problem is when i call a async function from async function by it self. should be not a bug.

jackbbhua commented 1 year ago

Good afternoon, how can you know your order is profited or lose money use the exc-OKX api ? Thank you.

Nouzan commented 1 year ago

Good afternoon, how can you know your order is profited or lose money use the exc-OKX api ? Thank you.

When you ask what is the profit of a trade, there must be at least two orders with opposite filled sizes (recall that buys have positive sizes and sells have negative sizes) and possibly different costs. You can use these pairs of cost and filled size to calculate the profit and loss of the given trade.

In fact, I have made a crate (Nouzan/positions) for this kind of calculation.

For unrealized profit and loss, just assume that you have closed it at the latest price, then everything is the same.

jackbbhua commented 1 year ago

Awesome,then I will follow your idea. Thank you so much.