Home > Net >  ByBit Api. Open Active Order. Return "empty param of timestamp". Libcurl c
ByBit Api. Open Active Order. Return "empty param of timestamp". Libcurl c

Time:05-15

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

CodePudding user response:

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;
}

CodePudding user response:

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.

  • Related