본문 바로가기
트레이딩/암호화폐

bybit . API V5 . rest . 주문하기

by i.got.it 2023. 10. 27.

 

Place Order . 주문 송신

 


HTTP Request :  POST /v5/order/create

 

 

Place Order요청

 



// Spot PostOnly normal order . 현물 일반 주문(내계좌 잔고만으로 거래하는것) 할려면 "isLeverage":0 로 함. 
{"category":"spot","symbol":"BTCUSDT","side":"Buy","orderType":"Limit","qty":"0.1","price":"15600","timeInForce":"PostOnly","orderLinkId":"spot-test-01","isLeverage":0,"orderFilter":"Order"}

// Spot TP/SL order
{"category":"spot","symbol":"BTCUSDT","side":"Buy","orderType":"Limit","qty":"0.1","price":"15600","triggerPrice": "15000", "timeInForce":"Limit","orderLinkId":"spot-test-02","isLeverage":0,"orderFilter":"tpslOrder"}

// Spot margin normal order (UTA) . 현물 마진 주문 할려면 "isLeverage":1 로 함. 
{"category":"spot","symbol":"BTCUSDT","side":"Buy","orderType":"Limit","qty":"0.1","price":"15600","timeInForce":"Limit","orderLinkId":"spot-test-limit","isLeverage":1,"orderFilter":"Order"}

// Spot Market Buy order, qty is quote currency
{"category":"spot","symbol":"BTCUSDT","side":"Buy","orderType":"Market","qty":"200","timeInForce":"IOC","orderLinkId":"spot-test-04","isLeverage":0,"orderFilter":"Order"}


// USDT Perp open long position (one-way mode)
{"category":"linear","symbol":"BTCUSDT","side":"Buy","orderType":"Limit","qty":"1","price":"25000","timeInForce":"GTC","positionIdx":0,"orderLinkId":"usdt-test-01","reduceOnly":false,"takeProfit":"28000","stopLoss":"20000","tpslMode":"Partial","tpOrderType":"Limit","slOrderType":"Limit","tpLimitPrice":"27500","slLimitPrice":"20500"}

// USDT Perp close long position (one-way mode)
{"category": "linear", "symbol": "BTCUSDT", "side": "Sell", "orderType": "Limit", "qty": "1", "price": "30000", "timeInForce": "GTC", "positionIdx": 0, "orderLinkId": "usdt-test-02", "reduceOnly": true}

 

 

Parameter Required Type Comments
category true string Product type
  • Unified account: spot, linear, inverse, option
  • Classic account: spot, linear, inverse
symbol true string Symbol name
isLeverage false integer Whether to borrow. Valid for Unified spot only. 0(default): false then spot trading, 1: true then margin trading
side true string Buy, Sell
orderType true string Market, Limit
qty true string Order quantity.
  • For Spot Market Buy order, please note that qty should be quote curreny amount, and make sure it satisfies quotePrecision in Spot instrument spec
  • For other cases, please make sure the input qty is the multiples of minOrderQty from instrument info endpoint
  • In particular, for Futures & Perps, if you pass qty="0", you can close the whole position of current symbol
price false string Order price
  • Market order will ignore this field
  • Please check the min price and price precision from instrument info endpoint
  • If you have position, price needs to be better than liquidation price
triggerDirection false integer Conditional order param. Used to identify the expected direction of the conditional order.
  • 1: triggered when market price rises to triggerPrice
  • 2: triggered when market price falls to triggerPrice
Valid for linear & inverse
orderFilter false string If it is not passed, Order by default.
  • Order
  • tpslOrder: Spot TP/SL order, the assets are occupied even before the order is triggered
  • StopOrder: Spot conditional order, the assets will not be occupied until the price of the underlying asset reaches the trigger price, and the required assets will be occupied after the Conditional order is triggered
Valid for spot only
triggerPrice false string
  • For Perps & Futures, it is the conditional order trigger price. If you expect the price to rise to trigger your conditional order, make sure:
    triggerPrice > market price
    Else, triggerPrice < market price
  • For spot, it is the TP/SL and Conditional order trigger price
triggerBy false string Trigger price type, Conditional order param for Perps & Futures. LastPrice, IndexPrice, MarkPrice
Valid for linear & inverse
orderIv false string Implied volatility. option only. Pass the real value, e.g for 10%, 0.1 should be passed. orderIv has a higher priority when price is passed as well
timeInForce false string Time in force
  • Market order will use IOC directly
  • If not passed, GTC is used by default
positionIdx false integer Used to identify positions in different position modes. Under hedge-mode, this param is required (USDT perps & Inverse contracts have hedge mode)
  • 0: one-way mode
  • 1: hedge-mode Buy side
  • 2: hedge-mode Sell side
