Error on getting Order Update Via Websocket Java

Prem
Hi Team,

I got following exception while getting an order update from websocket. Kindly go through it and do the needful. Below is the response:

{"type":"order","order_id":"200416001702034","variety":"regular","transaction_type":"BUY","quantity":20,"average_price":0,"exchange":"NFO","tradingsymbol":"BANKNIFTY2041618700PE","status":"REJECTED","status_message":"Insufficient funds. Required margin is 3198.00 but available margin is 0.00.","meta":{}}

Exception is below:

JSONObject["data"] not found.

I am testing some of my logic thats why I have not added any amount in my account as of now. Please do the needful.
  • sujith
    It seems fine from our end. I would suggest using the latest version of the javakiteconnect library.
  • Prem
    I am already using latest library.
  • sujith
    Can you give us more details? We are not able to reproduce this issue at our end.
  • Prem
    Prem edited April 2020
    Hi Sujith,

    Simply while placing order I got this exception via websocket order update. May be JSON response which comes from server via websocket like below:

    {"type":"order","order_id":"200416001702034","variety":"regular","transaction_type":"BUY","quantity":20,"average_price":0,"exchange":"NFO","tradingsymbol":"BANKNIFTY2041618700PE","status":"REJECTED","status_message":"Insufficient funds. Required margin is 3198.00 but available margin is 0.00.","meta":{}}

    Above JSON response is not in appropriate structure and it does not contain "data" key. To reproduce this you have to place order from an account which contains 0 funds and simply print JSON response like this:

    //===================================================
    @Override
    public void onTextMessage(WebSocket websocket, String message) {
    parseTextMessage(message);
    }

    //===================================================
    /** Parses incoming text message.*/
    private void parseTextMessage(String message) {
    // This message contains JSON reponse described above.
    System.out.println("Order update JSON response is===>"+message);
    JSONObject data;
    try {
    data = new JSONObject(message);
    if(!data.has("type")){
    return;
    }

    String type = data.getString("type");
    if(type.equals("order")) {
    if(orderUpdateListener != null) {
    orderUpdateListener.onOrderUpdate(getOrder(data));
    }
    }

    if(type.equals("error")) {
    if(onErrorListener != null) {
    onErrorListener.onError(data.getString("data"));
    }
    }

    }catch (JSONException e) {
    e.printStackTrace();
    }
    }

    Please read comment above this line "return gson.fromJson(String.valueOf(data.get("data")), Order.class);"
    //===================================================
    public Order getOrder(JSONObject data) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

    @Override
    public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    try {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return format.parse(jsonElement.getAsString());
    } catch (ParseException e) {
    return null;
    }
    }
    });
    Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    // May be this line is cause for that exception. Because you want to get order JSON by using "data" key which actually not in passed JSON Object
    return gson.fromJson(String.valueOf(data.get("data")), Order.class);
    }
    Maybe this JSON response should be like below JSON format which is described in https://kite.trade/docs/connect/v3/orders/ to parse in Order Model appropriately.

    {
    "status": "success",
    "data": [{
    "order_id": "151220000000000",
    "parent_order_id": "151210000000000",
    "exchange_order_id": null,
    "placed_by": "AB0012",
    "variety": "regular",
    "status": "REJECTED",

    "tradingsymbol": "ACC",
    "exchange": "NSE",
    "instrument_token": 22,
    "transaction_type": "BUY",
    "order_type": "MARKET",
    "product": "NRML",
    "validity": "DAY",

    "price": 0.0,
    "quantity": 75,
    "trigger_price": 0.0,

    "average_price": 0.0,
    "pending_quantity": 0,
    "filled_quantity": 0,
    "disclosed_quantity": 0,
    "market_protection": 0,

    "order_timestamp": "2015-12-20 15:01:43",
    "exchange_timestamp": null,

    "status_message": "RMS:Margin Exceeds, Required:0, Available:0",
    "tag": null,
    "meta": {}
    }]
    }
    Now please go through this and do the needful ASAP.
  • sujith
    The correct format of the websocket order update is this
    {
    "data": {
    "placed_by": "XXYYZZA",
    "status_message": "Insufficient funds. Required margin is 3530.00 but available margin is 578.22.",
    "meta_raw": null,
    "exchange_order_id": null,
    "variety": "regular",
    "price": 0,
    "exchange_update_timestamp": null,
    "checksum": "",
    "tradingsymbol": "BANKNIFTY2042318700PE",
    "parent_order_id": null,
    "tag": null,
    "filled_quantity": 0,
    "order_type": "MARKET",
    "app_id": 123789,
    "disclosed_quantity": 0,
    "market_protection": 0,
    "product": "NRML",
    "quantity": 20,
    "order_timestamp": "2020-04-17 09:53:31",
    "instrument_token": 21626626,
    "average_price": 0,
    "pending_quantity": 0,
    "unfilled_quantity": 0,
    "transaction_type": "BUY",
    "status_message_raw": "RMS:Margin Exceeds,Required:3530.00, Available:578.22 for entity account-XXYYZZA across exchange across segment across product ",
    "account_id": "XXYYZZA",
    "exchange_timestamp": null,
    "trigger_price": 0,
    "meta": {},
    "cancelled_quantity": 0,
    "guid": "adfweagad",
    "exchange": "NFO",
    "validity": "DAY",
    "order_id": "200417001502700",
    "status": "REJECTED"
    },
    "id": "",
    "type": "order"
    }
    I have tried this multiple times from yesterday. I am using javakiteconnect 3.1.1 I am getting this message which seems fine.

    I did try placing the order for the segment as you mentioned as well and orders were placed from the Kite Connect app and Kite web app as well. The result remains the same.
    I also tried to print all incoming text messages from the websocket and there were no other messages.
  • Prem
    But Sujith really an am getting this exception. Please check for all websocket server endpoints response. I don,t have much scope to debug it.
  • sujith
    @Prem,
    Did you modify the URL of Kite Ticker or make any other code modifications?
  • Prem
    Hi Sujith,
    Did you find cause?
Sign In or Register to comment.