Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/main/java/com/angelbroking/smartapi/SmartConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.angelbroking.smartapi.http.SmartAPIRequestHandler;
import com.angelbroking.smartapi.http.exceptions.SmartAPIException;
import com.angelbroking.smartapi.models.*;
import com.angelbroking.smartapi.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONArray;
Expand All @@ -15,7 +16,6 @@
import java.io.IOException;
import java.net.Proxy;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;

import static com.angelbroking.smartapi.utils.Constants.IO_EXCEPTION_ERROR_MSG;
import static com.angelbroking.smartapi.utils.Constants.IO_EXCEPTION_OCCURRED;
Expand Down Expand Up @@ -308,8 +308,16 @@ public Order placeOrder(OrderParams orderParams, String variety) {

JSONObject jsonObject = smartAPIRequestHandler.postRequest(this.apiKey, url, params, accessToken);
Order order = new Order();

if(jsonObject.getJSONObject("data") == null) {
log.error("Order placement failed: {}", jsonObject.getString("message"));
order.apiErrorMessage = jsonObject.getString("message") + " - " + jsonObject.getString("errorcode");
order.status= Constants.ORDER_API_CALL_ERROR;
return order;
}
order.orderId = jsonObject.getJSONObject("data").getString("orderid");
order.uniqueOrderId = jsonObject.getJSONObject("data").getString("uniqueorderid");
order.status = Constants.ORDER_API_CALL_SUCCESS;
log.info("order : {}",order);
return order;
} catch (Exception | SmartAPIException e) {
Expand Down Expand Up @@ -356,7 +364,14 @@ public Order modifyOrder(String orderId, OrderParams orderParams, String variety

JSONObject jsonObject = smartAPIRequestHandler.postRequest(this.apiKey, url, params, accessToken);
Order order = new Order();
if(jsonObject.getJSONObject("data") == null) {
log.error("Order modification failed: {}", jsonObject.getString("message"));
order.apiErrorMessage = jsonObject.getString("message") + " - " + jsonObject.getString("errorcode");
order.status= Constants.ORDER_API_CALL_ERROR;
return order;
}
order.orderId = jsonObject.getJSONObject("data").getString("orderid");
order.status = Constants.ORDER_API_CALL_SUCCESS;
return order;
} catch (Exception | SmartAPIException e) {
log.error(e.getMessage());
Expand All @@ -382,7 +397,16 @@ public Order cancelOrder(String orderId, String variety) {

JSONObject jsonObject = smartAPIRequestHandler.postRequest(this.apiKey, url, params, accessToken);
Order order = new Order();
if(jsonObject.getJSONObject("data") == null) {
log.error("Order cancellation failed: {}", jsonObject.getString("message"));
order.apiErrorMessage = jsonObject.getString("message") + " - " + jsonObject.getString("errorcode");
order.status= Constants.ORDER_API_CALL_ERROR;
return order;
}

order.orderId = jsonObject.getJSONObject("data").getString("orderid");
order.status= Constants.ORDER_API_CALL_SUCCESS;

return order;
} catch (Exception | SmartAPIException e) {
log.error(e.getMessage());
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/angelbroking/smartapi/models/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public class Order {
@SerializedName("uniqueorderid")
public String uniqueOrderId;

@SerializedName("errorMessage")
public String apiErrorMessage;

@Override
public String toString() {
return "Order [disclosedQuantity=" + disclosedQuantity + ", duration=" + duration + ", tradingSymbol="
Expand All @@ -125,7 +128,7 @@ public String toString() {
+ instrumentType + ", strikePrice=" + strikePrice + ", optionType=" + optionType + ", expiryDate="
+ expiryDate + ", lotSize=" + lotSize + ", cancelSize=" + cancelSize + ", filledShares=" + filledShares
+ ", orderStatus=" + orderStatus + ", unfilledShares=" + unfilledShares + ", fillId=" + fillId
+ ", fillTime=" + fillTime + ", uniqueorderid=" + uniqueOrderId + "]";
+ ", fillTime=" + fillTime + ", uniqueorderid=" + uniqueOrderId + ", errorMessage=" + apiErrorMessage + "]";
}

}
5 changes: 4 additions & 1 deletion src/main/java/com/angelbroking/smartapi/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public class Constants {
public static String EXCHANGE_CDS = "CDS";
public static String EXCHANGE_NCDEX = "NCDEX";
public static String EXCHANGE_MCX = "MCX";


public static String ORDER_API_CALL_SUCCESS = "APICallSuccess";
public static String ORDER_API_CALL_ERROR = "APICallError";

/**
* LTP QUOTE SNAPQUOTE Constants
*/
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/angelbroking/smartapi/SmartConnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public void placeOrder() throws SmartAPIException, IOException {

Order placeOrder = smartConnect.placeOrder(orderParams,"STOPLOSS");
assertNotNull(placeOrder);
assertEquals(Constants.ORDER_API_CALL_SUCCESS, placeOrder.status);
}

@Test(expected = SmartAPIException.class)
Expand Down Expand Up @@ -224,6 +225,7 @@ public void modifyOrder() throws SmartAPIException,IOException {

Order modifyOrder = smartConnect.modifyOrder(orderResponse.orderId,orderParams, orderParams.variety);
assertNotNull(modifyOrder);
assertEquals(Constants.ORDER_API_CALL_SUCCESS, modifyOrder.status);
}

@Test
Expand All @@ -236,6 +238,7 @@ public void cancelOrder() throws SmartAPIException,IOException {

Order cancelOrder = smartConnect.cancelOrder(orderResponse.orderId,Constants.VARIETY_NORMAL);
assertNotNull(cancelOrder);
assertEquals(Constants.ORDER_API_CALL_SUCCESS, cancelOrder.status);
}

@Test
Expand Down