orderLinkId false string User customised order ID. A max of 36 characters. Combinations of numbers, letters (upper and lower cases), dashes, and underscores are supported.
Futures & Perps: orderLinkId rules:
  • optional param
  • always unique
  • required param
  • always unique
takeProfit false string Take profit price, valid for linear & inverse
stopLoss false string Stop loss price, valid for linear & inverse
tpTriggerBy false string The price type to trigger take profit. MarkPrice, IndexPrice, default: LastPrice. Valid for linear & inverse
slTriggerBy false string The price type to trigger stop loss. MarkPrice, IndexPrice, default: LastPrice. Valid for linear & inverse
reduceOnly false boolean What is a reduce-only order? true means your position can only reduce in size if this order is triggered.
  • You must specify it as true when you are about to close/reduce the position
  • When reduceOnly is true, take profit/stop loss cannot be set
Valid for linear, inverse & option
closeOnTrigger false boolean What is a close on trigger order? For a closing order. It can only reduce your position, not increase it. If the account has insufficient available balance when the closing order is triggered, then other active orders of similar contracts will be cancelled or reduced. It can be used to ensure your stop loss reduces your position regardless of current available margin.
Valid for linear & inverse
smpType false string Smp execution type. What is SMP?
mmp false boolean Market maker protection. option only. true means set the order as a market maker protection order. What is mmp?
tpslMode false string TP/SL mode
  • Full: entire position for TP/SL. Then, tpOrderType or slOrderType must be Market
  • Partial: partial position tp/sl. Limit TP/SL order are supported. Note: When create limit tp/sl, tpslMode is required and it must be Partial
Valid for linear & inverse
tpLimitPrice false string The limit order price when take profit price is triggered. Only works when tpslMode=Partial and tpOrderType=Limit.
Valid for linear & inverse
slLimitPrice false string The limit order price when stop loss price is triggered. Only works when tpslMode=Partial and slOrderType=Limit.
Valid for linear & inverse
tpOrderType false string The order type when take profit is triggered. Market(default), Limit. For tpslMode=Full, it only supports tpOrderType=Market.
Valid for linear & inverse
slOrderType false string The order type when stop loss is triggered. Market(default), Limit. For tpslMode=Full, it only supports slOrderType=Market.
Valid for linear & inverse

 

 

Place Order . 응답 

 


{
    "retCode": 0,
    "retMsg": "OK",
    "result": {
        "orderId": "1321003749386327552",
        "orderLinkId": "spot-test-postonly"
    },
    "retExtInfo": {},
    "time": 1672211918471
}

 

Parameter Type Comments
orderId string Order ID
orderLinkId string User customised order ID

 

 

VC++ 에서 Place Order  구현예 


/*
2023.10.27
bybit api v5 적용 작업.
 

응답. api v5 에서는 구버전 대비 주문세부(수량,가격 , 상태등) 정보 제공안되고 orderID ,  orderLinkId 만 제공. 
{
    "retCode": 0,
    "retMsg": "OK",
    "result": {
        "orderId": "1321003749386327552",
        "orderLinkId": "spot-test-postonly"
    },
    "retExtInfo": {},
    "time": 1672211918471
}

*/
int CCyRestBybit_Spot::Send_OrderNew(CCyD_CyFinOrder::OrderNew* p_order_new)
{

    std::string str_post_data;


    // 시작. str_post_data 구성. 
    str_post_data = "{\"category\":\"spot\"";
    str_post_data.append(",\"symbol\":\"" + p_order_new->map_para["symbol"] + "\"");
    str_post_data.append(",\"side\":\"" + p_order_new->map_para["side"] + "\"");

    std::string type = "Limit";
    if (p_order_new->map_para["type"].compare("MARKET") == 0) { type = "Market"; }

    str_post_data.append(",\"orderType\":\"" + type + "\"");
    str_post_data.append(",\"qty\":\"" + p_order_new->map_para["qty"] + "\"");
    str_post_data.append(",\"price\":\"" + p_order_new->map_para["price"] + "\"");
    //str_post_data.append(",\"timeInForce\":\"PostOnly\"");// 이 설정으로요청하면 주문접수 안됨. 설정하지 않으면 기본 GTC 로되며 정상 접수됨. 
    str_post_data.append(",\"orderLinkId\":\"" + p_order_new->map_para["orderLinkId"] + "\"}");

    //Display_Status_Ext("OrderNew data = ", str_post_data);

    // 끝. str_post_data 구성. 

    // 보안처리. 
    // 처리1. timestamp+api_key+recv_window+jsonBodyString  
    std::string timestamp = std::to_string(CyUtilTime::get_time_ms()); // 밀리초 단위의 현재시각.
    std::string recv_window = "5000";
    std::string str_hmac_input = timestamp + m_ApiKey + recv_window + str_post_data;

    // 처리2. HMAC_SHA256
    std::string sign = m_CCyUtilSSL.hmac_sha256(m_ApiSecret.c_str(), str_hmac_input.c_str());

    // 처리3. http header 
    std::string http_header = "";

    http_header.append("X-BAPI-SIGN: " + sign);
    http_header.append("\nX-BAPI-API-KEY: " + m_ApiKey);
    http_header.append("\nX-BAPI-TIMESTAMP: " + timestamp);
    http_header.append("\nX-BAPI-RECV-WINDOW: " + recv_window);
    http_header.append("\nContent-Type: application/json");

    // 처리4. http post with header. 
    std::string result;
    m_CCyRestAPI.https_post_header(m_AddressBase + URL_OrderNew_V5, str_post_data, http_header, &result);



    rapidjson::Document m_RJDoc;

    m_RJDoc.Parse(result.c_str());

    if (m_RJDoc.HasParseError())
    {
        //Display_Status_Ext("CCyRestBybit_Spot::Parsing_Result_Check", "error doc_json parse");
        return -1;
    }

    int ret_code = m_RJDoc["retCode"].GetInt();

    if (0 != ret_code) // 0 이면 성공.
    {
        CString cst_msg(m_RJDoc["retMsg"].GetString());
        CString cst_ret_code; cst_ret_code.Format(L"retCode = %d  . ", ret_code);

        Display_Status_Ext(L"CCyRestBybit_Spot::Send_OrderNew. error message = ", cst_ret_code + cst_msg);


        return -2;
    }

    if (m_RJDoc.HasMember("result") == 0) {
        Display_Status_Ext("CCyRestBybit_Spot::Send_OrderNew. error m_RJDoc.HasMember = ", p_order_new->map_para["symbol"] + " No key result");
        return -2;
    }

    return 1;

}

 

 

 

 

