ในการพัฒนาระบบเทรดอัตโนมัติด้วย Bybit API สำหรับผมนั้น ปัญหาที่พบบ่อยที่สุดไม่ใช่การเขียนโค้ดผิด แต่เป็นการตีความรหัสข้อผิดพลาด (Error Code) ที่ Bybit ตอบกลับมาอย่างไม่ถูกต้อง จนทำให้เสียเวลาหลายชั่วโมงในการแก้ปัญหาที่แท้จริงคืออะไร

Bybit API Error Code พื้นฐานที่ต้องจำ

Bybit ใช้รหัสข้อผิดพลาดที่เป็นตัวเลขร่วมกับข้อความอธิบาย โดยรหัสที่พบบ่อยที่สุดในการใช้งานจริงมีดังนี้

รหัสข้อผิดพลาด HTTP และการจัดการ

200 - สำเร็จ (แต่อย่าเพิ่งดีใจ)

รหัส 200 หมายความว่า Bybit รับคำขอสำเร็จ แต่นี่ไม่ได้หมายความว่าคำสั่งซื้อขายของคุณสำเร็จเสมอไป ในกรณีของ Order ใหม่ คุณต้องตรวจสอบ ret_code ใน body ของ response ด้วย

400 - Bad Request

นี่คือรหัสที่พบบ่อยที่สุดในการพัฒนา และมักมาพร้อมกับ ret_msg ที่บอกสาเหตุชัดเจน แต่มีบางกรณีที่ข้อความไม่ชัดเจนเท่าที่ควร

401 - Unauthorized

รหัสนี้หมายความว่า API Key ของคุณไม่ถูกต้องหรือหมดอายุ ในประสบการณ์ของผม ส่วนใหญ่เกิดจากการลืมเปลี่ยนโหมดจาก Testnet เป็น Mainnet หรือในทางกลับกัน

403 - Forbidden

รหัสนี้บอกว่าคุณไม่มีสิทธิ์เรียก API นี้ มักเกิดจากการใช้ API Key ที่มีเพียง Read Permission กับ endpoint ที่ต้องการ Write Permission

429 - Rate Limit Exceeded

นี่คือศัตรูหลักของระบบเทรดอัตโนมัติ การเรียก API บ่อยเกินไปจะทำให้ถูกบล็อกชั่วคราว โดยแต่ละ endpoint มี rate limit ต่างกัน

รหัสข้อผิดพลาด Bybit Ret Code ที่พบบ่อย

Ret Code 0 - สำเร็จสมบูรณ์

{
  "ret_code": 0,
  "ret_msg": "OK",
  "ext_code": "",
  "ext_info": "",
  "result": {...},
  "time_now": "1703123456.789456"
}

Ret Code 10001 - Authentication failed

รหัส 10001 เป็นหนึ่งในข้อผิดพลาดที่น่าหงุดหงิดที่สุด เพราะมันบอกว่า "การยืนยันตัวตนล้มเหลว" โดยไม่บอกว่าทำไม สาเหตุหลักมักมาจาก

Ret Code 10002 - Request expired

ผมเคยเสียเวลาหลายชั่วโมงกับรหัสนี้ สาเหตุคือเวลาของ server ที่ใช้ Python หรือ Node.js ไม่ตรงกับ Bybit server โดยเฉพาะเมื่อใช้ container หรือ cloud service

Ret Code 10003 - Invalid request

รหัสนี้บอกว่า request ไม่ถูก format หรือ parameter บางตัวไม่ถูกต้อง มักเกิดจากการส่งค่าที่เป็น string ไปแทนที่ number หรือในทางกลับกัน

Ret Code 10004 - Invalid project

รหัสนี้พบเมื่อใช้ API Key ที่ไม่ตรงกับ environment ที่เรียก เช่น ใช้ mainnet API key กับ testnet endpoint

Ret Code 10005 - Permission denied

