Kucoin / kucoin-php-sdk

PHP SDK for KuCoin API.
https://docs.kucoin.com
MIT License
79 stars 41 forks source link

Error 400100: Price increment invalid. #126

Closed BaseMax closed 2 years ago

BaseMax commented 2 years ago

POST /api/v1/orders

Document: https://docs.kucoin.com/#place-a-new-order

Input:

{"clientOid":"62935e628a049","price":"1.08171","size":"15","symbol":"GMT-USDT","type":"limit","side":"buy","remark":"ORDER#1653825122"}

Response:

{"code":"400100","msg":"Price increment invalid."}

Can you help what is this error and how can fix it?

codewc commented 2 years ago

POST /api/v1/orders

Document: https://docs.kucoin.com/#place-a-new-order

Input:

{"clientOid":"62935e628a049","price":"1.08171","size":"15","symbol":"GMT-USDT","type":"limit","side":"buy","remark":"ORDER#1653825122"}

Response:

{"code":"400100","msg":"Price increment invalid."}

Can you help what is this error and how can fix it?

The priceIncrement field specifies the min order price as well as the price increment.This also applies to quote currency. The order price must be a positive integer multiple of this priceIncrement (i.e. if the increment is 0.01, the 0.001 and 0.021 order prices would be rejected). refer to: https://docs.kucoin.com/#get-symbols-list

The data is what I queried from the GET /api/v1/symbols interface

       {
            "symbol": "GMT-USDT",
            "name": "GMT-USDT",
            "baseCurrency": "GMT",
            "quoteCurrency": "USDT",
            "feeCurrency": "USDT",
            "market": "USDS",
            "baseMinSize": "1",
            "quoteMinSize": "0.1",
            "baseMaxSize": "10000000000",
            "quoteMaxSize": "99999999",
            "baseIncrement": "0.0001",
            "quoteIncrement": "0.0001",
            "priceIncrement": "0.0001",
            "priceLimitRate": "0.1",
            "minFunds": "0.1",
            "isMarginEnabled": true,
            "enableTrading": true
        },

the "priceIncrement" is "0.0001", so your correct price should be 1.0817 not 1.08171.

BaseMax commented 2 years ago

Thank you for your help and description.

BaseMax commented 2 years ago

Solution code to fix the price for this coin:

<?php
function fixPrice($price, $priceIncrement) {
    return number_format($price, 4);
}
// "priceIncrement": "0.0001",
$price = 1.08171;
print fixPrice($price, "0.0001");
BaseMax commented 2 years ago

A bit more complicated solution, Anyway this is not a perfect and smart solution.

<?php
// GMT:    0.0001
// KCS:    0.001
function fixPrice(float $price, string $priceIncrement) : string
{
    $price = (string) $price;
    if ($priceIncrement === "0.000000001") {
        return number_format($price, 9);
    }
    else if ($priceIncrement === "0.00000001") {
        return number_format($price, 8);
    }
    else if ($priceIncrement === "0.0000001") {
        return number_format($price, 7);
    }
    else if ($priceIncrement === "0.000001") {
        return number_format($price, 6);
    }
    else if ($priceIncrement === "0.00001") {
        return number_format($price, 5);
    }
    else if ($priceIncrement === "0.0001") {
        return number_format($price, 4);
    }
    else if ($priceIncrement === "0.001") {
        return number_format($price, 3);
    }
    else if ($priceIncrement === "0.01") {
        return number_format($price, 2);
    }
    else if ($priceIncrement === "0.1") {
        return number_format($price, 1);
    }
    return $price;
}

Test:

$price = 1.08171;
print fixPrice($price, "0.0001");

The best way is to call GET /api/v1/symbols and automatically fix the price format by priceIncrement.

hhxsv5 commented 2 years ago
BaseMax commented 2 years ago

Your solution is too smart. @hhxsv5 👍 Thanks, Biao.