최대 주문 건수 

각 종목당 500건. 

  • Open orders up limit:
    Futures: Each account can hold a maximum of 500 active orders simultaneously. This is contract-specific, so the following situation is allowed: the same account can hold 300 BTCUSD active orders and 280 ETHUSD active orders at the same time. For conditional orders, each account can hold a maximum of 10 active orders simultaneously. When the upper limit of orders is reached, you can still place orders with parameters of reduceOnly or closeOnTrigger.
    Spot: 500 orders in total, including a maximum of 30 open TP/SL orders, a maximum of 30 open conditional orders
    Option: a maximum of 50 open orders

 

 

 

 

Cancel Order .  주문 취소 


HTTP Request :  POST /v5/order/cancel

 

Cancel Order . 요청 

 


{
  "category": "linear",
  "symbol": "BTCPERP",
  "orderLinkId": null,
  "orderId":"c6f055d9-7f21-4079-913d-e6523a9cfffa"
}

 

Parameter Required Type Comments
category true string Product type
  • Unified account: spot, linear, inverse, option
  • Classic account: spot, linear, inverse
symbol true string Symbol name
orderId true  string Order ID. Either orderId or orderLinkId is required
orderLinkId true string User customised order ID. Either orderId or orderLinkId is required
orderFilter false string Valid for spot only. Order,tpslOrder,StopOrder. If not passed, Order by default

 

 

Cancel Order . 응답  

 


{
    "retCode": 0,
    "retMsg": "OK",
    "result": {
        "orderId": "c6f055d9-7f21-4079-913d-e6523a9cfffa",
        "orderLinkId": "linear-004"
    },
    "retExtInfo": {},
    "time": 1672217377164
}

 

Parameter Type Comments
orderId string Order ID
orderLinkId string User customised order ID

 

 

VC++ 에서 Cancel  구현예 



/*
2023.10.27
bybit api v5 적용.

*/