{
  "ret_code": 10005,
  "ret_msg": "Permission denied for this action",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110001 - Order does not exist

รหัสนี้หมายความว่า order_id หรือ order_link_id ที่คุณส่งมาไม่พบในระบบ สาเหตุอาจเกิดจาก order ถูก cancel ไปแล้ว หรือใช้ order_id จาก testnet กับ mainnet

Ret Code 110002 - Price is out of range

ราคาที่ส่งมาอยู่นอกขอบเขตที่ Bybit กำหนด ซึ่งอาจเกิน price ceiling หรือต่ำกว่า price floor ของสินทรัพย์นั้นๆ

Ret Code 110003 - Quantity is out of range

{
  "ret_code": 110003,
  "ret_msg": "qty is out of bounds",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110004 - Insufficient balance

ยอดเงินในบัญชีไม่เพียงพอสำหรับคำสั่งซื้อขายนี้ อย่าลืมว่า Bybit ใช้ isolated margin หรือ cross margin ซึ่งมีผลต่อวิธีการคำนวณ available balance

Ret Code 110005 - Order price has no market trend

ราคาที่ตั้งไว้ไม่อยู่ในช่วงราคาตลาดที่ยอมรับได้ มักเกิดเมื่อ spread กว้างมากในช่วง volatility สูง

Ret Code 110006 - Position value exceeds leverage-implied value

ขนาดของ position เกินกว่าที่ leverage ปัจจุบันจะรองรับได้ ต้องลดขนาดหรือเพิ่ม leverage

Ret Code 110007 - Unavailable to place order

ระบบไม่อนุญาตให้วางคำสั่งในขณะนี้ อาจเป็นเพราะ market ปิด หรือ maintenance

Ret Code 110008 - Not a valid contract type

ประเภทของ contract ไม่ถูกต้อง เช่น พยายาม trade linear ด้วย API ที่รองรับเฉพาะ inverse หรือในทางกลับกัน

Ret Code 110009 - Not a valid symbol

{
  "ret_code": 110009,
  "ret_msg": "invalid symbol",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110010 - OrderType is not valid

ประเภทของ order ไม่ถูกต้อง เช่น ส่ง Market Order ไปยัง endpoint ที่รองรับเฉพาะ Limit Order

Ret Code 110011 - Reduce only is not allowed

คำสั่ง reduce-only ไม่ได้รับอนุญาตในสถานการณ์ปัจจุบัน เช่น เมื่อไม่มี position อยู่แล้ว

Ret Code 110012 - Close order is rejected

คำสั่งปิด position ถูกปฏิเสธ มักเกิดจากขนาดใหญ่เกินไปหรือราคาไม่เหมาะสม

Ret Code 110013 - Tpsl order count is out of bounds

จำนวน take profit หรือ stop loss order เกินขีดจำกัดที่กำหนด

Ret Code 110014 - The contract status cannot place order

{
  "ret_code": 110014,
  "ret_msg": "contract is in settlement or delivery",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110015 - Position size is below the minimum required

ขนาดของ position น้อยกว่าขั้นต่ำที่กำหนดสำหรับสินทรัพย์นี้

Ret Code 110016 - Position size is above the maximum allowed

ขนาดของ position เกินขีดจำกัดสูงสุดสำหรับสินทรัพย์นี้

Ret Code 110017 - Position already has similar tpsl order

มี TP/SL order ที่คล้ายกันอยู่แล้ว ต้องยกเลิกอันเดิมก่อน

Ret Code 110018 - Trigger price is out of bounds

ราคา trigger ไม่อยู่ในช่วงที่ยอมรับได้ ต้องอยู่ระหว่าง last price และ liquidation price

Ret Code 110019 - The number of trading units exceeded

{
  "ret_code": 110019,
  "ret_msg": "the number of trading units exceeded",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110020 - Number of orders exceeds limit

จำนวน open orders เกินขีดจำกัดที่กำหนด ต้องยกเลิกบาง orders ก่อน

Ret Code 110021 - Number of positions exceeds limit

จำนวน positions เกินขีดจำกัดที่กำหนด

Ret Code 110022 - Price is too high

ราคาสูงเกินไปสำหรับคำสั่งซื้อ หรือต่ำเกินไปสำหรับคำสั่งขาย

Ret Code 110023 - Price is too low

{
  "ret_code": 110023,
  "ret_msg": "price is too low",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110024 - Minimal quantity to trade is not met

ปริมาณการซื้อขายต่ำกว่าขั้นต่ำที่กำหนดสำหรับ symbol นี้

Ret Code 110025 - Price deviation is too large

ราคาที่ส่งมาเบี่ยงเบนจากราคาตลาดมากเกินไป มักเกิดเมื่อใช้ stale price ในการวางคำสั่ง

Ret Code 110026 - Duplicate order link id

order_link_id ซ้ำกับที่ใช้ไปแล้ว ต้องใช้ค่าใหม่ที่ไม่ซ้ำกัน

Ret Code 110027 - Cancel fails due to the order has been finalized

ไม่สามารถยกเลิก order ได้เพราะ order ได้ถูก filled หรือ partially filled ไปแล้ว

Ret Code 110028 - Order status cannot be modified

{
  "ret_code": 110028,
  "ret_msg": "order status cannot be modified",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110029 - Modify price is invalid

ราคาใหม่ที่ต้องการแก้ไขไม่ถูกต้อง เช่น ส่งค่าติดลบหรือค่าที่ไม่ใช่ตัวเลข

Ret Code 110030 - Modify quantity is invalid

ปริมาณใหม่ที่ต้องการแก้ไขไม่ถูกต้อง

Ret Code 110031 - This contract type does not support stop loss order

สัญญานี้ไม่รองรับ stop loss order

Ret Code 110032 - This contract type does not support take profit order

{
  "ret_code": 110032,
  "ret_msg": "this contract type does not support take profit order",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110033 - Stop loss order cannot be placed for this position

ไม่สามารถวาง stop loss order สำหรับ position นี้ได้ เช่น position ที่ใช้ full position mode

Ret Code 110034 - Take profit order cannot be placed for this position

ไม่สามารถวาง take profit order สำหรับ position นี้ได้

Ret Code 110035 - Trailing stop order cannot be placed for this position

ไม่สามารถวาง trailing stop order สำหรับ position นี้ได้

Ret Code 110036 - Only partial close is supported for this order

{
  "ret_code": 110036,
  "ret_msg": "only partial close is supported",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110037 - Not allowed to close the position

ไม่ได้รับอนุญาตให้ปิด position ในสถานการณ์ปัจจุบัน

Ret Code 110038 - Order cannot be placed due to insufficient liquidity

ไม่สามารถวางคำสั่งได้เนื่องจากสภาพคล่องไม่เพียงพอ มักเกิดกับ orders ขนาดใหญ่ในคู่เทรดที่มี volume ต่ำ

Ret Code 110039 - Only limit order is supported for this contract type

{
  "ret_code": 110039,
  "ret_msg": "only limit order is supported",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110040 - Order price must be higher than liquidation price

ราคา order ต้องสูงกว่าราคา liquidation สำหรับ long position

Ret Code 110041 - Order price must be lower than liquidation price

ราคา order ต้องต่ำกว่าราคา liquidation สำหรับ short position

Ret Code 110042 - Order price is above the acceptable trading price

{
  "ret_code": 110042,
  "ret_msg": "price is above acceptable trading price",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110043 - Order price is below the acceptable trading price

ราคา order ต่ำเกินไปสำหรับ market ปัจจุบัน

Ret Code 110044 - Too many requests for this endpoint

มีการเรียก endpoint นี้บ่อยเกินไป ต้องลดความถี่ในการเรียก

Ret Code 110045 - Request timestamp expired

{
  "ret_code": 110045,
  "ret_msg": "request timestamp expired",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110046 - Signatures do not match

signature ไม่ตรงกัน เกิดจากการเข้ารหัสที่ไม่ถูกต้องหรือ parameter ที่ใช้ในการ sign ไม่ตรงกัน

Ret Code 110047 - API key is frozen

API key ถูก freeze เนื่องจากการละเมิดข้อกำหนดหรือความปลอดภัย

Ret Code 110048 - Request IP is not in whitelist

{
  "ret_code": 110048,
  "ret_msg": "ip is not in whitelist",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110049 - API key does not have permission for this endpoint

API key ไม่มีสิทธิ์เรียก endpoint นี้ ต้องตรวจสอบ permission ของ key

Ret Code 110050 - Request header does not contain required parameter

header ของ request ไม่มี parameter ที่จำเป็น เช่น X-BAPI-API-KEY, X-BAPI-SIGN, X-BAPI-SIGN-TYPE, X-BAPI-TIMESTAMP, X-BAPI-RECV-WINDOW

Ret Code 110051 - Request header contains invalid parameter

{
  "ret_code": 110051,
  "ret_msg": "invalid request header parameter",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110052 - Signature type is not supported

ประเภทของ signature ไม่ถูกต้อง ปัจจุบัน Bybit รองรับเฉพาะ HMAC-SHA256

Ret Code 110053 - WebSocket connection limit exceeded

มี WebSocket connection มากเกินจำนวนที่อนุญาต

Ret Code 110054 - WebSocket subscriptions limit exceeded

{
  "ret_code": 110054,
  "ret_msg": "websocket subscriptions limit exceeded",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110055 - WebSocket connection failed

การเชื่อมต่อ WebSocket ล้มเหลว

Ret Code 110056 - WebSocket authorization failed

การยืนยันตัวตน WebSocket ล้มเหลว

Ret Code 110057 - WebSocket subscription topic not found

{
  "ret_code": 110057,
  "ret_msg": "subscription topic not found",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110058 - WebSocket subscription format is invalid

format ของ subscription ไม่ถูกต้อง

Ret Code 110059 - Request parameter is duplicated

มี parameter ที่ซ้ำกันใน request

Ret Code 110060 - Request parameter value is out of range

{
  "ret_code": 110060,
  "ret_msg": "parameter value is out of range",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110061 - Request parameter type is incorrect

ประเภทของ parameter ไม่ถูกต้อง เช่น ส่ง string แทน number

Ret Code 110062 - Missing required parameter

ไม่มี parameter ที่จำเป็นใน request

Ret Code 110063 - Category is required

{
  "ret_code": 110063,
  "ret_msg": "category is required",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110064 - Category value is invalid

ค่า category ไม่ถูกต้อง ใน Unified Margin API ต้องเป็น linear หรือ inverse

Ret Code 110065 - Symbol is required

ไม่ได้ระบุ symbol

Ret Code 110066 - Symbol does not exist

{
  "ret_code": 110066,
  "ret_msg": "symbol does not exist",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110067 - This endpoint requires order id or order link id

ต้องระบุ order_id หรือ order_link_id

Ret Code 110068 - Order id and order link id cannot be used together

ไม่สามารถใช้ทั้ง order_id และ order_link_id พร้อมกันได้

Ret Code 110069 - The operation of canceling all orders is not supported

{
  "ret_code": 110069,
  "ret_msg": "cancel all orders is not supported",
  "ext_code": "",
  "result": null,
  "time_now": "1703123456.789456"
}

Ret Code 110070 - The operation of amending all orders is not supported

ไม่รองรับการแก้ไข orders ทั้งหมดพร้อมกัน

Ret Code 110071 - Only Kline/Candlestick data is available for this symbol

มีเฉพาะข้อมูล Kline/Candlestick สำหรับ symbol นี้

Ret Code 110072 - Data does not exist

{
  "ret_code