0

Please help!!!! Is there a working example of opening an order in C++

Constantly sending a request to open an order POST https://api.bybit.com/private/linear/order/create

{"api_key":"WTVpIZqOwx2LGYY3TB","order_type":"Market","qty":5,"side":"Buy","symbol":"MATICUSDT","time_in_force":"GoodTillCancel","timestamp":1652444871610,"sign":"433b79f452a6c43f3b507df4b5cee84314c7aae584546a889cde7d750ac2f4a6"}

I get an error

{"ret_code":10001,"ret_msg":"empty param of timestamp","ext_code":"","ext_info":"","result":null,"time_now":"1652444872.324646"

And no one can explain why

the code itself

int main(int, char **)
{
    CURL *curl;
    CURLcode res;

    unsigned long long timestamp = chrono::duration_cast<chrono::milliseconds>(chrono::_V2::system_clock::now().time_since_epoch()).count();

    string api_key = "WT***************3TB";
    string secret_key = "Ik**************4vE";

    string reqParam = "api_key=" + api_key + "&order_type=Market&qty=5&side=Buy&symbol=MATICUSDT&time_in_force=GoodTillCancel&timestamp=" + to_string(timestamp);

    string sign = hmacEncode(reqParam, secret_key);

    string json = "{\"api_key\":\"" + api_key + "\",\"side\"=\"Buy\",\"symbol\"=\"MATICUSDT\",\"order_type\":\"Market\",\"qty\":2,\"time_in_force\":\"GoodTillCancel\",\"timestamp\":" + to_string(timestamp) + " ,\"sign\":\"" + sign + "\"}";

    cout << json << endl;

    curl = curl_easy_init();
    if (curl)
    {
        // set params
        curl_easy_setopt(curl, CURLOPT_POST, 1);          // post req
        curl_easy_setopt(curl, CURLOPT_URL, "https://api.bybit.com/private/linear/order/create"); // url
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

        // Header "Content-Type: application/json"
        struct curl_slist *headers = NULL;
        curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}

https://github.com/alex290/bybitorder-api/blob/master/src/main.cpp

Alexandr
  • 1
  • 1

2 Answers2

0

I see some strange things. Your wrong JSON in the code:

"\"side\"=\"Buy\",\"symbol\"=\"MATICUSDT\"

It is not consistent with the dumped JSON

"side":"Buy","symbol":"MATICUSDT".

Your code has space after the timestamp, the dumped JSON has no spaces. And the parameter order is different.

273K
  • 29,503
  • 10
  • 41
  • 64
  • I tried seconds, and with a point of milliseconds. The result is the same.. Let me remind you that the Documentation costs milliseconds. [**Here is an example**](https://bybit-exchange.github.io/docs/linear/#t-authenticationparameters) – Alexandr May 14 '22 at 15:31
  • I'm sending `{"api_key":"WTVpIZqOwx2LGYY3TB","side"="Buy","symbol"="MATICUSDT","order_type":"Market","qty":2,"time_in_force":"GoodTillCancel","timestamp":1652542431.505 ,"sign":"79539f6c8182c50617b0d77c763d8868a497d0849420f1b247818659e6039510"}` Receive `{"ret_code":10001,"ret_msg":"emptyparam of timestamp","ext_code":"","ext_info":"","result":null,"time_now":"1652542432.559505"}` – Alexandr May 14 '22 at 15:35
  • Sorry, you are right. I see some strange things. Your code: `"\"side\"=\"Buy\",\"symbol\"=\"MATICUSDT\"` is not consistent with the dumped JSON `"side":"Buy","symbol":"MATICUSDT"`. Your code has space after the timestamp, the dumped JSON has no spaces. And the parameter order is different. – 273K May 14 '22 at 16:48
  • Everything solved the problem. [Here is the working code](https://github.com/alex290/bybitorder-api/blob/master/src/main.cpp "Here is the working code") – Alexandr May 14 '22 at 21:26
0

Everything solved the problem. Here is the working code

int main(int, char **)
{
    CURL *curl;
    CURLcode res;

    unsigned long long timestamp = chrono::duration_cast<chrono::milliseconds>(chrono::_V2::system_clock::now().time_since_epoch()).count();

    string api_key = "WT***************3TB";
    string secret_key = "Ik**************4vE";

    string reqParam = "api_key=" + api_key + "&close_on_trigger=false&order_type=Market&position_idx=0&qty=5&reduce_only=false&side=Buy&symbol=MATICUSDT&time_in_force=GoodTillCancel&timestamp=" + to_string(timestamp);


    string sign = hmacEncode(reqParam, secret_key);

    reqParam = reqParam + "&sign=" + sign;
 
    string urlStr = "https://api.bybit.com/private/linear/order/create?" + reqParam;

    cout << urlStr << endl;

    curl = curl_easy_init();
    if (curl)
    {
        // set params
        curl_easy_setopt(curl, CURLOPT_POST, 1);             // post req
        curl_easy_setopt(curl, CURLOPT_URL, urlStr.c_str()); // url
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

        // Header "Content-Type: application/json"
        struct curl_slist *headers = NULL;
        curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}
Alexandr
  • 1
  • 1