int CCyRestBybit_Spot::Send_OrderCancel(CCyD_CyFinOrder::OrderCancel* p_order_cancel)
{

    /// 취소 주문 송신 전에 취소될 대상 주문의 수량 받아두기.필요한 이유 : 봇에서 주문 취소하고 신규주문 낼때 OrderCancel 개체의 수량 정보 활용하여 신규주문 수량 결정함. 


    if (p_order_cancel->m_str_Side.compare("Buy") == 0) { // 취소대상 주문이 Buy 인것. 
        p_order_cancel->m_str_qty_orig = p_order_cancel->m_pSymbol->m_pAliveOrders->map_pAliveOrder_Buy[p_order_cancel->m_oid_exchange_cancel_target]->str_Qty; //   원주문 수량. 
        p_order_cancel->m_str_qty_filled = p_order_cancel->m_pSymbol->m_pAliveOrders->map_pAliveOrder_Buy[p_order_cancel->m_oid_exchange_cancel_target]->str_Qty_Fill;// rj_doc["result"]["executedQty"].GetString();//체결된 수량.
    }
    else if (p_order_cancel->m_str_Side.compare("Sell") == 0) { // 취소대상 주문이 Sell 인것. 

        p_order_cancel->m_str_qty_orig = p_order_cancel->m_pSymbol->m_pAliveOrders->map_pAliveOrder_Sell[p_order_cancel->m_oid_exchange_cancel_target]->str_Qty; //   원주문 수량. 
        p_order_cancel->m_str_qty_filled = p_order_cancel->m_pSymbol->m_pAliveOrders->map_pAliveOrder_Sell[p_order_cancel->m_oid_exchange_cancel_target]->str_Qty_Fill;//체결된 수량. 

    }
   

    //// 취소주문 개시 
    std::string str_post_data;

    std::string str_symbol = p_order_cancel->m_pSymbol->map_FieldValue["name_api"];

    // 시작. str_post_data 구성. 
    str_post_data = "{\"category\":\"spot\"";
    str_post_data.append(",\"symbol\":\"" + str_symbol + "\"");
    str_post_data.append(",\"orderId\":\"" + p_order_cancel->m_oid_exchange_cancel_target + "\"}");

    // 끝. str_post_data 구성. 

    
    // 보안처리. 
    // 처리1. timestamp+api_key+recv_window+jsonBodyString  
    std::string timestamp = std::to_string(CyUtilTime::get_time_ms()); // 밀리초 단위의 현재시각.
    std::string recv_window = "5000";
    std::string str_hmac_input = timestamp + m_ApiKey + recv_window + str_post_data;

    // 처리2. HMAC_SHA256
    std::string sign = m_CCyUtilSSL.hmac_sha256(m_ApiSecret.c_str(), str_hmac_input.c_str());

   
    // 처리3. http header 
    std::string http_header = "";

    http_header.append("X-BAPI-SIGN: " + sign);
    http_header.append("\nX-BAPI-API-KEY: " + m_ApiKey);
    http_header.append("\nX-BAPI-TIMESTAMP: " + timestamp);
    http_header.append("\nX-BAPI-RECV-WINDOW: " + recv_window);
    http_header.append("\nContent-Type: application/json");

    // 처리4. http post with header. 
    std::string result;
    m_CCyRestAPI.https_post_header(m_AddressBase + URL_OrderCancel_V5, str_post_data, http_header, &result);

    rapidjson::Document rj_doc;

    rj_doc.Parse(result.c_str());

    if (rj_doc.HasParseError())
    {
        Display_Status_Ext("CCyRestBybit_Spot::Send_OrderCancel", "error doc_json parse");
        Display_Status_Ext("CCyRestBybit_Spot::Send_OrderCancel string all = ", result);
        return -1;
    }

    int ret_code = rj_doc["retCode"].GetInt();

    if (0 != ret_code) // 0 이면 성공.
    {
        CString cst_msg(rj_doc["retMsg"].GetString());
        CString cst_ret_code; cst_ret_code.Format(L"retCode = %d  . ", ret_code);

        Display_Status_Ext(L"CCyRestBybit_Spot::Send_OrderCancel. error message = ", cst_ret_code + cst_msg);

        return -2;
    }

    if (rj_doc.HasMember("result") == 0) {
        Display_Status_Ext("CCyRestBybit_Spot::Send_OrderCancel error rj_doc.HasMember NO key result ", p_order_cancel->m_pSymbol->map_FieldValue["name_user"]);
        return -2;
    }

    if (p_order_cancel->m_pSymbol == NULL)  return 1; // aliveorder 수정없이 리턴한다는 의미.

    if (p_order_cancel->m_pSymbol->m_pAliveOrders == NULL) return 1;// 상동.



    //취소된 주문 AliveOrders 에서 제거처리. 웹소켓 order stream 에서 처리하고 있기에 이곳에서 수행하지 않아도 되나, 무결성 위하여 여기서도제거처리 수행. 
    p_order_cancel->m_pSymbol->m_pAliveOrders->remove_aliveorder(p_order_cancel->m_oid_exchange_cancel_target, p_order_cancel->m_str_Side, p_order_cancel->m_pSymbol->map_FieldValue["CFS_ID"]);

    return 2; // aliveorder 에 취소 주문 된것 반영 수정하여 리턴한다는 의미.
}

 

 

상위정리

 

 

 

bybit api v5 . 활용방법 정리

bybit API v5 . 서버 주소 bybit. API V5. 서버 주소 정리. 암호화폐 거래소 bybit 의 API V5 서버 주소 bybit 전종목 Rest 서버 - Rest 서버는 전 종목 동일 주소. 실거래 서버 시험용 서버 Rest 서버 주소 https://api.

igotit.tistory.com

 


첫 등록 : 2023.10.27

최종 수정 : 

단축 주소 : https://igotit.tistory.com/4958


 

댓글



 

비트코인




암호화폐       외환/나스닥/골드         암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)         프랍 트레이더 온라인 지원가능. MT4,MT5